`
wezly
  • 浏览: 473003 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

The WebSocket protocol

阅读更多
HyBi Working Group                                            I. Hickson
Internet-Draft                                              Google, Inc.
Intended status: Standards Track                         August 16, 2010
Expires: February 17, 2011


                         The WebSocket protocol
                draft-ietf-hybi-thewebsocketprotocol-latest

Abstract

   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).





   This is the latest version of this specification.






   Please send feedback to the hybi@ietf.org mailing list.

















Hickson                 Expires February 17, 2011               [Page 1]

Internet-Draft           The WebSocket protocol              August 2010





Status of this Memo

   This Internet-Draft is submitted to IETF 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 February 17, 2011.

Copyright Notice

   Copyright (c) 2010 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.

















Hickson                 Expires February 17, 2011               [Page 2]

Internet-Draft           The WebSocket protocol              August 2010


Table of Contents

   1.  Introduction . . . . . . . . . . . . . . . . . . . . . . . . .  4
     1.1.  Background . . . . . . . . . . . . . . . . . . . . . . . .  4
     1.2.  Protocol overview  . . . . . . . . . . . . . . . . . . . .  4
     1.3.  Opening handshake  . . . . . . . . . . . . . . . . . . . .  7
     1.4.  Closing handshake  . . . . . . . . . . . . . . . . . . . . 10
     1.5.  Design philosophy  . . . . . . . . . . . . . . . . . . . . 10
     1.6.  Security model . . . . . . . . . . . . . . . . . . . . . . 11
     1.7.  Relationship to TCP and HTTP . . . . . . . . . . . . . . . 12
     1.8.  Establishing a connection  . . . . . . . . . . . . . . . . 12
     1.9.  Subprotocols using the WebSocket protocol  . . . . . . . . 12
   2.  Conformance requirements . . . . . . . . . . . . . . . . . . . 14
   3.  Terminology  . . . . . . . . . . . . . . . . . . . . . . . . . 15
   4.  WebSocket URLs . . . . . . . . . . . . . . . . . . . . . . . . 16
     4.1.  Parsing WebSocket URLs . . . . . . . . . . . . . . . . . . 16
     4.2.  Constructing WebSocket URLs  . . . . . . . . . . . . . . . 17
   5.  Client-side requirements . . . . . . . . . . . . . . . . . . . 18
     5.1.  Opening handshake  . . . . . . . . . . . . . . . . . . . . 18
     5.2.  Data framing . . . . . . . . . . . . . . . . . . . . . . . 28
     5.3.  Handling errors in UTF-8 from the server . . . . . . . . . 31
   6.  Server-side requirements . . . . . . . . . . . . . . . . . . . 32
     6.1.  Reading the client's opening handshake . . . . . . . . . . 32
     6.2.  Sending the server's opening handshake . . . . . . . . . . 35
     6.3.  Data framing . . . . . . . . . . . . . . . . . . . . . . . 39
     6.4.  Handling errors in UTF-8 from the client . . . . . . . . . 41
   7.  Closing the connection . . . . . . . . . . . . . . . . . . . . 42
     7.1.  Client-initiated closure . . . . . . . . . . . . . . . . . 42
     7.2.  Server-initiated closure . . . . . . . . . . . . . . . . . 42
     7.3.  Closure  . . . . . . . . . . . . . . . . . . . . . . . . . 42
   8.  Security considerations  . . . . . . . . . . . . . . . . . . . 44
   9.  IANA considerations  . . . . . . . . . . . . . . . . . . . . . 45
     9.1.  Registration of ws: scheme . . . . . . . . . . . . . . . . 45
     9.2.  Registration of wss: scheme  . . . . . . . . . . . . . . . 46
     9.3.  Registration of the "WebSocket" HTTP Upgrade keyword . . . 47
     9.4.  Sec-WebSocket-Key1 and Sec-WebSocket-Key2  . . . . . . . . 47
     9.5.  Sec-WebSocket-Location . . . . . . . . . . . . . . . . . . 48
     9.6.  Sec-WebSocket-Origin . . . . . . . . . . . . . . . . . . . 49
     9.7.  Sec-WebSocket-Protocol . . . . . . . . . . . . . . . . . . 50
   10. Using the WebSocket protocol from other specifications . . . . 51
   11. Acknowledgements . . . . . . . . . . . . . . . . . . . . . . . 52
   12. Normative References . . . . . . . . . . . . . . . . . . . . . 53
   Author's Address . . . . . . . . . . . . . . . . . . . . . . . . . 55








Hickson                 Expires February 17, 2011               [Page 3]

Internet-Draft           The WebSocket protocol              August 2010


1.  Introduction

1.1.  Background

   _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:

   o  The server is forced to use a number of different underlying TCP
      connections for each client: one for sending information to the
      client, and a new one for each incoming message.

   o  The wire protocol has a high overhead, with each client-to-server
      message having an HTTP header.

   o  The client-side script is forced to maintain a mapping from the
      outgoing connections to the incoming connection to track replies.

   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, for
   instance games, stock tickers, multiuser applications with
   simultaneous editing, and user interfaces exposing server-side
   services in real time.

1.2.  Protocol overview

   _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:











Hickson                 Expires February 17, 2011               [Page 4]

Internet-Draft           The WebSocket protocol              August 2010


        GET /demo HTTP/1.1
        Host: example.com
        Connection: Upgrade
        Sec-WebSocket-Key2: 12998 5 Y3 1  .P00
        Sec-WebSocket-Protocol: sample
        Upgrade: WebSocket
        Sec-WebSocket-Key1: 4 @1  46546xW%0l 1 5
        Origin: http://example.com

        ^n:ds[4U

   The handshake from the server looks as follows:

        HTTP/1.1 101 WebSocket Protocol Handshake
        Upgrade: WebSocket
        Connection: Upgrade
        Sec-WebSocket-Origin: http://example.com
        Sec-WebSocket-Location: ws://example.com/demo
        Sec-WebSocket-Protocol: sample

        8jKS'y:G*Co,Wxa-

   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 the HTTP
   specification.

   After the leading line in both cases come an unordered ASCII case-
   insensitive set of fields, one per line, that each match the
   following non-normative ABNF: [RFC5234]

     field         = 1*name-char colon [ space ] *any-char cr lf
     colon         = %x003A ; U+003A COLON (:)
     space         = %x0020 ; U+0020 SPACE
     cr            = %x000D ; U+000D CARRIAGE RETURN (CR)
     lf            = %x000A ; U+000A LINE FEED (LF)
     name-char     = %x0000-0009 / %x000B-000C / %x000E-0039 / %x003B-10FFFF
                     ; a Unicode character other than U+000A LINE FEED (LF), U+000D CARRIAGE RETURN (CR), or U+003A COLON (:)
     any-char      = %x0000-0009 / %x000B-000C / %x000E-10FFFF
                     ; a Unicode character other than U+000A LINE FEED (LF) or U+000D CARRIAGE RETURN (CR)

   NOTE: The character set for the above ABNF is Unicode.  The fields
   themselves are encoded as UTF-8.

   Lines that don't match the above production cause the connection to
   be aborted.

   Finally, after the last field, the client sends 10 bytes starting



Hickson                 Expires February 17, 2011               [Page 5]

Internet-Draft           The WebSocket protocol              August 2010


   with 0x0D 0x0A and followed by 8 random bytes, part of a challenge,
   and the server sends 18 bytes starting with 0x0D 0x0A and followed by
   16 bytes consisting of a challenge response.  The details of this
   challenge and other parts of the handshake are described in the next
   section.


   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.

   Data is sent in the form of UTF-8 text.  Each frame of data starts
   with a 0xFF byte identifying the frame type, followed by the number
   of bytes in the data expressed as a big-endian 64 bit unsigned
   integer, followed by the UTF-8 data.

   NOTE: The length is the number of UTF-8 _bytes_, and not the number
   of characters.  For example, the string "H̶e̸͜҉ll̡o̵̶͘" has 13 Unicode code
   points, but is 21 bytes long.

   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.

   To close the connection cleanly, a frame consisting of just nine 0x00
   bytes in a row is sent from one peer to ask that the other peer close
   the connection.  That corresponds to a frame type of 0x00 followed by
   a length of zero, indicating an empty frame.

   In addition to the 0x00 and 0xFF frame types, other types might in
   future be defined, to support binary data, fragmentation,
   compression, multiplexing, or other features.

   The wire format for the data transfer part is described by the
   |frames| production of the following non-normative ABNF.  [RFC5234]

        frames        = *frame
        frame         = frame-type frame-length data
        frame-type    = OCTET
        frame-length  = 8OCTET
        data          = *OCTET ; count must match the given length

   NOTE: The above ABNF is intended for a binary octet environment.

   !!!  WARNING: At this time, the WebSocket protocol cannot be used to



Hickson                 Expires February 17, 2011               [Page 6]

Internet-Draft           The WebSocket protocol              August 2010


   send binary data.  Using any of the frame types other than 0x00 and
   0xFF is invalid.  All other frame types are reserved for future use
   by future versions of this protocol.

1.3.  Opening handshake

   _This section is non-normative._

   The opening handshake is intended to be compatible with HTTP-based
   server-side software, 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 appears to
   HTTP servers to be a regular GET request with an Upgrade offer:

        GET / HTTP/1.1
        Upgrade: WebSocket
        Connection: Upgrade

   Fields in the handshake are sent by the client in a random order; the
   order is not meaningful.

   Additional fields are used to select options in the WebSocket
   protocol.  The only 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| field takes a space-
   separated list of strings:

        Sec-WebSocket-Protocol: org.example.chat wsxmpp ACME.COM-IM-2

   This field indicates the subprotocols (the application-level protocol
   layered over the WebSocket protocol) that the client can use.  The
   server reports which subprotocol it is going to use in its handshake
   response.

   The other fields in the handshake are all security-related.  The
   |Host| field is used to protect against DNS rebinding attacks and to
   allow multiple domains to be served from one IP address.

        Host: example.com

   The server includes the hostname in the |Sec-WebSocket-Location|
   field of its handshake, so that both the client and the server can
   verify that they agree on which host is in use.

   The |Origin| field 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 specifies which origin it is willing to



Hickson                 Expires February 17, 2011               [Page 7]

Internet-Draft           The WebSocket protocol              August 2010


   receive requests from by including a |Sec-WebSocket-Origin| field
   with that origin.  If multiple origins are authorized, the server
   echoes the value in the |Origin| field of the client's handshake.

        Origin: http://example.com

   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
   three pieces of information and combine them to form a response.  The
   first two pieces of information come from the |Sec-WebSocket-Key1|
   and |Sec-WebSocket-Key2| fields in the client handshake:

        Sec-WebSocket-Key1: 18x 6]8vM;54 *(5:  {   U1]8  z [  8
        Sec-WebSocket-Key2: 1_ tx7X d  <  nw  334J702) 7]o}` 0

   For each of these fields, the server has to take the digits from the
   value to obtain a number (in this case 1868545188 and 1733470270
   respectively), then divide that number by the number of spaces
   characters in the value (in this case 12 and 10) to obtain a 32-bit
   number (155712099 and 173347027).  These two resulting numbers are
   then used in the server handshake, as described below.

   The counting of spaces is intended to make it impossible to smuggle
   this field into the resource name; making this even harder is the
   presence of _two_ such fields, and the use of a newline as the only
   reliable indicator that the end of the key has been reached.  The use
   of random characters interspersed with the spaces and the numbers
   ensures that the implementor actually looks for spaces and newlines,
   instead of being treating any character like a space, which would
   make it again easy to smuggle the fields into the path and trick the
   server.  Finally, _dividing_ by this number of spaces is intended to
   make sure that even the most naive of implementations will check for
   spaces, since if ther server does not verify that there are some
   spaces, the server will try to divide by zero, which is usually fatal
   (a correct handshake will always have at least one space).

   The third piece of information is given after the fields, in the last
   eight bytes of the handshake, expressed here as they would be seen if
   interpreted as UTF-8:

        Tm[K T2u

   The concatenation of the number obtained from processing the |Sec-



Hickson                 Expires February 17, 2011               [Page 8]

Internet-Draft           The WebSocket protocol              August 2010


   WebSocket-Key1| field, expressed as a big-endian 32-bit number, the
   number obtained from processing the |Sec-WebSocket-Key2| field, again
   expressed as a big-endian 32-bit number, and finally the eight bytes
   at the end of the handshake, form a 128 bit string whose MD5 sum is
   then used by the server to prove that it read the handshake.


   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 (the HTTP version and reason phrase aren't important):

        HTTP/1.1 101 WebSocket Protocol Handshake

   The fields follow.  Two of the fields are just for compatibility with
   HTTP:

        Upgrade: WebSocket
        Connection: Upgrade

   Two of the fields are part of the security model described above,
   echoing the origin and stating the exact host, port, resource name,
   and whether the connection is expected to be encrypted:

        Sec-WebSocket-Origin: http://example.com
        Sec-WebSocket-Location: ws://example.com/

   These fields are checked by the Web browser when it is acting as a
   |WebSocket| client for scripted pages.  A server that only handles
   one origin and only serves one resource can therefore just return
   hard-coded values and does not need to parse the client's handshake
   to verify the correctness of the values.

   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 speaks.  Web browsers verify that the
   server specified one of the values that was specified in the
   |WebSocket| constructor, so a server that speaks multiple
   subprotocols has to make sure it selects one based on the client's
   handshake and specifies the right one in its handshake.

        Sec-WebSocket-Protocol: org.example.chat

   The server can also set cookie-related option fields to _set_
   cookies, as in HTTP.

   After the fields, the server sends the aforementioned MD5 sum, a 16
   byte (128 bit) value, shown here as if interpreted as UTF-8:




Hickson                 Expires February 17, 2011               [Page 9]

Internet-Draft           The WebSocket protocol              August 2010


        fQJ,fN/4F4!~K~MH

   This value depends on what the client sends, as described above.  If
   it doesn't match what the client is expecting, the client would
   disconnect.

   Having part of the handshake appear after the fields ensures that
   both the server and the client verify that the connection is not
   being interrupted by an HTTP intermediary such as a man-in-the-middle
   cache or proxy.

1.4.  Closing handshake

   _This section is non-normative._

   The closing handshake is far simpler than the opening handshake.

   Either peer can send a 0xFF frame with length 0x00 to begin the
   closing handshake.  Upon receiving a 0xFF frame, the other peer sends
   an identical 0xFF frame in acknowledgement, if it hasn't already sent
   one.  Upon receiving _that_ 0xFF frame, the first peer then closes
   the connection, safe in the knowledge that no further data is
   forthcoming.

   After sending a 0xFF frame, a peer does not send any further data;
   after receiving a 0xFF frame, 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.

1.5.  Design philosophy

   _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



Hickson                 Expires February 17, 2011              [Page 10]

Internet-Draft           The WebSocket protocol              August 2010


   adds a Web "origin"-based security model for browsers; adds an
   addressing and subprotocol 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
   reimplements 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 a mechanism to compress data and might support
   sending binary data.  To do this, future versions of this protocol
   are likely to introduce option fields that clients can send in its
   handshake to announce support for a feature (such as compression), to
   which the server can react by including a similar option field in
   _its_ handshake.

1.6.  Security model

   _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, fields starting with
   |Sec-| cannot be set by an attacker from a Web browser, even when
   using |XMLHttpRequest|.



Hickson                 Expires February 17, 2011              [Page 11]

Internet-Draft           The WebSocket protocol              August 2010


1.7.  Relationship to TCP and HTTP

   _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.

1.8.  Establishing a connection

   _This section is non-normative._

   There are several options for establishing a WebSocket connection.

   On the face of it, the simplest method would seem to be to use port
   80 to get a direct connection to a WebSocket server.  Port 80
   traffic, however, will often be intercepted by man-in-the-middle HTTP
   proxies, which can lead to the connection failing to be established.

   The most reliable method, therefore, is to use TLS encryption and
   port 443 to connect directly to a WebSocket server.  This has the
   advantage of being more secure; however, TLS encryption can be
   computationally expensive.

   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.

1.9.  Subprotocols using the WebSocket protocol

   _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 an equivalent field in its
   response for the connection to be established.




Hickson                 Expires February 17, 2011              [Page 12]

Internet-Draft           The WebSocket protocol              August 2010


   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 Organisation called their competing subprotocol "the-
   example.org-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, eg. going from "bookings.example.net"
   to "bookings.example.net2".  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.

   Subprotocol names are sequences of one or more characters in the
   range U+0021 to U+007F.




























Hickson                 Expires February 17, 2011              [Page 13]

Internet-Draft           The WebSocket protocol              August 2010


2.  Conformance requirements

   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.





















Hickson                 Expires February 17, 2011              [Page 14]

Internet-Draft           The WebSocket protocol              August 2010


3.  Terminology

   *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 "URL" 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.  [HTML]

   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.



























Hickson                 Expires February 17, 2011              [Page 15]

Internet-Draft           The WebSocket protocol              August 2010


4.  WebSocket URLs

4.1.  Parsing WebSocket URLs

   The steps to *parse a WebSocket URL's components* from a string /url/
   are as follows.  These steps return either a /host/, a /port/, a
   /resource name/, and a /secure/ flag, or they fail.

   1.   If the /url/ string is not an absolute URL, then fail this
        algorithm.  [WEBADDRESSES]

   2.   Resolve the /url/ string using the resolve a Web address
        algorithm defined by the Web addresses specification, with the
        URL character encoding set to UTF-8.  [WEBADDRESSES] [RFC3629]

        NOTE: It doesn't matter what it is resolved relative to, since
        we already know it is an absolute URL at this point.

   3.   If /url/ does not have a <scheme> component whose value, when
        converted to ASCII lowercase, is either "ws" or "wss", then fail
        this algorithm.

   4.   If /url/ has a <fragment> component, then fail this algorithm.

   5.   If the <scheme> component of /url/ is "ws", set /secure/ to
        false; otherwise, the <scheme> component is "wss", set /secure/
        to true.

   6.   Let /host/ be the value of the <host> component of /url/,
        converted to ASCII lowercase.

   7.   If /url/ has a <port> component, then let /port/ be that
        component's value; otherwise, there is no explicit /port/.

   8.   If there is no explicit /port/, then: if /secure/ is false, let
        /port/ be 80, otherwise let /port/ be 443.

   9.   Let /resource name/ be the value of the <path> component (which
        might be empty) of /url/.

   10.  If /resource name/ is the empty string, set it to a single
        character U+002F SOLIDUS (/).

   11.  If /url/ has a <query> component, then append a single U+003F
        QUESTION MARK character (?) to /resource name/, followed by the
        value of the <query> component.





Hickson                 Expires February 17, 2011              [Page 16]

Internet-Draft           The WebSocket protocol              August 2010


   12.  Return /host/, /port/, /resource name/, and /secure/.

4.2.  Constructing WebSocket URLs

   The steps to *construct a WebSocket URL* from a /host/, a /port/, a
   /resource name/, and a /secure/ flag, are as follows:

   1.  Let /url/ be the empty string.

   2.  If the /secure/ flag is false, then append the string "ws://" to
       /url/.  Otherwise, append the string "wss://" to /url/.

   3.  Append /host/ to /url/.

   4.  If the /secure/ flag is false and port is not 80, or if the
       /secure/ flag is true and port is not 443, then append the string
       ":" followed by /port/ to /url/.

   5.  Append /resource name/ to /url/.

   6.  Return /url/.






























Hickson                 Expires February 17, 2011              [Page 17]

Internet-Draft           The WebSocket protocol              August 2010


5.  Client-side requirements

   _This section only applies to user agents, not to servers._

   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.

   NOTE: This specification doesn't currently define a limit to the
   number of simultaneous connections that a client can establish to a
   server.

5.1.  Opening handshake

   When the user agent is to *establish a WebSocket connection* to a
   host /host/, on a port /port/, from an origin whose ASCII
   serialization is /origin/, with a flag /secure/, with a string giving
   a /resource name/, with a (possibly empty) list of strings giving the
   /protocols/, and optionally with a /defer cookies/ flag, it must run
   the following steps.  The /host/ must have been punycode-encoded
   already if necessary (i.e. it does not contain characters above
   U+007E).  The /origin/ must not contain characters in the range
   U+0041 to U+005A (i.e.  LATIN CAPITAL LETTER A to LATIN CAPITAL
   LETTER Z).  The /resource name/ string must be a non-empty string of
   characters in the range U+0021 to U+007E that starts with a U+002F
   SOLIDUS character (/).  The various strings in /protocols/ must all
   be non-empty strings with characters in the range U+0021 to U+007E,
   and must all be unique.  [ORIGIN]

   1.   If the user agent already has a WebSocket connection to the
        remote host (IP address) identified by /host/, even if known by
        another name, wait until that connection has been established or
        for that connection to have failed.  If multiple connections to
        the same IP address are attempted simultaneously, the user agent
        must serialize them so that there is no more than one connection
        at a time running through the following steps.

        If the user agent cannot determine the IP address of the remote
        host (for example because all communication is being done
        through a proxy server that performs DNS queries itself), then
        the user agent must assume for the purposes of this step that
        each host name refers to a distinct remote host, but should
        instead limit the total number of simultaneous connections that
        are not established to a reasonably low number (e.g., in a Web
        browser, to the number of tabs the user has open).




Hickson                 Expires February 17, 2011              [Page 18]

Internet-Draft           The WebSocket protocol              August 2010


        NOTE: This makes it harder for a script to perform a denial of
        service attack by just opening a large number of WebSocket
        connections to a remote host.  A server can further reduce the
        load on itself when attacked by making use of this by pausing
        before closing the connection, as that will reduce the rate at
        which the client reconnects.

        NOTE: There is no limit to the number of established WebSocket
        connections a user agent can have with a single remote host.
        Servers can refuse to connect users with an excessive number of
        connections, or disconnect resource-hogging users when suffering
        high load.

   2.   _Connect_: If the user agent is configured to use a proxy when
        using the WebSocket protocol to connect to host /host/ and/or
        port /port/, then connect to that proxy and ask it to open a TCP
        connection to the host given by /host/ and the port given by
        /port/.

           EXAMPLE: For example, if the user agent uses an HTTP proxy
           for all traffic, then if it was to try to connect to port 80
           on server example.com, it might send the following lines to
           the proxy server:

              CONNECT example.com:80 HTTP/1.1
              Host: example.com

           If there was a password, the connection might look like:

              CONNECT example.com:80 HTTP/1.1
              Host: example.com
              Proxy-authorization: Basic ZWRuYW1vZGU6bm9jYXBlcyE=

        Otherwise, if the user agent is not configured to use a proxy,
        then open a TCP connection to the host given by /host/ and the
        port given by /port/.

        NOTE: Implementations that do not expose explicit UI for
        selecting a proxy for WebSocket connections separate from other
        proxies are encouraged to use a SOCKS proxy for WebSocket
        connections, if available, or failing that, to prefer the proxy
        configured for HTTPS connections over the proxy configured for
        HTTP connections.

        For the purpose of proxy autoconfiguration scripts, the URL to
        pass the function must be constructed from /host/, /port/,
        /resource name/, and the /secure/ flag using the steps to
        construct a WebSocket URL.



Hickson                 Expires February 17, 2011              [Page 19]

Internet-Draft           The WebSocket protocol              August 2010


        NOTE: The WebSocket protocol can be identified in proxy
        autoconfiguration scripts from the scheme ("ws:" for unencrypted
        connections and "wss:" for encrypted connections).

   3.   If the connection could not be opened, then fail the WebSocket
        connection and abort these steps.

   4.   If /secure/ is true, perform a TLS handshake over the
        connection.  If this fails (e.g. the server's certificate could
        not be verified), then fail the WebSocket connection and abort
        these steps.  Otherwise, all further communication on this
        channel must run through the encrypted tunnel.  [RFC2246]

        User agents must use the Server Name Indication extension in the
        TLS handshake.  [RFC4366]

        User agents must use the Next Protocol Negotiation extension in
        the TLS handshake, selecting the "776562736f636b6574" protocol
        ("websocket" in UTF-8).  [NPN]

   5.   Send the UTF-8 string "GET" followed by a UTF-8-encoded U+0020
        SPACE character to the remote side (the server).

        Send the /resource name/ value, encoded as UTF-8.

        Send another UTF-8-encoded U+0020 SPACE character, followed by
        the UTF-8 string "HTTP/1.1", followed by a UTF-8-encoded U+000D
        CARRIAGE RETURN U+000A LINE FEED character pair (CRLF).

   6.   Let /fields/ be an empty list of strings.

   7.   Add the string "Upgrade: WebSocket" to /fields/.

   8.   Add the string "Connection: Upgrade" to /fields/.

   9.   Let /hostport/ be an empty string.

   10.  Append the /host/ value, converted to ASCII lowercase, to
        /hostport/.

   11.  If /secure/ is false, and /port/ is not 80, or if /secure/ is
        true, and /port/ is not 443, then append a U+003A COLON
        character (:) followed by the value of /port/, expressed as a
        base-ten integer, to /hostport/.

   12.  Add the string consisting of the concatenation of the string
        "Host:", a U+0020 SPACE character, and /hostport/, to /fields/.




Hickson                 Expires February 17, 2011              [Page 20]

Internet-Draft           The WebSocket protocol              August 2010


   13.  Add the string consisting of the concatenation of the string
        "Origin:", a U+0020 SPACE character, and the /origin/ value, to
        /fields/.

   14.  If the /protocols/ list is empty, then skip this step.

        Otherwise, add the string consisting of the concatenation of the
        string "Sec-WebSocket-Protocol:", a U+0020 SPACE character, and
        the strings in /protocols/, maintaining their relative order and
        each separated from the next by a single U+0020 SPACE character,
        to /fields/.

   15.  If the client has any cookies that would be relevant to a
        resource accessed over HTTP, if /secure/ is false, or HTTPS, if
        it is true, on host /host/, port /port/, with /resource name/ as
        the path (and possibly query parameters), then add to /fields/
        any HTTP headers that would be appropriate for that information.
        [RFC2616] [RFC2109] [RFC2965]

        This includes "HttpOnly" cookies (cookies with the http-only-
        flag set to true); the WebSocket protocol is not considered a
        non-HTTP API for the purpose of cookie processing.

        When one or more HTTP headers are to be added to /fields/ for
        this step, each header must be added separately, and each header
        must be added as one entry consisting of the header's name in
        its canonical case, followed by a U+003A COLON character (:) and
        a U+0020 SPACE character, followed by the value with no use of
        continuation lines (i.e. containing no U+000A LINE FEED
        characters).

   16.  Let /spaces_1/ be a random integer from 1 to 12 inclusive.

        Let /spaces_2/ be a random integer from 1 to 12 inclusive.

        EXAMPLE: For example, 5 and 9.

   17.  Let /max_1/ be the largest integer not greater than
        4,294,967,295 divided by /spaces_1/.

        Let /max_2/ be the largest integer not greater than
        4,294,967,295 divided by /spaces_2/.

        EXAMPLE: Continuing the example, 858,993,459 and 477,218,588.

   18.  Let /number_1/ be a random integer from 0 to /max_1/ inclusive.

        Let /number_2/ be a random integer from 0 to /max_2/ inclusive.



Hickson                 Expires February 17, 2011              [Page 21]

Internet-Draft           The WebSocket protocol              August 2010


        EXAMPLE: For example, 777,007,543 and 114,997,259.

   19.  Let /product_1/ be the result of multiplying /number_1/ and
        /spaces_1/ together.

        Let /product_2/ be the result of multiplying /number_2/ and
        /spaces_2/ together.

        EXAMPLE: Continuing the example, 3,885,037,715 and
        1,034,975,331.

   20.  Let /key_1/ be a string consisting of /product_1/, expressed in
        base ten using the numerals in the range U+0030 DIGIT ZERO (0)
        to U+0039 DIGIT NINE (9).

        Let /key_2/ be a string consisting of /product_2/, expressed in
        base ten using the numerals in the range U+0030 DIGIT ZERO (0)
        to U+0039 DIGIT NINE (9).

        EXAMPLE: Continuing the example, "3885037715" and "1034975331".

   21.  Insert between one and twelve random characters from the ranges
        U+0021 to U+002F and U+003A to U+007E into /key_1/ at random
        positions.

        Insert between one and twelve random characters from the ranges
        U+0021 to U+002F and U+003A to U+007E into /key_2/ at random
        positions.

        NOTE: This corresponds to random printable ASCII characters
        other than the digits and the U+0020 SPACE character.

        EXAMPLE: Continuing the example, this could lead to "P388O503D&
        ul7{K%gX(%715" and "1N?|kUT0or3o4I97N5-S3O31".

   22.  Insert /spaces_1/ U+0020 SPACE characters into /key_1/ at random
        positions other than the start or end of the string.

        Insert /spaces_2/ U+0020 SPACE characters into /key_2/ at random
        positions other than the start or end of the string.

        EXAMPLE: Continuing the example, this could lead to "P388 O503D&
        ul7 {K%gX( %7  15" and "1 N ?|k UT0or 3o  4 I97N 5-S3O 31".

   23.  Add the string consisting of the concatenation of the string
        "Sec-WebSocket-Key1:", a U+0020 SPACE character, and the /key_1/
        value, to /fields/.




Hickson                 Expires February 17, 2011              [Page 22]

Internet-Draft           The WebSocket protocol              August 2010


        Add the string consisting of the concatenation of the string
        "Sec-WebSocket-Key2:", a U+0020 SPACE character, and the /key_2/
        value, to /fields/.

   24.  For each string in /fields/, in a random order: send the string,
        encoded as UTF-8, followed by a UTF-8-encoded U+000D CARRIAGE
        RETURN U+000A LINE FEED character pair (CRLF).  It is important
        that the fields be output in a random order so that servers not
        depend on the particular order used by any particular client.

        NOTE: Only fields explicitly mentioned in the requirements of
        this specification are sent.  For example, user agents do not
        send fields such as |User-Agent|, |Accept-Language|, or
        |Content-Type| in the WebSocket handshake.

   25.  Send a UTF-8-encoded U+000D CARRIAGE RETURN U+000A LINE FEED
        character pair (CRLF).

   26.  Let /key_3/ be a string consisting of eight random bytes (or
        equivalently, a random 64 bit unsigned integer encoded in big-
        endian order).

        EXAMPLE: For example, 0x47 0x30 0x22 0x2D 0x5A 0x3F 0x47 0x58.

   27.  Send /key_3/ to the server.

   28.  Read bytes from the server until either the connection closes,
        or a 0x0A byte is read.  Let /field/ be these bytes, including
        the 0x0A byte.

        If /field/ is not at least seven bytes long, or if the last two
        bytes aren't 0x0D and 0x0A respectively, or if /field/ contains
        any 0x0D bytes other than the penultimate byte, or if /field/
        does not contain at least two 0x20 bytes, then fail the
        WebSocket connection and abort these steps.

        User agents may apply a timeout to this step, failing the
        WebSocket connection if the server does not send back data in a
        suitable time period.

   29.  Let /code/ be the substring of /field/ that starts from the byte
        after the first 0x20 byte, and ends with the byte before the
        second 0x20 byte.

   30.  If /code/, interpreted as UTF-8, is "101", then move to the next
        step.

        If /code/, interpreted as UTF-8, is "407", then either close the



Hickson                 Expires February 17, 2011              [Page 23]

Internet-Draft           The WebSocket protocol              August 2010


        connection and jump back to step 2, providing appropriate
        authentication information, or fail the WebSocket connection.
        407 is the code used by HTTP meaning "Proxy Authentication
        Required".  User agents that support proxy authentication must
        interpret the response as defined by HTTP (e.g. to find and
        interpret the |Proxy-Authenticate| header).

        Otherwise, fail the WebSocket connection and abort these steps.

   31.  Let /fields/ be a list of name-value pairs, initially empty.

   32.  _Field_: Let /name/ and /value/ be empty byte arrays.

   33.  Read a byte from the server.

        If the connection closes before this byte is received, then fail
        the WebSocket connection and abort these steps.

        Otherwise, handle the byte as described in the appropriate entry
        below:

        -> If the byte is 0x0D (UTF-8 CR)
           If the /name/ byte array is empty, then jump to the fields
           processing step.  Otherwise, fail the WebSocket connection
           and abort these steps.

        -> If the byte is 0x0A (UTF-8 LF)
           Fail the WebSocket connection and abort these steps.

        -> If the byte is 0x3A (UTF-8 :)
           Move on to the next step.

        -> If the byte is in the range 0x41 to 0x5A (UTF-8 A-Z)
           Append a byte whose value is the byte's value plus 0x20 to
           the /name/ byte array and redo this step for the next byte.

        -> Otherwise
           Append the byte to the /name/ byte array and redo this step
           for the next byte.

        NOTE: This reads a field name, terminated by a colon, converting
        upper-case letters in the range A-Z to lowercase, and aborting
        if a stray CR or LF is found.

   34.  Let /count/ equal 0.

        NOTE: This is used in the next step to skip past a space
        character after the colon, if necessary.



Hickson                 Expires February 17, 2011              [Page 24]

Internet-Draft           The WebSocket protocol              August 2010


   35.  Read a byte from the server and increment /count/ by 1.

        If the connection closes before this byte is received, then fail
        the WebSocket connection and abort these steps.

        Otherwise, handle the byte as described in the appropriate entry
        below:

        -> If the byte is 0x20 (UTF-8 space) and /count/ equals 1
           Ignore the byte and redo this step for the next byte.

        -> If the byte is 0x0D (UTF-8 CR)
           Move on to the next step.

        -> If the byte is 0x0A (UTF-8 LF)
           Fail the WebSocket connection and abort these steps.

        -> Otherwise
           Append the byte to the /value/ byte array and redo this step
           for the next byte.

        NOTE: This reads a field value, terminated by a CRLF, skipping
        past a single space after the colon if there is one.

   36.  Read a byte from the server.

        If the connection closes before this byte is received, or if the
        byte is not a 0x0A byte (UTF-8 LF), then fail the WebSocket
        connection and abort these steps.

        NOTE: This skips past the LF byte of the CRLF after the field.

   37.  Append an entry to the /fields/ list that has the name given by
        the string obtained by interpreting the /name/ byte array as a
        UTF-8 byte stream and the value given by the string obtained by
        interpreting the /value/ byte array as a UTF-8 byte stream.

   38.  Return to the "Field" step above.

   39.  _Fields processing_: Read a byte from the server.

        If the connection closes before this byte is received, or if the
        byte is not a 0x0A byte (UTF-8 LF), then fail the WebSocket
        connection and abort these steps.

        NOTE: This skips past the LF byte of the CRLF after the blank
        line after the fields.




Hickson                 Expires February 17, 2011              [Page 25]

Internet-Draft           The WebSocket protocol              August 2010


   40.  Let the /list of cookies/ be empty.

   41.  If there is not exactly one entry in the /fields/ list whose
        name is "upgrade", or if there is not exactly one entry in the
        /fields/ list whose name is "connection", or if there is not
        exactly one entry in the /fields/ list whose name is "sec-
        websocket-origin", or if there is not exactly one entry in the
        /fields/ list whose name is "sec-websocket-location", or if the
        /protocol/ was specified but there is not exactly one entry in
        the /fields/ list whose name is "sec-websocket-protocol", or if
        there are any entries in the /fields/ list whose names are the
        empty string, then fail the WebSocket connection and abort these
        steps.  Otherwise, handle each entry in the /fields/ list as
        follows:

        -> If the entry's name is "upgrade"
           If the value, converted to ASCII lowercase, is not exactly
           equal to the string "websocket", then fail the WebSocket
           connection and abort these steps.

        -> If the entry's name is "connection"
           If the value, converted to ASCII lowercase, is not exactly
           equal to the string "upgrade", then fail the WebSocket
           connection and abort these steps.

        -> If the entry's name is "sec-websocket-origin"
           If the value is not exactly equal to /origin/, then fail the
           WebSocket connection and abort these steps.  [ORIGIN]

        -> If the entry's name is "sec-websocket-location"
           If the value is not exactly equal to a string obtained from
           the steps to construct a WebSocket URL from /host/, /port/,
           /resource name/, and the /secure/ flag, then fail the
           WebSocket connection and abort these steps.

        -> If the entry's name is "sec-websocket-protocol"
           If the /protocols/ list was not empty, and the value is not
           exactly equal to one of the strings in the /protocols/ list,
           then fail the WebSocket connection and abort these steps.

           If the the value contains any characters outside the range
           U+0021 to U+007F, then fail the WebSocket connection and
           abort these steps.

           Otherwise, let the *selected WebSocket subprotocol* be the
           entry's value.





Hickson                 Expires February 17, 2011              [Page 26]

Internet-Draft           The WebSocket protocol              August 2010


        -> If the entry's name is "set-cookie" or "set-cookie2" or
        another cookie-related field name
           If the relevant specification is supported by the user agent,
           add the cookie, interpreted as defined by the appropriate
           specification, to the /list of cookies/, with the resource
           being the one with the host /host/, the port /port/, the path
           (and possibly query parameters) /resource name/, and the
           scheme |http| if /secure/ is false and |https| if /secure/ is
           true.  [RFC2109] [RFC2965]

           If the relevant specification is not supported by the user
           agent, then the field must be ignored.

           NOTE: The cookies added to the /list of cookies/ are
           discarded if the connection fails to be established.  Only if
           and when the connection is established do the cookies
           actually get applied.

        -> Any other name
           Ignore it.


   42.  Let /challenge/ be the concatenation of /number_1/, expressed as
        a big-endian unsigned 32-bit integer, /number_2/, expressed as a
        big-endian unsigned 32-bit integer, and the eight bytes of
        /key_3/ in the order they were sent on the wire.

        EXAMPLE: Using the examples given earlier, this leads to the 16
        bytes 0x2E 0x50 0x31 0xB7 0x06 0xDA 0xB8 0x0B 0x47 0x30 0x22
        0x2D 0x5A 0x3F 0x47 0x58.

   43.  Let /expected/ be the MD5 fingerprint of /challenge/ as a big-
        endian 128 bit string.  [RFC1321]

        EXAMPLE: Using the examples given earlier, this leads to the 16
        bytes 0x30 0x73 0x74 0x33 0x52 0x6C 0x26 0x71 0x2D 0x32 0x5A
        0x55 0x5E 0x77 0x65 0x75.  In UTF-8, these bytes correspond to
        the string "0st3Rl&q-2ZU^weu".

   44.  Read sixteen bytes from the server.  Let /reply/ be those bytes.

        If the connection closes before these bytes are received, then
        fail the WebSocket connection and abort these steps.

   45.  If /reply/ does not exactly equal /expected/, then fail the
        WebSocket connection and abort these steps.





Hickson                 Expires February 17, 2011              [Page 27]

Internet-Draft           The WebSocket protocol              August 2010


   46.  If the /defer cookies/ flag is not set, apply the cookies in the
        /list of cookies/.

   47.  The *WebSocket connection is established*.  Now the user agent
        must send and receive to and from the connection as described in
        the next section.

        If the /defer cookies/ flag is set, store the /list of cookies/
        for use by the component that invoked this algorithm.

   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 subtlely 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.

   When the user agent is to *apply the cookies* in a /list of cookies/,
   it must handle each cookie in the /list of cookies/ as defined by the
   appropriate specification.  [RFC2109] [RFC2965]

5.2.  Data framing

   Once a WebSocket connection is established, the user agent must run
   through the following state machine for the bytes sent by the server.
   If at any point during these steps a read is attempted but fails
   because the WebSocket connection is closed, then abort.

   1.  Try to read a byte from the server.  Let /frame type/ be that
       byte.

   2.  Try to read eight more bytes from the server.  Let /frame length/
       be the result of interpreting those eight bytes as a big-endian
       64 bit unsigned integer.





Hickson                 Expires February 17, 2011              [Page 28]

Internet-Draft           The WebSocket protocol              August 2010


   3.  Try to read /frame length/ bytes from the server.  Let /raw data/
       be those bytes.

   4.  Let /error/ be false.

   5.  If /frame length/ is greater than the length of data the user
       agent is able to deal with at this time, set /error/ to true.

       User agents should ensure they are always able to deal with
       lengths up to at least 65536 bytes.

       NOTE: The minimum expected supported length will likely be
       increased in future revisions of this protocol.

   6.  If /error/ is still false, handle the /frame type/ byte as
       follows:

       If the /frame type/ byte is 0x00
          If /frame length/ is not zero, then set /error/ to true.
          Otherwise, run these steps:

          1.  If the WebSocket closing handshake has not yet started,
              then start the WebSocket closing handshake.

          2.  Wait until either the WebSocket closing handshake has
              started or the WebSocket connection is closed.

          3.  If the WebSocket connection is not already closed, then
              close the WebSocket connection: *The WebSocket closing
              handshake has finished*.  (If the connection closes before
              this happens, then the closing handshake doesn't finish.)

          4.  Abort these steps.  Any data on the connection after the
              0x00 frame is discarded.

       If the /frame type/ byte is 0xFF
          *A WebSocket message has been received* with obtained by
          interpreting /raw data/ as a UTF-8 string.

       Otherwise (the /frame type/ byte is in the range 0x01 to 0xFE)
          Set /error/ to true.

   7.  If /error/ is true, then *a WebSocket error has been detected*.
       Discard /raw data/.

   8.  Return to the first step to read the next byte.

   If the user agent runs out of resources for buffering incoming data,



Hickson                 Expires February 17, 2011              [Page 29]

Internet-Draft           The WebSocket protocol              August 2010


   or hits an artificial resource limit intended to avoid resource
   starvation, then it must fail the WebSocket connection.


   Once a WebSocket connection is established, but before the WebSocket
   closing handshake has started, the user agent must use the following
   steps to *send /data/ using the WebSocket*:

   1.  Encode the text in /data/ using UTF-8 to obtain the byte stream
       /bytes/.

   2.  Send a 0xFF byte to the server.

   3.  Send the number of bytes in /bytes/, as a 64 bit big-endian
       unsigned integer, to the server.

   4.  Send /raw data/ to the server.

   Once the WebSocket closing handshake has started, the user agent must
   not send any further data on the connection.


   Once a WebSocket connection is established, the user agent must use
   the following steps to *start the WebSocket closing handshake*.
   These steps must be run asynchronously relative to whatever algorithm
   invoked this one.

   1.  If the WebSocket closing handshake has started, then abort these
       steps.

   2.  Send nine 0x00 bytes to the server.

   3.  *The WebSocket closing handshake has started*.

   4.  Wait a user-agent-determined length of time, or until the
       WebSocket connection is closed.

   5.  If the WebSocket connection is not already closed, then close the
       WebSocket connection.  (If this happens, then the closing
       handshake doesn't finish.)

   NOTE: If the user agent initiates it, the closing handshake finishes
   once the server returns the 0x00 frame, as described above.


   If at any point there is a fatal problem with sending data to the
   server, the user agent must fail the WebSocket connection.




Hickson                 Expires February 17, 2011              [Page 30]

Internet-Draft           The WebSocket protocol              August 2010


5.3.  Handling errors in UTF-8 from the server

   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.













































Hickson                 Expires February 17, 2011              [Page 31]

Internet-Draft           The WebSocket protocol              August 

  


  
分享到:
评论

相关推荐

    The WebSocket Protocol RFC

    WebSocket协议的RFC文档,详细的描述了WebSocket协议的原理,及协议细节

    The WebSocket Protocol

    The WebSocket Protocol enables two-way communication between a client running untrusted code in a controlled environment to a remote host that has opted-in to communications from that code. The ...

    RFC 6455 - The WebSocket Protocol

    The WebSocket Protocol enables two-way communication between a client running untrusted code in a controlled environment to a remote host that has opted-in to communications from that code. The ...

    The WebSocket Protocol-rfc6455

    Internet Engineering Task Force (IETF),WebSocket协议,RFC,6455,英文原版,国际标准,December 2011

    WinHTTP WebSocket 代码

    The WebSocket protocol was standardized by the IETF as RFC 6455 in 2011, and the WebSocket API for in Web IDL is being standardized by the W3C.WebSocket is designed to be implemented in web browsers ...

    realtime web apps

    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 ...

    as3.0 websocket

    AS3 implementation of a client library of the WebSocket protocol,as3.0 websocket 封装了 socket,可以测试,很好用

    as3 websocket

    his is an AS3 implementation of a client library of the WebSocket protocol, as specified in the RFC6455 standard.

    django-c10k-demo.zip

    这是一个演示程序,用来实现同时 10000 个并发连接到...涉及的概念包括:the C10k problem, the WebSocket protocol, the Django web framework, and Python's upcoming asynchronous IO support. 标签:django

    A C++ Websocket server for realtime interaction with Web clients

    A Websocket protocol implementation atop the ush Framework real time library plus a demo example featuring four types of communication workflows between the HTML5 web client and the server.

    Android代码-async-http-client

    The library also supports the WebSocket Protocol. It's built on top of Netty. It's currently compiled on Java 8 but runs on Java 9 too. Installation Binaries are deployed on Maven central: org....

    WebSocket Client 模块纯源码实现以及分析-易语言

    最权威的资料当然是RFC 6455 [The WebSocket Protocol],里面有各种标准定义,本源码以及分析也都是基于这个RFC:https://tools.ietf.org/html/rfc6455 这也是我的学习习惯,尽量看原版的权威资料 ,翻译或者博客...

    protocol-websocket:协议

    名称协议:: WebSocket-WebSocket协议概要 # Server sidemy $hs = Protocol::WebSocket::Handshake::Server-&gt;new;$hs-&gt;parse('some data from the client');$hs-&gt;is_done; # tells us when handshake is donemy $...

    Android代码-WebRTCAndroidExample

    I'm using WebSocket protocol to communicate with the server. How to run it First install the Web Socket module npm install websocket Having the module install, you only need to have nodeJS installed ...

    ws-promise:一个微小的、基于 Promise 的 WebSocket 协议,允许在 ECMAScript 中使用请求-响应

    在幕后,WebSocket API 首先被包裹在一个Promise层中,然后不同的端点通过一个微小的 RPC 协议连接在一起。 总之,它使您能够在客户端编写这样的代码......: 客户端.mjs import Client from "ws-promise/...

    hyperswarm-proxy-ws:具有自动重新连接逻辑的Websocket上的代理超群连接

    超群代理 具有自动重新连接逻辑的Websocket上的代理超群连接 npm -s hyperswarm-proxy-ws ...// Start listening on clients via websocket protocol server . listen ( 3472 ) const HyperswarmClient

    jdk-9.0.1_doc-all 最新版

    Defines the high-level HTTP and WebSocket API. jdk.jartool Defines tools for manipulating Java Archive (JAR) files, including the jar and jarsigner tools. jdk.javadoc Defines the implementation of ...

Global site tag (gtag.js) - Google Analytics