idnits 2.17.1 draft-hixie-thewebsocketprotocol-39.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 4, 2009) is 5319 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' -- Possible downref: Non-RFC (?) normative reference: ref. 'WSAPI' Summary: 6 errors (**), 0 flaws (~~), 2 warnings (==), 5 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 4, 2009 5 Expires: March 8, 2010 7 The Web Socket protocol 8 draft-hixie-thewebsocketprotocol-39 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 8, 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. Background . . . . . . . . . . . . . . . . . . . . . . . . 4 65 1.2. Protocol overview . . . . . . . . . . . . . . . . . . . . 4 66 1.3. Design philosophy . . . . . . . . . . . . . . . . . . . . 5 67 1.4. Security model . . . . . . . . . . . . . . . . . . . . . . 6 68 1.5. Relationship to TCP/IP and HTTP . . . . . . . . . . . . . 6 69 1.6. Establishing a connection . . . . . . . . . . . . . . . . 6 70 2. Conformance requirements . . . . . . . . . . . . . . . . . . . 8 71 2.1. Terminology . . . . . . . . . . . . . . . . . . . . . . . 8 72 3. Web Socket URLs . . . . . . . . . . . . . . . . . . . . . . . 9 73 3.1. Parsing Web Socket URLs . . . . . . . . . . . . . . . . . 9 74 3.2. Constructing Web Socket URLs . . . . . . . . . . . . . . . 10 75 4. Client-side requirements . . . . . . . . . . . . . . . . . . . 11 76 4.1. Handshake . . . . . . . . . . . . . . . . . . . . . . . . 11 77 4.2. Data framing . . . . . . . . . . . . . . . . . . . . . . . 19 78 4.3. Closing the connection . . . . . . . . . . . . . . . . . . 20 79 4.4. Handling errors in UTF-8 . . . . . . . . . . . . . . . . . 20 80 5. Server-side requirements . . . . . . . . . . . . . . . . . . . 22 81 5.1. Minimal handshake . . . . . . . . . . . . . . . . . . . . 22 82 5.2. Handshake details . . . . . . . . . . . . . . . . . . . . 23 83 5.3. Data framing . . . . . . . . . . . . . . . . . . . . . . . 24 84 6. Closing the connection . . . . . . . . . . . . . . . . . . . . 25 85 7. Security considerations . . . . . . . . . . . . . . . . . . . 26 86 8. IANA considerations . . . . . . . . . . . . . . . . . . . . . 27 87 8.1. Registration of ws: scheme . . . . . . . . . . . . . . . . 27 88 8.2. Registration of wss: scheme . . . . . . . . . . . . . . . 28 89 8.3. Registration of the "WebSocket" HTTP Upgrade keyword . . . 29 90 9. Using the Web Socket protocol from other specifications . . . 30 91 10. Normative References . . . . . . . . . . . . . . . . . . . . . 31 92 Author's Address . . . . . . . . . . . . . . . . . . . . . . . . . 32 94 1. Introduction 96 1.1. Background 98 _This section is non-normative._ 100 Historically, creating an instant messenger chat client as a Web 101 application has required an abuse of HTTP to poll the server for 102 updates while sending upstream notifications as distinct HTTP calls. 104 This results in a variety of problems: 106 o The server is forced to use a number of different underlying TCP 107 connections for each client: one for sending information to the 108 client, and a new one for each incoming message. 110 o The wire protocol has a high overhead, with each client-to-server 111 message having an HTTP header. 113 o The client-side script is forced to maintain a mapping from the 114 outgoing connections to the incoming connection to track replies. 116 A simpler solution would be to use a single TCP connection for 117 traffic in both directions. This is what the Web Socket protocol 118 provides. Combined with the Web Socket API, it provides an 119 alternative to HTTP polling for two-way communication from a Web page 120 to a remote server. [WSAPI] 122 The same technique can be used for a variety of Web applications: 123 games, stock tickers, multiuser applications with simultaneous 124 editing, user interfaces exposing server-side services in real time, 125 etc. 127 1.2. Protocol overview 129 _This section is non-normative._ 131 The protocol has two parts: a handshake, and then the data transfer. 133 The handshake from the client looks as follows: 135 GET /demo HTTP/1.1 136 Upgrade: WebSocket 137 Connection: Upgrade 138 Host: example.com 139 Origin: http://example.com 140 WebSocket-Protocol: sample 142 The handshake from the server looks as follows: 144 HTTP/1.1 101 Web Socket Protocol Handshake 145 Upgrade: WebSocket 146 Connection: Upgrade 147 WebSocket-Origin: http://example.com 148 WebSocket-Location: ws://example.com/demo 149 WebSocket-Protocol: sample 151 Once the client and server have both sent their handshakes, and if 152 the handshake was successful, then the data transfer part starts. 153 This is a two-way communication channel where each side can, 154 independently from the other, send data at will. 156 Data is sent in the form of UTF-8 text. Each frame of data starts 157 with a 0x00 byte and ends with a 0xFF byte, with the UTF-8 text in 158 between. 160 The Web Socket protocol uses this framing so that specifications that 161 use the Web Socket protocol can expose such connections using an 162 event-based mechanism instead of requiring users of those 163 specifications to implement buffering and piecing together of 164 messages manually. 166 1.3. Design philosophy 168 _This section is non-normative._ 170 The Web Socket protocol is designed on the principle that there 171 should be minimal framing (the only framing that exists is to make 172 the protocol frame-based instead of stream-based, and to support a 173 distinction between Unicode text and binary frames). It is expected 174 that metadata would be layered on top of Web Socket by the 175 application layer, in the same way that metadata is layered on top of 176 TCP/IP by the application layer (HTTP). 178 Conceptually, Web Socket is really just a layer on top of TCP/IP that 179 adds a Web "origin"-based security model for browsers; adds an 180 addressing and protocol naming mechanism to support multiple services 181 on one port and multiple host names on one IP address; and layers a 182 framing mechanism on top of TCP to get back to the IP packet 183 mechanism that TCP is built on, but without length limits. Other 184 than that, it adds nothing. Basically it is intended to be as close 185 as possible to just exposing raw TCP/IP to script as possible given 186 the constraints of the Web. It's also designed in such a way that its 187 servers can share a port with HTTP servers, by having its handshake 188 be a valid HTTP Upgrade handshake also. 190 1.4. Security model 192 _This section is non-normative._ 194 The Web Socket protocol uses the origin model used by Web browsers to 195 restrict which Web pages can contact a Web Socket server when the Web 196 Socket protocol is used from a Web page. Naturally, when the Web 197 Socket protocol is used directly (not from a Web page), the origin 198 model is not useful, as the client can provide any arbitrary origin 199 string. 201 1.5. Relationship to TCP/IP and HTTP 203 _This section is non-normative._ 205 The Web Socket protocol is an independent TCP-based protocol. Its 206 only relationship to HTTP is that its handshake is interpreted by 207 HTTP servers as an Upgrade request. 209 Based on the expert recommendation of the IANA, the Web Socket 210 protocol by default uses port 80 for regular Web Socket connections 211 and port 443 for Web Socket connections tunneled over TLS. 213 1.6. Establishing a connection 215 _This section is non-normative._ 217 There are several options for establishing a Web Socket connection. 219 The simplest method is to use port 80 to get a direct connection to a 220 Web Socket server. Port 80 traffic, however, will often be 221 intercepted by HTTP proxies, which can lead to the connection failing 222 to be established. 224 The second simplest method is to use TLS encryption and port 443 to 225 connect directly to a Web Socket server. This has the advantage of 226 being more secure; however, TLS encryption can be computationally 227 expensive. 229 When a connection is to be made to a port that is shared by an HTTP 230 server (a situation that is quite likely to occur with traffic to 231 ports 80 and 443), the connection will appear to the HTTP server to 232 be a regular GET request with an Upgrade offer. In relatively simple 233 setups with just one IP address and a single server for all traffic 234 to a single hostname, this might allow a practical way for systems 235 based on the Web Socket protocol to be deployed. In more elaborate 236 setups (e.g. with load balancers and multiple servers), a dedicated 237 set of hosts for Web Socket connections separate from the HTTP 238 servers is probably easier to manage. 240 2. Conformance requirements 242 All diagrams, examples, and notes in this specification are non- 243 normative, as are all sections explicitly marked non-normative. 244 Everything else in this specification is normative. 246 The key words "MUST", "MUST NOT", "REQUIRED", "SHOULD", "SHOULD NOT", 247 "RECOMMENDED", "MAY", and "OPTIONAL" in the normative parts of this 248 document are to be interpreted as described in RFC2119. For 249 readability, these words do not appear in all uppercase letters in 250 this specification. [RFC2119] 252 Requirements phrased in the imperative as part of algorithms (such as 253 "strip any leading space characters" or "return false and abort these 254 steps") are to be interpreted with the meaning of the key word 255 ("must", "should", "may", etc) used in introducing the algorithm. 257 Conformance requirements phrased as algorithms or specific steps may 258 be implemented in any manner, so long as the end result is 259 equivalent. (In particular, the algorithms defined in this 260 specification are intended to be easy to follow, and not intended to 261 be performant.) 263 Implementations may impose implementation-specific limits on 264 otherwise unconstrained inputs, e.g. to prevent denial of service 265 attacks, to guard against running out of memory, or to work around 266 platform-specific limitations. 268 The conformance classes defined by this specification are user agents 269 and servers. 271 2.1. Terminology 273 *Converting a string to ASCII lowercase* means replacing all 274 characters in the range U+0041 .. U+005A (i.e. LATIN CAPITAL LETTER 275 A to LATIN CAPITAL LETTER Z) with the corresponding characters in the 276 range U+0061 .. U+007A (i.e. LATIN SMALL LETTER A to LATIN SMALL 277 LETTER Z). 279 The term "URL" is used in this section in a manner consistent with 280 the terminology used in HTML, namely, to denote a string that might 281 or might not be a valid URI or IRI and to which certain error 282 handling behaviors will be applied when the string is parsed. 283 [HTML5] 285 3. Web Socket URLs 287 3.1. Parsing Web Socket URLs 289 The steps to *parse a Web Socket URL's components* from a string 290 /url/ are as follows. These steps return either a /host/, a /port/, 291 a /resource name/, and a /secure/ flag, or they fail. 293 1. If /protocol/ is specified but is either the empty string or 294 contains characters that are not in the range U+0021 .. U+007E, 295 then fail this algorithm. 297 2. If the /url/ string is not an absolute URL, then fail this 298 algorithm. [WEBADDRESSES] 300 3. Resolve the /url/ string using the resolve a Web address 301 algorithm defined by the Web addresses specification, with the 302 URL character encoding set to UTF-8. [WEBADDRESSES] [RFC3629] 304 NOTE: It doesn't matter what it is resolved relative to, since 305 we already know it is an absolute URL at this point. 307 4. If /url/ does not have a component whose value is 308 either "ws" or "wss", when compared in an ASCII case-insensitive 309 manner, then fail this algorithm. 311 5. If the component of /url/ is "ws", set /secure/ to 312 false; otherwise, the component is "wss", set /secure/ 313 to true. 315 6. Let /host/ be the value of the component of /url/, 316 converted to ASCII lowercase. 318 7. If /url/ has a component, then let /port/ be that 319 component's value; otherwise, there is no explicit /port/. 321 8. If there is no explicit /port/, then: if /secure/ is false, let 322 /port/ be 80, otherwise let /port/ be 443. 324 9. Let /resource name/ be the value of the component (which 325 might be empty) of /url/. 327 10. If /resource name/ is the empty string, set it to a single 328 character U+002F SOLIDUS (/). 330 11. If /url/ has a component, then append a single U+003F 331 QUESTION MARK (?) character to /resource name/, followed by the 332 value of the component. 334 12. Return /host/, /port/, /resource name/, and /secure/. 336 3.2. Constructing Web Socket URLs 338 The steps to *construct a Web Socket URL* from a /host/, a /port/, a 339 /resource name/, and a /secure/ flag, are as follows: 341 1. Let /url/ be the empty string. 343 2. If the /secure/ flag is false, then append the string "ws://" to 344 /url/. Otherwise, append the string "wss://" to /url/. 346 3. Append /host/ to /url/. 348 4. If the /secure/ flag is false and port is not 80, or if the 349 /secure/ flag is true and port is not 443, then append the string 350 ":" followed by /port/ to /url/. 352 5. Append /resource name/ to /url/. 354 6. Return /url/. 356 4. Client-side requirements 358 _This section only applies to user agents, not to servers._ 360 NOTE: This specification doesn't currently define a limit to the 361 number of simultaneous connections that a client can establish to a 362 server. 364 4.1. Handshake 366 When the user agent is to *establish a Web Socket connection* to a 367 host /host/, on a port /port/, from an origin whose ASCII 368 serialization is /origin/, with a flag /secure/, with a string giving 369 a /resource name/, and optionally with a string giving a /protocol/, 370 it must run the following steps. The /resource name/ string must 371 start with a U+002F SOLIDUS (/) character. [ORIGIN] 373 1. If the user agent already has a Web Socket connection to the 374 remote host (IP address) identified by /host/, even if known by 375 another name, wait until that connection has been established or 376 for that connection to have failed. 378 NOTE: This makes it harder for a script to perform a denial of 379 service attack by just opening a large number of Web Socket 380 connections to a remote host. 382 NOTE: There is no limit to the number of established Web Socket 383 connections a user agent can have with a single remote host. 384 Servers can refuse to connect users with an excessive number of 385 connections, or disconnect resource-hogging users when suffering 386 high load. 388 2. _Connect_: If the user agent is configured to use a proxy when 389 using the Web Socket protocol to connect to host /host/ and/or 390 port /port/, then connect to that proxy and ask it to open a 391 TCP/IP connection to the host given by /host/ and the port given 392 by /port/. 394 EXAMPLE: For example, if the user agent uses an HTTP proxy 395 for all traffic, then if it was to try to connect to port 80 396 on server example.com, it might send the following lines to 397 the proxy server: 399 CONNECT example.com:80 HTTP/1.1 400 Host: example.com 402 If there was a password, the connection might look like: 404 CONNECT example.com:80 HTTP/1.1 405 Host: example.com 406 Proxy-authorization: Basic ZWRuYW1vZGU6bm9jYXBlcyE= 408 Otherwise, if the user agent is not configured to use a proxy, 409 then open a TCP/IP connection to the host given by /host/ and 410 the port given by /port/. 412 NOTE: Implementations that do not expose explicit UI for 413 selecting a proxy for Web Socket connections separate from other 414 proxies are encouraged to use a SOCKS proxy for Web Socket 415 connections, if available, or failing that, to prefer the proxy 416 configured for HTTPS connections over the proxy configured for 417 HTTP connections. 419 For the purpose of proxy autoconfiguration scripts, the URL to 420 pass the function must be constructed from /host/, /port/, 421 /resource name/, and the /secure/ flag using the steps to 422 construct a Web Socket URL. 424 NOTE: The WebSocket protocol can be identified in proxy 425 autoconfiguration scripts from the scheme ("ws:" for unencrypted 426 connections and "wss:" for encrypted connections). 428 3. If the connection could not be opened, then fail the Web Socket 429 connection and abort these steps. 431 4. If /secure/ is true, perform a TLS handshake over the 432 connection. If this fails (e.g. the server's certificate could 433 not be verified), then fail the Web Socket connection and abort 434 these steps. Otherwise, all further communication on this 435 channel must run through the encrypted tunnel. [RFC2246] 437 5. Send the following bytes to the remote side (the server): 439 47 45 54 20 441 Send the /resource name/ value, encoded as US-ASCII. 443 Send the following bytes: 445 20 48 54 54 50 2f 31 2e 31 0d 0a 55 70 67 72 61 446 64 65 3a 20 57 65 62 53 6f 63 6b 65 74 0d 0a 43 447 6f 6e 6e 65 63 74 69 6f 6e 3a 20 55 70 67 72 61 448 64 65 0d 0a 450 NOTE: The string "GET ", the path, " HTTP/1.1", CRLF, the string 451 "Upgrade: WebSocket", CRLF, and the string "Connection: 453 Upgrade", CRLF. 455 6. Send the following bytes: 457 48 6f 73 74 3a 20 459 Send the /host/ value, converted to ASCII lowercase, and encoded 460 as US-ASCII. 462 If /secure/ is false, and /port/ is not 80, or if /secure/ is 463 true, and /port/ is not 443, then send an 0x3a byte (":") 464 followed by the value of /port/, expressed as a base-ten 465 integer, encoded as US-ASCII. 467 Send the following bytes: 469 0d 0a 471 NOTE: The string "Host: ", the host, and CRLF. 473 7. Send the following bytes: 475 4f 72 69 67 69 6e 3a 20 477 Send the /origin/ value, converted to ASCII lowercase, encoded 478 as US-ASCII. [ORIGIN] 480 NOTE: The /origin/ value is a string that was passed to this 481 algorithm. 483 Send the following bytes: 485 0d 0a 487 NOTE: The string "Origin: ", the origin, and CRLF. 489 8. If there is no /protocol/, then skip this step. 491 Otherwise, send the following bytes: 493 57 65 62 53 6f 63 6b 65 74 2d 50 72 6f 74 6f 63 494 6f 6c 3a 20 496 Send the /protocol/ value, encoded as US-ASCII. 498 Send the following bytes: 500 0d 0a 502 NOTE: The string "WebSocket-Protocol: ", the protocol, and CRLF. 504 9. If the client has any authentication information or cookies that 505 would be relevant to a resource accessed over HTTP, if /secure/ 506 is false, or HTTPS, if it is true, on host /host/, port /port/, 507 with /resource name/ as the path (and possibly query 508 parameters), then HTTP headers that would be appropriate for 509 that information should be sent at this point. [RFC2616] 510 [RFC2109] [RFC2965] 512 Each header must be on a line of its own (each ending with a CR 513 LF sequence). For the purposes of this step, each header must 514 not be split into multiple lines (despite HTTP otherwise 515 allowing this with continuation lines). 517 EXAMPLE: For example, if the server had a username and 518 password that applied to |http://example.com/socket|, and the 519 Web Socket was being opened to |ws://example.com/socket|, it 520 could send them: 522 Authorization: Basic d2FsbGU6ZXZl 524 10. Send the following bytes: 526 0d 0a 528 NOTE: Just a CRLF (a blank line). 530 11. Read bytes from the server until either the connection closes, 531 or a 0x0a byte is read. Let /header/ be these bytes, including 532 the 0x0a byte. 534 If /header/ is not at least two bytes long, or if the last two 535 bytes aren't 0x0d and 0x0a respectively, then fail the Web 536 Socket connection and abort these steps. 538 User agents may apply a timeout to this step, failing the Web 539 Socket connection if the server does not send back data in a 540 suitable time period. 542 12. If /header/ consists of 44 bytes that exactly match the 543 following, then let /mode/ be _normal_. 545 48 54 54 50 2f 31 2e 31 20 31 30 31 20 57 65 62 546 20 53 6f 63 6b 65 74 20 50 72 6f 74 6f 63 6f 6c 547 20 48 61 6e 64 73 68 61 6b 65 0d 0a 549 NOTE: The string "HTTP/1.1 101 Web Socket Protocol Handshake" 550 followed by a CRLF pair. 552 Otherwise, let /code/ be the substring of /header/ that starts 553 from the byte after the first 0x20 byte, and ends with the byte 554 before the second 0x20 byte. If there are not at least two 0x20 555 bytes in /header/, then fail the Web Socket connection and abort 556 these steps. 558 If /code/, interpreted as ASCII, is "401", then let /mode/ be 559 _authenticate_. 561 Otherwise, fail the Web Socket connection and abort these steps. 563 13. If /mode/ is _normal_, then read 41 bytes from the server. 565 If the connection closes before 41 bytes are received, or if the 566 41 bytes aren't exactly equal to the following bytes, then fail 567 the Web Socket connection and abort these steps. 569 55 70 67 72 61 64 65 3a 20 57 65 62 53 6f 63 6b 570 65 74 0d 0a 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20 571 55 70 67 72 61 64 65 0d 0a 573 NOTE: The string "Upgrade: WebSocket", CRLF, the string 574 "Connection: Upgrade", CRLF. 576 User agents may apply a timeout to this step, failing the Web 577 Socket connection if the server does not respond with the above 578 bytes within a suitable time period. 580 NOTE: This step is skipped if /mode/ is _authenticate_. 582 14. Let /headers/ be a list of name-value pairs, initially empty. 584 15. _Header_: Let /name/ and /value/ be empty byte arrays. 586 16. Read a byte from the server. 588 If the connection closes before this byte is received, then fail 589 the Web Socket connection and abort these steps. 591 Otherwise, handle the byte as described in the appropriate entry 592 below: 594 -> If the byte is 0x0d (ASCII CR) 595 If the /name/ byte array is empty, then jump to the headers 596 processing step. Otherwise, fail the Web Socket connection 597 and abort these steps. 599 -> If the byte is 0x0a (ASCII LF) 600 Fail the Web Socket connection and abort these steps. 602 -> If the byte is 0x3a (ASCII ":") 603 Move on to the next step. 605 -> If the byte is in the range 0x41 .. 0x5a (ASCII "A" .. "Z") 606 Append a byte whose value is the byte's value plus 0x20 to 607 the /name/ byte array and redo this step for the next byte. 609 -> Otherwise 610 Append the byte to the /name/ byte array and redo this step 611 for the next byte. 613 NOTE: This reads a header name, terminated by a colon, 614 converting upper-case ASCII letters to lowercase, and aborting 615 if a stray CR or LF is found. 617 17. Read a byte from the server. 619 If the connection closes before this byte is received, then fail 620 the Web Socket connection and abort these steps. 622 Otherwise, handle the byte as described in the appropriate entry 623 below: 625 -> If the byte is 0x20 (ASCII space) 626 Ignore the byte and move on to the next step. 628 -> Otherwise 629 Treat the byte as described by the list in the next step, 630 then move on to that next step for real. 632 NOTE: This skips past a space character after the colon, if 633 necessary. 635 18. Read a byte from the server. 637 If the connection closes before this byte is received, then fail 638 the Web Socket connection and abort these steps. 640 Otherwise, handle the byte as described in the appropriate entry 641 below: 643 -> If the byte is 0x0d (ASCII CR) 644 Move on to the next step. 646 -> If the byte is 0x0a (ASCII LF) 647 Fail the Web Socket connection and abort these steps. 649 -> Otherwise 650 Append the byte to the /value/ byte array and redo this step 651 for the next byte. 653 NOTE: This reads a header value, terminated by a CRLF. 655 19. Read a byte from the server. 657 If the connection closes before this byte is received, or if the 658 byte is not a 0x0a byte (ASCII LF), then fail the Web Socket 659 connection and abort these steps. 661 NOTE: This skips past the LF byte of the CRLF after the header. 663 20. Append an entry to the /headers/ list that has the name given by 664 the string obtained by interpreting the /name/ byte array as a 665 UTF-8 byte stream and the value given by the string obtained by 666 interpreting the /value/ byte array as a UTF-8 byte stream. 668 21. Return to the "Header" step above. 670 22. _Headers processing_: Read a byte from the server. 672 If the connection closes before this byte is received, or if the 673 byte is not a 0x0a byte (ASCII LF), then fail the Web Socket 674 connection and abort these steps. 676 NOTE: This skips past the LF byte of the CRLF after the blank 677 line after the headers. 679 23. If /mode/ is _normal_, then: If there is not exactly one entry 680 in the /headers/ list whose name is "websocket-origin", or if 681 there is not exactly one entry in the /headers/ list whose name 682 is "websocket-location", or if the /protocol/ was specified but 683 there is not exactly one entry in the /headers/ list whose name 684 is "websocket-protocol", or if there are any entries in the 685 /headers/ list whose names are the empty string, then fail the 686 Web Socket connection and abort these steps. Otherwise, handle 687 each entry in the /headers/ list as follows: 689 -> If the entry's name is "websocket-origin" 690 If the value is not exactly equal to /origin/, converted to 691 ASCII lowercase, then fail the Web Socket connection and 692 abort these steps. [ORIGIN] 694 -> If the entry's name is "websocket-location" 695 If the value is not exactly equal to a string obtained from 696 the steps to construct a Web Socket URL from /host/, /port/, 697 /resource name/, and the /secure/ flag, then fail the Web 698 Socket connection and abort these steps. 700 -> If the entry's name is "websocket-protocol" 701 If there was a /protocol/ specified, and the value is not 702 exactly equal to /protocol/, then fail the Web Socket 703 connection and abort these steps. (If no /protocol/ was 704 specified, the header is ignored.) 706 -> If the entry's name is "set-cookie" or "set-cookie2" or 707 another cookie-related header name 708 Handle the cookie as defined by the appropriate spec, with 709 the resource being the one with the host /host/, the port 710 /port/, the path (and possibly query parameters) /resource 711 name/, and the scheme |http| if /secure/ is false and |https| 712 if /secure/ is true. [RFC2109] [RFC2965] 714 -> Any other name 715 Ignore it. 717 If /mode/ is _authenticate_, then: If there is not exactly one 718 entry in the /headers/ list whose name is "www-authenticate", 719 then fail the Web Socket connection and abort these steps. 720 Otherwise, handle each entry in the /headers/ list as follows: 722 -> If the entry's name is "www-authenticate" 723 Obtain credentials in a manner consistent with the 724 requirements for handling the |WWW-Authenticate| header in 725 HTTP, and then close the connection (if the server has not 726 already done so) and jump back to the step labeled _connect_, 727 including the relevant authentication headers in the new 728 request. [RFC2616] 730 -> Any other name 731 Ignore it. 733 24. The *Web Socket connection is established*. Now the user agent 734 must send and receive to and from the connection as described in 735 the next section. 737 4.2. Data framing 739 Once a Web Socket connection is established, the user agent must run 740 through the following state machine for the bytes sent by the server. 742 1. Try to read a byte from the server. Let /frame type/ be that 743 byte. 745 If no byte could be read because the Web Socket connection is 746 closed, then abort. 748 2. Handle the /frame type/ byte as follows: 750 If the high-order bit of the /frame type/ byte is set (i.e. if 751 /frame type/ _and_ed with 0x80 returns 0x80) 752 Run these steps. If at any point during these steps a read is 753 attempted but fails because the Web Socket connection is 754 closed, then abort. 756 1. Let /length/ be zero. 758 2. _Length_: Read a byte, let /b/ be that byte. 760 3. Let /b_v/ be integer corresponding to the low 7 bits of 761 /b/ (the value you would get by _and_ing /b/ with 0x7f). 763 4. Multiply /length/ by 128, add /b_v/ to that result, and 764 store the final result in /length/. 766 5. If the high-order bit of /b/ is set (i.e. if /b/ _and_ed 767 with 0x80 returns 0x80), then return to the step above 768 labeled _length_. 770 6. Read /length/ bytes. 772 7. Discard the read bytes. 774 If the high-order bit of the /frame type/ byte is _not_ set (i.e. 775 if /frame type/ _and_ed with 0x80 returns 0x00) 776 Run these steps. If at any point during these steps a read is 777 attempted but fails because the Web Socket connection is 778 closed, then abort. 780 1. Let /raw data/ be an empty byte array. 782 2. _Data_: Read a byte, let /b/ be that byte. If the client 783 runs out of resources for buffering the incoming data, or 784 hits an artificial resource limit intended to avoid 785 resource starvation, then it must fail the Web Socket 786 connection and abort these steps. 788 3. If /b/ is not 0xff, then append /b/ to /raw data/ and 789 return to the previous step (labeled _data_). 791 4. Interpret /raw data/ as a UTF-8 string, and store that 792 string in /data/. 794 5. If /frame type/ is 0x00, then *a message has been 795 received* with text /data/. Otherwise, discard the data. 797 3. Return to the first step to read the next byte. 799 If the user agent is faced with content that is too large to be 800 handled appropriately, then it must fail the Web Socket connection. 802 Once a Web Socket connection is established, the user agent must use 803 the following steps to *send /data/ using the Web Socket*: 805 1. Send a 0x00 byte to the server. 807 2. Encode /data/ using UTF-8 and send the resulting byte stream to 808 the server. 810 3. Send a 0xff byte to the server. 812 If at any point there is a fatal problem with sending data to the 813 server, the user agent must fail the Web Socket connection. 815 4.3. Closing the connection 817 To *fail the Web Socket connection*, the user agent must close the 818 Web Socket connection, and may report the problem to the user (which 819 would be especially useful for developers). However, user agents 820 must not convey the failure information to the script that attempted 821 the connection in a way distinguishable from the Web Socket being 822 closed normally. 824 Except as indicated above or as specified by the application layer 825 (e.g. a script using the Web Socket API), user agents should not 826 close the connection. 828 4.4. Handling errors in UTF-8 830 When a client is to interpret a byte stream as UTF-8 but finds that 831 the byte stream is not in fact a valid UTF-8 stream, then any bytes 832 or sequences of bytes that are not valid UTF-8 sequences must be 833 interpreted as a U+FFFD REPLACEMENT CHARACTER. 835 5. Server-side requirements 837 _This section only applies to servers._ 839 5.1. Minimal handshake 841 NOTE: This section describes the minimal requirements for a server- 842 side implementation of Web Sockets. 844 Listen on a port for TCP/IP. Upon receiving a connection request, 845 open a connection and send the following bytes back to the client: 847 48 54 54 50 2f 31 2e 31 20 31 30 31 20 57 65 62 848 20 53 6f 63 6b 65 74 20 50 72 6f 74 6f 63 6f 6c 849 20 48 61 6e 64 73 68 61 6b 65 0d 0a 55 70 67 72 850 61 64 65 3a 20 57 65 62 53 6f 63 6b 65 74 0d 0a 851 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20 55 70 67 72 852 61 64 65 0d 0a 854 Send the string "WebSocket-Origin" followed by a U+003A COLON (":") 855 and a U+0020 SPACE, followed by the ASCII serialization of the origin 856 from which the server is willing to accept connections, followed by a 857 CRLF pair (0x0d 0x0a). [ORIGIN] 859 For instance: 861 WebSocket-Origin: http://example.com 863 Send the string "WebSocket-Location" followed by a U+003A COLON (":") 864 and a U+0020 SPACE, followed by the URL of the Web Socket script, 865 followed by a CRLF pair (0x0d 0x0a). 867 For instance: 869 WebSocket-Location: ws://example.com/demo 871 NOTE: Do not include the port if it is the default port for Web 872 Socket protocol connections of the type in question (80 for 873 unencrypted connections and 443 for encrypted connections). 875 Send another CRLF pair (0x0d 0x0a). 877 Read data from the client until four bytes 0x0d 0x0a 0x0d 0x0a are 878 read. This data must either be discarded or handled as described in 879 the following section describing the handshake details. 881 If the connection isn't dropped at this point, go to the data framing 882 section. 884 5.2. Handshake details 886 The previous section ignores the data that is transmitted by the 887 client during the handshake. 889 The data sent by the client consists of a number of fields separated 890 by CR LF pairs (bytes 0x0d 0x0a). 892 The first field consists of three tokens separated by space 893 characters (byte 0x20). The middle token is the path being opened. 894 If the server supports multiple paths, then the server should echo 895 the value of this field in the initial handshake, as part of the URL 896 given on the |WebSocket-Location| line (after the appropriate scheme 897 and host). 899 If the first field does not have three tokens, the server should 900 abort the connection as it probably represents an errorneous client. 902 The remaining fields consist of name-value pairs, with the name part 903 separated from the value part by a colon and a space (bytes 0x3a 904 0x20). Of these, several are interesting: 906 Host (bytes 48 6f 73 74) 907 The value gives the hostname that the client intended to use when 908 opening the Web Socket. It would be of interest in particular to 909 virtual hosting environments, where one server might serve 910 multiple hosts, and might therefore want to return different data. 912 The right host has to be output as part of the URL given on the 913 |WebSocket-Location| line of the handshake described above, to 914 verify that the server knows that it is really representing that 915 host. 917 Origin (bytes 4f 72 69 67 69 6e) 918 The value gives the scheme, hostname, and port (if it's not the 919 default port for the given scheme) of the page that asked the 920 client to open the Web Socket. It would be interesting if the 921 server's operator had deals with operators of other sites, since 922 the server could then decide how to respond (or indeed, _whether_ 923 to respond) based on which site was requesting a connection. 925 If the server supports connections from more than one origin, then 926 the server should echo the value of this field in the initial 927 handshake, on the |WebSocket-Origin| line. 929 Other fields 930 Other fields can be used, such as "Cookie" or "Authorization", for 931 authentication purposes. 933 Any fields that lack the colon-space separator should be discarded 934 and may cause the server to disconnect. 936 5.3. Data framing 938 NOTE: This section only describes how to handle content that this 939 specification allows user agents to send (text). It doesn't handle 940 any arbitrary content in the same way that the requirements on user 941 agents defined earlier handle any content including possible future 942 extensions to the protocols. 944 The server must run through the following steps to process the bytes 945 sent by the client: 947 1. Read a byte from the client. Assuming everything is going 948 according to plan, it will be a 0x00 byte. If the byte is not a 949 0x00 byte, then the server may disconnect. 951 2. Let /raw data/ be an empty byte array. 953 3. _Data_: Read a byte, let /b/ be that byte. 955 4. If /b/ is not 0xff, then append /b/ to /raw data/ and return to 956 the previous step (labeled _data_). 958 5. Interpret /raw data/ as a UTF-8 string, and apply whatever 959 server-specific processing is to occur for the resulting string. 961 6. Return to the first step to read the next byte. 963 The server must run through the following steps to send strings to 964 the client: 966 1. Send a 0x00 byte to the client to indicate the start of a string. 968 2. Encode /data/ using UTF-8 and send the resulting byte stream to 969 the client. 971 3. Send a 0xff byte to the client to indicate the end of the 972 message. 974 6. Closing the connection 976 To *close the Web Socket connection*, either the user agent or the 977 server closes the TCP/IP connection. There is no closing handshake. 978 Whether the user agent or the server closes the connection, it is 979 said that the *Web Socket connection is closed*. 981 Servers may close the Web Socket connection whenever desired. 983 User agents should not close the Web Socket connection arbitrarily. 985 7. Security considerations 987 While this protocol is intended to be used by scripts in Web pages, 988 it can also be used directly by hosts. Such hosts are acting on 989 their own behalf, and can therefore send fake "Origin" headers, 990 misleading the server. Servers should therefore be careful about 991 assuming that they are talking directly to scripts from known 992 origins, and must consider that they might be accessed in unexpected 993 ways. In particular, a server should not trust that any input is 994 valid. 996 EXAMPLE: For example, if the server uses input as part of SQL 997 queries, all input text should be escaped before being passed to the 998 SQL server, lest the server be susceptible to SQL injection. 1000 Servers that are not intended to process input from any Web page but 1001 only for certain sites should verify the "Origin" header is an origin 1002 they expect, and should only respond with the corresponding 1003 "WebSocket-Origin" if it is an accepted origin. Servers that only 1004 accept input from one origin can just send back that value in the 1005 "WebSocket-Origin" header, without bothering to check the client's 1006 value. 1008 If at any time a server is faced with data that it does not 1009 understand, or that violates some criteria by which the server 1010 determines safety of input, or when the server sees a handshake that 1011 does not correspond to the values the server is expecting (e.g. 1012 incorrect path or origin), the server should just disconnect. It is 1013 always safe to disconnect. 1015 8. IANA considerations 1017 8.1. Registration of ws: scheme 1019 A |ws:| URL identifies a Web Socket server and resource name. 1021 URI scheme name. 1022 ws 1024 Status. 1025 Permanent. 1027 URI scheme syntax. 1028 In ABNF terms using the terminals from the IRI specifications: 1029 [RFC5234] [RFC3987] 1031 "ws" ":" ihier-part [ "?" iquery ] 1033 The path and query components form the resource name sent to the 1034 server to identify the kind of service desired. Other components 1035 have the meanings described in RFC3987. 1037 URI scheme semantics. 1038 The only operation for this scheme is to open a connection using 1039 the Web Socket protocol. 1041 Encoding considerations. 1042 See RFC3987. [RFC3987] 1044 Applications/protocols that use this URI scheme name. 1045 Web Socket protocol. 1047 Interoperability considerations. 1048 None. 1050 Security considerations. 1051 See "Security considerations" section above. 1053 Contact. 1054 Ian Hickson 1056 Author/Change controller. 1057 Ian Hickson 1059 References. 1060 This document. 1062 8.2. Registration of wss: scheme 1064 A |wss:| URL identifies a Web Socket server and resource name, and 1065 indicates that traffic over that connection is to be encrypted. 1067 URI scheme name. 1068 wss 1070 Status. 1071 Permanent. 1073 URI scheme syntax. 1074 In ABNF terms using the terminals from the IRI specifications: 1075 [RFC5234] [RFC3987] 1077 "ws" ":" ihier-part [ "?" iquery ] 1079 The path and query components form the resource name sent to the 1080 server to identify the kind of service desired. Other components 1081 have the meanings described in RFC3987. 1083 URI scheme semantics. 1084 The only operation for this scheme is to open a connection using 1085 the Web Socket protocol, encrypted using TLS. 1087 Encoding considerations. 1088 See RFC3987. [RFC3987] 1090 Applications/protocols that use this URI scheme name. 1091 Web Socket protocol over TLS. 1093 Interoperability considerations. 1094 None. 1096 Security considerations. 1097 See "Security considerations" section above. 1099 Contact. 1100 Ian Hickson 1102 Author/Change controller. 1103 Ian Hickson 1105 References. 1106 This document. 1108 8.3. Registration of the "WebSocket" HTTP Upgrade keyword 1110 Name of token. 1111 WebSocket 1113 Author/Change controller. 1114 Ian Hickson 1116 Contact. 1117 Ian Hickson 1119 References. 1120 This document. 1122 9. Using the Web Socket protocol from other specifications 1124 The Web Socket protocol is intended to be used by another 1125 specification to provide a generic mechanism for dynamic author- 1126 defined content, e.g. in a specification defining a scripted API. 1128 Such a specification first needs to "establish a Web Socket 1129 connection", providing that algorithm with: 1131 o The destination, consisting of a /host/ and a /port/. 1133 o A /resource name/, which allows for multiple services to be 1134 identified at one host and port. 1136 o A /secure/ flag, which is true if the connection is to be 1137 encrypted, and false otherwise. 1139 o An ASCII serialization of an origin that is being made responsible 1140 for the connection. [ORIGIN] 1142 o Optionally a string identifying a protocol that is to be layered 1143 over the Web Socket connection. 1145 The /host/, /port/, /resource name/, and /secure/ flag are usually 1146 obtained from a URL using the steps to parse a Web Socket URL's 1147 components. These steps fail if the URL does not specify a Web 1148 Socket. 1150 If a connection can be established, then it is said that the "Web 1151 Socket connection is established". 1153 If at any time the connection is to be closed, then the specification 1154 needs to use the "close the Web Socket connection" algorithm. 1156 When the connection is closed, for any reason including failure to 1157 establish the connection in the first place, it is said that the "Web 1158 Socket connection is closed". 1160 While a connection is open, the specification will need to handle the 1161 cases when "a Web Socket message has been received" with text /data/. 1163 To send some text /data/ to an open connection, the specification 1164 needs to "send /data/ using the Web Socket". 1166 10. Normative References 1168 [HTML5] Hickson, I., "HTML5", September 2009. 1170 [ORIGIN] Barth, A., Jackson, C., and I. Hickson, "The HTTP Sec-From 1171 Header", July 2009. 1173 [RFC2109] Kristol, D. and L. Montulli, "HTTP State Management 1174 Mechanism", RFC 2109, February 1997. 1176 [RFC2119] Bradner, S., "Key words for use in RFCs to Indicate 1177 Requirement Levels", BCP 14, RFC 2119, March 1997. 1179 [RFC2246] Dierks, T. and C. Allen, "The TLS Protocol Version 1.0", 1180 RFC 2246, January 1999. 1182 [RFC2616] Fielding, R., Gettys, J., Mogul, J., Frystyk, H., 1183 Masinter, L., Leach, P., and T. Berners-Lee, "Hypertext 1184 Transfer Protocol -- HTTP/1.1", RFC 2616, June 1999. 1186 [RFC2965] Kristol, D. and L. Montulli, "HTTP State Management 1187 Mechanism", RFC 2965, October 2000. 1189 [RFC3629] Yergeau, F., "UTF-8, a transformation format of ISO 1190 10646", STD 63, RFC 3629, November 2003. 1192 [RFC3987] Duerst, M. and M. Suignard, "Internationalized Resource 1193 Identifiers (IRIs)", RFC 3987, January 2005. 1195 [RFC5234] Crocker, D. and P. Overell, "Augmented BNF for Syntax 1196 Specifications: ABNF", STD 68, RFC 5234, January 2008. 1198 [WEBADDRESSES] 1199 Connolly, D. and C. Sperberg-McQueen, "Web addresses in 1200 HTML 5", May 2009. 1202 [WSAPI] Hickson, I., "The Web Sockets API", September 2009. 1204 Author's Address 1206 Ian Hickson 1207 Google, Inc. 1209 Email: ian@hixie.ch 1210 URI: http://ln.hixie.ch/