idnits 2.17.1 draft-hixie-thewebsocketprotocol-43.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 21, 2009) is 5303 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) ** Obsolete normative reference: RFC 3490 (Obsoleted by RFC 5890, RFC 5891) -- Possible downref: Non-RFC (?) normative reference: ref. 'WEBADDRESSES' -- Possible downref: Non-RFC (?) normative reference: ref. 'WSAPI' Summary: 7 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 21, 2009 5 Expires: March 25, 2010 7 The Web Socket protocol 8 draft-hixie-thewebsocketprotocol-43 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 25, 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 . . . . . . . . . . . . . . . . . . . . 6 67 1.4. Security model . . . . . . . . . . . . . . . . . . . . . . 6 68 1.5. Relationship to TCP/IP and HTTP . . . . . . . . . . . . . 6 69 1.6. Establishing a connection . . . . . . . . . . . . . . . . 7 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 The protocol is designed to support other frame types in future. 167 Instead of the 0x00 byte, other bytes might in future be defined. 168 Frames denoted by bytes that do not have the high bit set (0x00 to 169 0x7F) are treated as described above (a stream of bytes terminated by 170 0xFF). Frames denoted by bytes that have the high bit set (0x80 to 171 0xFF) or equal to 0x80) have a leading length indicator, which is 172 encoded as a series of 7-bit bytes stored in octets with the 8th bit 173 being set for all but the last byte. The remainder of the frame is 174 then as much data as was specified. 176 The following diagrams summarise the protocol: 178 Handshake 179 | 180 \|/ 181 Frame type byte <-------------------------------------. 182 | | | 183 | `-- (0x00 .. 0x7F) --> Data... --> 0xFF -->-+ 184 | | 185 `-- (0x80 .. 0xFF) --> Length --> Data... ------->-' 187 1.3. Design philosophy 189 _This section is non-normative._ 191 The Web Socket protocol is designed on the principle that there 192 should be minimal framing (the only framing that exists is to make 193 the protocol frame-based instead of stream-based, and to support a 194 distinction between Unicode text and binary frames). It is expected 195 that metadata would be layered on top of Web Socket by the 196 application layer, in the same way that metadata is layered on top of 197 TCP/IP by the application layer (HTTP). 199 Conceptually, Web Socket is really just a layer on top of TCP/IP that 200 adds a Web "origin"-based security model for browsers; adds an 201 addressing and protocol naming mechanism to support multiple services 202 on one port and multiple host names on one IP address; and layers a 203 framing mechanism on top of TCP to get back to the IP packet 204 mechanism that TCP is built on, but without length limits. Other 205 than that, it adds nothing. Basically it is intended to be as close 206 as possible to just exposing raw TCP/IP to script as possible given 207 the constraints of the Web. It's also designed in such a way that its 208 servers can share a port with HTTP servers, by having its handshake 209 be a valid HTTP Upgrade handshake also. 211 1.4. Security model 213 _This section is non-normative._ 215 The Web Socket protocol uses the origin model used by Web browsers to 216 restrict which Web pages can contact a Web Socket server when the Web 217 Socket protocol is used from a Web page. Naturally, when the Web 218 Socket protocol is used directly (not from a Web page), the origin 219 model is not useful, as the client can provide any arbitrary origin 220 string. 222 1.5. Relationship to TCP/IP and HTTP 224 _This section is non-normative._ 226 The Web Socket protocol is an independent TCP-based protocol. Its 227 only relationship to HTTP is that its handshake is interpreted by 228 HTTP servers as an Upgrade request. 230 Based on the expert recommendation of the IANA, the Web Socket 231 protocol by default uses port 80 for regular Web Socket connections 232 and port 443 for Web Socket connections tunneled over TLS. 234 1.6. Establishing a connection 236 _This section is non-normative._ 238 There are several options for establishing a Web Socket connection. 240 The simplest method is to use port 80 to get a direct connection to a 241 Web Socket server. Port 80 traffic, however, will often be 242 intercepted by HTTP proxies, which can lead to the connection failing 243 to be established. 245 The second simplest method is to use TLS encryption and port 443 to 246 connect directly to a Web Socket server. This has the advantage of 247 being more secure; however, TLS encryption can be computationally 248 expensive. 250 When a connection is to be made to a port that is shared by an HTTP 251 server (a situation that is quite likely to occur with traffic to 252 ports 80 and 443), the connection will appear to the HTTP server to 253 be a regular GET request with an Upgrade offer. In relatively simple 254 setups with just one IP address and a single server for all traffic 255 to a single hostname, this might allow a practical way for systems 256 based on the Web Socket protocol to be deployed. In more elaborate 257 setups (e.g. with load balancers and multiple servers), a dedicated 258 set of hosts for Web Socket connections separate from the HTTP 259 servers is probably easier to manage. 261 2. Conformance requirements 263 All diagrams, examples, and notes in this specification are non- 264 normative, as are all sections explicitly marked non-normative. 265 Everything else in this specification is normative. 267 The key words "MUST", "MUST NOT", "REQUIRED", "SHOULD", "SHOULD NOT", 268 "RECOMMENDED", "MAY", and "OPTIONAL" in the normative parts of this 269 document are to be interpreted as described in RFC2119. For 270 readability, these words do not appear in all uppercase letters in 271 this specification. [RFC2119] 273 Requirements phrased in the imperative as part of algorithms (such as 274 "strip any leading space characters" or "return false and abort these 275 steps") are to be interpreted with the meaning of the key word 276 ("must", "should", "may", etc) used in introducing the algorithm. 278 Conformance requirements phrased as algorithms or specific steps may 279 be implemented in any manner, so long as the end result is 280 equivalent. (In particular, the algorithms defined in this 281 specification are intended to be easy to follow, and not intended to 282 be performant.) 284 Implementations may impose implementation-specific limits on 285 otherwise unconstrained inputs, e.g. to prevent denial of service 286 attacks, to guard against running out of memory, or to work around 287 platform-specific limitations. 289 The conformance classes defined by this specification are user agents 290 and servers. 292 2.1. Terminology 294 *Converting a string to ASCII lowercase* means replacing all 295 characters in the range U+0041 .. U+005A (i.e. LATIN CAPITAL LETTER 296 A to LATIN CAPITAL LETTER Z) with the corresponding characters in the 297 range U+0061 .. U+007A (i.e. LATIN SMALL LETTER A to LATIN SMALL 298 LETTER Z). 300 The term "URL" is used in this section in a manner consistent with 301 the terminology used in HTML, namely, to denote a string that might 302 or might not be a valid URI or IRI and to which certain error 303 handling behaviors will be applied when the string is parsed. 304 [HTML5] 306 3. Web Socket URLs 308 3.1. Parsing Web Socket URLs 310 The steps to *parse a Web Socket URL's components* from a string 311 /url/ are as follows. These steps return either a /host/, a /port/, 312 a /resource name/, and a /secure/ flag, or they fail. 314 1. If /protocol/ is specified but is either the empty string or 315 contains characters that are not in the range U+0021 .. U+007E, 316 then fail this algorithm. 318 2. If the /url/ string is not an absolute URL, then fail this 319 algorithm. [WEBADDRESSES] 321 3. Resolve the /url/ string using the resolve a Web address 322 algorithm defined by the Web addresses specification, with the 323 URL character encoding set to UTF-8. [WEBADDRESSES] [RFC3629] 325 NOTE: It doesn't matter what it is resolved relative to, since 326 we already know it is an absolute URL at this point. 328 4. If /url/ does not have a component whose value is 329 either "ws" or "wss", when compared in an ASCII case-insensitive 330 manner, then fail this algorithm. 332 5. If the component of /url/ is "ws", set /secure/ to 333 false; otherwise, the component is "wss", set /secure/ 334 to true. 336 6. Let /host/ be the value of the component of /url/, 337 converted to ASCII lowercase. 339 7. If /url/ has a component, then let /port/ be that 340 component's value; otherwise, there is no explicit /port/. 342 8. If there is no explicit /port/, then: if /secure/ is false, let 343 /port/ be 80, otherwise let /port/ be 443. 345 9. Let /resource name/ be the value of the component (which 346 might be empty) of /url/. 348 10. If /resource name/ is the empty string, set it to a single 349 character U+002F SOLIDUS (/). 351 11. If /url/ has a component, then append a single U+003F 352 QUESTION MARK (?) character to /resource name/, followed by the 353 value of the component. 355 12. Return /host/, /port/, /resource name/, and /secure/. 357 3.2. Constructing Web Socket URLs 359 The steps to *construct a Web Socket URL* from a /host/, a /port/, a 360 /resource name/, and a /secure/ flag, are as follows: 362 1. Let /url/ be the empty string. 364 2. If the /secure/ flag is false, then append the string "ws://" to 365 /url/. Otherwise, append the string "wss://" to /url/. 367 3. Append /host/ to /url/. 369 4. If the /secure/ flag is false and port is not 80, or if the 370 /secure/ flag is true and port is not 443, then append the string 371 ":" followed by /port/ to /url/. 373 5. Append /resource name/ to /url/. 375 6. Return /url/. 377 4. Client-side requirements 379 _This section only applies to user agents, not to servers._ 381 NOTE: This specification doesn't currently define a limit to the 382 number of simultaneous connections that a client can establish to a 383 server. 385 4.1. Handshake 387 When the user agent is to *establish a Web Socket connection* to a 388 host /host/, on a port /port/, from an origin whose ASCII 389 serialization is /origin/, with a flag /secure/, with a string giving 390 a /resource name/, and optionally with a string giving a /protocol/, 391 it must run the following steps. The /resource name/ string must 392 start with a U+002F SOLIDUS (/) character. [ORIGIN] 394 1. If the user agent already has a Web Socket connection to the 395 remote host (IP address) identified by /host/, even if known by 396 another name, wait until that connection has been established or 397 for that connection to have failed. 399 NOTE: This makes it harder for a script to perform a denial of 400 service attack by just opening a large number of Web Socket 401 connections to a remote host. 403 NOTE: There is no limit to the number of established Web Socket 404 connections a user agent can have with a single remote host. 405 Servers can refuse to connect users with an excessive number of 406 connections, or disconnect resource-hogging users when suffering 407 high load. 409 2. _Connect_: If the user agent is configured to use a proxy when 410 using the Web Socket protocol to connect to host /host/ and/or 411 port /port/, then connect to that proxy and ask it to open a 412 TCP/IP connection to the host given by /host/ and the port given 413 by /port/. 415 EXAMPLE: For example, if the user agent uses an HTTP proxy 416 for all traffic, then if it was to try to connect to port 80 417 on server example.com, it might send the following lines to 418 the proxy server: 420 CONNECT example.com:80 HTTP/1.1 421 Host: example.com 423 If there was a password, the connection might look like: 425 CONNECT example.com:80 HTTP/1.1 426 Host: example.com 427 Proxy-authorization: Basic ZWRuYW1vZGU6bm9jYXBlcyE= 429 Otherwise, if the user agent is not configured to use a proxy, 430 then open a TCP/IP connection to the host given by /host/ and 431 the port given by /port/. 433 NOTE: Implementations that do not expose explicit UI for 434 selecting a proxy for Web Socket connections separate from other 435 proxies are encouraged to use a SOCKS proxy for Web Socket 436 connections, if available, or failing that, to prefer the proxy 437 configured for HTTPS connections over the proxy configured for 438 HTTP connections. 440 For the purpose of proxy autoconfiguration scripts, the URL to 441 pass the function must be constructed from /host/, /port/, 442 /resource name/, and the /secure/ flag using the steps to 443 construct a Web Socket URL. 445 NOTE: The WebSocket protocol can be identified in proxy 446 autoconfiguration scripts from the scheme ("ws:" for unencrypted 447 connections and "wss:" for encrypted connections). 449 3. If the connection could not be opened, then fail the Web Socket 450 connection and abort these steps. 452 4. If /secure/ is true, perform a TLS handshake over the 453 connection. If this fails (e.g. the server's certificate could 454 not be verified), then fail the Web Socket connection and abort 455 these steps. Otherwise, all further communication on this 456 channel must run through the encrypted tunnel. [RFC2246] 458 5. Send the following bytes to the remote side (the server): 460 47 45 54 20 462 Send the /resource name/ value, encoded as US-ASCII. 464 Send the following bytes: 466 20 48 54 54 50 2f 31 2e 31 0d 0a 55 70 67 72 61 467 64 65 3a 20 57 65 62 53 6f 63 6b 65 74 0d 0a 43 468 6f 6e 6e 65 63 74 69 6f 6e 3a 20 55 70 67 72 61 469 64 65 0d 0a 471 NOTE: The string "GET ", the path, " HTTP/1.1", CRLF, the string 472 "Upgrade: WebSocket", CRLF, and the string "Connection: 474 Upgrade", CRLF. 476 6. Send the following bytes: 478 48 6f 73 74 3a 20 480 Send the /host/ value, converted to ASCII lowercase, and encoded 481 as US-ASCII. 483 If /secure/ is false, and /port/ is not 80, or if /secure/ is 484 true, and /port/ is not 443, then send an 0x3a byte (":") 485 followed by the value of /port/, expressed as a base-ten 486 integer, encoded as US-ASCII. 488 Send the following bytes: 490 0d 0a 492 NOTE: The string "Host: ", the host, and CRLF. 494 7. Send the following bytes: 496 4f 72 69 67 69 6e 3a 20 498 Send the /origin/ value, converted to ASCII lowercase, encoded 499 as US-ASCII. [ORIGIN] 501 NOTE: The /origin/ value is a string that was passed to this 502 algorithm. 504 Send the following bytes: 506 0d 0a 508 NOTE: The string "Origin: ", the origin, and CRLF. 510 8. If there is no /protocol/, then skip this step. 512 Otherwise, send the following bytes: 514 57 65 62 53 6f 63 6b 65 74 2d 50 72 6f 74 6f 63 515 6f 6c 3a 20 517 Send the /protocol/ value, encoded as US-ASCII. 519 Send the following bytes: 521 0d 0a 523 NOTE: The string "WebSocket-Protocol: ", the protocol, and CRLF. 525 9. If the client has any authentication information or cookies that 526 would be relevant to a resource accessed over HTTP, if /secure/ 527 is false, or HTTPS, if it is true, on host /host/, port /port/, 528 with /resource name/ as the path (and possibly query 529 parameters), then HTTP headers that would be appropriate for 530 that information should be sent at this point. [RFC2616] 531 [RFC2109] [RFC2965] 533 Each header must be on a line of its own (each ending with a CR 534 LF sequence). For the purposes of this step, each header must 535 not be split into multiple lines (despite HTTP otherwise 536 allowing this with continuation lines). 538 EXAMPLE: For example, if the server had a username and 539 password that applied to |http://example.com/socket|, and the 540 Web Socket was being opened to |ws://example.com/socket|, it 541 could send them: 543 Authorization: Basic d2FsbGU6ZXZl 545 10. Send the following bytes: 547 0d 0a 549 NOTE: Just a CRLF (a blank line). 551 11. Read bytes from the server until either the connection closes, 552 or a 0x0a byte is read. Let /header/ be these bytes, including 553 the 0x0a byte. 555 If /header/ is not at least two bytes long, or if the last two 556 bytes aren't 0x0d and 0x0a respectively, then fail the Web 557 Socket connection and abort these steps. 559 User agents may apply a timeout to this step, failing the Web 560 Socket connection if the server does not send back data in a 561 suitable time period. 563 12. If /header/ consists of 44 bytes that exactly match the 564 following, then let /mode/ be _normal_. 566 48 54 54 50 2f 31 2e 31 20 31 30 31 20 57 65 62 567 20 53 6f 63 6b 65 74 20 50 72 6f 74 6f 63 6f 6c 568 20 48 61 6e 64 73 68 61 6b 65 0d 0a 570 NOTE: The string "HTTP/1.1 101 Web Socket Protocol Handshake" 571 followed by a CRLF pair. 573 Otherwise, let /code/ be the substring of /header/ that starts 574 from the byte after the first 0x20 byte, and ends with the byte 575 before the second 0x20 byte. If there are not at least two 0x20 576 bytes in /header/, then fail the Web Socket connection and abort 577 these steps. 579 If /code/, interpreted as ASCII, is "401", then let /mode/ be 580 _authenticate_. 582 Otherwise, fail the Web Socket connection and abort these steps. 584 13. If /mode/ is _normal_, then read 41 bytes from the server. 586 If the connection closes before 41 bytes are received, or if the 587 41 bytes aren't exactly equal to the following bytes, then fail 588 the Web Socket connection and abort these steps. 590 55 70 67 72 61 64 65 3a 20 57 65 62 53 6f 63 6b 591 65 74 0d 0a 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20 592 55 70 67 72 61 64 65 0d 0a 594 NOTE: The string "Upgrade: WebSocket", CRLF, the string 595 "Connection: Upgrade", CRLF. 597 User agents may apply a timeout to this step, failing the Web 598 Socket connection if the server does not respond with the above 599 bytes within a suitable time period. 601 NOTE: This step is skipped if /mode/ is _authenticate_. 603 14. Let /headers/ be a list of name-value pairs, initially empty. 605 15. _Header_: Let /name/ and /value/ be empty byte arrays. 607 16. Read a byte from the server. 609 If the connection closes before this byte is received, then fail 610 the Web Socket connection and abort these steps. 612 Otherwise, handle the byte as described in the appropriate entry 613 below: 615 -> If the byte is 0x0d (ASCII CR) 616 If the /name/ byte array is empty, then jump to the headers 617 processing step. Otherwise, fail the Web Socket connection 618 and abort these steps. 620 -> If the byte is 0x0a (ASCII LF) 621 Fail the Web Socket connection and abort these steps. 623 -> If the byte is 0x3a (ASCII ":") 624 Move on to the next step. 626 -> If the byte is in the range 0x41 .. 0x5a (ASCII "A" .. "Z") 627 Append a byte whose value is the byte's value plus 0x20 to 628 the /name/ byte array and redo this step for the next byte. 630 -> Otherwise 631 Append the byte to the /name/ byte array and redo this step 632 for the next byte. 634 NOTE: This reads a header name, terminated by a colon, 635 converting upper-case ASCII letters to lowercase, and aborting 636 if a stray CR or LF is found. 638 17. Read a byte from the server. 640 If the connection closes before this byte is received, then fail 641 the Web Socket connection and abort these steps. 643 Otherwise, handle the byte as described in the appropriate entry 644 below: 646 -> If the byte is 0x20 (ASCII space) 647 Ignore the byte and move on to the next step. 649 -> Otherwise 650 Treat the byte as described by the list in the next step, 651 then move on to that next step for real. 653 NOTE: This skips past a space character after the colon, if 654 necessary. 656 18. Read a byte from the server. 658 If the connection closes before this byte is received, then fail 659 the Web Socket connection and abort these steps. 661 Otherwise, handle the byte as described in the appropriate entry 662 below: 664 -> If the byte is 0x0d (ASCII CR) 665 Move on to the next step. 667 -> If the byte is 0x0a (ASCII LF) 668 Fail the Web Socket connection and abort these steps. 670 -> Otherwise 671 Append the byte to the /value/ byte array and redo this step 672 for the next byte. 674 NOTE: This reads a header value, terminated by a CRLF. 676 19. Read a byte from the server. 678 If the connection closes before this byte is received, or if the 679 byte is not a 0x0a byte (ASCII LF), then fail the Web Socket 680 connection and abort these steps. 682 NOTE: This skips past the LF byte of the CRLF after the header. 684 20. Append an entry to the /headers/ list that has the name given by 685 the string obtained by interpreting the /name/ byte array as a 686 UTF-8 byte stream and the value given by the string obtained by 687 interpreting the /value/ byte array as a UTF-8 byte stream. 689 21. Return to the "Header" step above. 691 22. _Headers processing_: Read a byte from the server. 693 If the connection closes before this byte is received, or if the 694 byte is not a 0x0a byte (ASCII LF), then fail the Web Socket 695 connection and abort these steps. 697 NOTE: This skips past the LF byte of the CRLF after the blank 698 line after the headers. 700 23. If /mode/ is _normal_, then: If there is not exactly one entry 701 in the /headers/ list whose name is "websocket-origin", or if 702 there is not exactly one entry in the /headers/ list whose name 703 is "websocket-location", or if the /protocol/ was specified but 704 there is not exactly one entry in the /headers/ list whose name 705 is "websocket-protocol", or if there are any entries in the 706 /headers/ list whose names are the empty string, then fail the 707 Web Socket connection and abort these steps. Otherwise, handle 708 each entry in the /headers/ list as follows: 710 -> If the entry's name is "websocket-origin" 711 If the value is not exactly equal to /origin/, converted to 712 ASCII lowercase, then fail the Web Socket connection and 713 abort these steps. [ORIGIN] 715 -> If the entry's name is "websocket-location" 716 If the value is not exactly equal to a string obtained from 717 the steps to construct a Web Socket URL from /host/, /port/, 718 /resource name/, and the /secure/ flag, then fail the Web 719 Socket connection and abort these steps. 721 -> If the entry's name is "websocket-protocol" 722 If there was a /protocol/ specified, and the value is not 723 exactly equal to /protocol/, then fail the Web Socket 724 connection and abort these steps. (If no /protocol/ was 725 specified, the header is ignored.) 727 -> If the entry's name is "set-cookie" or "set-cookie2" or 728 another cookie-related header name 729 Handle the cookie as defined by the appropriate spec, with 730 the resource being the one with the host /host/, the port 731 /port/, the path (and possibly query parameters) /resource 732 name/, and the scheme |http| if /secure/ is false and |https| 733 if /secure/ is true. [RFC2109] [RFC2965] 735 -> Any other name 736 Ignore it. 738 If /mode/ is _authenticate_, then: If there is not exactly one 739 entry in the /headers/ list whose name is "www-authenticate", 740 then fail the Web Socket connection and abort these steps. 741 Otherwise, handle each entry in the /headers/ list as follows: 743 -> If the entry's name is "www-authenticate" 744 Obtain credentials in a manner consistent with the 745 requirements for handling the |WWW-Authenticate| header in 746 HTTP, and then close the connection (if the server has not 747 already done so) and jump back to the step labeled _connect_, 748 including the relevant authentication headers in the new 749 request. [RFC2616] 751 -> Any other name 752 Ignore it. 754 24. The *Web Socket connection is established*. Now the user agent 755 must send and receive to and from the connection as described in 756 the next section. 758 4.2. Data framing 760 Once a Web Socket connection is established, the user agent must run 761 through the following state machine for the bytes sent by the server. 763 1. Try to read a byte from the server. Let /frame type/ be that 764 byte. 766 If no byte could be read because the Web Socket connection is 767 closed, then abort. 769 2. Handle the /frame type/ byte as follows: 771 If the high-order bit of the /frame type/ byte is set (i.e. if 772 /frame type/ _and_ed with 0x80 returns 0x80) 773 Run these steps. If at any point during these steps a read is 774 attempted but fails because the Web Socket connection is 775 closed, then abort. 777 1. Let /length/ be zero. 779 2. _Length_: Read a byte, let /b/ be that byte. 781 3. Let /b_v/ be integer corresponding to the low 7 bits of 782 /b/ (the value you would get by _and_ing /b/ with 0x7f). 784 4. Multiply /length/ by 128, add /b_v/ to that result, and 785 store the final result in /length/. 787 5. If the high-order bit of /b/ is set (i.e. if /b/ _and_ed 788 with 0x80 returns 0x80), then return to the step above 789 labeled _length_. 791 6. Read /length/ bytes. 793 7. Discard the read bytes. 795 If the high-order bit of the /frame type/ byte is _not_ set (i.e. 796 if /frame type/ _and_ed with 0x80 returns 0x00) 797 Run these steps. If at any point during these steps a read is 798 attempted but fails because the Web Socket connection is 799 closed, then abort. 801 1. Let /raw data/ be an empty byte array. 803 2. _Data_: Read a byte, let /b/ be that byte. If the client 804 runs out of resources for buffering the incoming data, or 805 hits an artificial resource limit intended to avoid 806 resource starvation, then it must fail the Web Socket 807 connection and abort these steps. 809 3. If /b/ is not 0xff, then append /b/ to /raw data/ and 810 return to the previous step (labeled _data_). 812 4. Interpret /raw data/ as a UTF-8 string, and store that 813 string in /data/. 815 5. If /frame type/ is 0x00, then *a message has been 816 received* with text /data/. Otherwise, discard the data. 818 3. Return to the first step to read the next byte. 820 If the user agent is faced with content that is too large to be 821 handled appropriately, then it must fail the Web Socket connection. 823 Once a Web Socket connection is established, the user agent must use 824 the following steps to *send /data/ using the Web Socket*: 826 1. Send a 0x00 byte to the server. 828 2. Encode /data/ using UTF-8 and send the resulting byte stream to 829 the server. 831 3. Send a 0xff byte to the server. 833 If at any point there is a fatal problem with sending data to the 834 server, the user agent must fail the Web Socket connection. 836 4.3. Closing the connection 838 To *fail the Web Socket connection*, the user agent must close the 839 Web Socket connection, and may report the problem to the user (which 840 would be especially useful for developers). However, user agents 841 must not convey the failure information to the script that attempted 842 the connection in a way distinguishable from the Web Socket being 843 closed normally. 845 Except as indicated above or as specified by the application layer 846 (e.g. a script using the Web Socket API), user agents should not 847 close the connection. 849 4.4. Handling errors in UTF-8 851 When a client is to interpret a byte stream as UTF-8 but finds that 852 the byte stream is not in fact a valid UTF-8 stream, then any bytes 853 or sequences of bytes that are not valid UTF-8 sequences must be 854 interpreted as a U+FFFD REPLACEMENT CHARACTER. 856 5. Server-side requirements 858 _This section only applies to servers._ 860 5.1. Minimal handshake 862 NOTE: This section describes the minimal requirements for a server- 863 side implementation of Web Sockets. 865 Listen on a port for TCP/IP. Upon receiving a connection request, 866 open a connection and send the following bytes back to the client: 868 48 54 54 50 2f 31 2e 31 20 31 30 31 20 57 65 62 869 20 53 6f 63 6b 65 74 20 50 72 6f 74 6f 63 6f 6c 870 20 48 61 6e 64 73 68 61 6b 65 0d 0a 55 70 67 72 871 61 64 65 3a 20 57 65 62 53 6f 63 6b 65 74 0d 0a 872 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20 55 70 67 72 873 61 64 65 0d 0a 875 Send the string "WebSocket-Origin" followed by a U+003A COLON (":") 876 and a U+0020 SPACE, followed by the ASCII serialization of the origin 877 from which the server is willing to accept connections, followed by a 878 CRLF pair (0x0d 0x0a). [ORIGIN] 880 For instance: 882 WebSocket-Origin: http://example.com 884 Send the string "WebSocket-Location" followed by a U+003A COLON (":") 885 and a U+0020 SPACE, followed by the URL of the Web Socket script, 886 followed by a CRLF pair (0x0d 0x0a). 888 For instance: 890 WebSocket-Location: ws://example.com/demo 892 NOTE: Do not include the port if it is the default port for Web 893 Socket protocol connections of the type in question (80 for 894 unencrypted connections and 443 for encrypted connections). 896 Send another CRLF pair (0x0d 0x0a). 898 Read data from the client until four bytes 0x0d 0x0a 0x0d 0x0a are 899 read. This data must either be discarded or handled as described in 900 the following section describing the handshake details. 902 If the connection isn't dropped at this point, go to the data framing 903 section. 905 5.2. Handshake details 907 The previous section ignores the data that is transmitted by the 908 client during the handshake. 910 The data sent by the client consists of a number of fields separated 911 by CR LF pairs (bytes 0x0d 0x0a). 913 The first field consists of three tokens separated by space 914 characters (byte 0x20). The middle token is the path being opened. 915 If the server supports multiple paths, then the server should echo 916 the value of this field in the initial handshake, as part of the URL 917 given on the |WebSocket-Location| line (after the appropriate scheme 918 and host). 920 If the first field does not have three tokens, the server should 921 abort the connection as it probably represents an errorneous client. 923 The remaining fields consist of name-value pairs, with the name part 924 separated from the value part by a colon and a space (bytes 0x3a 925 0x20). Of these, several are interesting: 927 Host (bytes 48 6f 73 74) 928 The value gives the hostname that the client intended to use when 929 opening the Web Socket. It would be of interest in particular to 930 virtual hosting environments, where one server might serve 931 multiple hosts, and might therefore want to return different data. 933 The right host has to be output as part of the URL given on the 934 |WebSocket-Location| line of the handshake described above, to 935 verify that the server knows that it is really representing that 936 host. 938 Origin (bytes 4f 72 69 67 69 6e) 939 The value gives the scheme, hostname, and port (if it's not the 940 default port for the given scheme) of the page that asked the 941 client to open the Web Socket. It would be interesting if the 942 server's operator had deals with operators of other sites, since 943 the server could then decide how to respond (or indeed, _whether_ 944 to respond) based on which site was requesting a connection. 946 If the server supports connections from more than one origin, then 947 the server should echo the value of this field in the initial 948 handshake, on the |WebSocket-Origin| line. 950 Other fields 951 Other fields can be used, such as "Cookie" or "Authorization", for 952 authentication purposes. 954 Any fields that lack the colon-space separator should be discarded 955 and may cause the server to disconnect. 957 5.3. Data framing 959 NOTE: This section only describes how to handle content that this 960 specification allows user agents to send (text). It doesn't handle 961 any arbitrary content in the same way that the requirements on user 962 agents defined earlier handle any content including possible future 963 extensions to the protocols. 965 The server must run through the following steps to process the bytes 966 sent by the client: 968 1. Read a byte from the client. Assuming everything is going 969 according to plan, it will be a 0x00 byte. If the byte is not a 970 0x00 byte, then the server may disconnect. 972 2. Let /raw data/ be an empty byte array. 974 3. _Data_: Read a byte, let /b/ be that byte. 976 4. If /b/ is not 0xff, then append /b/ to /raw data/ and return to 977 the previous step (labeled _data_). 979 5. Interpret /raw data/ as a UTF-8 string, and apply whatever 980 server-specific processing is to occur for the resulting string. 982 6. Return to the first step to read the next byte. 984 The server must run through the following steps to send strings to 985 the client: 987 1. Send a 0x00 byte to the client to indicate the start of a string. 989 2. Encode /data/ using UTF-8 and send the resulting byte stream to 990 the client. 992 3. Send a 0xff byte to the client to indicate the end of the 993 message. 995 6. Closing the connection 997 To *close the Web Socket connection*, either the user agent or the 998 server closes the TCP/IP connection. There is no closing handshake. 999 Whether the user agent or the server closes the connection, it is 1000 said that the *Web Socket connection is closed*. 1002 When a user agent is to close the Web Socket connection, it must drop 1003 all subsequent data from the server and must act as if the server had 1004 immediately closed its side of the connection. 1006 When a user agent notices that the Web Socket connection is closed, 1007 it must immediately close its side of the connection. 1009 Servers may close the Web Socket connection whenever desired. 1011 User agents should not close the Web Socket connection arbitrarily. 1013 7. Security considerations 1015 While this protocol is intended to be used by scripts in Web pages, 1016 it can also be used directly by hosts. Such hosts are acting on 1017 their own behalf, and can therefore send fake "Origin" headers, 1018 misleading the server. Servers should therefore be careful about 1019 assuming that they are talking directly to scripts from known 1020 origins, and must consider that they might be accessed in unexpected 1021 ways. In particular, a server should not trust that any input is 1022 valid. 1024 EXAMPLE: For example, if the server uses input as part of SQL 1025 queries, all input text should be escaped before being passed to the 1026 SQL server, lest the server be susceptible to SQL injection. 1028 Servers that are not intended to process input from any Web page but 1029 only for certain sites should verify the "Origin" header is an origin 1030 they expect, and should only respond with the corresponding 1031 "WebSocket-Origin" if it is an accepted origin. Servers that only 1032 accept input from one origin can just send back that value in the 1033 "WebSocket-Origin" header, without bothering to check the client's 1034 value. 1036 If at any time a server is faced with data that it does not 1037 understand, or that violates some criteria by which the server 1038 determines safety of input, or when the server sees a handshake that 1039 does not correspond to the values the server is expecting (e.g. 1040 incorrect path or origin), the server should just disconnect. It is 1041 always safe to disconnect. 1043 8. IANA considerations 1045 8.1. Registration of ws: scheme 1047 A |ws:| URL identifies a Web Socket server and resource name. 1049 URI scheme name. 1050 ws 1052 Status. 1053 Permanent. 1055 URI scheme syntax. 1056 In ABNF terms using the terminals from the URI specifications: 1057 [RFC5234] [RFC3986] 1059 "ws" ":" hier-part [ "?" query ] 1061 The path and query components form the resource name sent to the 1062 server to identify the kind of service desired. Other components 1063 have the meanings described in RFC3986. 1065 URI scheme semantics. 1066 The only operation for this scheme is to open a connection using 1067 the Web Socket protocol. 1069 Encoding considerations. 1070 Characters in the host component that are excluded by the syntax 1071 defined above must be converted from Unicode to ASCII by applying 1072 the IDNA ToASCII algorithm to the Unicode host name, with both the 1073 AllowUnassigned and UseSTD3ASCIIRules flags set, and using the 1074 result of this algorithm as the host in the URI. [RFC3490] 1076 Characters in other components that are excluded by the syntax 1077 defined above must be converted from Unicode to ASCII by first 1078 encoding the characters as UTF-8 and then replacing the 1079 corresponding bytes using their percent-encoded form as defined in 1080 the URI and IRI specification. [RFC3986] [RFC3987] 1082 Applications/protocols that use this URI scheme name. 1083 Web Socket protocol. 1085 Interoperability considerations. 1086 None. 1088 Security considerations. 1089 See "Security considerations" section above. 1091 Contact. 1092 Ian Hickson 1094 Author/Change controller. 1095 Ian Hickson 1097 References. 1098 This document. 1100 8.2. Registration of wss: scheme 1102 A |wss:| URL identifies a Web Socket server and resource name, and 1103 indicates that traffic over that connection is to be encrypted. 1105 URI scheme name. 1106 wss 1108 Status. 1109 Permanent. 1111 URI scheme syntax. 1112 In ABNF terms using the terminals from the URI specifications: 1113 [RFC5234] [RFC3986] 1115 "wss" ":" hier-part [ "?" query ] 1117 The path and query components form the resource name sent to the 1118 server to identify the kind of service desired. Other components 1119 have the meanings described in RFC3986. 1121 URI scheme semantics. 1122 The only operation for this scheme is to open a connection using 1123 the Web Socket protocol, encrypted using TLS. 1125 Encoding considerations. 1126 Characters in the host component that are excluded by the syntax 1127 defined above must be converted from Unicode to ASCII by applying 1128 the IDNA ToASCII algorithm to the Unicode host name, with both the 1129 AllowUnassigned and UseSTD3ASCIIRules flags set, and using the 1130 result of this algorithm as the host in the URI. [RFC3490] 1132 Characters in other components that are excluded by the syntax 1133 defined above must be converted from Unicode to ASCII by first 1134 encoding the characters as UTF-8 and then replacing the 1135 corresponding bytes using their percent-encoded form as defined in 1136 the URI and IRI specification. [RFC3986] [RFC3987] 1138 Applications/protocols that use this URI scheme name. 1139 Web Socket protocol over TLS. 1141 Interoperability considerations. 1142 None. 1144 Security considerations. 1145 See "Security considerations" section above. 1147 Contact. 1148 Ian Hickson 1150 Author/Change controller. 1151 Ian Hickson 1153 References. 1154 This document. 1156 8.3. Registration of the "WebSocket" HTTP Upgrade keyword 1158 Name of token. 1159 WebSocket 1161 Author/Change controller. 1162 Ian Hickson 1164 Contact. 1165 Ian Hickson 1167 References. 1168 This document. 1170 9. Using the Web Socket protocol from other specifications 1172 The Web Socket protocol is intended to be used by another 1173 specification to provide a generic mechanism for dynamic author- 1174 defined content, e.g. in a specification defining a scripted API. 1176 Such a specification first needs to "establish a Web Socket 1177 connection", providing that algorithm with: 1179 o The destination, consisting of a /host/ and a /port/. 1181 o A /resource name/, which allows for multiple services to be 1182 identified at one host and port. 1184 o A /secure/ flag, which is true if the connection is to be 1185 encrypted, and false otherwise. 1187 o An ASCII serialization of an origin that is being made responsible 1188 for the connection. [ORIGIN] 1190 o Optionally a string identifying a protocol that is to be layered 1191 over the Web Socket connection. 1193 The /host/, /port/, /resource name/, and /secure/ flag are usually 1194 obtained from a URL using the steps to parse a Web Socket URL's 1195 components. These steps fail if the URL does not specify a Web 1196 Socket. 1198 If a connection can be established, then it is said that the "Web 1199 Socket connection is established". 1201 If at any time the connection is to be closed, then the specification 1202 needs to use the "close the Web Socket connection" algorithm. 1204 When the connection is closed, for any reason including failure to 1205 establish the connection in the first place, it is said that the "Web 1206 Socket connection is closed". 1208 While a connection is open, the specification will need to handle the 1209 cases when "a Web Socket message has been received" with text /data/. 1211 To send some text /data/ to an open connection, the specification 1212 needs to "send /data/ using the Web Socket". 1214 10. Normative References 1216 [HTML5] Hickson, I., "HTML5", September 2009. 1218 [ORIGIN] Barth, A., Jackson, C., and I. Hickson, "The HTTP Sec-From 1219 Header", July 2009. 1221 [RFC2109] Kristol, D. and L. Montulli, "HTTP State Management 1222 Mechanism", RFC 2109, February 1997. 1224 [RFC2119] Bradner, S., "Key words for use in RFCs to Indicate 1225 Requirement Levels", BCP 14, RFC 2119, March 1997. 1227 [RFC2246] Dierks, T. and C. Allen, "The TLS Protocol Version 1.0", 1228 RFC 2246, January 1999. 1230 [RFC2616] Fielding, R., Gettys, J., Mogul, J., Frystyk, H., 1231 Masinter, L., Leach, P., and T. Berners-Lee, "Hypertext 1232 Transfer Protocol -- HTTP/1.1", RFC 2616, June 1999. 1234 [RFC2965] Kristol, D. and L. Montulli, "HTTP State Management 1235 Mechanism", RFC 2965, October 2000. 1237 [RFC3490] Faltstrom, P., Hoffman, P., and A. Costello, 1238 "Internationalizing Domain Names in Applications (IDNA)", 1239 RFC 3490, March 2003. 1241 [RFC3629] Yergeau, F., "UTF-8, a transformation format of ISO 1242 10646", STD 63, RFC 3629, November 2003. 1244 [RFC3986] Berners-Lee, T., Fielding, R., and L. Masinter, "Uniform 1245 Resource Identifier (URI): Generic Syntax", STD 66, 1246 RFC 3986, January 2005. 1248 [RFC3987] Duerst, M. and M. Suignard, "Internationalized Resource 1249 Identifiers (IRIs)", RFC 3987, January 2005. 1251 [RFC5234] Crocker, D. and P. Overell, "Augmented BNF for Syntax 1252 Specifications: ABNF", STD 68, RFC 5234, January 2008. 1254 [WEBADDRESSES] 1255 Connolly, D. and C. Sperberg-McQueen, "Web addresses in 1256 HTML 5", May 2009. 1258 [WSAPI] Hickson, I., "The Web Sockets API", September 2009. 1260 Author's Address 1262 Ian Hickson 1263 Google, Inc. 1265 Email: ian@hixie.ch 1266 URI: http://ln.hixie.ch/