idnits 2.17.1 draft-hixie-thewebsocketprotocol-36.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 doesn't use any RFC 2119 keywords, yet has text resembling RFC 2119 boilerplate text. -- The document date (September 2, 2009) is 5350 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' -- Possible downref: Non-RFC (?) normative reference: ref. 'ORIGIN' ** 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) -- Possible downref: Non-RFC (?) normative reference: ref. 'WEBADDRESSES' Summary: 6 errors (**), 0 flaws (~~), 2 warnings (==), 4 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 September 2, 2009 5 Expires: March 6, 2010 7 The Web Socket protocol 8 draft-hixie-thewebsocketprotocol-36 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 March 6, 2010. 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 the same source 56 document as the HTML5 specification. [HTML5] 58 Please send feedback to either the hybi@ietf.org list or the 59 whatwg@whatwg.org list. 61 Table of Contents 63 1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . 4 64 1.1. Security model . . . . . . . . . . . . . . . . . . . . . . 4 65 1.2. Relationship to TCP/IP and HTTP . . . . . . . . . . . . . 4 66 1.3. Establishing a connection . . . . . . . . . . . . . . . . 5 67 1.4. The Web Socket protocol . . . . . . . . . . . . . . . . . 5 68 2. Conformance requirements . . . . . . . . . . . . . . . . . . . 7 69 2.1. Terminology . . . . . . . . . . . . . . . . . . . . . . . 7 70 3. Web Socket URLs . . . . . . . . . . . . . . . . . . . . . . . 8 71 3.1. Parsing Web Socket URLs . . . . . . . . . . . . . . . . . 8 72 3.2. Constructing Web Socket URLs . . . . . . . . . . . . . . . 9 73 4. Client-side requirements . . . . . . . . . . . . . . . . . . . 10 74 4.1. Handshake . . . . . . . . . . . . . . . . . . . . . . . . 10 75 4.2. Data framing . . . . . . . . . . . . . . . . . . . . . . . 18 76 4.3. Closing the connection . . . . . . . . . . . . . . . . . . 19 77 4.4. Handling errors in UTF-8 . . . . . . . . . . . . . . . . . 19 78 5. Server-side requirements . . . . . . . . . . . . . . . . . . . 21 79 5.1. Minimal handshake . . . . . . . . . . . . . . . . . . . . 21 80 5.2. Handshake details . . . . . . . . . . . . . . . . . . . . 22 81 5.3. Data framing . . . . . . . . . . . . . . . . . . . . . . . 23 82 6. Closing the connection . . . . . . . . . . . . . . . . . . . . 24 83 7. Security considerations . . . . . . . . . . . . . . . . . . . 25 84 8. IANA considerations . . . . . . . . . . . . . . . . . . . . . 26 85 8.1. Registration of ws: scheme . . . . . . . . . . . . . . . . 26 86 8.2. Registration of wss: scheme . . . . . . . . . . . . . . . 26 87 8.3. Registration of the "WebSocket" HTTP Upgrade keyword . . . 27 88 9. Using the Web Socket protocol from other specifications . . . 29 89 10. Normative References . . . . . . . . . . . . . . . . . . . . . 30 90 Author's Address . . . . . . . . . . . . . . . . . . . . . . . . . 31 92 1. Introduction 94 _This section is non-normative._ 96 The Web Socket protocol is designed on the principle that there 97 should be minimal framing (the only framing that exists is to make 98 the protocol frame-based instead of stream-based, and to support a 99 distinction between Unicode text and binary frames). It is expected 100 that metadata would be layered on top of Web Socket by the 101 application layer, in the same way that metadata is layered on top of 102 TCP/IP by the application layer (HTTP). 104 Conceptually, Web Socket is really just a layer on top of TCP/IP that 105 adds a Web "origin"-based security model for browsers; adds an 106 addressing and protocol naming mechanism to support multiple services 107 on one port and multiple host names on one IP address; and layers a 108 framing mechanism on top of TCP to get back to the IP packet 109 mechanism that TCP is built on, but without length limits. Other 110 than that, it adds nothing. Basically it is intended to be as close 111 as possible to just exposing raw TCP/IP to script as possible given 112 the constraints of the Web. It's also designed in such a way that its 113 servers can share a port with HTTP servers, by having its handshake 114 be a valid HTTP Upgrade handshake also. 116 1.1. Security model 118 _This section is non-normative._ 120 The Web Socket protocol uses the origin model used by Web browsers to 121 restrict which Web pages can contact a Web Socket server when the Web 122 Socket protocol is used from a Web page. Naturally, when the Web 123 Socket protocol is used directly (not from a Web page), the origin 124 model is not useful, as the client can provide any arbitrary origin 125 string. 127 1.2. Relationship to TCP/IP and HTTP 129 _This section is non-normative._ 131 The Web Socket protocol is an independent TCP-based protocol. Its 132 only relationship to HTTP is that its handshake is interpreted by 133 HTTP servers as an Upgrade request. 135 Based on the expert recommendation of the IANA, the Web Socket 136 protocol by default uses port 80 for regular Web Socket connections 137 and port 443 for Web Socket connections tunneled over TLS. 139 1.3. Establishing a connection 141 _This section is non-normative._ 143 There are several options for establishing a Web Socket connection. 145 The simplest method is to use port 80 to get a direct connection to a 146 Web Socket server. Port 80 traffic, however, will often be 147 intercepted by HTTP proxies, which can lead to the connection failing 148 to be established. 150 The second simplest method is to use TLS encryption and port 443 to 151 connect directly to a Web Socket server. This has the advantage of 152 being more secure; however, TLS encryption can be computationally 153 expensive. 155 When a connection is to be made to a port that is shared by an HTTP 156 server (a situation that is quite likely to occur with traffic to 157 ports 80 and 443), the connection will appear to the HTTP server to 158 be a regular GET request with an Upgrade offer. In relatively simple 159 setups with just one IP address and a single server for all traffic 160 to a single hostname, this might allow a practical way for systems 161 based on the Web Socket protocol to be deployed. In more elaborate 162 setups (e.g. with load balancers and multiple servers), a dedicated 163 set of hosts for Web Socket connections separate from the HTTP 164 servers is probably easier to manage. 166 1.4. The Web Socket protocol 168 _This section is non-normative._ 170 The protocol has two parts: a handshake, and then the data transfer. 172 The handshake from the client looks as follows: 174 GET /demo HTTP/1.1 175 Upgrade: WebSocket 176 Connection: Upgrade 177 Host: example.com 178 Origin: http://example.com 179 WebSocket-Protocol: sample 181 The handshake from the server looks as follows: 183 HTTP/1.1 101 Web Socket Protocol Handshake 184 Upgrade: WebSocket 185 Connection: Upgrade 186 WebSocket-Origin: http://example.com 187 WebSocket-Location: ws://example.com/demo 188 WebSocket-Protocol: sample 190 Once the client and server have both sent their handshakes, and if 191 the handshake was successful, then the data transfer part starts. 192 This is a two-way communication channel where each side can, 193 independently from the other, send data at will. 195 Data is sent in the form of UTF-8 text. Each frame of data starts 196 with a 0x00 byte and ends with a 0xFF byte, with the UTF-8 text in 197 between. 199 The Web Socket protocol uses this framing so that specifications that 200 use the Web Socket protocol can expose such connections using an 201 event-based mechanism instead of requiring users of those 202 specifications to implement buffering and piecing together of 203 messages manually. 205 2. Conformance requirements 207 All diagrams, examples, and notes in this specification are non- 208 normative, as are all sections explicitly marked non-normative. 209 Everything else in this specification is normative. 211 The key words "MUST", "MUST NOT", "REQUIRED", "SHOULD", "SHOULD NOT", 212 "RECOMMENDED", "MAY", and "OPTIONAL" in the normative parts of this 213 document are to be interpreted as described in RFC2119. For 214 readability, these words do not appear in all uppercase letters in 215 this specification. [RFC2119] 217 Requirements phrased in the imperative as part of algorithms (such as 218 "strip any leading space characters" or "return false and abort these 219 steps") are to be interpreted with the meaning of the key word 220 ("must", "should", "may", etc) used in introducing the algorithm. 222 Conformance requirements phrased as algorithms or specific steps may 223 be implemented in any manner, so long as the end result is 224 equivalent. (In particular, the algorithms defined in this 225 specification are intended to be easy to follow, and not intended to 226 be performant.) 228 Implementations may impose implementation-specific limits on 229 otherwise unconstrained inputs, e.g. to prevent denial of service 230 attacks, to guard against running out of memory, or to work around 231 platform-specific limitations. 233 The conformance classes defined by this specification are user agents 234 and servers. 236 2.1. Terminology 238 *Converting a string to ASCII lowercase* means replacing all 239 characters in the range U+0041 .. U+005A (i.e. LATIN CAPITAL LETTER 240 A to LATIN CAPITAL LETTER Z) with the corresponding characters in the 241 range U+0061 .. U+007A (i.e. LATIN SMALL LETTER A to LATIN SMALL 242 LETTER Z). 244 The term "URL" is used in this section in a manner consistent with 245 the terminology used in HTML, namely, to denote a string that might 246 or might not be a valid URI or IRI and to which certain error 247 handling behaviors will be applied when the string is parsed. 248 [HTML5] 250 3. Web Socket URLs 252 3.1. Parsing Web Socket URLs 254 The steps to *parse a Web Socket URL's components* from a string 255 /url/ are as follows. These steps return either a /host/, a /port/, 256 a /resource name/, and a /secure/ flag, or they fail. 258 1. Parse the /url/ string using the parse a Web address algorithm 259 defined by the Web addresses specification. [WEBADDRESSES] 261 2. If the previous step failed, or if /url/ does not have a 262 component whose value is either "ws" or "wss", when 263 compared in an ASCII case-insensitive manner, then fail this 264 algorithm. 266 3. If /protocol/ is specified but is either the empty string or 267 contains characters that are not in the range U+0021 .. U+007E, 268 then fail this algorithm. 270 4. If the component of /url/ is "ws", set /secure/ to 271 false; otherwise, the component is "wss", set /secure/ 272 to true. 274 5. Let /host/ be the value of the component of /url/, 275 converted to ASCII lowercase. 277 6. If /url/ has a component, then let /port/ be that 278 component's value; otherwise, there is no explicit /port/. 280 7. If there is no explicit /port/, then: if /secure/ is false, let 281 /port/ be 80, otherwise let /port/ be 443. 283 8. Let /resource name/ be the value of the component (which 284 might be empty) of /url/. 286 9. If /resource name/ is the empty string, set it to a single 287 character U+002F SOLIDUS (/). 289 10. If /url/ has a component, then append a single U+003F 290 QUESTION MARK (?) character to /resource name/, followed by the 291 value of the component. 293 11. Return /host/, /port/, /resource name/, and /secure/. 295 3.2. Constructing Web Socket URLs 297 The steps to *construct a Web Socket URL* from a /host/, a /port/, a 298 /resource name/, and a /secure/ flag, are as follows: 300 1. Let /url/ be the empty string. 302 2. If the /secure/ flag is false, then append the string "ws://" to 303 /url/. Otherwise, append the string "wss://" to /url/. 305 3. Append /host/ to /url/. 307 4. If the /secure/ flag is false and port is not 80, or if the 308 /secure/ flag is true and port is not 443, then append the string 309 ":" followed by /port/ to /url/. 311 5. Append /resource name/ to /url/. 313 6. Return /url/. 315 4. Client-side requirements 317 _This section only applies to user agents, not to servers._ 319 NOTE: This specification doesn't currently define a limit to the 320 number of simultaneous connections that a client can establish to a 321 server. 323 4.1. Handshake 325 When the user agent is to *establish a Web Socket connection* to a 326 host /host/, on a port /port/, from an origin whose ASCII 327 serialization is /origin/, with a flag /secure/, with a string giving 328 a /resource name/, and optionally with a string giving a /protocol/, 329 it must run the following steps. The /resource name/ string must 330 start with a U+002F SOLIDUS (/) character. [ORIGIN] 332 1. If the user agent already has a Web Socket connection to the 333 remote host (IP address) identified by /host/, even if known by 334 another name, wait until that connection has been established or 335 for that connection to have failed. 337 NOTE: This makes it harder for a script to perform a denial of 338 service attack by just opening a large number of Web Socket 339 connections to a remote host. 341 NOTE: There is no limit to the number of established Web Socket 342 connections a user agent can have with a single remote host. 343 Servers can refuse to connect users with an excessive number of 344 connections, or disconnect resource-hogging users when suffering 345 high load. 347 2. _Connect_: If the user agent is configured to use a proxy when 348 using the Web Socket protocol to connect to host /host/ and/or 349 port /port/, then connect to that proxy and ask it to open a 350 TCP/IP connection to the host given by /host/ and the port given 351 by /port/. 353 EXAMPLE: For example, if the user agent uses an HTTP proxy 354 for all traffic, then if it was to try to connect to port 80 355 on server example.com, it might send the following lines to 356 the proxy server: 358 CONNECT example.com:80 HTTP/1.1 359 Host: example.com 361 If there was a password, the connection might look like: 363 CONNECT example.com:80 HTTP/1.1 364 Host: example.com 365 Proxy-authorization: Basic ZWRuYW1vZGU6bm9jYXBlcyE= 367 Otherwise, if the user agent is not configured to use a proxy, 368 then open a TCP/IP connection to the host given by /host/ and 369 the port given by /port/. 371 NOTE: Implementations that do not expose explicit UI for 372 selecting a proxy for Web Socket connections separate from other 373 proxies are encouraged to use a SOCKS proxy for Web Socket 374 connections, if available, or failing that, to prefer the proxy 375 configured for HTTPS connections over the proxy configured for 376 HTTP connections. 378 For the purpose of proxy autoconfiguration scripts, the URL to 379 pass the function must be constructed from /host/, /port/, 380 /resource name/, and the /secure/ flag using the steps to 381 construct a Web Socket URL. 383 NOTE: The WebSocket protocol can be identified in proxy 384 autoconfiguration scripts from the scheme ("ws:" for unencrypted 385 connections and "wss:" for encrypted connections). 387 3. If the connection could not be opened, then fail the Web Socket 388 connection and abort these steps. 390 4. If /secure/ is true, perform a TLS handshake over the 391 connection. If this fails (e.g. the server's certificate could 392 not be verified), then fail the Web Socket connection and abort 393 these steps. Otherwise, all further communication on this 394 channel must run through the encrypted tunnel. [RFC2246] 396 5. Send the following bytes to the remote side (the server): 398 47 45 54 20 400 Send the /resource name/ value, encoded as US-ASCII. 402 Send the following bytes: 404 20 48 54 54 50 2f 31 2e 31 0d 0a 55 70 67 72 61 405 64 65 3a 20 57 65 62 53 6f 63 6b 65 74 0d 0a 43 406 6f 6e 6e 65 63 74 69 6f 6e 3a 20 55 70 67 72 61 407 64 65 0d 0a 409 NOTE: The string "GET ", the path, " HTTP/1.1", CRLF, the string 410 "Upgrade: WebSocket", CRLF, and the string "Connection: 412 Upgrade", CRLF. 414 6. Send the following bytes: 416 48 6f 73 74 3a 20 418 Send the /host/ value, converted to ASCII lowercase, and encoded 419 as US-ASCII. 421 If /secure/ is false, and /port/ is not 80, or if /secure/ is 422 true, and /port/ is not 443, then send an 0x3a byte (":") 423 followed by the value of /port/, expressed as a base-ten 424 integer, encoded as US-ASCII. 426 Send the following bytes: 428 0d 0a 430 NOTE: The string "Host: ", the host, and CRLF. 432 7. Send the following bytes: 434 4f 72 69 67 69 6e 3a 20 436 Send the /origin/ value, converted to ASCII lowercase, encoded 437 as US-ASCII. [ORIGIN] 439 NOTE: The /origin/ value is a string that was passed to this 440 algorithm. 442 Send the following bytes: 444 0d 0a 446 NOTE: The string "Origin: ", the origin, and CRLF. 448 8. If there is no /protocol/, then skip this step. 450 Otherwise, send the following bytes: 452 57 65 62 53 6f 63 6b 65 74 2d 50 72 6f 74 6f 63 453 6f 6c 3a 20 455 Send the /protocol/ value, encoded as US-ASCII. 457 Send the following bytes: 459 0d 0a 461 NOTE: The string "WebSocket-Protocol: ", the protocol, and CRLF. 463 9. If the client has any authentication information or cookies that 464 would be relevant to a resource accessed over HTTP, if /secure/ 465 is false, or HTTPS, if it is true, on host /host/, port /port/, 466 with /resource name/ as the path (and possibly query 467 parameters), then HTTP headers that would be appropriate for 468 that information should be sent at this point. [RFC2616] 469 [RFC2109] [RFC2965] 471 Each header must be on a line of its own (each ending with a CR 472 LF sequence). For the purposes of this step, each header must 473 not be split into multiple lines (despite HTTP otherwise 474 allowing this with continuation lines). 476 EXAMPLE: For example, if the server had a username and 477 password that applied to |http://example.com/socket|, and the 478 Web Socket was being opened to |ws://example.com/socket|, it 479 could send them: 481 Authorization: Basic d2FsbGU6ZXZl 483 10. Send the following bytes: 485 0d 0a 487 NOTE: Just a CRLF (a blank line). 489 11. Read bytes from the server until either the connection closes, 490 or a 0x0a byte is read. Let /header/ be these bytes, including 491 the 0x0a byte. 493 If /header/ is not at least two bytes long, or if the last two 494 bytes aren't 0x0d and 0x0a respectively, then fail the Web 495 Socket connection and abort these steps. 497 User agents may apply a timeout to this step, failing the Web 498 Socket connection if the server does not send back data in a 499 suitable time period. 501 12. If /header/ consists of 44 bytes that exactly match the 502 following, then let /mode/ be _normal_. 504 48 54 54 50 2f 31 2e 31 20 31 30 31 20 57 65 62 505 20 53 6f 63 6b 65 74 20 50 72 6f 74 6f 63 6f 6c 506 20 48 61 6e 64 73 68 61 6b 65 0d 0a 508 NOTE: The string "HTTP/1.1 101 Web Socket Protocol Handshake" 509 followed by a CRLF pair. 511 Otherwise, let /code/ be the substring of /header/ that starts 512 from the byte after the first 0x20 byte, and ends with the byte 513 before the second 0x20 byte. If there are not at least two 0x20 514 bytes in /header/, then fail the Web Socket connection and abort 515 these steps. 517 If /code/, interpreted as ASCII, is "401", then let /mode/ be 518 _authenticate_. 520 Otherwise, fail the Web Socket connection and abort these steps. 522 13. If /mode/ is _normal_, then read 41 bytes from the server. 524 If the connection closes before 41 bytes are received, or if the 525 41 bytes aren't exactly equal to the following bytes, then fail 526 the Web Socket connection and abort these steps. 528 55 70 67 72 61 64 65 3a 20 57 65 62 53 6f 63 6b 529 65 74 0d 0a 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20 530 55 70 67 72 61 64 65 0d 0a 532 NOTE: The string "Upgrade: WebSocket", CRLF, the string 533 "Connection: Upgrade", CRLF. 535 User agents may apply a timeout to this step, failing the Web 536 Socket connection if the server does not respond with the above 537 bytes within a suitable time period. 539 NOTE: This step is skipped if /mode/ is _authenticate_. 541 14. Let /headers/ be a list of name-value pairs, initially empty. 543 15. _Header_: Let /name/ and /value/ be empty byte arrays. 545 16. Read a byte from the server. 547 If the connection closes before this byte is received, then fail 548 the Web Socket connection and abort these steps. 550 Otherwise, handle the byte as described in the appropriate entry 551 below: 553 -> If the byte is 0x0d (ASCII CR) 554 If the /name/ byte array is empty, then jump to the headers 555 processing step. Otherwise, fail the Web Socket connection 556 and abort these steps. 558 -> If the byte is 0x0a (ASCII LF) 559 Fail the Web Socket connection and abort these steps. 561 -> If the byte is 0x3a (ASCII ":") 562 Move on to the next step. 564 -> If the byte is in the range 0x41 .. 0x5a (ASCII "A" .. "Z") 565 Append a byte whose value is the byte's value plus 0x20 to 566 the /name/ byte array and redo this step for the next byte. 568 -> Otherwise 569 Append the byte to the /name/ byte array and redo this step 570 for the next byte. 572 NOTE: This reads a header name, terminated by a colon, 573 converting upper-case ASCII letters to lowercase, and aborting 574 if a stray CR or LF is found. 576 17. Read a byte from the server. 578 If the connection closes before this byte is received, then fail 579 the Web Socket connection and abort these steps. 581 Otherwise, handle the byte as described in the appropriate entry 582 below: 584 -> If the byte is 0x20 (ASCII space) 585 Ignore the byte and move on to the next step. 587 -> Otherwise 588 Treat the byte as described by the list in the next step, 589 then move on to that next step for real. 591 NOTE: This skips past a space character after the colon, if 592 necessary. 594 18. Read a byte from the server. 596 If the connection closes before this byte is received, then fail 597 the Web Socket connection and abort these steps. 599 Otherwise, handle the byte as described in the appropriate entry 600 below: 602 -> If the byte is 0x0d (ASCII CR) 603 Move on to the next step. 605 -> If the byte is 0x0a (ASCII LF) 606 Fail the Web Socket connection and abort these steps. 608 -> Otherwise 609 Append the byte to the /value/ byte array and redo this step 610 for the next byte. 612 NOTE: This reads a header value, terminated by a CRLF. 614 19. Read a byte from the server. 616 If the connection closes before this byte is received, or if the 617 byte is not a 0x0a byte (ASCII LF), then fail the Web Socket 618 connection and abort these steps. 620 NOTE: This skips past the LF byte of the CRLF after the header. 622 20. Append an entry to the /headers/ list that has the name given by 623 the string obtained by interpreting the /name/ byte array as a 624 UTF-8 byte stream and the value given by the string obtained by 625 interpreting the /value/ byte array as a UTF-8 byte stream. 627 21. Return to the "Header" step above. 629 22. _Headers processing_: Read a byte from the server. 631 If the connection closes before this byte is received, or if the 632 byte is not a 0x0a byte (ASCII LF), then fail the Web Socket 633 connection and abort these steps. 635 NOTE: This skips past the LF byte of the CRLF after the blank 636 line after the headers. 638 23. If /mode/ is _normal_, then: If there is not exactly one entry 639 in the /headers/ list whose name is "websocket-origin", or if 640 there is not exactly one entry in the /headers/ list whose name 641 is "websocket-location", or if the /protocol/ was specified but 642 there is not exactly one entry in the /headers/ list whose name 643 is "websocket-protocol", or if there are any entries in the 644 /headers/ list whose names are the empty string, then fail the 645 Web Socket connection and abort these steps. Otherwise, handle 646 each entry in the /headers/ list as follows: 648 -> If the entry's name is "websocket-origin" 649 If the value is not exactly equal to /origin/, converted to 650 ASCII lowercase, then fail the Web Socket connection and 651 abort these steps. [ORIGIN] 653 -> If the entry's name is "websocket-location" 654 If the value is not exactly equal to a string obtained from 655 the steps to construct a Web Socket URL from /host/, /port/, 656 /resource name/, and the /secure/ flag, then fail the Web 657 Socket connection and abort these steps. 659 -> If the entry's name is "websocket-protocol" 660 If there was a /protocol/ specified, and the value is not 661 exactly equal to /protocol/, then fail the Web Socket 662 connection and abort these steps. (If no /protocol/ was 663 specified, the header is ignored.) 665 -> If the entry's name is "set-cookie" or "set-cookie2" or 666 another cookie-related header name 667 Handle the cookie as defined by the appropriate spec, with 668 the resource being the one with the host /host/, the port 669 /port/, the path (and possibly query parameters) /resource 670 name/, and the scheme |http| if /secure/ is false and |https| 671 if /secure/ is true. [RFC2109] [RFC2965] 673 -> Any other name 674 Ignore it. 676 If /mode/ is _authenticate_, then: If there is not exactly one 677 entry in the /headers/ list whose name is "www-authenticate", 678 then fail the Web Socket connection and abort these steps. 679 Otherwise, handle each entry in the /headers/ list as follows: 681 -> If the entry's name is "www-authenticate" 682 Obtain credentials in a manner consistent with the 683 requirements for handling the |WWW-Authenticate| header in 684 HTTP, and then close the connection (if the server has not 685 already done so) and jump back to the step labeled _connect_, 686 including the relevant authentication headers in the new 687 request. [RFC2616] 689 -> Any other name 690 Ignore it. 692 24. The *Web Socket connection is established*. Now the user agent 693 must send and receive to and from the connection as described in 694 the next section. 696 4.2. Data framing 698 Once a Web Socket connection is established, the user agent must run 699 through the following state machine for the bytes sent by the server. 701 1. Try to read a byte from the server. Let /frame type/ be that 702 byte. 704 If no byte could be read because the Web Socket connection is 705 closed, then abort. 707 2. Handle the /frame type/ byte as follows: 709 If the high-order bit of the /frame type/ byte is set (i.e. if 710 /frame type/ _and_ed with 0x80 returns 0x80) 711 Run these steps. If at any point during these steps a read is 712 attempted but fails because the Web Socket connection is 713 closed, then abort. 715 1. Let /length/ be zero. 717 2. _Length_: Read a byte, let /b/ be that byte. 719 3. Let /b_v/ be integer corresponding to the low 7 bits of 720 /b/ (the value you would get by _and_ing /b/ with 0x7f). 722 4. Multiply /length/ by 128, add /b_v/ to that result, and 723 store the final result in /length/. 725 5. If the high-order bit of /b/ is set (i.e. if /b/ _and_ed 726 with 0x80 returns 0x80), then return to the step above 727 labeled _length_. 729 6. Read /length/ bytes. 731 7. Discard the read bytes. 733 If the high-order bit of the /frame type/ byte is _not_ set (i.e. 734 if /frame type/ _and_ed with 0x80 returns 0x00) 735 Run these steps. If at any point during these steps a read is 736 attempted but fails because the Web Socket connection is 737 closed, then abort. 739 1. Let /raw data/ be an empty byte array. 741 2. _Data_: Read a byte, let /b/ be that byte. If the client 742 runs out of resources for buffering the incoming data, or 743 hits an artificial resource limit intended to avoid 744 resource starvation, then it must fail the Web Socket 745 connection and abort these steps. 747 3. If /b/ is not 0xff, then append /b/ to /raw data/ and 748 return to the previous step (labeled _data_). 750 4. Interpret /raw data/ as a UTF-8 string, and store that 751 string in /data/. 753 5. If /frame type/ is 0x00, then *a message has been 754 received* with text /data/. Otherwise, discard the data. 756 3. Return to the first step to read the next byte. 758 If the user agent is faced with content that is too large to be 759 handled appropriately, then it must fail the Web Socket connection. 761 Once a Web Socket connection is established, the user agent must use 762 the following steps to *send /data/ using the Web Socket*: 764 1. Send a 0x00 byte to the server. 766 2. Encode /data/ using UTF-8 and send the resulting byte stream to 767 the server. 769 3. Send a 0xff byte to the server. 771 If at any point there is a fatal problem with sending data to the 772 server, the user agent must fail the Web Socket connection. 774 4.3. Closing the connection 776 To *fail the Web Socket connection*, the user agent must close the 777 Web Socket connection, and may report the problem to the user (which 778 would be especially useful for developers). However, user agents 779 must not convey the failure information to the script that attempted 780 the connection in a way distinguishable from the Web Socket being 781 closed normally. 783 Except as indicated above or as specified by the application layer 784 (e.g. a script using the Web Socket API), user agents should not 785 close the connection. 787 4.4. Handling errors in UTF-8 789 When a client is to interpret a byte stream as UTF-8 but finds that 790 the byte stream is not in fact a valid UTF-8 stream, then any bytes 791 or sequences of bytes that are not valid UTF-8 sequences must be 792 interpreted as a U+FFFD REPLACEMENT CHARACTER. 794 5. Server-side requirements 796 _This section only applies to servers._ 798 5.1. Minimal handshake 800 NOTE: This section describes the minimal requirements for a server- 801 side implementation of Web Sockets. 803 Listen on a port for TCP/IP. Upon receiving a connection request, 804 open a connection and send the following bytes back to the client: 806 48 54 54 50 2f 31 2e 31 20 31 30 31 20 57 65 62 807 20 53 6f 63 6b 65 74 20 50 72 6f 74 6f 63 6f 6c 808 20 48 61 6e 64 73 68 61 6b 65 0d 0a 55 70 67 72 809 61 64 65 3a 20 57 65 62 53 6f 63 6b 65 74 0d 0a 810 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20 55 70 67 72 811 61 64 65 0d 0a 813 Send the string "WebSocket-Origin" followed by a U+003A COLON (":") 814 and a U+0020 SPACE, followed by the ASCII serialization of the origin 815 from which the server is willing to accept connections, followed by a 816 CRLF pair (0x0d 0x0a). [ORIGIN] 818 For instance: 820 WebSocket-Origin: http://example.com 822 Send the string "WebSocket-Location" followed by a U+003A COLON (":") 823 and a U+0020 SPACE, followed by the URL of the Web Socket script, 824 followed by a CRLF pair (0x0d 0x0a). 826 For instance: 828 WebSocket-Location: ws://example.com/demo 830 NOTE: Do not include the port if it is the default port for Web 831 Socket protocol connections of the type in question (80 for 832 unencrypted connections and 443 for encrypted connections). 834 Send another CRLF pair (0x0d 0x0a). 836 Read data from the client until four bytes 0x0d 0x0a 0x0d 0x0a are 837 read. This data must either be discarded or handled as described in 838 the following section describing the handshake details. 840 If the connection isn't dropped at this point, go to the data framing 841 section. 843 5.2. Handshake details 845 The previous section ignores the data that is transmitted by the 846 client during the handshake. 848 The data sent by the client consists of a number of fields separated 849 by CR LF pairs (bytes 0x0d 0x0a). 851 The first field consists of three tokens separated by space 852 characters (byte 0x20). The middle token is the path being opened. 853 If the server supports multiple paths, then the server should echo 854 the value of this field in the initial handshake, as part of the URL 855 given on the |WebSocket-Location| line (after the appropriate scheme 856 and host). 858 If the first field does not have three tokens, the server should 859 abort the connection as it probably represents an errorneous client. 861 The remaining fields consist of name-value pairs, with the name part 862 separated from the value part by a colon and a space (bytes 0x3a 863 0x20). Of these, several are interesting: 865 Host (bytes 48 6f 73 74) 866 The value gives the hostname that the client intended to use when 867 opening the Web Socket. It would be of interest in particular to 868 virtual hosting environments, where one server might serve 869 multiple hosts, and might therefore want to return different data. 871 The right host has to be output as part of the URL given on the 872 |WebSocket-Location| line of the handshake described above, to 873 verify that the server knows that it is really representing that 874 host. 876 Origin (bytes 4f 72 69 67 69 6e) 877 The value gives the scheme, hostname, and port (if it's not the 878 default port for the given scheme) of the page that asked the 879 client to open the Web Socket. It would be interesting if the 880 server's operator had deals with operators of other sites, since 881 the server could then decide how to respond (or indeed, _whether_ 882 to respond) based on which site was requesting a connection. 884 If the server supports connections from more than one origin, then 885 the server should echo the value of this field in the initial 886 handshake, on the |WebSocket-Origin| line. 888 Other fields 889 Other fields can be used, such as "Cookie" or "Authorization", for 890 authentication purposes. 892 Any fields that lack the colon-space separator should be discarded 893 and may cause the server to disconnect. 895 5.3. Data framing 897 NOTE: This section only describes how to handle content that this 898 specification allows user agents to send (text). It doesn't handle 899 any arbitrary content in the same way that the requirements on user 900 agents defined earlier handle any content including possible future 901 extensions to the protocols. 903 The server must run through the following steps to process the bytes 904 sent by the client: 906 1. Read a byte from the client. Assuming everything is going 907 according to plan, it will be a 0x00 byte. If the byte is not a 908 0x00 byte, then the server may disconnect. 910 2. Let /raw data/ be an empty byte array. 912 3. _Data_: Read a byte, let /b/ be that byte. 914 4. If /b/ is not 0xff, then append /b/ to /raw data/ and return to 915 the previous step (labeled _data_). 917 5. Interpret /raw data/ as a UTF-8 string, and apply whatever 918 server-specific processing is to occur for the resulting string. 920 6. Return to the first step to read the next byte. 922 The server must run through the following steps to send strings to 923 the client: 925 1. Send a 0x00 byte to the client to indicate the start of a string. 927 2. Encode /data/ using UTF-8 and send the resulting byte stream to 928 the client. 930 3. Send a 0xff byte to the client to indicate the end of the 931 message. 933 6. Closing the connection 935 To *close the Web Socket connection*, either the user agent or the 936 server closes the TCP/IP connection. There is no closing handshake. 937 Whether the user agent or the server closes the connection, it is 938 said that the *Web Socket connection is closed*. 940 Servers may close the Web Socket connection whenever desired. 942 User agents should not close the Web Socket connection arbitrarily. 944 7. Security considerations 946 While this protocol is intended to be used by scripts in Web pages, 947 it can also be used directly by hosts. Such hosts are acting on 948 their own behalf, and can therefore send fake "Origin" headers, 949 misleading the server. Servers should therefore be careful about 950 assuming that they are talking directly to scripts from known 951 origins, and must consider that they might be accessed in unexpected 952 ways. In particular, a server should not trust that any input is 953 valid. 955 EXAMPLE: For example, if the server uses input as part of SQL 956 queries, all input text should be escaped before being passed to the 957 SQL server, lest the server be susceptible to SQL injection. 959 Servers that are not intended to process input from any Web page but 960 only for certain sites should verify the "Origin" header is an origin 961 they expect, and should only respond with the corresponding 962 "WebSocket-Origin" if it is an accepted origin. Servers that only 963 accept input from one origin can just send back that value in the 964 "WebSocket-Origin" header, without bothering to check the client's 965 value. 967 If at any time a server is faced with data that it does not 968 understand, or that violates some criteria by which the server 969 determines safety of input, or when the server sees a handshake that 970 does not correspond to the values the server is expecting (e.g. 971 incorrect path or origin), the server should just disconnect. It is 972 always safe to disconnect. 974 8. IANA considerations 976 8.1. Registration of ws: scheme 978 A |ws:| URL identifies a Web Socket server and resource name. 980 URI scheme name. 981 ws 983 Status. 984 Permanent. 986 URI scheme syntax. 987 In ABNF terms using the terminals from the IRI specifications: 988 [RFC5238] [RFC3987] 990 "ws" ":" ihier-part [ "?" iquery ] 992 URI scheme semantics. 993 The only operation for this scheme is to open a connection using 994 the Web Socket protocol. 996 Encoding considerations. 997 UTF-8 only. 999 Applications/protocols that use this URI scheme name. 1000 Web Socket protocol. 1002 Interoperability considerations. 1003 None. 1005 Security considerations. 1006 See "Security considerations" section above. 1008 Contact. 1009 Ian Hickson 1011 Author/Change controller. 1012 Ian Hickson 1014 References. 1015 This document. 1017 8.2. Registration of wss: scheme 1019 A |wss:| URL identifies a Web Socket server and resource name, and 1020 indicates that traffic over that connection is to be encrypted. 1022 URI scheme name. 1023 wss 1025 Status. 1026 Permanent. 1028 URI scheme syntax. 1029 In ABNF terms using the terminals from the IRI specifications: 1030 [RFC5238] [RFC3987] 1032 "ws" ":" ihier-part [ "?" iquery ] 1034 URI scheme semantics. 1035 The only operation for this scheme is to open a connection using 1036 the Web Socket protocol, encrypted using TLS. 1038 Encoding considerations. 1039 UTF-8 only. 1041 Applications/protocols that use this URI scheme name. 1042 Web Socket protocol over TLS. 1044 Interoperability considerations. 1045 None. 1047 Security considerations. 1048 See "Security considerations" section above. 1050 Contact. 1051 Ian Hickson 1053 Author/Change controller. 1054 Ian Hickson 1056 References. 1057 This document. 1059 8.3. Registration of the "WebSocket" HTTP Upgrade keyword 1061 Name of token. 1062 WebSocket 1064 Author/Change controller. 1065 Ian Hickson 1067 Contact. 1068 Ian Hickson 1070 References. 1071 This document. 1073 9. Using the Web Socket protocol from other specifications 1075 The Web Socket protocol is intended to be used by another 1076 specification to provide a generic mechanism for dynamic author- 1077 defined content, e.g. in a specification defining a scripted API. 1079 Such a specification first needs to "establish a Web Socket 1080 connection", providing that algorithm with: 1082 o The destination, consisting of a /host/ and a /port/. 1084 o A /resource name/, which allows for multiple services to be 1085 identified at one host and port. 1087 o A /secure/ flag, which is true if the connection is to be 1088 encrypted, and false otherwise. 1090 o An ASCII serialization of an origin that is being made responsible 1091 for the connection. [ORIGIN] 1093 o Optionally a string identifying a protocol that is to be layered 1094 over the Web Socket connection. 1096 The /host/, /port/, /resource name/, and /secure/ flag are usually 1097 obtained from a URL using the steps to parse a Web Socket URL's 1098 components. These steps fail if the URL does not specify a Web 1099 Socket. 1101 If a connection can be established, then it is said that the "Web 1102 Socket connection is established". 1104 If at any time the connection is to be closed, then the specification 1105 needs to use the "close the Web Socket connection" algorithm. 1107 When the connection is closed, for any reason including failure to 1108 establish the connection in the first place, it is said that the "Web 1109 Socket connection is closed". 1111 While a connection is open, the specification will need to handle the 1112 cases when "a Web Socket message has been received" with text /data/. 1114 To send some text /data/ to an open connection, the specification 1115 needs to "send /data/ using the Web Socket". 1117 10. Normative References 1119 [HTML5] Hickson, I., "HTML5", September 2009. 1121 [ORIGIN] Barth, A., Jackson, C., and I. Hickson, "The HTTP Sec-From 1122 Header", July 2009. 1124 [RFC2109] Kristol, D. and L. Montulli, "HTTP State Management 1125 Mechanism", RFC 2109, February 1997. 1127 [RFC2119] Bradner, S., "Key words for use in RFCs to Indicate 1128 Requirement Levels", BCP 14, RFC 2119, March 1997. 1130 [RFC2246] Dierks, T. and C. Allen, "The TLS Protocol Version 1.0", 1131 RFC 2246, January 1999. 1133 [RFC2616] Fielding, R., Gettys, J., Mogul, J., Frystyk, H., 1134 Masinter, L., Leach, P., and T. Berners-Lee, "Hypertext 1135 Transfer Protocol -- HTTP/1.1", RFC 2616, June 1999. 1137 [RFC2965] Kristol, D. and L. Montulli, "HTTP State Management 1138 Mechanism", RFC 2965, October 2000. 1140 [RFC3987] Duerst, M. and M. Suignard, "Internationalized Resource 1141 Identifiers (IRIs)", RFC 3987, January 2005. 1143 [RFC5238] Phelan, T., "Datagram Transport Layer Security (DTLS) over 1144 the Datagram Congestion Control Protocol (DCCP)", 1145 RFC 5238, May 2008. 1147 [WEBADDRESSES] 1148 Connolly, D. and C. Sperberg-McQueen, "Web addresses in 1149 HTML 5", May 2009. 1151 Author's Address 1153 Ian Hickson 1154 Google, Inc. 1156 Email: ian@hixie.ch 1157 URI: http://ln.hixie.ch/