idnits 2.17.1 draft-hixie-thewebsocketprotocol-03.txt: Checking boilerplate required by RFC 5378 and the IETF Trust (see https://trustee.ietf.org/license-info): ---------------------------------------------------------------------------- ** The document seems to lack a License Notice according IETF Trust Provisions of 28 Dec 2009, Section 6.b.ii or Provisions of 12 Sep 2009 Section 6.b -- however, there's a paragraph with a matching beginning. Boilerplate error? (You're using the IETF Trust Provisions' Section 6.b License Notice from 12 Feb 2009 rather than one of the newer Notices. See https://trustee.ietf.org/license-info/.) Checking nits according to https://www.ietf.org/id-info/1id-guidelines.txt: ---------------------------------------------------------------------------- No issues found here. Checking nits according to https://www.ietf.org/id-info/checklist : ---------------------------------------------------------------------------- ** The abstract seems to contain references ([HTML5]), which it shouldn't. Please replace those with straight textual mentions of the documents in question. Miscellaneous warnings: ---------------------------------------------------------------------------- == The copyright year in the IETF Trust and authors Copyright Line does not match the current year -- The document date (February 25, 2009) is 5539 days in the past. Is this intentional? Checking references for intended status: Proposed Standard ---------------------------------------------------------------------------- (See RFCs 3967 and 4897 for information about using normative references to lower-maturity documents in RFCs) -- Possible downref: Non-RFC (?) normative reference: ref. 'HTML5' ** Obsolete normative reference: RFC 2109 (Obsoleted by RFC 2965) ** Obsolete normative reference: RFC 2246 (Obsoleted by RFC 4346) ** Obsolete normative reference: RFC 2616 (Obsoleted by RFC 7230, RFC 7231, RFC 7232, RFC 7233, RFC 7234, RFC 7235) ** Obsolete normative reference: RFC 2965 (Obsoleted by RFC 6265) Summary: 6 errors (**), 0 flaws (~~), 1 warning (==), 2 comments (--). Run idnits with the --verbose option for more detailed information about the items above. -------------------------------------------------------------------------------- 2 Network Working Group I. Hickson 3 Internet-Draft Google, Inc. 4 Intended status: Standards Track February 25, 2009 5 Expires: August 29, 2009 7 The Web Socket protocol 8 draft-hixie-thewebsocketprotocol-03 10 Status of this Memo 12 This Internet-Draft is submitted to IETF in full conformance with the 13 provisions of BCP 78 and BCP 79. 15 Internet-Drafts are working documents of the Internet Engineering 16 Task Force (IETF), its areas, and its working groups. Note that 17 other groups may also distribute working documents as Internet- 18 Drafts. 20 Internet-Drafts are draft documents valid for a maximum of six months 21 and may be updated, replaced, or obsoleted by other documents at any 22 time. It is inappropriate to use Internet-Drafts as reference 23 material or to cite them other than as "work in progress." 25 The list of current Internet-Drafts can be accessed at 26 http://www.ietf.org/ietf/1id-abstracts.txt. 28 The list of Internet-Draft Shadow Directories can be accessed at 29 http://www.ietf.org/shadow.html. 31 This Internet-Draft will expire on August 29, 2009. 33 Copyright Notice 35 Copyright (c) 2009 IETF Trust and the persons identified as the 36 document authors. All rights reserved. 38 This document is subject to BCP 78 and the IETF Trust's Legal 39 Provisions Relating to IETF Documents in effect on the date of 40 publication of this document (http://trustee.ietf.org/license-info). 41 Please review these documents carefully, as they describe your rights 42 and restrictions with respect to this document. 44 Abstract 46 This protocol enables two-way communication between a user agent 47 running untrusted code running in a controlled environment to a 48 remote host that understands the protocol. It is intended to fail to 49 communicate with servers of pre-existing protocols like SMTP or HTTP, 50 while allowing HTTP servers to opt-in to supporting this protocol if 51 desired. It is designed to be easy to implement on the server side. 53 Author's note 55 This document is automatically generated from, and is therefore a 56 subset of, the HTML5 specification produced by the WHATWG. [HTML5] 58 Table of Contents 60 1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . 4 61 2. Client-side requirements . . . . . . . . . . . . . . . . . . . 5 62 2.1. Handshake . . . . . . . . . . . . . . . . . . . . . . . . 5 63 2.2. Data framing . . . . . . . . . . . . . . . . . . . . . . . 11 64 3. Server-side requirements . . . . . . . . . . . . . . . . . . . 13 65 3.1. Minimal handshake . . . . . . . . . . . . . . . . . . . . 13 66 3.2. Handshake details . . . . . . . . . . . . . . . . . . . . 13 67 3.3. Data framing . . . . . . . . . . . . . . . . . . . . . . . 14 68 4. Closing the connection . . . . . . . . . . . . . . . . . . . . 16 69 5. Security considerations . . . . . . . . . . . . . . . . . . . 17 70 6. IANA considerations . . . . . . . . . . . . . . . . . . . . . 18 71 7. Normative References . . . . . . . . . . . . . . . . . . . . . 19 72 Author's Address . . . . . . . . . . . . . . . . . . . . . . . . . 20 74 1. Introduction 76 ** ISSUE ** ... 78 2. Client-side requirements 80 _This section only applies to user agents, not to servers._ 82 NOTE: This specification doesn't currently define a limit to the 83 number of simultaneous connections that a client can establish to a 84 server. 86 2.1. Handshake 88 When the user agent is to *establish a Web Socket connection* to a 89 host /host/, optionally on port /port/, from an origin /origin/, with 90 a flag /secure/, and with a particular /resource name/, it must run 91 the following steps. 93 NOTE: The /host/ and /origin/ strings will be all-lowercase when this 94 algorithm is invoked. 96 1. If there is no explicit /port/, then: if /secure/ is false, let 97 /port/ be 81, otherwise let /port/ be 815. 99 2. If the user agent is configured to use a proxy to connect to 100 host /host/ and/or port /port/, then connect to that proxy and 101 ask it to open a TCP/IP connection to the host given by /host/ 102 and the port given by /port/. 104 EXAMPLE: For example, if the user agent uses an HTTP proxy 105 for all traffic, then if it was to try to connect to port 80 106 on server example.com, it might send the following lines to 107 the proxy server: 109 CONNECT example.com HTTP/1.1 111 If there was a password, the connection might look like: 113 CONNECT example.com HTTP/1.1 114 Proxy-authorization: Basic ZWRuYW1vZGU6bm9jYXBlcyE= 116 Otherwise, if the user agent is not configured to use a proxy, 117 then open a TCP/IP connection to the host given by /host/ and 118 the port given by /port/. 120 3. If the connection could not be opened, then fail the Web Socket 121 connection and abort these steps. 123 4. If /secure/ is true, perform a TLS handshake over the 124 connection. If this fails (e.g. the server's certificate could 125 not be verified), then fail the Web Socket connection and abort 126 these steps. Otherwise, all further communication on this 127 channel must run through the encrypted tunnel. [RFC2246] 129 5. Send the following bytes to the remote side (the server): 131 47 45 54 20 133 Send the /resource name/ value, encoded as US-ASCII. 135 Send the following bytes: 137 20 48 54 54 50 2f 31 2e 31 0d 0a 55 70 67 72 61 138 64 65 3a 20 57 65 62 53 6f 63 6b 65 74 0d 0a 43 139 6f 6e 6e 65 63 74 69 6f 6e 3a 20 55 70 67 72 61 140 64 65 0d 0a 142 NOTE: The string "GET ", the path, " HTTP/1.1", CRLF, the string 143 "Upgrade: WebSocket", CRLF, and the string "Connection: 144 Upgrade", CRLF. 146 6. Send the following bytes: 148 48 6f 73 74 3a 20 150 Send the /host/ value, encoded as US-ASCII. 152 Send the following bytes: 154 0d 0a 156 NOTE: The string "Host: ", the host, and CRLF. 158 7. Send the following bytes: 160 4f 72 69 67 69 6e 3a 20 162 Send the /origin/ value, encoded as US-ASCII. 164 NOTE: The /origin/ value is a string that was passed to this 165 algorithm. 167 Send the following bytes: 169 0d 0a 171 NOTE: The string "Origin: ", the origin, and CRLF. 173 8. If the client has any authentication information or cookies that 174 would be relevant to a resource accessed over HTTP, if /secure/ 175 is false, or HTTPS, if it is true, on host /host/, port /port/, 176 with /resource name/ as the path (and possibly query 177 parameters), then HTTP headers that would be appropriate for 178 that information should be sent at this point. [RFC2616] 179 [RFC2109] [RFC2965] 181 Each header must be on a line of its own (each ending with a CR 182 LF sequence). For the purposes of this step, each header must 183 not be split into multiple lines (despite HTTP otherwise 184 allowing this with continuation lines). 186 EXAMPLE: For example, if the server had a username and 187 password that applied to |http://example.com/socket|, and the 188 Web Socket was being opened to |ws://example.com:80/socket|, 189 it could send them: 191 Authorization: Basic d2FsbGU6ZXZl 193 However, it would not send them if the Web Socket was being 194 opened to |ws://example.com/socket|, as that uses a different 195 port (81, not 80). 197 9. Send the following bytes: 199 0d 0a 201 NOTE: Just a CRLF (a blank line). 203 10. Read the first 85 bytes from the server. If the connection 204 closes before 85 bytes are received, or if the first 85 bytes 205 aren't exactly equal to the following bytes, then fail the Web 206 Socket connection and abort these steps. 208 48 54 54 50 2f 31 2e 31 20 31 30 31 20 57 65 62 209 20 53 6f 63 6b 65 74 20 50 72 6f 74 6f 63 6f 6c 210 20 48 61 6e 64 73 68 61 6b 65 0d 0a 55 70 67 72 211 61 64 65 3a 20 57 65 62 53 6f 63 6b 65 74 0d 0a 212 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20 55 70 67 72 213 61 64 65 0d 0a 215 NOTE: The string "HTTP/1.1 101 Web Socket Protocol Handshake", 216 CRLF, the string "Upgrade: WebSocket", CRLF, the string 217 "Connection: Upgrade", CRLF. 219 11. Let /headers/ be a list of name-value pairs, initially empty. 221 12. _Header_: Let /name/ and /value/ be empty byte arrays. 223 13. Read a byte from the server. 225 If the connection closes before this byte is received, then fail 226 the Web Socket connection and abort these steps. 228 Otherwise, handle the byte as described in the appropriate entry 229 below: 231 -> If the byte is 0x0d (ASCII CR) 232 If the /name/ byte array is empty, then jump to the headers 233 processing step. Otherwise, fail the Web Socket connection 234 and abort these steps. 236 -> If the byte is 0x0a (ASCII LF) 237 Fail the Web Socket connection and abort these steps. 239 -> If the byte is 0x3a (ASCII ":") 240 Move on to the next step. 242 -> If the byte is in the range 0x41 .. 0x5a (ASCII "A" .. "Z") 243 Append a byte whose value is the byte's value plus 0x20 to 244 the /name/ byte array and redo this step for the next byte. 246 -> Otherwise 247 Append the byte to the /name/ byte array and redo this step 248 for the next byte. 250 NOTE: This reads a header name, terminated by a colon, 251 converting upper-case ASCII letters to lowercase, and aborting 252 if a stray CR or LF is found. 254 14. Read a byte from the server. 256 If the connection closes before this byte is received, then fail 257 the Web Socket connection and abort these steps. 259 Otherwise, handle the byte as described in the appropriate entry 260 below: 262 -> If the byte is 0x20 (ASCII space) 263 Ignore the byte and move on to the next step. 265 -> Otherwise 266 Treat the byte as described by the list in the next step, 267 then move on to that next step for real. 269 NOTE: This skips past a space character after the colon, if 270 necessary. 272 15. Read a byte from the server. 274 If the connection closes before this byte is received, then fail 275 the Web Socket connection and abort these steps. 277 Otherwise, handle the byte as described in the appropriate entry 278 below: 280 -> If the byte is 0x0d (ASCII CR) 281 Move on to the next step. 283 -> If the byte is 0x0a (ASCII LF) 284 Fail the Web Socket connection and abort these steps. 286 -> Otherwise 287 Append the byte to the /name/ byte array and redo this step 288 for the next byte. 290 NOTE: This reads a header value, terminated by a CRLF. 292 16. Read a byte from the server. 294 If the connection closes before this byte is received, or if the 295 byte is not a 0x0a byte (ASCII LF), then fail the Web Socket 296 connection and abort these steps. 298 NOTE: This skips past the LF byte of the CRLF after the header. 300 17. Append an entry to the /headers/ list that has the name given by 301 the string obtained by interpreting the /name/ byte array as a 302 UTF-8 byte stream and the value given by the string obtained by 303 interpreting the /value/ byte array as a UTF-8 byte stream. 305 18. Return to the header step above. 307 19. _Headers processing_: If there is not exactly one entry in the 308 /headers/ list whose name is "websocket-origin", or if there is 309 not exactly one entry in the /headers/ list whose name is 310 "websocket-location", or if there are any entries in the 311 /headers/ list whose names are the empty string, then fail the 312 Web Socket connection and abort these steps. 314 20. Handle each entry in the /headers/ list as follows: 316 -> If the entry's name is "websocket-origin|" 317 If the value is not exactly equal to /origin/, converted to 318 lowercase, then fail the Web Socket connection and abort 319 these steps. 321 -> If the entry's name is "websocket-location|" 322 If the value is not exactly equal to a string consisting of 323 the following components in the same order, then fail the Web 324 Socket connection and abort these steps: 326 1. The string "ws" if /secure/ is false and "wss" if 327 /secure/ is true 329 2. The three characters "://". 331 3. The value of /host/. 333 4. If /secure/ is false and /port/ is not 81, or if /secure/ 334 is true and /port/ is not 815: a ":" character followed 335 by the value of /port/. 337 5. The value of /resource name/. 339 -> If the entry's name is "set-cookie|" or "set-cookie2|" or 340 another cookie-related header name 341 Handle the cookie as defined by the appropriate spec, with 342 the resource being the one with the host /host/, the port 343 /port/, the path (and possibly query parameters) /resource 344 name/, and the scheme |http| if /secure/ is false and |https| 345 if /secure/ is true. [RFC2109] [RFC2965] 347 -> Any other name 348 Ignore it. 350 21. The *Web Socket connection is established*. Now the user agent 351 must send and receive to and from the connection as described in 352 the next section. 354 To *fail the Web Socket connection*, the user agent must close the 355 Web Socket connection, and may report the problem to the user (which 356 would be especially useful for developers). However, user agents 357 must not convey the failure information to the script that attempted 358 the connection in a way distinguishable from the Web Socket being 359 closed normally. 361 2.2. Data framing 363 Once a Web Socket connection is established, the user agent must run 364 through the following state machine for the bytes sent by the server. 366 1. Try to read a byte from the server. Let /frame type/ be that 367 byte. 369 If no byte could be read because the Web Socket connection is 370 closed, then abort. 372 2. Handle the /frame type/ byte as follows: 374 If the high-order bit of the /frame type/ byte is set (i.e. if 375 /frame type/ _and_ed with 0x80 returns 0x80) 376 Run these steps. If at any point during these steps a read is 377 attempted but fails because the Web Socket connection is 378 closed, then abort. 380 1. Let /length/ be zero. 382 2. _Length_: Read a byte, let /b/ be that byte. 384 3. Let /b_v/ be integer corresponding to the low 7 bits of 385 /b/ (the value you would get by _and_ing /b/ with 0x7f). 387 4. Multiply /length/ by 128, add /b_v/ to that result, and 388 store the final result in /length/. 390 5. If the high-order bit of /b/ is set (i.e. if /b/ _and_ed 391 with 0x80 returns 0x80), then return to the step above 392 labeled _length_. 394 6. Read /length/ bytes. 396 7. Discard the read bytes. 398 If the high-order bit of the /frame type/ byte is _not_ set (i.e. 399 if /frame type/ _and_ed with 0x80 returns 0x00) 400 Run these steps. If at any point during these steps a read is 401 attempted but fails because the Web Socket connection is 402 closed, then abort. 404 1. Let /raw data/ be an empty byte array. 406 2. _Data_: Read a byte, let /b/ be that byte. 408 3. If /b/ is not 0xff, then append /b/ to /raw data/ and 409 return to the previous step (labeled _data_). 411 4. Interpret /raw data/ as a UTF-8 string, and store that 412 string in /data/. 414 5. If /frame type/ is 0x00, then *a message has been 415 received* with text /data/. Otherwise, discard the data. 417 3. Return to the first step to read the next byte. 419 If the user agent is faced with content that is too large to be 420 handled appropriately, then it must fail the Web Socket connection. 422 Once a Web Socket connection is established, the user agent must use 423 the following steps to *send /data/ using the Web Socket*: 425 1. Send a 0x00 byte to the server. 427 2. Encode /data/ using UTF-8 and send the resulting byte stream to 428 the server. 430 3. Send a 0xff byte to the server. 432 3. Server-side requirements 434 _This section only applies to servers._ 436 3.1. Minimal handshake 438 NOTE: This section describes the minimal requirements for a server- 439 side implementation of Web Sockets. 441 Listen on a port for TCP/IP. Upon receiving a connection request, 442 open a connection and send the following bytes back to the client: 444 48 54 54 50 2f 31 2e 31 20 31 30 31 20 57 65 62 445 20 53 6f 63 6b 65 74 20 50 72 6f 74 6f 63 6f 6c 446 20 48 61 6e 64 73 68 61 6b 65 0d 0a 55 70 67 72 447 61 64 65 3a 20 57 65 62 53 6f 63 6b 65 74 0d 0a 448 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20 55 70 67 72 449 61 64 65 0d 0a 451 Send the string "WebSocket-Origin" followed by a U+003A COLON (":") 452 followed by the ASCII serialization of the origin from which the 453 server is willing to accept connections, followed by a CRLF pair 454 (0x0d 0x0a). 456 For instance: 458 WebSocket-Origin: http://example.com 460 Send the string "WebSocket-Location" followed by a U+003A COLON (":") 461 followed by the URL of the Web Socket script, followed by a CRLF pair 462 (0x0d 0x0a). 464 For instance: 466 WebSocket-Location: ws://example.com:80/demo 468 Send another CRLF pair (0x0d 0x0a). 470 Read (and discard) data from the client until four bytes 0x0d 0x0a 471 0x0d 0x0a are read. 473 If the connection isn't dropped at this point, go to the data framing 474 section. 476 3.2. Handshake details 478 The previous section ignores the data that is transmitted by the 479 client during the handshake. 481 The data sent by the client consists of a number of fields separated 482 by CR LF pairs (bytes 0x0d 0x0a). 484 The first field consists of three tokens separated by space 485 characters (byte 0x20). The middle token is the path being opened. 486 If the server supports multiple paths, then the server should echo 487 the value of this field in the initial handshake, as part of the URL 488 given on the |WebSocket-Location| line (after the appropriate scheme 489 and host). 491 The remaining fields consist of name-value pairs, with the name part 492 separated from the value part by a colon and a space (bytes 0x3a 493 0x20). Of these, several are interesting: 495 Host (bytes 48 6f 73 74) 496 The value gives the hostname that the client intended to use when 497 opening the Web Socket. It would be of interest in particular to 498 virtual hosting environments, where one server might serve 499 multiple hosts, and might therefore want to return different data. 501 The right host has to be output as part of the URL given on the 502 |WebSocket-Location| line of the handshake described above, to 503 verify that the server knows that it is really representing that 504 host. 506 Origin (bytes 4f 72 69 67 69 6e) 507 The value gives the scheme, hostname, and port (if it's not the 508 default port for the given scheme) of the page that asked the 509 client to open the Web Socket. It would be interesting if the 510 server's operator had deals with operators of other sites, since 511 the server could then decide how to respond (or indeed, _whether_ 512 to respond) based on which site was requesting a connection. 514 If the server supports connections from more than one origin, then 515 the server should echo the value of this field in the initial 516 handshake, on the |WebSocket-Origin| line. 518 Other fields 519 Other fields can be used, such as "Cookie" or "Authorization", for 520 authentication purposes. 522 3.3. Data framing 524 NOTE: This section only describes how to handle content that this 525 specification allows user agents to send (text). It doesn't handle 526 any arbitrary content in the same way that the requirements on user 527 agents defined earlier handle any content including possible future 528 extensions to the protocols. 530 The server should run through the following steps to process the 531 bytes sent by the client: 533 1. Read a byte from the client. Assuming everything is going 534 according to plan, it will be a 0x00 byte. Behaviour for the 535 server is undefined if the byte is not 0x00. 537 2. Let /raw data/ be an empty byte array. 539 3. _Data_: Read a byte, let /b/ be that byte. 541 4. If /b/ is not 0xff, then append /b/ to /raw data/ and return to 542 the previous step (labeled _data_). 544 5. Interpret /raw data/ as a UTF-8 string, and apply whatever 545 server-specific processing should occur for the resulting string. 547 6. Return to the first step to read the next byte. 549 The server should run through the following steps to send strings to 550 the client: 552 1. Send a 0x00 byte to the client to indicate the start of a string. 554 2. Encode /data/ using UTF-8 and send the resulting byte stream to 555 the client. 557 3. Send a 0xff byte to the client to indicate the end of the 558 message. 560 4. Closing the connection 562 To *close the Web Socket connection*, either the user agent or the 563 server closes the TCP/IP connection. There is no closing handshake. 564 Whether the user agent or the server closes the connection, it is 565 said that the *Web Socket connection is closed*. 567 Servers may close the Web Socket connection whenever desired. 569 User agents should not close the Web Socket connection arbitrarily. 571 5. Security considerations 573 ** ISSUE ** ... 575 6. IANA considerations 577 ** ISSUE ** ...(two URI schemes, two ports, HTTP Upgrade keyword) 579 7. Normative References 581 [HTML5] Hickson, I., "HTML5", February 2009. 583 [RFC2109] Kristol, D. and L. Montulli, "HTTP State Management 584 Mechanism", RFC 2109, February 1997. 586 [RFC2246] Dierks, T. and C. Allen, "The TLS Protocol Version 1.0", 587 RFC 2246, January 1999. 589 [RFC2616] Fielding, R., Gettys, J., Mogul, J., Frystyk, H., 590 Masinter, L., Leach, P., and T. Berners-Lee, "Hypertext 591 Transfer Protocol -- HTTP/1.1", RFC 2616, June 1999. 593 [RFC2965] Kristol, D. and L. Montulli, "HTTP State Management 594 Mechanism", RFC 2965, October 2000. 596 Author's Address 598 Ian Hickson 599 Google, Inc. 601 Email: ian@hixie.ch 602 URI: http://ln.hixie.ch/