HyBi Working Group                                            T. Yoshino
Internet-Draft                                              Google, Inc.
Intended status: Standards Track                        January 24,                          March 13, 2013
Expires: July 28, September 14, 2013

                   WebSocket Per-message

                  Compression
               draft-ietf-hybi-permessage-compression-05 Extensions for WebSocket
               draft-ietf-hybi-permessage-compression-06

Abstract

   This document specifies a framework for creating WebSocket extensions
   that add compression functionality to the WebSocket Protocol.
   Extensions based on this framework compress the payload data portion
   of non-
   control non-control WebSocket messages on per-message basis using a
   specified compression algorithm.  One reserved bit RSV1 in the
   WebSocket frame header is allocated to control application of
   compression for each message.  This document also specifies one
   specific compression extension using DEFLATE. the DEFLATE algorithm.

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

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 July 28, September 14, 2013.

Copyright Notice

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

Table of Contents

   1.  Introduction . . . . . . . . . . . . . . . . . . . . . . . . .  3
   2.  Conformance Requirements and Terminology . . . . . . . . . . .  4
   3.  WebSocket Per-message Compression Extension  . . . . . . . . .  4
   3.  5
   4.  Extension Negotiation  . . . . . . . . . . . . . . . . . . . .  5
     3.1.  6
     4.1.  Negotiation Example Examples . . . . . . . . . . . . . . . . . . .  5
   4.  6
   5.  Framing  . . . . . . . . . . . . . . . . . . . . . . . . . . .  7
     4.1.  8
     5.1.  Sending  . . . . . . . . . . . . . . . . . . . . . . . . .  7
     4.2.  8
     5.2.  Receiving  . . . . . . . . . . . . . . . . . . . . . . . .  7
   5.  8
   6.  permessage-deflate extension . . . . . . . . . . . . . . . . .  8
     5.1.  9
     6.1.  Method Parameters  . . . . . . . . . . . . . . . . . . . .  8
       5.1.1.  Disallow compression context takeover 10
       6.1.1.  Context Takeover Control . . . . . . . .  8
       5.1.2.  Limit maximum . . . . . . . 10
       6.1.2.  Limiting the LZ77 sliding window size  . . . . . . . .  9
       5.1.3. 10
       6.1.3.  Example  . . . . . . . . . . . . . . . . . . . . . . . 10
     5.2.  Application 11
     6.2.  Payload Data Transformation  . . . . . . . . . . . . . 10
       5.2.1. . . 12
       6.2.1.  Compression  . . . . . . . . . . . . . . . . . . . . . 10
       5.2.2. 12
       6.2.2.  Decompression  . . . . . . . . . . . . . . . . . . . . 11
       5.2.3. 13
       6.2.3.  Examples . . . . . . . . . . . . . . . . . . . . . . . 12
     5.3. 14
     6.3.  Intermediaries . . . . . . . . . . . . . . . . . . . . . . 14
     5.4. 17
     6.4.  Implementation Notes . . . . . . . . . . . . . . . . . . . 15
   6. 17
   7.  Security Considerations  . . . . . . . . . . . . . . . . . . . 16
   7. 18
   8.  IANA Considerations  . . . . . . . . . . . . . . . . . . . . . 17
     7.1. 19
     8.1.  Registration of the "permessage-deflate" WebSocket
           Extension Name . . . . . . . . . . . . . . . . . . . . . . 17
     7.2. 19
     8.2.  Registration of the "Per-message Compressed" WebSocket
           Framing Header Bit . . . . . . . . . . . . . . . . . . . . 17
   8. 19
   9.  Acknowledgements . . . . . . . . . . . . . . . . . . . . . . . 18
   9. 20
   10. References . . . . . . . . . . . . . . . . . . . . . . . . . . 19
     9.1. 21
     10.1. Normative References . . . . . . . . . . . . . . . . . . . 19
     9.2. 21
     10.2. Informative References . . . . . . . . . . . . . . . . . . 19 21
   Author's Address . . . . . . . . . . . . . . . . . . . . . . . . . 20 22

1.  Introduction

   _This section is non-normative._

   As well as other communication protocols, the WebSocket Protocol
   [RFC6455] can benefit from compression technology.

   This document specifies a framework for creating WebSocket extensions that to apply a compression algorithm
   to octets exchanged over the WebSocket Protocol
   using its [RFC6455].  This
   framework uses the extension framework.  Extensions based on concept for the WebSocket Protocol is
   introduced in the Section 9 of [RFC6455].  By specifying basic
   extension negotiation process excluding algorithm specific extension
   parameters in detail and a general method of transforming contents of
   WebSocket messages using a compression algorithm, this framework
   negotiate
   allows us to define WebSocket Per-message Compression Extensions
   (PMCEs) to the WebSocket Protocol individually for various
   compression algorithms.  A WebSocket client and a WebSocket server
   negotiate use of a PMCE and determines parameters to configure the
   compression algorithm during the WebSocket opening handshake, handshake.  The
   client and server then compress the octets in exchange non-control messages.  Extensions for
   various compression algorithms can be specified by describing messages using frames
   with compressed data in the payload data portion.  Documents
   specifying individual PMCEs describe how to negotiate parameters and
   how to transform data octets in payloads. the payload data portion.  A WebSocket
   client may offer multiple compression algorithms PMCEs during the WebSocket opening handshake by
   listing multiple compression extensions.
   handshake.  The WebSocket server received those offers may choose and
   accept preferred one from them.  Extensions based on this framework share  PMCEs use the RSV1 bit of the
   WebSocket frame header to indicate whether the message is compressed
   or not, so that we can choose not to skip compress messages with
   incompressible contents avoiding extra compression. contents.

   This document also specifies one specific extension "permessage-
   deflate" which is PMCE based on the DEFLATE
   [RFC1951] algorithm.  The extension name of the PMCE is "permessage-
   deflate".  We chose the DEFLATE since it's widely available as a
   library on various platforms and the overhead it adds for each chunk of the DEFLATE is
   small.  To align the end of compressed data to octet boundary, this
   extension uses the algorithm described in the Section 2.1 of the PPP
   Deflate Protocol [RFC1979].  Endpoints can take over the LZ77 sliding
   window [LZ77] used to build frames for previous messages to get
   better compression ratio.  For resource-limited devices, this
   extension provides parameters to limit memory usage for compression
   context.

   The simplest "Sec-WebSocket-Extensions" header in the client's
   opening handshake to request permessage-deflate is the following:

       Sec-WebSocket-Extensions: permessage-deflate

   The simplest header from the server to accept this extension is the
   same.

2.  Conformance Requirements

   Everything in this specification except for sections explicitly
   marked non-normative is normative. and Terminology

   The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT",
   "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this
   document are to be interpreted as described in [RFC2119].

3.  Extension Negotiation

   Extension names and negotiation methods are specified individually
   for each compression algorithm.  There is no additional rule for
   extension naming.  Extensions build based on this framework are
   collectively called "Per-message Compression Extensions".

   To request use of a Per-message Compression Extension, a client MUST
   include an element with its extension token

   Requirements phrased in the
   "Sec-WebSocket-Extensions" header in its opening handshake.  The
   element contains extension parameters imperative as specified by the
   specification part of the extension.  A client MAY list multiple Per-
   message Compression Extensions algorithms (such as
   "strip any leading space characters" or "return false and abort these
   steps") are to be interpreted with the same name to offer use meaning of the
   same algorithm with different configurations.

   To accept use of a Per-message Compression Extension, a server MUST
   include an element with its extension token key word
   ("MUST", "SHOULD", "MAY", etc.) used in introducing the
   "Sec-WebSocket-Extensions" header algorithm.

   Conformance requirements phrased as algorithms or specific steps can
   be implemented in its opening handshake.  The
   element contains extension parameters any manner, so long as specified by the
   specification of end result is
   equivalent.  In particular, the extension.  The parameters MUST algorithms defined in this
   specification are intended to be derived from
   the parameters sent by the client easy to understand and are not
   intended to be performant.

   This document references the server's capability.  To
   reject use of a Per-message Compression Extension, a server MUST
   simply ignore procedure to _Fail the element WebSocket
   Connection_.  This procedure is defined in the "Sec-WebSocket-Extensions" header Section 7.1.7 of
   [RFC6455].

   This document references the event that _the WebSocket Connection is
   established_.  This event is defined in the client's opening handshake.

   If Section 4.1 of [RFC6455].

   This document uses the Argumented Backus-Naur Form (ABNF) notation of
   [RFC5234].  The DIGIT (decimal 0-9) rule is included by reference, as
   defined in the Appendix B.1 of [RFC5234].

3.  WebSocket Per-message Compression Extension

   WebSocket Per-message Compression Extensions (PMCEs) are individually
   defined for various compression algorithms, and are registered in the
   WebSocket Extension Name Registry.  Each PMCE refers to this
   framework and defines:

   o  The content to put in the "Sec-WebSocket-Extensions" header,
      including the extension name of the PMCE and any applicable
      extension parameters

   o  How to interpret extension parameters exchanged during the opening
      handshake

   o  How to transform payload data portion of messages.

   One such extension is defined in Section 6 of this document and is
   registered in Section 8.  Other PMCEs may be defined in other
   documents.

   PMCEs operate only on non-control messages.

   This document allocates the RSV1 bit of the WebSocket header for
   PMCEs, and calls the bit the "Per-message Compressed" bit.  This bit
   indicates whether the compression method is applied to the contents
   of the message or not.  An endpoint MUST NOT offer or accept use of
   any other extension using the RSV1 bit together with a PMCE.  The
   "Per-message Compressed" bit MUST NOT be set on control frames and
   non-first fragments of a data message.  Messages with the
   "Per-message Compressed" bit set (only on the first fragment if the
   message is fragmented) are called "compressed messages" and have
   compressed data in their payload data portion.  Messages with the
   "Per-message Compressed" bit unset are called "uncompressed messages"
   and have uncompressed data in their payload data portion.

   A server MUST NOT accept a PMCE offer together with a non-PMCE
   extension if the PMCE will be applied to output of the non-PMCE and
   any of the following conditions is met:

   o  Frame boundary of frames output by the non-PMCE extension needs to
      be preserved.

   o  The non-PMCE uses the "Extension data" field or any of the
      reserved bits on the WebSocket header as per-frame attribute.

   Section 4 describes basic extension negotiation process.  Section 5
   describes how to apply the compression algorithm with negotiated
   parameters to the contents of WebSocket messages.

4.  Extension Negotiation

   To offer use of a PMCE, a client includes a
   "Sec-WebSocket-Extensions" header element with the extension name of
   the offered PMCE in the "Sec-WebSocket-Extensions" header in the
   client's opening handshake of the WebSocket connection.  Extension
   parameters in the element represent the PMCE offer in detail for
   example by listing capability of the client and preferred values for
   the algorithm's configuration parameters to use.  A client offers
   multiple PMCE choices to the server by including multiple elements,
   one for each PMCE offered.  The set of elements MAY include multiple
   PMCEs with the same extension name to offer use of the same algorithm
   with different configurations.

   To accept use of an offered PMCE, a client doesn't support server includes a
   "Sec-WebSocket-Extensions" header element with the extension and its name of
   the offered extension in the "Sec-WebSocket-Extensions" header in the
   server's opening handshake of the WebSocket connection.  Extension
   parameters replied
   from in the element represent the configuration parameters of
   the PMCE to use in detail.  The element MUST represent a PMCE that is
   fully supported by the server.  The server rejects all offered PMCEs
   by not including any element with PMCE names, in which case the
   connection proceeds without Per-message Compression.

   If the server, server responds with no PMCE element in the
   "Sec-WebSocket-Extensions" header and _the WebSocket Connection is
   established_, both endpoints MUST proceed without Per-message
   Compression.  If the server gives an invalid response, such as
   accepting a PMCE that the client did not offer, the client MUST _Fail
   the WebSocket Connection_.
   Otherwise, once

   If the server responds with a valid PMCE element in the
   "Sec-WebSocket-Extensions" header and _the WebSocket Connection is
   established_, both endpoints MUST use the algorithm described in
   Section 4 5 to exchange
   messages.

3.1. messages, using the payload data transformation
   procedure of the PMCE returned by the server.

4.1.  Negotiation Example

   _This section is non-normative._

   These Examples

   The followings are example values for the "Sec-WebSocket-Extensions"
   header value examples that
   negotiate the Per-message Compression Extension. offering PMCEs. permessage-foo and permessage-bar in the
   examples are hypothetical extension names of Per-message
   Compression Extensions PMCEs for hypothetical compression
   algorithm foo and bar.

   o  Request foo.  Offer the permessage-foo.

          permessage-foo

   o  Request foo  Offer the permessage-foo with a parameter x with 10 as its value.

          permessage-foo; x=10

   o  Request foo with a parameter z with "Hello World" (quotation for
      clarification) as its value.  Since "Hello World" contains a
      space, it needs to be quoted.

          permessage-foo; z="Hello World"

   o  Request foo and bar.

          permessage-foo, permessage-bar

   o  Request foo with a parameter use_y which enables a feature y as
      first choice, and also list one without the parameter as a
      fallback plan.

          permessage-foo; use_y, permessage-foo

4.  Framing

   This section describes how to apply the negotiated compression method
   to the contents of WebSocket messages.

   This document allocates the RSV1 bit of the WebSocket header for
   extensions based on this framework, and names it the "Per-message
   Compressed" bit.  Any other extension requiring the use value of the RSV1
   bit is incompatible with these extensions.  This bit 10.

          permessage-foo; x=10

      The value MAY be set only
   on quoted.

          permessage-foo; x="10"

   o  Offer the permessage-foo as first fragment of a message.  This bit indicates whether the
   compression method is applied to choice and the message or not.  Messages with permessage-bar as
      a fallback plan.

          permessage-foo, permessage-bar

   o  Offer the "Per-message Compressed" bit set (on its first fragment) are
   called "compressed messages".  They have compressed data in their
   payload.  Messages permessage-foo with the bit unset are called "uncompressed
   messages".  They have uncompressed data in their payload.

   Per-message Compression Extensions MUST NOT be used after any
   extension for a parameter use_y which frame boundary needs to be preserved.  Per-
   message Compression Extensions MUST NOT be used after any extension
   that uses "Extension data" field or any of enables a
      feature y as first choice, and the reserved bits on permessage-foo without the
   WebSocket header
      use_y parameter as per-frame attribute.

   Per-message Compression Extensions operates only on data frames.

4.1.  Sending

   To send a compressed message, an fallback plan.

          permessage-foo; use_y, permessage-foo

5.  Framing

5.1.  Sending

   An endpoint MUST use uses the following
   algorithm. algorithm to compressed a message to
   send.

   1.  Compress the payload data portion of the message using the
       compression method. algorithm.

   2.  Build frame(s) for the message by putting the resulting octets
       instead of the original octets.

   3.  Set the "Per-message Compressed" bit of the first fragment to 1.

   PMCEs don't change the opcode field.  The payload data portion in
   outgoing frames output by a PMCE is not subject to the constraints
   for the original data type.  At the receiver, the payload data
   portion after decompressing is subject to the constraints for the
   original data type again.

   To send an uncompressed message, an endpoint MUST set sets the "Per-message
   Compressed" bit of the first fragment of the message to 0.  The
   payload data portion of the message MUST be is sent as-is without applying
   the compression method.

4.2. compression.

5.2.  Receiving

   To receive a compressed message, an endpoint MUST decompress its
   payload. the payload
   data portion in the frames of the message.

   An endpoint MUST receive receives an uncompressed message as-is without
   decompression.

5.

6.  permessage-deflate extension

   This section specifies a specific extension PMCE called
   "permessage-deflate" that "permessage-deflate".
   It compresses the payload data portion of messages using the DEFLATE
   [RFC1951] and the byte boundary alignment aligning method introduced in
   [RFC1979].

   The registered extension token name for this extension is
   "permessage-deflate".

5.1.  Method Parameters

   The following 4 extension parameters are defined in the following subsections for this extension.

   o  "s2c_no_context_takeover"

   o  "c2s_no_context_takeover"

   o  "s2c_max_window_bits"

   o  "c2s_max_window_bits"

   A server MUST ignore decline a "permessage-deflate" extension entry offer if any of the
   following conditions is true: met:

   o  It  The offer has any extension parameter unknown to the server server.

   o  It  The offer has any extension parameter with an invalid value

   o  It is not supported by an invalid value.

   o  The offer has multiple extension parameters with the same name.

   o  The server doesn't support the offered configuration.

   A client MUST _Fail the WebSocket Connection_ if the server accepted
   a "permessage-deflate" offer with a response meeting any of the
   following
   is true about the received "permessage-deflate" extension entry: condition:

   o  It  The response has any extension parameter unknown to the client client.

   o  It  The response has any extension parameter with an invalid value value.

   o  It is not supported by  The response has multiple extension parameters with the same name.

   o  The client

5.1.1.  Disallow compression context takeover doesn't support the configuration the response
      represents.

6.1.  Method Parameters

6.1.1.  Context Takeover Control

   A client MAY attach the "s2c_no_context_takeover" extension
   parameter.  The "s2c_no_context_takeover" extension parameter to
   disallow has no
   value.  If a server received the "s2c_no_context_takeover" extension
   parameter, the server to take over MUST NOT use the same LZ77 sliding window used to
   build previous
   compress two or more messages.  Servers SHOULD be able to accept the
   "s2c_no_context_takeover" parameter.  To accept a request  A server accepts an offer with
   this
   parameter, extension parameter by including the "s2c_no_context_takeover"
   extension parameter in the response.  If a server:

   o  MUST attach server accepted an offer
   with this parameter to its response

   o extension parameter, the server MUST reset empty its LZ77 sliding
   window for sending to empty for compress messages to send each
      message time the server builds a new
   message.

   A server MAY attach the "c2s_no_context_takeover" extension parameter
   to disallow the client to take over use the LZ77 sliding window used to build previous messages.
   frames for the last message the client sent to build frames for the
   next message to send.  The "c2s_no_context_takeover" extension
   parameter has no value.  Clients SHOULD be able to accept the
   "c2s_no_context_takeover" parameter.  A client that received this
   parameter MUST reset its LZ77 sliding window for sending to empty for
   each message.

   These parameters have no value.

5.1.2.  Limit maximum

6.1.2.  Limiting the LZ77 sliding window size

   A client MAY attach the "s2c_max_window_bits" extension parameter to
   limit the LZ77 sliding window size that the server uses to build
   messages.  This extension parameter MUST have a decimal integer value
   in the range between 8 to 15 indicating the base-2 logarithm of the
   LZ77 sliding window size.  The ABNF [RFC5234] for the value of

       s2c_max_window_bits = 1*DIGIT

   A server declines an offer with this extension parameter is 1*DIGIT.
   Servers MAY be able to accept if the "s2c_max_window_bits"
   server doesn't support the extension parameter.
   To accept a request  A server accepts an
   offer with this parameter, extension parameter by including the server:

   o  MUST attach this extension
   parameter with the same value as one of the
      "accepted request" to its response

   o offer in the response.  If a
   server accepts an offer with this extension parameter, the server
   MUST NOT use LZ77 sliding window size greater than the size specified
   by this the extension parameter to build compress messages

   A client MAY attach the "c2s_max_window_bits" extension parameter if
   the client can adjust LZ77 sliding window size based on the
   "c2s_max_window_bits" sent by the server.  This parameter has no
   value.

   If the a server received request has and accepts an offer with the
   "c2s_max_window_bits" extension parameter, the server MAY respond to the request with include the
   "c2s_max_window_bits" parameter in the response to the offer to limit
   the LZ77 sliding window size that the client uses to build messages.  Otherwise,
   If a server received and accepts an offer without the
   "c2s_max_window_bits" extension parameter, the server MUST NOT accept
   include the "c2s_max_window_bits" extension parameter in the request
   with a response with
   to the parameter.  This offer.  The "c2s_max_window_bits" extension parameter sent by in the
   server
   server's opening handshake MUST have a decimal integer value in the
   range between 8 to 15 indicating the base-2 logarithm of the LZ77
   sliding window size.  The
   ABNF for the value of this parameter is 1*DIGIT.  A

       c2s_max_window_bits = 1*DIGIT

   If a client that received this parameter the "c2s_max_window_bits" extension parameter,
   the client MUST NOT use LZ77 sliding window size greater than the
   size specified by this the extension parameter to build messages.

5.1.3.

6.1.3.  Example

   _This section

   The simplest "Sec-WebSocket-Extensions" header in a client's opening
   handshake to offer use of the permessage-deflate is non-normative._

   This example the following:

       Sec-WebSocket-Extensions: permessage-deflate

   Since the "c2s_max_window_bits" extension parameter is not specified,
   the server may not accept the offer with the "c2s_max_window_bits"
   extension parameter.  The simplest "Sec-WebSocket-Extensions" header
   in a server's opening handshake to accept use of the permessage-
   deflate is the same.

   The following offer sent by a client is asking the server to use the
   LZ77 sliding window size of 1,024 bytes or less and declaring that
   the client can accept the "c2s_max_window_bits" extension parameter.

       Sec-WebSocket-Extensions:
           permessage-deflate;
           c2s_max_window_bits; s2c_max_window_bits=10

   This request offer might be rejected by the server because it the server doesn't
   support the "s2c_max_window_bits" parameter.  Since there's only one
   compression extension listed in the header, the server need to give
   up use of the Per-message Compression Extension entirely.  If
   reduction of LZ77 sliding window size by parameter.  This is fine
   if the server "s2c_max_window_bits" is mandatory for the client, this is fine.

   The next but if the
   client want to fallback to the "permessage-deflate" without the
   "s2c_max_window_bits", the client should offer the fallback option in
   addition like this:

       Sec-WebSocket-Extensions:
           permessage-deflate;
           c2s_max_window_bits; s2c_max_window_bits=10,
           permessage-deflate;
           c2s_max_window_bits

   This example lists offers two configurations so that the server can accept
   permessage-deflate by picking supported one from them.  To accept the
   first option, the server sends back this for example:

       Sec-WebSocket-Extensions:
           permessage-deflate; s2c_max_window_bits=10,
           permessage-deflate

   The server can choose s2c_max_window_bits=10

   And to accept the second extension entry by sending option, the server sends back this for
   example:

       Sec-WebSocket-Extensions: permessage-deflate

   Since the "c2s_max_window_bits" parameter was not specified for both
   of the extensions, the server cannot use the "c2s_max_window_bits"
   parameter.

5.2.  Application

6.2.  Payload Data Transformation

5.2.1.

6.2.1.  Compression

   An endpoint MUST use uses the following algorithm to compress a message.

   1.  Compress all the octets of the payload data portion of the
       message using the DEFLATE.

   2.  If the resulting data does not end with an empty DEFLATE block
       with no compression ("BTYPE" (the "BTYPE" bit is set to 0), append an
       empty DEFLATE block with no compression to the tail. tail end.

   3.  Remove 4 octets (that are 0x00 0x00 0xff 0xff) from the tail. tail end.
       After this step, the last octet of the compressed data contains
       the (part
       (possibly part of) the DEFLATE header bits with the "BTYPE" bit
       set to 0.

   In using the DEFLATE in the first step: step above:

   o  Multiple blocks  An endpoints MAY be used. use multiple DEFLATE blocks to compress one
      message.

   o  Any type of block  An endpoints MAY be used. use DEFLATE blocks of any type.

   o  Both block  An endpoints MAY use both DEFLATE blocks with the "BFINAL" bit set
      to 0 and 1 MAY be used. DEFLATE blocks with the "BFINAL" bit set to 1.

   o  When any DEFLATE block with the "BFINAL" bit set to 1 doesn't end
      at byte boundary, an endpoint adds minimal padding bits of 0 MUST be added to
      make it end at byte boundary, and then the boundary.  The next DEFLATE block MUST start at follows the byte
      boundary
      padded data if any.

   An endpoint MUST NOT use an LZ77 sliding window greater longer than 32,768
   bytes to build compress messages to send.

   If the a server specified accepts an offer with the "s2c_no_context_takeover" "c2s_no_context_takeover"
   extension parameter, the
   server client MUST reset empty its LZ77 sliding window for sending to empty for
   compress messages to send each message. time the client compresses a new
   message to send.  Otherwise, the server client MAY take over the LZ77
   sliding window used to build the last compressed message.

   If the a server specified accepts an offer with the "c2s_no_context_takeover" "s2c_no_context_takeover"
   extension parameter, the
   client server MUST reset empty its LZ77 sliding window for sending to empty for
   compress messages to send each message. time the server compresses a new
   message to send.  Otherwise, the client server MAY take over the LZ77
   sliding window used to build the last compressed message.

   If the a server specified accepts an offer with the "s2c_max_window_bits" "c2s_max_window_bits" extension
   parameter and its with a value is of w, the server client MUST NOT use an LZ77 sliding
   window greater longer than w-th power of 2 bytes to build compress messages to
   send.

   If the a server specified accepts an offer with the "c2s_max_window_bits" "s2c_max_window_bits" extension
   parameter and its with a value is of w, the client server MUST NOT use an LZ77 sliding
   window greater longer than w-th power of 2 bytes to build compress messages to
   send.

5.2.2.

6.2.2.  Decompression

   An endpoint MUST use uses the following algorithm to decompress a message.

   1.  Append 4 octets of 0x00 0x00 0xff 0xff to the tail end of the
       payload data portion of the message.

   2.  Decompress the resulting octets data using the DEFLATE.

   If the a server specified accepts an offer with the "s2c_no_context_takeover"
   extension parameter, the client MAY reset empty its LZ77 sliding window for receiving to empty for
   decompress received messages each time the client decompresses a new
   received message.  Otherwise, the client MUST take over the LZ77
   sliding window used to parse process the last compressed message.

   If the a server specified accepts an offer with the "c2s_no_context_takeover"
   extension parameter, the server MAY reset empty its LZ77 sliding window for receiving to empty for
   decompress received messages each time the server decompresses a new
   received message.  Otherwise, the server MUST take over the LZ77
   sliding window used to parse process the last compressed message.

   If the a server specified accepts an offer with the "s2c_max_window_bits" extension
   parameter and its with a value is of w, the client MAY reduce the size of the its
   LZ77 sliding window to decompress received messages down to the w-th
   power of 2 bytes.  Otherwise, the client MUST use a 32,768 byte LZ77
   sliding window to decompress received messages.

   If the a server specified accepts an offer with the "c2s_max_window_bits" extension
   parameter and its with a value is of w, the server MAY reduce the size of the its
   LZ77 sliding window to decompress received messages down to the w-th
   power of 2 bytes.  Otherwise, the server MUST use a 32,768 byte LZ77
   sliding window to decompress received messages.

5.2.3.

6.2.3.  Examples

   _This section is non-normative._

   This section introduces examples of how the permessage-deflate
   transforms messages.

5.2.3.1.

6.2.3.1.  A message compressed using 1 compressed DEFLATE block

   Suppose that an endpoint sends a text message "Hello" is sent.  When "Hello".  If the
   endpoint uses 1 compressed DEFLATE block (compressed with fixed
   Huffman code, code and the "BFINAL" bit is not set) is used, to compress the
   message, the endpoint obtains the compressed data to be sent put in the
   payload is obtained data portion as follows.

   Compress

   The endpoint compresses "Hello" into 1 compressed DEFLATE block and flush it
   flushes the resulting data into a byte array using an empty DEFLATE
   block with no compression:

       0xf2 0x48 0xcd 0xc9 0xc9 0x07 0x00 0x00 0x00 0xff 0xff

   Strip

   By stripping 0x00 0x00 0xff 0xff from the tail: tail end, the endpoint gets
   the data to put in the payload data portion:

       0xf2 0x48 0xcd 0xc9 0xc9 0x07 0x00

   To send it

   Suppose that the endpoint sends this compressed message without fragmentation, just build a
   fragmentation.  The endpoint builds one frame by putting the whole
   compressed data in the payload data: data portion of the frame:

       0xc1 0x07 0xf2 0x48 0xcd 0xc9 0xc9 0x07 0x00

   The first 2 octets (0xc1 0x07) are the WebSocket protocol's overhead frame header (FIN=1,
   RSV1=1, RSV2=0, RSV3=0, opcode=text, MASK=0, Payload length=7).

   To send it after fragmentation, split  The
   following figure shows what value is set in each field of the
   WebSocket frame header.

        0                   1
        0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
       +-+-+-+-+-------+-+-------------+
       |F|R|R|R| opcode|M| Payload len |
       |I|S|S|S|       |A|             |
       |N|V|V|V|       |S|             |
       | |1|2|3|       |K|             |
       +-+-+-+-+-------+-+-------------+
       |1|1|0|0|   1   |0|      7      |
       +-+-+-+-+-------+-+-------------+

   Suppose that the endpoint sends the compressed payload message with
   fragmentation.  The endpoint splits the compressed data into
   fragments and
   build builds frames for each of split data as well as fragmentation process
   done when the compression extension is not used. fragment.  For example, if the
   first fragment may contain
   fragments are 3 octets of and 4 octet, the payload: first frame is:

       0x41 0x03 0xf2 0x48 0xcd

   and the second (last) fragment contain 4 octets of the payload: frame is:

       0x80 0x04 0xc9 0xc9 0x07 0x00

   Note that the RSV1 bit is set only on the first fragment.

5.2.3.2. frame.

6.2.3.2.  Sharing LZ77 Sliding Window

   Suppose that the next a client has sent a message to "Hello" as a compressed
   message and will send is also "Hello". the same message "Hello" again as a compressed
   message.  If it's
   disallowed by the other peer (using some server has accepted the offer with the
   "c2s_no_context_takeover" extension parameter) to take
   over parameter, the LZ77 sliding window used for server compresses
   the last message, payload data portion of the next message is compressed into the same byte array bytes (if
   the server uses the same "BTYPE" value and "BFINAL" value are used). value):

       0xf2 0x48 0xcd 0xc9 0xc9 0x07 0x00

   If it's allowed, the server hasn't accepted the offer with the
   "c2s_no_context_takeover" extension parameter, the server can
   compress the payload data portion of the next message can
   be compressed into shorter payload:
   bytes utilizing the history in the LZ77 sliding window:

       0xf2 0x00 0x11 0x00 0x00

   instead of:

       0xf2 0x48 0xcd 0xc9 0xc9 0x07 0x00

   Note that even if any uncompressed message (any message with the RSV1
   bit unset) is inserted between the two "Hello" messages, it such a
   message doesn't affect context sharing between make any change on the
   two "Hello" messages.

5.2.3.3. LZ77 sliding window.

6.2.3.3.  Using a DEFLATE Block with No Compression

   Blocks

   Suppose that an endpoint compresses a text message "Hello" using a
   DEFLATE block with no compression can be also used. compression.  A DEFLATE block with no
   compression containing "Hello" flushed into a byte array using an
   another but empty DEFLATE block with no compression is:

       0x00 0x05 0x00 0xfa 0xff 0x48 0x65 0x6c 0x6c 0x6f 0x00
       0x00 0x00 0xff 0xff

   So, payload of a message containing "Hello" converted into a DEFLATE
   block with no compression is:

   The endpoint strips the 4 octets at the tail end:

       0x00 0x05 0x00 0xfa 0xff 0x48 0x65 0x6c 0x6c 0x6f 0x00

   If it's not fragmented, the

   The endpoint builds a frame for this message is: by putting the resulting data in the
   payload data portion of the frame:

       0xc1 0x0b 0x00 0x05 0x00 0xfa 0xff 0x48 0x65 0x6c 0x6c 0x6f 0x00

   The first 2 octets (0xc1 0x0b) are the WebSocket protocol's overhead frame header (FIN=1,
   RSV1=1, RSV2=0, RSV3=0, opcode=text, MASK=0, Payload length=7).  Note
   that the RSV1 must be bit is set for this message (only on the first fragment of
   it)
   if the message is fragmented) because the RSV1 indicates whether bit is set when the
   DEFLATE is applied to the message
   including use of and it includes the case only
   DEFLATE blocks with no compression or not.

5.2.3.4. are used.

6.2.3.4.  Using a DEFLATE Block with BFINAL Set to 1

   On platform where the flush method based on using an empty DEFLATE block with
   no compression is not avaiable, implementors can choose to flush data
   using DEFLATE blocks with "BFINAL" set to 1.  Using a DEFLATE block
   with "BFINAL" set to 1 and "BTYPE" set to 1, "Hello" is compressed
   into:

       0xf3 0x48 0xcd 0xc9 0xc9 0x07 0x00

   So, payload of a message containing "Hello" compressed using this
   parameter setting
   method is:

       0xf3 0x48 0xcd 0xc9 0xc9 0x07 0x00 0x00

   The last 1 octet (0x00) contains the header bits with "BFINAL" set to
   0 and "BTYPE" set to 0, and 7 padding bits of 0.  It's  This octet is
   necessary to make allow the payload able to be processed by decompressed in the same manner
   as messages flushed using DEFLATE blocks with BFINAL unset.

5.2.3.5.

6.2.3.5.  Two DEFLATE Blocks in 1 Message

   Two or more DEFLATE blocks may be used in 1 message.

       0xf2 0x48 0x05 0x00 0x00 0x00 0xff 0xff 0xca 0xc9 0xc9 0x07 0x00

   The first 3 octets (0xf2 0x48 0x05) and the least significant two
   bits of the 4th octet (0x00) consist one DEFLATE block with "BFINAL"
   set to 0 and "BTYPE" set to 1 containing "He". "He";.  The rest of the 4th
   octet contains the header bits with "BFINAL" set to 0 and "BTYPE" set
   to 0, and the 3 padding bits of 0.  Together with the following 4
   octets (0x00 0x00 0xff 0xff), the header bits consist an empty
   DEFLATE block with no compression.  Then, a  A DEFLATE block containing "llo" follows.

5.3.
   follows the empty DEFLATE block.

6.3.  Intermediaries

   When intermediaries forward an intermediary forwards messages, they the intermediary MAY decompress and/or
   compress add,
   change or remove Per-message Compression on the messages according to messages.  The
   elements in the constraints negotiated during "Sec-WebSocket-Extensions" for the PMCE in the
   opening handshake of handshakes with the connected client and server must be
   altered by the intermediary accordingly to match the connection(s).

5.4. new framing.

6.4.  Implementation Notes

   _This section is non-normative._

   On most common software development platforms, the operation of
   aligning their DEFLATE
   compression library provide a method to align compressed data to byte
   boundaries using an empty DEFLATE block with no compression is available as a library. compression.  For
   example, Zlib [Zlib] does this when "Z_SYNC_FLUSH" is passed to the
   deflate function.

   To get attain sufficient compression ratio, the LZ77 sliding window size
   of 1,024 or more is recommended.

6. RECOMMENDED.

7.  Security Considerations

   There are no security concerns is a known exploit for now.

7. combination of a secure transport
   protocol and a dictionary based compression [CRIME].  Implementors
   should give attention to this point when integrating this extension
   with other extensions or protocols.

8.  IANA Considerations

7.1.

8.1.  Registration of the "permessage-deflate" WebSocket Extension Name

   This section describes a WebSocket extension name registration in the
   WebSocket Extension Name Registry [RFC6455].

   Extension Identifier
      permessage-deflate

   Extension Common Name
      WebSocket Per-message Deflate

   Extension Definition
      This document.

   Known Incompatible Extensions
      None

   The "permessage-deflate" token extension name is used in the
   "Sec-WebSocket-Extensions" header in the WebSocket opening handshake
   to negotiate use of the permessage-deflate extension.

7.2.

8.2.  Registration of the "Per-message Compressed" WebSocket Framing
      Header Bit

   This section describes a WebSocket framing header bit registration in
   the WebSocket Framing Header Bits Registry [RFC6455].

   Header Bit
      RSV1

   Common Name
      Per-message Compressed

   Meaning
      The message is compressed or not.

   Reference
      Section 4 5 of this document.

   The "Per-message Compressed" framing header bit is used on the first
   fragment of non-control messages to indicate whether the payload data
   portion of the message is compressed by the Per-message Compression Extension PMCE or not.

8.

9.  Acknowledgements

   Special thanks to Patrick McManus who wrote up the initial
   specification of DEFLATE based a DEFLATE-based compression extension for the
   WebSocket Protocol to which I referred to write this specification.

   Thank you to the following people who participated in discussions on
   the HyBi WG and contributed ideas and/or provided detailed reviews
   (the list is likely to be incomplete): Alexey Melnikov, Arman
   Djusupov, Bjoern Hoehrmann, Brian McKelvey, Greg Wilkins, Inaki Baz
   Castillo, Jamie Lokier, Joakim Erdfelt, John A. Tamplin, Julian
   Reschke, Kenichi Ishibashi, Mark Nottingham, Peter Thorson, Roberto
   Peon and Simone Bordet.  Note that people listed above didn't
   necessarily endorse the end result of this work.

9.

10.  References

9.1.

10.1.  Normative References

   [RFC5234]  Crocker, D. and P. Overell, "Augmented BNF for Syntax
              Specifications: ABNF", STD 68, RFC 5234, January 2008.

   [RFC6455]  Fette, I. and A. Melnikov, "The WebSocket Protocol",
              RFC 6455, December 2011.

   [RFC2119]  Bradner, S., "Key words for use in RFCs to Indicate
              Requirement Levels", BCP 14, RFC 2119, March 1997.

   [LZ77]     Ziv, J. and A. Lempel, "A Universal Algorithm for
              Sequential Data Compression", IEEE Transactions on
              Information Theory, Vol. 23, No. 3, pp. 337-343.

9.2.

10.2.  Informative References

   [RFC1951]  Deutsch, P., "DEFLATE Compressed Data Format Specification
              version 1.3", RFC 1951, May 1996.

   [RFC1979]  Woods, J., "PPP Deflate Protocol", RFC 1979, August 1996.

   [Zlib]     Gailly, J. and M. Adler, "Zlib", <http://zlib.net/>.

   [CRIME]    Rizzo, J. and T. Duong, "The CRIME attack", Ekoparty 2012,
              September 2012.

Author's Address

   Takeshi Yoshino
   Google, Inc.

   Email: tyoshino@google.com