HyBi Working Group | I.F. Fette |
Internet-Draft | Google, Inc. |
Intended status: Standards Track | February 26, 2011 |
Expires: August 30, 2011 |
The WebSocket protocol
draft-ietf-hybi-thewebsocketprotocol-06
The WebSocket protocol enables two-way communication between a user agent running untrusted code running in a controlled environment to a remote host that has opted-in to communications from that code. The security model used for this is the Origin-based security model commonly used by Web browsers. The protocol consists of an initial handshake followed by basic message framing, layered over TCP. The goal of this technology is to provide a mechanism for browser-based applications that need two-way communication with servers that does not rely on opening multiple HTTP connections (e.g. using XMLHttpRequest or <iframe>s and long polling).
Please send feedback to the hybi@ietf.org mailing list.
This Internet-Draft is submitted in full conformance with the provisions of BCP 78 and BCP 79.
Internet-Drafts are working documents of the Internet Engineering Task Force (IETF). Note that other groups may also distribute working documents as Internet-Drafts. The list of current Internet- Drafts is at http://datatracker.ietf.org/drafts/current/.
Internet-Drafts are draft documents valid for a maximum of six months and may be updated, replaced, or obsoleted by other documents at any time. It is inappropriate to use Internet-Drafts as reference material or to cite them other than as "work in progress."
This Internet-Draft will expire on August 30, 2011.
Copyright (c) 2011 IETF Trust and the persons identified as the document authors. All rights reserved.
This document is subject to BCP 78 and the IETF Trust's Legal Provisions Relating to IETF Documents (http://trustee.ietf.org/license-info) in effect on the date of publication of this document. Please review these documents carefully, as they describe your rights and restrictions with respect to this document. Code Components extracted from this document must include Simplified BSD License text as described in Section 4.e of the Trust Legal Provisions and are provided without warranty as described in the Simplified BSD License.
This section is non-normative.
Historically, creating an instant messenger chat client as a Web application has required an abuse of HTTP to poll the server for updates while sending upstream notifications as distinct HTTP calls.
This results in a variety of problems:
A simpler solution would be to use a single TCP connection for traffic in both directions. This is what the WebSocket protocol provides. Combined with the WebSocket API, it provides an alternative to HTTP polling for two-way communication from a Web page to a remote server. [WSAPI]
The same technique can be used for a variety of Web applications: games, stock tickers, multiuser applications with simultaneous editing, user interfaces exposing server-side services in real time, etc.
This section is non-normative.
The protocol has two parts: a handshake, and then the data transfer.
The handshake from the client looks as follows:
GET /chat HTTP/1.1 Host: server.example.com Upgrade: websocket Connection: Upgrade Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ== Sec-WebSocket-Origin: http://example.com Sec-WebSocket-Protocol: chat, superchat Sec-WebSocket-Version: 6
The handshake from the server looks as follows:
HTTP/1.1 101 Switching Protocols Upgrade: websocket Connection: Upgrade Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo= Sec-WebSocket-Protocol: chat
The leading line from the client follows the Request-Line format. The leading line from the server follows the Status-Line format. The Request-Line and Status-Line productions are defined in [RFC2616].
After the leading line in both cases come an unordered set of headers. The meaning of these headers is specified in Section 5 of this document. Additional headers may also be present, such as cookies required to identify the user. The format and parsing of headers is as defined in [RFC2616].
Once the client and server have both sent their handshakes, and if the handshake was successful, then the data transfer part starts. This is a two-way communication channel where each side can, independently from the other, send data at will.
Clients and servers, after a successful handshake, transfer data back and forth in conceptual units referred to in this specification as "messages". A message is a complete unit of data at an application level, with the expectation that many or most applications implementing this protocol (such as web user agents) provide APIs in terms of sending and receiving messages. The websocket message does not necessarily correspond to a particular network layer framing, as a fragmented message may be coalesced, or vice versa, e.g. by an intermediary.
Data is sent on the wire in the form of frames that have an associated type. Broadly speaking, there are types for textual data, which is interpreted as UTF-8 text, binary data (whose interpretation is left up to the application), and control frames, which are not intended to carry data for the application, but instead for protocol-level signaling, such as to signal that the connection should be closed. This version of the protocol defines six frame types and leaves ten reserved for future use.
The WebSocket protocol uses this framing so that specifications that use the WebSocket protocol can expose such connections using an event-based mechanism instead of requiring users of those specifications to implement buffering and piecing together of messages manually.
This section is non-normative.
The opening handshake is intended to be compatible with HTTP-based server-side software and intermediaries, so that a single port can be used by both HTTP clients talking to that server and WebSocket clients talking to that server. To this end, the WebSocket client's handshake is an HTTP Upgrade request:
GET /chat HTTP/1.1 Host: server.example.com Upgrade: websocket Connection: Upgrade Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ== Sec-WebSocket-Origin: http://example.com Sec-WebSocket-Protocol: chat, superchat Sec-WebSocket-Version: 6
Headers in the handshake are sent by the client in a random order; the order is not meaningful.
Additional headers are used to select options in the WebSocket protocol. Options available in this version are the subprotocol selector, |Sec-WebSocket-Protocol|, and |Cookie|, which can used for sending cookies to the server (e.g. as an authentication mechanism). The |Sec-WebSocket-Protocol| request-header field can be used to indicate what subprotocols (application-level protocols layered over the WebSocket protocol) are acceptable to the client. The server selects one of the acceptable protocols and echoes that value in its handshake to indicate that it has selected that protocol.
Sec-WebSocket-Protocol: chat
The "Request-URI" of the GET method [RFC2616] is used to identify the endpoint of the WebSocket connection, both to allow multiple domains to be served from one IP address and to allow multiple WebSocket endpoints to be served by a single server.
The client includes the hostname in the Host header of its handshake as per [RFC2616], so that both the client and the server can verify that they agree on which host is in use.
The |Sec-WebSocket-Origin| header is used to protect against unauthorized cross-origin use of a WebSocket server by scripts using the |WebSocket| API in a Web browser. The server is informed of the script origin generating the WebSocket connection request. If the server does not wish to accept connections from this origin, it can choose to abort the connection. This header is sent by browser clients, for non-browser clients this header may be sent if it makes sense in the context of those clients.
Finally, the server has to prove to the client that it received the client's WebSocket handshake, so that the server doesn't accept connections that are not WebSocket connections. This prevents an attacker from tricking a WebSocket server by sending it carefully-crafted packets using |XMLHttpRequest| or a |form| submission.
To prove that the handshake was received, the server has to take two pieces of information and combine them to form a response. The first piece of information comes from the |Sec-WebSocket-Key| header in the client handshake:
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
For this header, the server has to take the value (as present in the header, e.g. the base64-encoded version), and concatenate this with the GUID "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" in string form, which is unlikely to be used by network endpoints that do not understand the WebSocket protocol. A SHA-1 hash, base64-encoded, of this concatenation is then returned in the server's handshake [FIPS.180-2.2002].
Concretely, if as in the example above, header |Sec-WebSocket-Key| had the value "dGhlIHNhbXBsZSBub25jZQ==", the server would concatenate the string "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" to form the string "dGhlIHNhbXBsZSBub25jZQ==258EAFA5-E914-47DA-95CA-C5AB0DC85B11". The server would then take the SHA-1 hash of this, giving the value 0xb3 0x7a 0x4f 0x2c 0xc0 0x62 0x4f 0x16 0x90 0xf6 0x46 0x06 0xcf 0x38 0x59 0x45 0xb2 0xbe 0xc4 0xea. This value is then base64-encoded, to give the value "s3pPLMBiTxaQ9kYGzzhZRbK+xOo=". This value would then be echoed in the header |Sec-WebSocket-Accept|.
The handshake from the server is much simpler than the client handshake. The first line is an HTTP Status-Line, with the status code 101:
HTTP/1.1 101 Switching Protocols
Any status code other than 101 MUST be treated as a failure if semantics of that status code are not defined in the context of a WebSocket connection, and the websocket connection aborted. The headers follow the status code.
The |Connection| and |Upgrade| headers complete the HTTP Upgrade. The |Sec-WebSocket-Accept| header indicates whether the server is willing to accept the connection. If present, this header must include a hash of the client's nonce sent in |Sec-WebSocket-Key| along with a predefined GUID. Any other value must not be interpreted as an acceptance of the connection by the server.
HTTP/1.1 101 Switching Protocols Upgrade: websocket Connection: Upgrade Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
These fields are checked by the Web browser when it is acting as a |WebSocket| client for scripted pages. If the |Sec-WebSocket-Accept| value does not match the expected value, or if the header is missing, or if the HTTP status code is not 101, the connection will not be established and WebSockets frames will not be sent.
Option fields can also be included. In this version of the protocol, the main option field is |Sec-WebSocket-Protocol|, which indicates the subprotocol that the server has selected. Web browsers verify that the server included one of the values as was specified in the |WebSocket| constructor. A server that speaks multiple subprotocols has to make sure it selects one based on the client's handshake and specifies it in its handshake.
Sec-WebSocket-Protocol: chat
The server can also set cookie-related option fields to set cookies, as in HTTP.
This section is non-normative.
The closing handshake is far simpler than the opening handshake.
Either peer can send a control frame with data containing a specified control sequence to begin the closing handshake (detailed in Section 4.5.1). Upon receiving such a frame, the other peer sends a close frame in response, if it hasn't already sent one. Upon receiving that control frame, the first peer then closes the connection, safe in the knowledge that no further data is forthcoming.
After sending a control frame indicating the connection should be closed, a peer does not send any further data; after receiving a control frame indicating the connection should be closed, a peer discards any further data received.
It is safe for both peers to initiate this handshake simultaneously.
The closing handshake is intended to replace the TCP closing handshake (FIN/ACK), on the basis that the TCP closing handshake is not always reliable end-to-end, especially in the presence of man-in-the-middle proxies and other intermediaries.
By sending a close frame and waiting for a close frame in response,
This section is non-normative.
The WebSocket protocol is designed on the principle that there should be minimal framing (the only framing that exists is to make the protocol frame-based instead of stream-based, and to support a distinction between Unicode text and binary frames). It is expected that metadata would be layered on top of WebSocket by the application layer, in the same way that metadata is layered on top of TCP by the application layer (HTTP).
Conceptually, WebSocket is really just a layer on top of TCP that adds a Web "origin"-based security model for browsers; adds an addressing and protocol naming mechanism to support multiple services on one port and multiple host names on one IP address; layers a framing mechanism on top of TCP to get back to the IP packet mechanism that TCP is built on, but without length limits; and re-implements the closing handshake in-band. Other than that, it adds nothing. Basically it is intended to be as close to just exposing raw TCP to script as possible given the constraints of the Web. It's also designed in such a way that its servers can share a port with HTTP servers, by having its handshake be a valid HTTP Upgrade handshake also.
The protocol is intended to be extensible; future versions will likely introduce additional concepts such as multiplexing.
This section is non-normative.
The WebSocket protocol uses the origin model used by Web browsers to restrict which Web pages can contact a WebSocket server when the WebSocket protocol is used from a Web page. Naturally, when the WebSocket protocol is used by a dedicated client directly (i.e. not from a Web page through a Web browser), the origin model is not useful, as the client can provide any arbitrary origin string.
This protocol is intended to fail to establish a connection with servers of pre-existing protocols like SMTP or HTTP, while allowing HTTP servers to opt-in to supporting this protocol if desired. This is achieved by having a strict and elaborate handshake, and by limiting the data that can be inserted into the connection before the handshake is finished (thus limiting how much the server can be influenced).
It is similarly intended to fail to establish a connection when data from other protocols, especially HTTP, is sent to a WebSocket server, for example as might happen if an HTML |form| were submitted to a WebSocket server. This is primarily achieved by requiring that the server prove that it read the handshake, which it can only do if the handshake contains the appropriate parts which themselves can only be sent by a WebSocket handshake. In particular, at the time of writing of this specification, fields starting with |Sec-| cannot be set by an attacker from a Web browser using only HTML and JavaScript APIs such as |XMLHttpRequest|.
This section is non-normative.
The WebSocket protocol is an independent TCP-based protocol. Its only relationship to HTTP is that its handshake is interpreted by HTTP servers as an Upgrade request.
Based on the expert recommendation of the IANA, the WebSocket protocol by default uses port 80 for regular WebSocket connections and port 443 for WebSocket connections tunneled over TLS.
This section is non-normative.
When a connection is to be made to a port that is shared by an HTTP server (a situation that is quite likely to occur with traffic to ports 80 and 443), the connection will appear to the HTTP server to be a regular GET request with an Upgrade offer. In relatively simple setups with just one IP address and a single server for all traffic to a single hostname, this might allow a practical way for systems based on the WebSocket protocol to be deployed. In more elaborate setups (e.g. with load balancers and multiple servers), a dedicated set of hosts for WebSocket connections separate from the HTTP servers is probably easier to manage. At the time of writing of this specification, it should be noted that connections on port 80 and 443 have significantly different success rates, with connections on port 443 being significantly more likely to succeed, though this may change with time.
This section is non-normative.
The client can request that the server use a specific subprotocol by including the |Sec-WebSocket-Protocol| field in its handshake. If it is specified, the server needs to include the same field and one of the selected subprotocol values in its response for the connection to be established.
These subprotocol names do not need to be registered, but if a subprotocol is intended to be implemented by multiple independent WebSocket servers, potential clashes with the names of subprotocols defined independently can be avoided by using names that contain the domain name of the subprotocol's originator. For example, if Example Corporation were to create a Chat subprotocol to be implemented by many servers around the Web, they could name it "chat.example.com". If the Example Organization called their competing subprotocol "example.org's chat protocol", then the two subprotocols could be implemented by servers simultaneously, with the server dynamically selecting which subprotocol to use based on the value sent by the client.
Subprotocols can be versioned in backwards-incompatible ways by changing the subprotocol name, e.g. going from "bookings.example.net" to "v2.bookings.example.net". These subprotocols would be considered completely separate by WebSocket clients. Backwards-compatible versioning can be implemented by reusing the same subprotocol string but carefully designing the actual subprotocol to support this kind of extensibility.
All diagrams, examples, and notes in this specification are non-normative, as are all sections explicitly marked non-normative. Everything else in this specification is normative.
The key words "MUST", "MUST NOT", "REQUIRED", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in the normative parts of this document are to be interpreted as described in RFC2119. For readability, these words do not appear in all uppercase letters in this specification. [RFC2119]
Requirements phrased in the imperative as part of algorithms (such as "strip any leading space characters" or "return false and abort these steps") are to be interpreted with the meaning of the key word ("must", "should", "may", etc) used in introducing the algorithm.
Conformance requirements phrased as algorithms or specific steps may be implemented in any manner, so long as the end result is equivalent. (In particular, the algorithms defined in this specification are intended to be easy to follow, and not intended to be performant.)
Implementations may impose implementation-specific limits on otherwise unconstrained inputs, e.g. to prevent denial of service attacks, to guard against running out of memory, or to work around platform-specific limitations.
The conformance classes defined by this specification are user agents and servers.
ASCII shall mean the character-encoding scheme defined in [ANSI.X3-4.1986].
Converting a string to ASCII lowercase means replacing all characters in the range U+0041 to U+005A (i.e. LATIN CAPITAL LETTER A to LATIN CAPITAL LETTER Z) with the corresponding characters in the range U+0061 to U+007A (i.e. LATIN SMALL LETTER A to LATIN SMALL LETTER Z).
Comparing two strings in an ASCII case-insensitive manner means comparing them exactly, code point for code point, except that the characters in the range U+0041 to U+005A (i.e. LATIN CAPITAL LETTER A to LATIN CAPITAL LETTER Z) and the corresponding characters in the range U+0061 to U+007A (i.e. LATIN SMALL LETTER A to LATIN SMALL LETTER Z) are considered to also match.
The term "URI" is used in this section in a manner consistent with the terminology used in HTML, namely, to denote a string that might or might not be a valid URI or IRI and to which certain error handling behaviors will be applied when the string is parsed. [RFC3986]
When an implementation is required to send data as part of the WebSocket protocol, the implementation may delay the actual transmission arbitrarily, e.g. buffering data so as to send fewer IP packets.
The steps to parse a WebSocket URI's components from a string /uri/ are as follows. These steps return either a /host/, a /port/, a /resource name/, and a /secure/ flag, or they fail.
The steps to construct a WebSocket URI from a /host/, a /port/, a /resource name/, and a /secure/ flag, are as follows:
For a WebSocket URI to be considered valid, the following conditions MUST hold.
Any WebSocket URIs not meeting the above criteria are considered invalid, and a client MUST NOT attempt to make a connection to an invalid WebSocket URI. A client SHOULD attempt to parse a URI obtained from any external source (such as a web site or a user) using the steps specified in Section 3.1 to obtain a valid WebSocket URI, but MUST NOT attempt to connect with such an unparsed URI, and instead only use the parsed version and only if that version is considered valid by the criteria above.
In the WebSocket protocol, data is transmitted using a sequence of frames. Frames sent from the client to the server are masked to avoid confusing network intermediaries, such as intercepting proxies. Frames sent from the server to the client are not masked.
The base framing protocol defines a frame type with an opcode, a payload length, and designated locations for extension and application data, which together define the payload data. Certain bits and opcodes are reserved for future expansion of the protocol. As such, In the absence of extensions negotiated during the opening handshake (Section 5), all reserved bits MUST be 0 and reserved opcode values MUST NOT be used.
A data frame MAY be transmitted by either the client or the server at any time after handshake completion and before that endpoint has sent a close message (Section 4.5.1).
The client MUST mask all frames sent to the server.
The masking-key is contained completely within the frame.
The masking-key is a 32-bit value chosen at random by the client. The masking-key MUST be derived from a strong source of entropy, and the masking-key for a given frame MUST NOT make it simple for a server to predict the masking-key for a subsequent frame.
masked-frame = masking-key masked-data masking-key = 4full-octet masked-data = *full-octet full-octet = %x00-FF
Each masked frame consists of a 32-bit masking-key followed by masked-data:
The masked-data is the clear-text frame "encrypted" using a simple XOR cipher as follows.
Octet i of the masked-data is the XOR of octet i of the clear text frame with octet i modulo 4 of the masking-key:
j = i MOD 4 masked-octet-i = clear-text-octet-i XOR octet-j-of-masking-key
When preparing a masked-frame, the client MUST pick a fresh masking-key uniformly at random from the set of allowed 32-bit values. The unpredictability of the masking-nonce is essential to prevent the author of malicious application data from selecting the bytes that appear on the wire.
This wire format for the data transfer part is described by the ABNF given in detail in this section. A high level overview of the framing is given in the following figure. [RFC5234]
0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-------+-+-------------+-------------------------------+ |F|R|R|R| opcode|R| Payload len | Extended payload length | |I|S|S|S| (4) |S| (7) | (16/63) | |N|V|V|V| |V| | (if payload len==126/127) | | |1|2|3| |4| | | +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - + | Extended payload length continued, if payload len == 127 | + - - - - - - - - - - - - - - - +-------------------------------+ | | Extension data | +-------------------------------+ - - - - - - - - - - - - - - - + : : +---------------------------------------------------------------+ : Application data : +---------------------------------------------------------------+
The base framing protocol is formally defined by the following ABNF [RFC5234]:
ws-frame = frame-fin frame-rsv1 frame-rsv2 frame-rsv3 frame-opcode frame-rsv4 frame-length frame-extension application-data; frame-fin = %x0 ; more frames of this message follow / %x1 ; final frame of message frame-rsv1 = %x0 ; 1 bit, must be 0 frame-rsv2 = %x0 ; 1 bit, must be 0 frame-rsv3 = %x0 ; 1 bit, must be 0 frame-opcode = %x0 ; continuation frame / %x1 ; connection close / %x2 ; ping / %x3 ; pong / %x4 ; text frame / %x5 ; binary frame / %x6-F ; reserved frame-rsv4 = %x0 ; 1 bit, must be 0 frame-length = %x00-7D / %x7E frame-length-16 / %x7F frame-length-63 frame-length-16 = %x0000-FFFF frame-length-63 = %x0000000000000000-7FFFFFFFFFFFFFFF frame-extension = *( %x00-FF ) ; to be defined later application-data = *( %x00-FF )
The primary purpose of fragmentation is to allow sending a message that is of unknown size when the message is started without having to buffer that message. If messages couldn't be fragmented, then an endpoint would have to buffer the entire message so its length could be counted before first byte is sent. With fragmentation, a server or intermediary may choose a reasonable size buffer, and when the buffer is full write a fragment to the network.
A secondary use-case for fragmentation is for multiplexing, where it is not desirable for a large message on one logical channel to monopolize the output channel, so the MUX needs to be free to split the message into smaller fragments to better share the output channel.
The following rules apply to fragmentation:
Control frames have opcodes of 0x01 (Close), 0x02 (Ping), or 0x03 (Pong). Control frames are used to communicate state about the websocket. Control frames can be interjected in the middle of a fragmented message.
All control frames MUST be 125 bytes or less in length and MUST NOT be fragmented.
The Close message contains an opcode of 0x01.
The Close message MAY contain a body (the "application data" portion of the frame) that indicates a reason for closing, such as an endpoint shutting down, an endpoint having received a message too large, or an endpoint having received a message that does not conform to the format expected by the other endpoint. If there is a body, the first two bytes of the body MUST be a 2-byte integer (in network byte order) representing a status code defined in Section 7.4. Following the 2-byte integer the body MAY contain UTF-8 encoded data, the interpretation of which is not defined by this specification.
The application MUST NOT send any more data messages after sending a close message.
If an endpoint receives a Close message and that endpoint did not previously send a Close message, the endpoint MUST send a Close message in response. It SHOULD do so as soon as is practical.
After both sending and receiving a close message, an endpoint considers the websocket connection closed, and SHOULD close the underlying TCP connection.
If a client and server both send a Close message at the same time, both endpoints will have sent and received a Close message and should consider the websocket connection closed and close the underlying TCP connection.
The Ping message contains an opcode of 0x02.
Upon receipt of a Ping message, an endpoint MUST send a Pong message in response. It SHOULD do so as soon as is practical. The message bodies of the Ping and Pong MUST be the same.
The Pong message contains an opcode of 0x03.
Upon receipt of a Ping message, an endpoint MUST send a Pong message in response. It SHOULD do so as soon as is practical. The message bodies of the Ping and Pong MUST be the same. A Pong is issued only in response to the most recent Ping.
All frame types not listed in Section 4.5 are data frames, which transport application-layer data. The opcode determines the interpretation of the application data:
This section is non-normative.
The protocol is designed to allow for extensions, which will add capabilities to the base protocols. The endpoints of a connection MUST negotiate the use of any extensions during the handshake. This specification provides opcodes 0x6 through 0xF, the extension data field, and the frame-rsv1, frame-rsv2, frame-rsv3, and frame-rsv4 bits of the frame header for use by extensions. The negotiation of extensions is discussed in further detail in Section 8.1. Below are some anticipated uses of extensions. This list is neither complete nor proscriptive.
User agents running in controlled environments, e.g. browsers on mobile handsets tied to specific carriers, may offload the management of the connection to another agent on the network. In such a situation, the user agent for the purposes of conformance is considered to include both the handset software and any such agents.
CONNECT example.com:80 HTTP/1.1 Host: example.com
CONNECT example.com:80 HTTP/1.1 Host: example.com Proxy-authorization: Basic ZWRuYW1vZGU6bm9jYXBlcyE=
When the user agent is to establish a WebSocket connection to a WebSocket URI /uri/, it must meet the following requirements. In the following text, we will use terms from Section 3 such as "/host/" and "/secure/ flag" as defined in that section.
If the user agent is not configured to use a proxy, then a direct TCP connection SHOULD be opened to the host given by /host/ and the port given by /port/.
Once a connection to the server has been established (including a connection via a proxy or over a TLS-encrypted tunnel), the client MUST send a handshake to the server. The handshake consists of an HTTP upgrade request, along with a list of required and optional headers. The requirements for this handshake are as follows.
Once the client's opening handshake has been sent, the client MUST wait for a response from the server before sending any further data. The client MUST validate the server's response as follows:
Where the algorithm above requires that a user agent fail the WebSocket connection, the user agent may first read an arbitrary number of further bytes from the connection (and then discard them) before actually failing the WebSocket connection. Similarly, if a user agent can show that the bytes read from the connection so far are such that there is no subsequent sequence of bytes that the server can send that would not result in the user agent being required to fail the WebSocket connection, the user agent may immediately fail the WebSocket connection without waiting for those bytes.
NOTE: The previous paragraph is intended to make it conforming for user agents to implement the algorithm in subtly different ways that are equivalent in all ways except that they terminate the connection at earlier or later points. For example, it enables an implementation to buffer the entire handshake response before checking it, or to verify each field as it is received rather than collecting all the fields and then checking them as a block.
This section only applies to servers.
Servers may offload the management of the connection to other agents on the network, for example load balancers and reverse proxies. In such a situation, the server for the purposes of conformance is considered to include all parts of the server-side infrastructure from the first device to terminate the TCP connection all the way to the server that processes requests and sends responses.
EXAMPLE: For example, a data center might have a server that responds to WebSocket requests with an appropriate handshake, and then passes the connection to another server to actually process the data frames. For the purposes of this specification, the "server" is the combination of both computers.
When a client starts a WebSocket connection, it sends its part of the opening handshake. The server must parse at least part of this handshake in order to obtain the necessary information to generate the server part of the handshake.
The client handshake consists of the following parts. If the server, while reading the handshake, finds that the client did not send a handshake that matches the description below, the server must abort the WebSocket connection.
When a client establishes a WebSocket connection to a server, the server must complete the following steps to accept the connection and send the server's opening handshake.
This completes the server's handshake. If the server finishes these steps without aborting the WebSocket connection, and if the client does not then fail the WebSocket connection, then the connection is established and the server may begin sending and receiving data, as described in the next section.
When a client is to interpret a byte stream as UTF-8 but finds that the byte stream is not in fact a valid UTF-8 stream, then any bytes or sequences of bytes that are not valid UTF-8 sequences must be interpreted as a U+FFFD REPLACEMENT CHARACTER.
When a server is to interpret a byte stream as UTF-8 but finds that the byte stream is not in fact a valid UTF-8 stream, behavior is undefined. A server could close the connection, convert invalid byte sequences to U+FFFD REPLACEMENT CHARACTERs, store the data verbatim, or perform application-specific processing. Subprotocols layered on the WebSocket protocol might define specific behavior for servers.
To Close the WebSocket Connection, an endpoint closes the underlying TCP connection. An endpoint SHOULD use a method that cleanly closes the TCP connection, discarding any trailing bytes that may be received. And endpoint MAY close the connection via any means available when necessary, such as when under attack.
As an example of how to obtain a clean closure in C using Berkeley sockets, one would call shutdown() with SHUT_WR on the socket, call recv() until obtaining a return value of 0 indicating that the peer has also performed an orderly shutdown, and finally calling close() on the socket.
To start the WebSocket closing handshake, and endpoint MUST send a Close control frame, as described in Section 4.5.1. Upon receiving a Close control frame, the other party sends a Close control frame in response. Once an endpoint has both sent and received a Close control frame, that endpoint should Close the WebSocket Connection as defined in Section 7.1.1.
When the underlying TCP connection is closed, it is said that the WebSocket connection is closed. If the tcp connection was closed after the WebSocket closing handshake was completed, the WebSocket connection is said to have been closed cleanly.
Certain algorithms and specifications require a user agent to fail the WebSocket connection. To do so, the user agent must Close the WebSocket Connection, and MAY report the problem to the user (which would be especially useful for developers) in an appropriate manner.
Except as indicated above or as specified by the application layer (e.g. a script using the WebSocket API), user agents SHOULD NOT close the connection.
Certain algorithms, namely during the initial handshake, require the user agent to fail the WebSocket connection. To do so, the user agent must Close the WebSocket connection as previously defined, and may report the problem to the user via an appropriate mechanism (which would be especially useful for developers).
Except as indicated above or as specified by the application layer (e.g. a script using the WebSocket API), user agents should not close the connection.
Certain algorithms require or recommend that the server abort the WebSocket connection during the opening handshake. To do so, the server must simply close the WebSocket connection (Section 7.1.1).
Servers MAY close the WebSocket connection whenever desired. User agents SHOULD NOT close the WebSocket connection arbitrarily. In either case, an endpoint initiates a closure by following the procedures to start the WebSocket closing handshake (Section 7.1.2).
When closing an established connection (e.g. when sending a Close frame, after the handshake has completed), an endpoint MAY indicate a reason for closure. The interpretation of this reason by an endpoint, and the action an endpoint should take given this reason, are left undefined by this specification. This specification defines a set of pre-defined status codes, and specifies which ranges may be used by extensions, frameworks, and end applications. The status code and any associated textual message are optional components of a Close frame.
Endpoints MAY use the following pre-defined status codes when sending a Close frame.
WebSocket clients MAY request extensions to this specification, and WebSocket servers MAY accept some or all extensions requested by the client. A server MUST NOT respond with any extension not requested by the client. If extension parameters are included in negotiations between the client and the server, those parameters MUST be chosen in accordance with the specification of the extension to which the parameters apply.
A client requests extensions by including a "Sec-WebSocket-Extensions" header, which follows the normal rules for HTTP headers (see [RFC2616] section 4.2) and the value of the header is defined by the following ABNF:
extension-list = 1#extension extension = extension-token *( ";" extension-param ) extension-token = registered-token | private-use-token registered-token = token private-use-token = "x-" token extension-param = token [ "=" ( token | quoted-string ) ]
Note that like other HTTP headers, this header may be split or combined across multiple lines. Ergo, the following are equivalent:
Sec-WebSocket-Extensions: foo Sec-WebSocket-Extensions: bar; baz=2
is exactly equivalent to
Sec-WebSocket-Extensions: foo, bar; baz=2
Any extension-token used must either be a registered token (registration TBD), or have a prefix of "x-" to indicate a private-use token. The parameters supplied with any given extension MUST be defined for that extension. Note that the client is only offering to use any advertised extensions, and MUST NOT use them unless the server accepts the extension.
Note that the order of extensions is significant. Any interactions between multiple extensions MAY be defined in the documents defining the extensions. In the absence of such definition, the interpretation is that the headers listed by the client in its request represent a preference of the headers it wishes to use, with the first options listed being most preferable. The extensions listed by the server in response represent the extensions actually in use. Should the extensions modify the data and/or framing, the order of operations on the data should be assumed to be the same as the order in which the extensions are listed in the server's response in the opening handshake.
For example, if there are two extensions "foo" and "bar", if the header |Sec-WebSocket-Extensions| sent by the server has the value "foo, bar" then operations on the data will be made as bar(foo(data)), be those changes to the data itself (such as compression) or changes to the framing thay may "stack".
Non-normative examples of acceptable extension headers:
Sec-WebSocket-Extensions: deflate-stream Sec-WebSocket-Extensions: mux; max-channels=4; flow-control, deflate-stream Sec-WebSocket-Extensions: x-private-extension
A server accepts one or more extensions by including a |Sec-WebSocket-Extensions| header containing one or more extensions which were requested by the client. The interpretation of any extension parameters, and what constitutes a valid response by a server to a requested set of parameters by a client, will be defined by each such extension.
Extensions provide a mechanism for implementations to opt-in to additional protocol features. This section defines the meaning of well-known extensions but implementations may use extensions defined separately as well.
The registered extension token for this compression extension is "deflate-stream".
The extension does not have any per message extension data and it does not define the use of any WebSocket reserved bits or op codes.
Senders using this extension MUST apply RFC 1951 encodings to all bytes of the data stream following the handshake including both data and control messages. The data stream MAY include multiple blocks of both compressed and uncompressed types as defined by RFC 1951. [RFC1951]
Senders MUST NOT delay the transmission of any portion of a WebSocket message because the deflate encoding of the message does not end on a byte boundary. The encodings for adjacent messages MAY appear in the same byte if no delay in transmission is occurred by doing so.
While this protocol is intended to be used by scripts in Web pages, it can also be used directly by hosts. Such hosts are acting on their own behalf, and can therefore send fake "Origin" fields, misleading the server. Servers should therefore be careful about assuming that they are talking directly to scripts from known origins, and must consider that they might be accessed in unexpected ways. In particular, a server should not trust that any input is valid.
EXAMPLE: For example, if the server uses input as part of SQL queries, all input text should be escaped before being passed to the SQL server, lest the server be susceptible to SQL injection.
Servers that are not intended to process input from any Web page but only for certain sites should verify the "Origin" field is an origin they expect, and should only respond with the corresponding "Sec-WebSocket-Origin" if it is an accepted origin. Servers that only accept input from one origin can just send back that value in the "Sec-WebSocket-Origin" field, without bothering to check the client's value.
If at any time a server is faced with data that it does not understand, or that violates some criteria by which the server determines safety of input, or when the server sees a handshake that does not correspond to the values the server is expecting (e.g. incorrect path or origin), the server should just disconnect. It is always safe to disconnect.
The biggest security risk when sending text data using this protocol is sending data using the wrong encoding. If an attacker can trick the server into sending data encoded as ISO-8859-1 verbatim (for instance), rather than encoded as UTF-8, then the attacker could inject arbitrary frames into the data stream.
In addition to endpoints being the target of attacks via WebSockets, other parts of web infrastructure, such as proxies, may be the subject of an attack. In particular, an intermediary may interpret a WebSocket message from a client as a request, and a message from the server as a response to that request. For instance, an attacker could get a browser to establish a connection to its server, get the browser to send a message that looks to an intermediary like a GET request for a common piece of JavaScript on another domain, and send back a message that is interpreted as a cacheable response to that request, thus poisioning the cache for other users. To prevent this attack, messages sent from clients are masked on the wire with a 32-bit value, to prevent an attacker from controlling the bits on the wire and thus lessen the probability of an attacker being able to construct a message that can be misinterpreted by a proxy as a non-WebSocket request.
"ws" ":" hier-part [ "?" query ]
A |ws:| URI identifies a WebSocket server and resource name.
"wss" ":" hier-part [ "?" query ]
A |wss:| URI identifies a WebSocket server and resource name, and indicates that traffic over that connection is to be encrypted.
This section describes a header field for registration in the Permanent Message Header Field Registry. [RFC3864]
The |Sec-WebSocket-Key| header is used in the WebSocket handshake. It is sent from the client to the server to provide part of the information used by the server to prove that it received a valid WebSocket handshake. This helps ensure that the server does not accept connections from non-WebSocket clients (e.g. HTTP clients) that are being abused to send data to unsuspecting WebSocket servers.
This section describes a header field for registration in the Permanent Message Header Field Registry. [RFC3864]
The |Sec-WebSocket-Extensions| header is used in the WebSocket handshake. It is initially sent from the client to the server, and then subsequently sent from the servver to the client, to agree on a set of protocol-level extensions to use during the connection.
This section describes a header field for registration in the Permanent Message Header Field Registry. [RFC3864]
The |Sec-WebSocket-Accept| header is used in the WebSocket handshake. It is sent from the server to the client to confirm that the server is willing to initiate the connection.
This section describes a header field for registration in the Permanent Message Header Field Registry. [RFC3864]
The |Sec-WebSocket-Origin| header is used in the WebSocket handshake. It is sent from the server to the client to confirm the origin of the script that opened the connection. This enables user agents to verify that the server is willing to serve the script that opened the connection.
This section describes a header field for registration in the Permanent Message Header Field Registry. [RFC3864]
The |Sec-WebSocket-Protocol| header is used in the WebSocket handshake. It is sent from the client to the server and back from the server to the client to confirm the subprotocol of the connection. This enables scripts to both select a subprotocol and be sure that the server agreed to serve that subprotocol.
This section describes a header field for registration in the Permanent Message Header Field Registry. [RFC3864]
The |Sec-WebSocket-Version| header is used in the WebSocket handshake. It is sent from the client to the server to indicate the protocol version of the connection. This enables servers to correctly interpret the handshake and subsequent data being sent from the data, and close the connection if the server cannot interpret that data in a safe manner.
The WebSocket protocol is intended to be used by another specification to provide a generic mechanism for dynamic author-defined content, e.g. in a specification defining a scripted API.
Such a specification first needs to "establish a WebSocket connection", providing that algorithm with:
The /host/, /port/, /resource name/, and /secure/ flag are usually obtained from a URI using the steps to parse a WebSocket URI's components. These steps fail if the URI does not specify a WebSocket.
If a connection can be established, then it is said that the "WebSocket connection is established".
If at any time the connection is to be closed, then the specification needs to use the "close the WebSocket connection" algorithm.
When the connection is closed, for any reason including failure to establish the connection in the first place, it is said that the "WebSocket connection is closed".
While a connection is open, the specification will need to handle the cases when "a WebSocket message has been received" with text /data/.
To send some text /data/ to an open connection, the specification needs to "send /data/ using the WebSocket".
Special thanks are due to Ian Hickson, who was the original author and editor of this protocol. The initial design of this specification benefitted from the participation of many people in the WHATWG and WHATWG mailing list. Contributions to that specification are not tracked by section, but a list of all who contributed to that specification is given in the WHATWG HTML specification at http://whatwg.org/html5.
Special thanks also to John Tamplin for providing a significant amount of text for the Data Framing section of this specification.
Special thanks also to Adam Barth for providing a significant amount of text and background research for the Data Masking section of this specification.
This section is not normative. This section was added at the request of the chairs to help track changes between versions. This section will be removed from the final version of this document.
Two major areas were changed in this draft. The closing handshake was clarified and re-written to add in terminology matching the API specification. The close frame was given an optional status code to indicate closure reason, and the notion of a body indicating which side initiated the close removed. Aside from this, many areas were clarified in areas previously ambiguous, though the meaning should remain consistent with the intent of previous drafts. Certain other material changes that are not as large as those previously mentioned are listed below, though for a complete list readers are reminded that a tool is available to diff two versions at http://tools.ietf.org/tools/rfcdiff/. The list below is my attempt at a changelog, not an authoritative guarantee, plese use the diff tool for a complete list.