Internet Engineering Task Force (IETF)                        T. Yoshino
Request for Comments: 7692                                  Google, Inc.
Category: Standards Track                                  December 2015
ISSN: 2070-1721
        
Internet Engineering Task Force (IETF)                        T. Yoshino
Request for Comments: 7692                                  Google, Inc.
Category: Standards Track                                  December 2015
ISSN: 2070-1721
        

Compression Extensions for WebSocket

WebSocket的压缩扩展

Abstract

摘要

This document defines a framework for creating WebSocket extensions that add compression functionality to the WebSocket Protocol. An extension based on this framework compresses the payload data portion of WebSocket data messages on a per-message basis using parameters negotiated during the opening handshake. This framework provides a general method for applying a compression algorithm to the contents of WebSocket messages. Each compression algorithm has to be defined in a document defining the extension by specifying the parameter negotiation and the payload transformation algorithm in detail. This document also specifies one specific compression extension using the DEFLATE algorithm.

本文档定义了一个用于创建WebSocket扩展的框架,该扩展将压缩功能添加到WebSocket协议中。基于此框架的扩展使用在开始握手期间协商的参数按每条消息压缩WebSocket数据消息的有效负载数据部分。该框架提供了一种将压缩算法应用于WebSocket消息内容的通用方法。每个压缩算法都必须在定义扩展的文档中定义,详细指定参数协商和有效负载转换算法。本文档还使用DEFLATE算法指定了一个特定的压缩扩展。

Status of This Memo

关于下段备忘

This is an Internet Standards Track document.

这是一份互联网标准跟踪文件。

This document is a product of the Internet Engineering Task Force (IETF). It represents the consensus of the IETF community. It has received public review and has been approved for publication by the Internet Engineering Steering Group (IESG). Further information on Internet Standards is available in Section 2 of RFC 5741.

本文件是互联网工程任务组(IETF)的产品。它代表了IETF社区的共识。它已经接受了公众审查,并已被互联网工程指导小组(IESG)批准出版。有关互联网标准的更多信息,请参见RFC 5741第2节。

Information about the current status of this document, any errata, and how to provide feedback on it may be obtained at http://www.rfc-editor.org/info/rfc7692.

有关本文件当前状态、任何勘误表以及如何提供反馈的信息,请访问http://www.rfc-editor.org/info/rfc7692.

Copyright Notice

版权公告

Copyright (c) 2015 IETF Trust and the persons identified as the document authors. All rights reserved.

版权所有(c)2015 IETF信托基金和确定为文件作者的人员。版权所有。

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.

本文件受BCP 78和IETF信托有关IETF文件的法律规定的约束(http://trustee.ietf.org/license-info)自本文件出版之日起生效。请仔细阅读这些文件,因为它们描述了您对本文件的权利和限制。从本文件中提取的代码组件必须包括信托法律条款第4.e节中所述的简化BSD许可证文本,并提供简化BSD许可证中所述的无担保。

Table of Contents

目录

   1. Introduction ....................................................3
   2. Conformance Requirements and Terminology ........................3
   3. Complementary Terminology .......................................4
   4. WebSocket Per-Message Compression Extension .....................5
   5. Extension Negotiation ...........................................5
      5.1. General Negotiation Flow ...................................9
      5.2. Negotiation Examples .......................................9
   6. Framing ........................................................10
      6.1. Compression ...............................................10
      6.2. Decompression .............................................12
   7. The "permessage-deflate" Extension .............................12
      7.1. Extension Parameters ......................................14
           7.1.1. Context Takeover Control ...........................14
           7.1.2. Limiting the LZ77 Sliding Window Size ..............16
           7.1.3. Examples ...........................................18
      7.2. Message Payload Transformation ............................19
           7.2.1. Compression ........................................19
           7.2.2. Decompression ......................................21
           7.2.3. Examples ...........................................22
      7.3. Implementation Notes ......................................25
   8. Security Considerations ........................................25
   9. IANA Considerations ............................................26
      9.1. Registration of the "permessage-deflate" WebSocket
           Extension Name ............................................26
      9.2. Registration of the "Per-Message Compressed"
           WebSocket Framing Header Bit ..............................26
   10. References ....................................................27
      10.1. Normative References .....................................27
      10.2. Informative References ...................................27
   Acknowledgements ..................................................28
   Author's Address ..................................................28
        
   1. Introduction ....................................................3
   2. Conformance Requirements and Terminology ........................3
   3. Complementary Terminology .......................................4
   4. WebSocket Per-Message Compression Extension .....................5
   5. Extension Negotiation ...........................................5
      5.1. General Negotiation Flow ...................................9
      5.2. Negotiation Examples .......................................9
   6. Framing ........................................................10
      6.1. Compression ...............................................10
      6.2. Decompression .............................................12
   7. The "permessage-deflate" Extension .............................12
      7.1. Extension Parameters ......................................14
           7.1.1. Context Takeover Control ...........................14
           7.1.2. Limiting the LZ77 Sliding Window Size ..............16
           7.1.3. Examples ...........................................18
      7.2. Message Payload Transformation ............................19
           7.2.1. Compression ........................................19
           7.2.2. Decompression ......................................21
           7.2.3. Examples ...........................................22
      7.3. Implementation Notes ......................................25
   8. Security Considerations ........................................25
   9. IANA Considerations ............................................26
      9.1. Registration of the "permessage-deflate" WebSocket
           Extension Name ............................................26
      9.2. Registration of the "Per-Message Compressed"
           WebSocket Framing Header Bit ..............................26
   10. References ....................................................27
      10.1. Normative References .....................................27
      10.2. Informative References ...................................27
   Acknowledgements ..................................................28
   Author's Address ..................................................28
        
1. Introduction
1. 介绍

This document specifies a framework for adding compression functionality to the WebSocket Protocol [RFC6455]. The framework specifies how to define WebSocket Per-Message Compression Extensions (PMCEs) for a compression algorithm based on the extension concept of the WebSocket Protocol specified in Section 9 of [RFC6455]. A WebSocket client and a peer WebSocket server negotiate the use of a PMCE and determine the parameters required to configure the compression algorithm during the WebSocket opening handshake. The client and server can then exchange data messages whose frames contain compressed data in the payload data portion.

本文档指定了向WebSocket协议[RFC6455]添加压缩功能的框架。该框架规定了如何根据[RFC6455]第9节中规定的WebSocket协议的扩展概念,为压缩算法定义WebSocket每消息压缩扩展(PMCE)。WebSocket客户端和对等WebSocket服务器协商使用PMCE,并确定在WebSocket打开握手期间配置压缩算法所需的参数。然后,客户机和服务器可以交换其帧包含有效负载数据部分中的压缩数据的数据消息。

This framework only specifies a general method for applying a compression algorithm to the contents of WebSocket messages. Each individual PMCE has to be specified in a document describing in detail how to negotiate the configuration parameters for the specific compression algorithm used by that PMCE and how to transform (compress and decompress) data in the payload data portion.

此框架仅指定了将压缩算法应用于WebSocket消息内容的一般方法。必须在详细描述如何协商该PMCE使用的特定压缩算法的配置参数以及如何转换(压缩和解压缩)有效负载数据部分中的数据的文档中指定每个单独的PMCE。

A WebSocket client may offer multiple PMCEs during the WebSocket opening handshake. A peer WebSocket server receiving the offer may choose to accept the preferred PMCE or decline all of them. PMCEs use the RSV1 bit of the WebSocket frame header to indicate whether a message is compressed or not so that an endpoint can choose not to compress messages with incompressible contents.

WebSocket客户端可以在WebSocket打开握手期间提供多个PMCE。接收报价的对等WebSocket服务器可以选择接受首选PMCE或拒绝所有PMCE。PMCS使用WebSocket帧头的RSV1位指示消息是否被压缩,以便端点可以选择不压缩包含不可压缩内容的消息。

This document also specifies one specific PMCE based on the DEFLATE [RFC1951] algorithm. The DEFLATE algorithm is widely available on various platforms, and its overhead is small. The extension name of this PMCE is "permessage-deflate". To align the end of compressed data to an octet boundary, this extension uses the algorithm described in Section 2.1 of [RFC1979]. Endpoints can take over the LZ77 sliding window [LZ77] used to build frames for previous messages to achieve a better compression ratio. For resource-limited devices, this extension provides parameters to limit memory usage for compression context.

本文件还规定了一种基于DEFLATE[RFC1951]算法的特定PMCE。DEFLATE算法在各种平台上广泛可用,并且其开销很小。此PMCE的扩展名为“permessage deflate”。为了将压缩数据的结尾与八位字节边界对齐,此扩展使用[RFC1979]第2.1节中描述的算法。端点可以接管LZ77滑动窗口[LZ77],该窗口用于为以前的消息构建帧,以实现更好的压缩比。对于资源有限的设备,此扩展提供参数来限制压缩上下文的内存使用。

2. Conformance Requirements and Terminology
2. 一致性要求和术语

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

本文件中的关键词“必须”、“不得”、“必需”、“应”、“不应”、“应”、“不应”、“建议”、“可”和“可选”应按照[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.

作为算法一部分的命令式要求(如“去除任何前导空格字符”或“返回false并中止这些步骤”)应使用介绍算法时使用的关键词(“必须”、“应该”、“可能”等)的含义进行解释。

Conformance requirements phrased as algorithms or specific steps can 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 understand and are not intended to be performant.

作为算法或特定步骤的一致性需求可以以任何方式实现,只要最终结果是等效的。特别是,本规范中定义的算法旨在易于理解,而不是用于执行。

This document references the procedure to _Fail the WebSocket Connection_. This procedure is defined in Section 7.1.7 of [RFC6455].

本文档引用了导致WebSocket连接失败的过程。该程序在[RFC6455]第7.1.7节中定义。

This document references the event that _The WebSocket Connection is Established_ and the event that _A WebSocket Message Has Been Received_. These events are defined in Sections 4.1 and 6.2, respectively, of [RFC6455].

本文档引用了“建立WebSocket连接”事件和“收到WebSocket消息”事件。[RFC6455]第4.1节和第6.2节分别对这些事件进行了定义。

This document uses the Augmented 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].

本文件使用[RFC5234]的扩充巴科斯诺尔形式(ABNF)符号。根据[RFC5234]附录B.1中的定义,数字(十进制0-9)规则通过引用包括在内。

3. Complementary Terminology
3. 补充术语

This document defines some terms about WebSocket and WebSocket extension mechanisms that are underspecified or not defined at all in [RFC6455].

本文档定义了有关WebSocket和WebSocket扩展机制的一些术语,这些术语在[RFC6455]中没有详细说明或定义。

data message - a message consisting of data frames as defined in Section 5.6 of [RFC6455].

数据报文-由[RFC6455]第5.6节中定义的数据帧组成的报文。

message payload (or payload of a message) - the concatenation of the payload data portion of all data frames (see Section 6.2 of [RFC6455]) representing a single message.

消息有效载荷(或消息的有效载荷)-表示单个消息的所有数据帧的有效载荷数据部分的串联(见[RFC6455]第6.2节)。

next extension in use after extension X - the next extension listed after X in the "Sec-WebSocket-Extensions" header in the server's opening handshake as defined in Section 9.1 of [RFC6455]. Such an extension is applied to outgoing data from the application right after X on the sender side but is applied right before X to incoming data from the underlying transport.

扩展X后使用的下一个扩展-服务器开始握手中“Sec WebSocket Extensions”标题中X后列出的下一个扩展,如[RFC6455]第9.1节所定义。这样的扩展应用于发送方X之后的应用程序传出数据,但在X之前应用于基础传输的传入数据。

extension in use preceding extension X - the extension listed right before X in the "Sec-WebSocket-Extensions" header in the server's opening handshake. Such an extension is applied to outgoing data from the application right before X on the sender side but is applied right after X to incoming data from the underlying transport.

在扩展X之前使用的扩展-服务器开始握手时“Sec WebSocket Extensions”标题中X前面列出的扩展。这样的扩展应用于发送方X之前的应用程序传出数据,但在X之后应用于基础传输的传入数据。

extension negotiation offer - each element in the "Sec-WebSocket-Extensions" header in the client's opening handshake.

extension negotiation offer-客户端开始握手时“Sec WebSocket Extensions”标题中的每个元素。

extension negotiation response - each element in the "Sec-WebSocket-Extensions" header in the server's opening handshake.

扩展协商响应-服务器开始握手时“Sec WebSocket Extensions”头中的每个元素。

corresponding extension negotiation response for an extension negotiation offer - an extension negotiation response that a server sends back to the peer client containing the same extension name as the offer and meeting the requirements represented by the offer.

扩展谈判报价的相应扩展谈判响应-服务器发送回对等客户端的扩展谈判响应,包含与报价相同的扩展名称,并满足报价所表示的要求。

Accepting an extension negotiation offer - including a corresponding extension negotiation response for the offer in the "Sec-WebSocket-Extensions" header in the server's opening handshake.

接受扩展协商提议-在服务器的开始握手中,在“Sec WebSocket Extensions”标题中包含该提议的相应扩展协商响应。

Declining an extension negotiation offer - not including a corresponding extension negotiation response for the offer in the "Sec-WebSocket-Extensions" header in the server's opening handshake.

拒绝扩展协商报价-在服务器的开始握手中,在“Sec WebSocket Extensions”标题中不包括该报价的相应扩展协商响应。

4. WebSocket Per-Message Compression Extension
4. 每个消息的WebSocket压缩扩展

WebSocket PMCEs are extensions to the WebSocket Protocol enabling compression functionality. PMCEs are built based on the extension concept of the WebSocket Protocol specified in Section 9 of [RFC6455]. PMCEs are individually defined for each compression algorithm to be implemented and are registered in the "WebSocket Extension Name Registry" created in Section 11.4 of [RFC6455]. Each PMCE referring to this framework MUST define the following:

WebSocket PMCE是WebSocket协议的扩展,支持压缩功能。PMCE基于[RFC6455]第9节中规定的WebSocket协议的扩展概念构建。PMCE分别为要实现的每个压缩算法定义,并在[RFC6455]第11.4节创建的“WebSocket扩展名注册表”中注册。引用本框架的各PMCE必须定义以下内容:

o The extension name of the PMCE and any applicable extension parameters that MUST be included in the "Sec-WebSocket-Extensions" header during the extension negotiation offer/response.

o 在扩展协商报价/响应期间,必须包含在“Sec WebSocket Extensions”标题中的PMCE扩展名和任何适用的扩展参数。

o How to interpret the extension parameters exchanged during the opening handshake.

o 如何解释开始握手时交换的扩展参数。

o How to transform the payload of a message.

o 如何转换消息的有效负载。

One PMCE is defined in Section 7 of this document and is registered in Section 9. Other PMCEs may be defined in the future in other documents.

本文件第7节定义了一个PMCE,并在第9节中注册。其他PMCE可能在将来的其他文件中定义。

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

第5节描述了基本的扩展协商过程。第6节描述了如何将带有协商参数的压缩算法应用于WebSocket消息的内容。

5. Extension Negotiation
5. 延期谈判

To offer use of a PMCE, a client MUST include the extension name of the PMCE in the "Sec-WebSocket-Extensions" header field of its opening handshake of the WebSocket connection. Extension parameters

要提供PMCE的使用,客户端必须在其WebSocket连接开始握手的“Sec WebSocket Extensions”标题字段中包含PMCE的扩展名。扩展参数

are used to specify the PMCE offer in detail. For example, a client lists its preferred configuration parameter values for the compression algorithm of the PMCE. A client may also offer multiple PMCE choices to the server by including multiple elements in the "Sec-WebSocket-Extensions" header, one for each PMCE offered. This set of elements MAY include multiple PMCEs with the same extension name to offer the possibility to use the same algorithm with different configuration parameters. The order of elements is important as it specifies the client's preference. An element preceding another element has higher preference. It is recommended that a server accepts PMCEs with higher preference if the server supports them.

用于详细指定PMCE报价。例如,客户机列出了PMCE压缩算法的首选配置参数值。客户机还可以通过在“Sec WebSocket Extensions”头中包含多个元素向服务器提供多个PMCE选择,每个PMCE提供一个元素。这组元素可能包括具有相同扩展名的多个PMCE,以提供使用具有不同配置参数的相同算法的可能性。元素的顺序很重要,因为它指定了客户的偏好。在另一个元素之前的元素具有更高的首选项。如果服务器支持PMCE,建议服务器以更高的优先级接受它们。

A PMCE negotiation offer provides requests and/or hints to the server.

PMCE协商服务向服务器提供请求和/或提示。

A request in a PMCE negotiation offer indicates constraints on the server's behavior that must be satisfied if the server accepts the offer. For example, suppose that a server sends data compressed with the DEFLATE algorithm to a client. The server must keep the original bytes of data that it recently compressed and sent to the client. The client must keep the result of decompressing the bytes of data that it recently received from the server. The amount of bytes of data kept is called the LZ77 window size. The LZ77 window size of the client must not be less than the LZ77 window size of the server. In a PMCE negotiation offer, the client MUST inform the server of its LZ77 window size so that the server uses an LZ77 window size that is not greater than the LZ77 window size of the client. This restriction on the LZ77 window size is an example of a request in a PMCE negotiation offer.

PMCE协商报价中的请求表示如果服务器接受报价,必须满足的服务器行为约束。例如,假设服务器向客户端发送使用DEFLATE算法压缩的数据。服务器必须保留最近压缩并发送给客户端的原始数据字节。客户端必须保留最近从服务器收到的数据字节解压缩结果。保存的数据字节数称为LZ77窗口大小。客户端的LZ77窗口大小不得小于服务器的LZ77窗口大小。在PMCE协商报价中,客户机必须通知服务器其LZ77窗口大小,以便服务器使用不大于客户机LZ77窗口大小的LZ77窗口大小。LZ77窗口大小的限制是PMCE协商报价中请求的一个示例。

A hint in a PMCE negotiation offer provides information about the client's behavior that the server may either safely ignore or refer to when the server decides its behavior. For example, suppose that a client sends data compressed with the DEFLATE algorithm to a server. The client must keep the original bytes of data that it recently compressed and sent to the server. The server must keep the result of decompressing the bytes of data that it recently received from the client. The LZ77 window size of the server must not be less than the LZ77 window size of the client. In a PMCE negotiation offer, the client MAY inform the server of the maximum LZ77 window size the client can afford so that the server can choose to use an LZ77 window size that is not greater than the maximum size of the client. This information is an example of a hint in a PMCE negotiation offer. It's waste of memory to use an LZ77 window size greater than the LZ77 window size the client actually uses. Using the hint, the server can avoid the waste of memory. Since the hint itself doesn't specify the

PMCE协商提议中的提示提供了有关客户端行为的信息,服务器在决定其行为时可以安全地忽略或引用这些信息。例如,假设客户机将使用DEFLATE算法压缩的数据发送到服务器。客户端必须保留最近压缩并发送到服务器的原始数据字节。服务器必须保留最近从客户端接收的数据字节解压缩结果。服务器的LZ77窗口大小不得小于客户端的LZ77窗口大小。在PMCE协商提议中,客户机可以通知服务器客户机可以承受的最大LZ77窗口大小,以便服务器可以选择使用不大于客户机最大大小的LZ77窗口大小。此信息是PMCE谈判报价中的提示示例。使用LZ77窗口大小大于客户端实际使用的LZ77窗口大小是内存浪费。使用提示,服务器可以避免内存浪费。因为提示本身没有指定

constraints on the endpoints, the server must use the "agreed parameters" (defined below) to explicitly ask the client not to use an LZ77 window size greater than the LZ77 window size of the server.

在端点限制条件下,服务器必须使用“约定参数”(定义见下文)明确要求客户端不要使用大于服务器LZ77窗口大小的LZ77窗口大小。

To accept the use of an offered PMCE, a server MUST include the extension name of the PMCE in the "Sec-WebSocket-Extensions" header field of its opening handshake of the WebSocket connection. Extension parameters represent the detailed configuration parameters for the PMCE to use. These extension parameters and their values are called "agreed parameters". The element MUST represent a PMCE that is fully supported by the server. The contents of the element don't need to be exactly the same as those of the received extension negotiation offers. For example, suppose that a server received a PMCE extension negotiation offer with an extension parameter "X" indicating that the client can enable an optional feature named X. The server may accept the PMCE offer with an element without the extension parameter "X", meaning that the server chose not to enable the feature X. In this case, the offer contains the extension parameter "X", but the "agreed parameters" don't contain the extension parameter "X".

要接受提供的PMCE的使用,服务器必须在其WebSocket连接开始握手的“Sec WebSocket Extensions”标题字段中包含PMCE的扩展名。扩展参数表示PMCE要使用的详细配置参数。这些扩展参数及其值称为“约定参数”。元素必须表示服务器完全支持的PMCE。元素的内容不需要与收到的扩展协商报价的内容完全相同。例如,假设服务器收到一个带有扩展参数“X”的PMCE扩展协商提议,表示客户端可以启用一个名为X的可选功能。服务器可以接受带有一个不带扩展参数“X”的元素的PMCE提议,这意味着服务器选择不启用功能X。在这种情况下,报价包含扩展参数“X”,但“约定参数”不包含扩展参数“X”。

"Agreed parameters" must represent how the requests and hints in the client's extension negotiation offer have been handled in addition to the server's requests and hints on the client's behavior, so that the client can configure its behavior without identifying exactly which PMCE extension negotiation offer has been accepted.

“约定参数”必须表示除服务器的请求和对客户行为的提示外,客户机扩展谈判报价中的请求和提示是如何处理的,以便客户机可以配置其行为,而无需准确确定接受了哪个PMCE扩展谈判报价。

For example, if a client sends an extension negotiation offer that includes a parameter "enable_compression" and another without this parameter, the server accepts the former and informs the client by sending back an element that includes parameter(s) acknowledging "enable_compression". The name of the acknowledging parameter doesn't need to be exactly the same as the offer. For example, two parameters, "enable_strong_compression" and "enable_weak_compression", may be defined as acknowledging parameters for "enable_compression".

例如,如果客户机发送包含参数“enable_compression”的扩展协商要约,而另一个扩展协商要约没有此参数,则服务器接受前者,并通过发回包含参数“enable_compression”的元素来通知客户机。确认参数的名称不需要与报价完全相同。例如,两个参数“enable_strong_compression”和“enable_Slong_compression”可以定义为“enable_compression”的确认参数。

Compression features can be applied differently for each direction. For such features, the acknowledging parameter and the parameter in the reverse direction must be chosen to distinguish them. For example, in order to make parameters distinguishable, a "server_" prefix can be added to parameters affecting data sent from a server, and a "client_" prefix can be added to parameters affecting data sent from a client.

压缩功能可以针对每个方向应用不同的功能。对于这些特征,必须选择确认参数和反向参数来区分它们。例如,为了使参数可区分,可以将“服务器”前缀添加到影响从服务器发送的数据的参数,并将“客户端”前缀添加到影响从客户端发送的数据的参数。

A server MUST NOT accept a PMCE extension negotiation offer together with another extension if the PMCE will conflict with the extension on their use of the RSV1 bit. A client that received a response accepting a PMCE extension negotiation offer together with such an extension MUST _Fail the WebSocket Connection_.

如果PMCE在使用RSV1位时与扩展冲突,则服务器不得同时接受PMCE扩展协商提议和其他扩展。接收到响应并接受PMCE扩展协商服务以及此类扩展的客户端必须使WebSocket连接失败。

A server MUST NOT accept a PMCE extension negotiation offer together with another extension if the PMCE will be applied to the output of the extension and any of the following conditions applies to the extension:

如果PMCE将应用于扩展的输出,并且以下任何条件适用于扩展,则服务器不得接受PMCE扩展协商要约和其他扩展:

o The extension requires the boundary of frames to be preserved between the output from the extension at the sender and the input to the extension at the receiver.

o 扩展要求在发送方扩展的输出和接收方扩展的输入之间保留帧的边界。

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

o 扩展使用“扩展数据”字段或WebSocket头上的任何保留位作为每帧属性。

A client that receives a response accepting a PMCE extension negotiation offer together with such an extension MUST _Fail the WebSocket Connection_.

接收到响应并接受PMCE扩展协商服务以及此类扩展的客户端必须使WebSocket连接失败。

A server declining all offered PMCEs MUST NOT include any element with PMCE names. If a server responds with no PMCE element in the "Sec-WebSocket-Extensions" header, both endpoints proceed without per-message compression once _the WebSocket Connection is established_.

拒绝所有提供的PMCE的服务器不得包含任何具有PMCE名称的元素。如果服务器响应时“Sec WebSocket Extensions”标头中没有PMCE元素,则一旦建立WebSocket连接,两个端点都将继续进行,而不进行每消息压缩。

If a server gives an invalid response, such as accepting a PMCE that the client did not offer, the client MUST _Fail the WebSocket Connection_.

如果服务器给出无效响应,例如接受客户端未提供的PMCE,则客户端必须使WebSocket连接失败。

If a 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 6 and the message payload transformation (compressing and decompressing) procedure of the PMCE configured with the "agreed parameters" returned by the server to exchange messages.

如果服务器在“Sec WebSocket Extensions”标头中使用有效的PMCE元素进行响应,并且建立了WebSocket连接,则两个端点都必须使用第6节中描述的算法以及配置了“商定参数”的PMCE的消息负载转换(压缩和解压缩)过程由服务器返回以交换消息。

5.1. General Negotiation Flow
5.1. 一般协商流程

This section describes a general negotiation flow. How to handle parameters in detail must be specified in the document specifying the PMCE.

本节介绍一般协商流程。必须在指定PMCE的文件中详细说明如何处理参数。

A client makes an offer including parameters identifying the following:

客户提供的报价包括以下参数:

o Hints about how the client is planning to compress data

o 关于客户端计划如何压缩数据的提示

o Requests about how the server compresses data

o 请求服务器如何压缩数据

o Limitations concerning the client's compression functionality

o 有关客户端压缩功能的限制

The peer server makes a determination of its behavior based on these parameters. If the server can and wants to proceed with this PMCE enabled, the server responds to the client with parameters identifying the following:

对等服务器根据这些参数确定其行为。如果服务器能够并且希望继续启用此PMCE,则服务器将使用标识以下内容的参数响应客户端:

o Requests about how the client compresses data

o 关于客户端如何压缩数据的请求

o How the server will compress data

o 服务器将如何压缩数据

Based on these parameters received from the server, the client determines its behavior and if it can and wants to proceed with this PMCE enabled. Otherwise, the client starts the closing handshake with close code 1010.

根据从服务器接收到的这些参数,客户机确定其行为,以及是否能够并且希望继续启用此PMCE。否则,客户端将使用关闭代码1010启动关闭握手。

5.2. Negotiation Examples
5.2. 谈判实例

The following are example values for the "Sec-WebSocket-Extensions" header offering PMCEs; permessage-foo and permessage-bar in the examples are hypothetical extension names of PMCEs for the compression algorithm foo and bar.

以下是提供PMCE的“Sec WebSocket Extensions”标题的示例值;示例中的permessage foo和permessage bar是压缩算法foo和bar的pmce的假设扩展名。

o Offer the permessage-foo.

o 提供永久消息foo。

permessage-foo

permessage foo

o Offer the permessage-foo with a parameter x with a value of 10.

o 为permessagefoo提供一个值为10的参数x。

permessage-foo; x=10

permessage-foo;x=10

The value may be quoted.

该值可以引用。

permessage-foo; x="10"

permessage-foo;x=“10”

o Offer the permessage-foo as first choice and the permessage-bar as a fallback plan.

o 将permessagefoo作为首选,将permessagebar作为备用计划。

permessage-foo, permessage-bar

permessagefoo,permessagebar

o Offer the permessage-foo with a parameter use_y, which enables a feature y as first choice, and the permessage-foo without the use_y parameter as a fallback plan.

o 为permessage foo提供一个参数use_y,使功能y成为首选,并为permessage foo提供不带use_y参数的备用计划。

permessage-foo; use_y, permessage-foo

permessage-foo;使用_y,permessage foo

6. Framing
6. 框架

PMCEs operate only on data messages.

PMCE仅对数据信息进行操作。

This document allocates the RSV1 bit of the WebSocket header for PMCEs and calls the bit the "Per-Message Compressed" bit. On a WebSocket connection where a PMCE is in use, this bit indicates whether a message is compressed or not.

本文档为PMCE分配WebSocket头的RSV1位,并将该位称为“每消息压缩”位。在使用PMCE的WebSocket连接上,此位指示消息是否被压缩。

A message with the "Per-Message Compressed" bit set on the first fragment of the message is called a "compressed message". Frames of a compressed message have compressed data in the payload data portion. An endpoint receiving a compressed message decompresses the concatenation of the compressed data of the frames of the message by following the decompression procedure specified by the PMCE in use. The endpoint uses the bytes corresponding to the application data portion in this decompressed data for the _A WebSocket Message Has Been Received_ event instead of the received data as is.

在消息的第一个片段上设置了“每条消息压缩”位的消息称为“压缩消息”。压缩消息的帧在有效载荷数据部分中具有压缩数据。接收压缩消息的端点通过遵循使用中的PMCE指定的解压缩过程来解压缩消息帧的压缩数据的级联。端点使用与此解压缩数据中的应用程序数据部分相对应的字节作为“已接收WebSocket消息”事件,而不是按原样接收数据。

A message with the "Per-Message Compressed" bit unset on the first fragment of the message is called an "uncompressed message". Frames of an uncompressed message have uncompressed original data as is in the payload data portion. An endpoint receiving an uncompressed message uses the concatenation of the application data portion of the frames of the message as is for the _A WebSocket Message Has Been Received_ event.

在消息的第一个片段上未设置“每条消息压缩”位的消息称为“未压缩消息”。未压缩消息的帧具有未压缩的原始数据,如同在有效负载数据部分中一样。接收未压缩消息的端点使用消息帧的应用程序数据部分的串联,就像“WebSocket消息已接收”事件一样。

6.1. Compression
6.1. 压缩

An endpoint MUST use the following algorithm to send a message in the form of a compressed message.

端点必须使用以下算法以压缩消息的形式发送消息。

1. Compress the message payload of the original message by following the compression procedure of the PMCE. The original message may be input from the application layer or output of another WebSocket extension, depending on which extensions were negotiated.

1. 按照PMCE的压缩程序压缩原始消息的消息有效负载。原始消息可能是从应用层输入的,也可能是另一个WebSocket扩展的输出,具体取决于协商的扩展。

2. Process the compressed data as follows:

2. 按如下方式处理压缩数据:

* If this PMCE is the last extension to process outgoing messages, build frame(s) using the compressed data instead of the original data for the message payload, set the "Per-Message Compressed" bit of the first frame, and then send the frame(s) as described in Section 6.1 of [RFC6455].

* 如果此PMCE是处理传出消息的最后一个扩展,则使用压缩数据而不是消息有效负载的原始数据构建帧,设置第一帧的“每条消息压缩”位,然后按照[RFC6455]第6.1节中的说明发送帧。

* Otherwise, pass the transformed message payload and modified header values, including the "Per-Message Compressed" bit value set to 1, to the next extension after the PMCE. If the extension expects frames for input, build a frame for the message and pass it.

* 否则,将转换后的消息有效负载和修改后的报头值(包括设置为1的“每条消息压缩”位值)传递给PMCE之后的下一个扩展。如果扩展需要输入帧,则为消息构建一个帧并传递它。

An endpoint MUST use the following algorithm to send a message in the form of an uncompressed message.

端点必须使用以下算法以未压缩消息的形式发送消息。

1. Process the original data as follows:

1. 按如下方式处理原始数据:

* If this PMCE is the last extension to process outgoing messages, build frame(s) using the original data for the payload data portion as is, unset the "Per-Message Compressed" bit of the first frame, and then send the frame(s) as described in Section 6.1 of [RFC6455].

* 如果此PMCE是处理传出消息的最后一个扩展,请按原样使用有效负载数据部分的原始数据构建帧,取消设置第一帧的“每条消息压缩”位,然后按照[RFC6455]第6.1节中的说明发送帧。

* Otherwise, pass the message payload and header values to the next extension after the PMCE as is. If the extension expects frames for input, build a frame for the message and pass it.

* 否则,按原样将消息有效负载和报头值传递给PMCE之后的下一个扩展。如果扩展需要输入帧,则为消息构建一个帧并传递它。

An endpoint MUST NOT set the "Per-Message Compressed" bit of control frames and non-first fragments of a data message. An endpoint receiving such a frame MUST _Fail the WebSocket Connection_.

端点不得设置控制帧的“每条消息压缩”位和数据消息的非第一个片段。接收这种帧的端点必须使WebSocket连接失败。

PMCEs do not change the opcode field. The opcode of the first frame of a compressed message indicates the opcode of the original message.

PMCE不会更改操作码字段。压缩消息的第一帧的操作码表示原始消息的操作码。

The payload data portion in frames generated by a PMCE is not subject to the constraints for the original data type. For example, the concatenation of the output data corresponding to the application data portion of frames of a compressed text message is not required to be valid UTF-8. At the receiver, the payload data portion after decompression is subject to the constraints for the original data type again.

由PMCE生成的帧中的有效载荷数据部分不受原始数据类型的约束。例如,与压缩文本消息的帧的应用数据部分相对应的输出数据的串联不需要是有效的UTF-8。在接收器处,解压缩后的有效载荷数据部分再次受制于原始数据类型的约束。

6.2. Decompression
6.2. 减压

An endpoint MUST use the following algorithm to receive a message in the form of a compressed message.

端点必须使用以下算法来接收压缩消息形式的消息。

1. Concatenate the payload data portion of the received frames of the compressed message. The received frames may be direct input from the underlying transport or output of another WebSocket extension, depending on which extensions were negotiated.

1. 连接压缩消息的接收帧的有效负载数据部分。接收到的帧可以是来自底层传输的直接输入,也可以是另一个WebSocket扩展的输出,具体取决于协商的扩展。

2. Decompress the concatenation by following the decompression procedure of the PMCE.

2. 按照PMCE的解压缩程序对串联进行解压缩。

3. Process the decompressed message as follows:

3. 按如下方式处理解压缩的消息:

* If this is the last extension to process incoming messages, deliver the _A WebSocket Message Has Been Received_ event to the application layer with the decompressed message payload and header values, including the "Per-Message Compressed" bit unset to 0.

* 如果这是处理传入消息的最后一个扩展,请将_awebsocket Message Has Received u事件传递到应用层,并使用解压缩的消息负载和头值,包括未设置为0的“每条消息压缩”位。

* Otherwise, pass the decompressed message payload and header values, including the "Per-Message Compressed" bit unset to 0, to the extension preceding the PMCE. If the extension expects frames for input, build a frame for the message and pass it.

* 否则,将解压缩的消息有效负载和头值(包括未设置为0的“每条消息压缩”位)传递给PMCE之前的扩展。如果扩展需要输入帧,则为消息构建一个帧并传递它。

An endpoint MUST use the following algorithm to receive a message in the form of an uncompressed message.

端点必须使用以下算法来接收未压缩消息形式的消息。

1. Process the received message as follows:

1. 按如下方式处理收到的消息:

* If this PMCE is the last extension to process incoming messages, deliver the _A WebSocket Message Has Been Received_ event to the application layer with the received message payload and header values as is.

* 如果此PMCE是处理传入消息的最后一个扩展,请将_awebsocket Message was Received u事件传递到应用层,并按原样提供接收到的消息负载和头值。

* Otherwise, pass the message payload and header values to the extension preceding the PMCE as is. If the extension expects frames for input, build a frame for the message and pass it.

* 否则,按原样将消息有效负载和报头值传递给PMCE前面的扩展。如果扩展需要输入帧,则为消息构建一个帧并传递它。

7. The "permessage-deflate" Extension
7. “permessagedeflate”扩展

This section defines a specific PMCE called "permessage-deflate". It compresses the payload of a message using the DEFLATE algorithm [RFC1951] and uses the byte boundary alignment method introduced in [RFC1979].

本节定义了一个称为“permessage deflate”的特定PMCE。它使用DEFLATE算法[RFC1951]压缩消息的有效负载,并使用[RFC1979]中介绍的字节边界对齐方法。

This section uses the term "byte" with the same meaning as used in [RFC1951], i.e., 8 bits stored or transmitted as a unit (same as an octet).

本节使用术语“字节”,其含义与[RFC1951]中使用的相同,即作为一个单元存储或传输8位(与八位字节相同)。

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

此扩展的注册扩展名为“permessage deflate”。

Four extension parameters are defined for "permessage-deflate" to help endpoints manage per-connection resource usage.

为“permessage deflate”定义了四个扩展参数,以帮助端点管理每个连接的资源使用情况。

o "server_no_context_takeover"

o “服务器\u无\u上下文\u接管”

o "client_no_context_takeover"

o “客户端\u无\u上下文\u接管”

o "server_max_window_bits"

o “服务器\u最大\u窗口\u位”

o "client_max_window_bits"

o “客户端\u最大\u窗口\u位”

These parameters enable two methods (no_context_takeover and max_window_bits) of constraining memory usage that may be applied independently to either direction of WebSocket traffic. The extension parameters with the "client_" prefix are used by the client to configure its compressor and by the server to configure its decompressor. The extension parameters with the "server_" prefix are used by the server to configure its compressor and by the client to configure its decompressor. All four parameters are defined for both a client's extension negotiation offer and a server's extension negotiation response.

这些参数支持两种限制内存使用的方法(无上下文接管和最大窗口位),这两种方法可以独立应用于WebSocket通信的任一方向。带有“client_”前缀的扩展参数由客户端用于配置其压缩器,由服务器用于配置其解压缩器。带有“服务器\前缀”的扩展参数由服务器用于配置其压缩器,由客户端用于配置其解压缩器。这四个参数都是为客户端的扩展协商提供和服务器的扩展协商响应定义的。

A server MUST decline an extension negotiation offer for this extension if any of the following conditions are met:

如果满足以下任何条件,服务器必须拒绝此扩展的扩展协商报价:

o The negotiation offer contains an extension parameter not defined for use in an offer.

o 协商报价包含未定义用于报价的扩展参数。

o The negotiation offer contains an extension parameter with an invalid value.

o 协商报价包含具有无效值的扩展参数。

o The negotiation offer contains multiple extension parameters with the same name.

o 协商报价包含多个同名的扩展参数。

o The server doesn't support the offered configuration.

o 服务器不支持提供的配置。

A client MUST _Fail the WebSocket Connection_ if the peer server accepted an extension negotiation offer for this extension with an extension negotiation response meeting any of the following conditions:

如果对等服务器接受了此扩展的扩展协商提议,且扩展协商响应满足以下任一条件,则客户端必须_使WebSocket连接失败u:

o The negotiation response contains an extension parameter not defined for use in a response.

o 协商响应包含未定义用于响应的扩展参数。

o The negotiation response contains an extension parameter with an invalid value.

o 协商响应包含具有无效值的扩展参数。

o The negotiation response contains multiple extension parameters with the same name.

o 协商响应包含多个同名的扩展参数。

o The client does not support the configuration that the response represents.

o 客户端不支持响应所表示的配置。

The term "LZ77 sliding window" [LZ77] used in this section means the buffer used by the DEFLATE algorithm to store recently processed input. The DEFLATE compression algorithm searches the buffer for a match with the following input.

本节中使用的术语“LZ77滑动窗口”[LZ77]是指DEFLATE算法用于存储最近处理的输入的缓冲区。DEFLATE压缩算法在缓冲区中搜索与以下输入匹配的内容。

The term "use context takeover" used in this section means that the same LZ77 sliding window used by the endpoint to build frames of the previous sent message is reused to build frames of the next message to be sent.

本节中使用的术语“use context takeover”(使用上下文接管)意味着端点用于构建前一条发送消息的帧的相同LZ77滑动窗口被重新用于构建下一条要发送消息的帧。

7.1. Extension Parameters
7.1. 扩展参数
7.1.1. Context Takeover Control
7.1.1. 上下文接管控制
7.1.1.1. The "server_no_context_takeover" Extension Parameter
7.1.1.1. “服务器\u无\u上下文\u接管”扩展参数

A client MAY include the "server_no_context_takeover" extension parameter in an extension negotiation offer. This extension parameter has no value. By including this extension parameter in an extension negotiation offer, a client prevents the peer server from using context takeover. If the peer server doesn't use context takeover, the client doesn't need to reserve memory to retain the LZ77 sliding window between messages.

客户机可以在扩展协商报价中包含“server\u no\u context\u takeover”扩展参数。此扩展参数没有值。通过在扩展协商提供中包含此扩展参数,客户机可以防止对等服务器使用上下文接管。如果对等服务器不使用上下文接管,则客户端不需要保留内存来保留消息之间的LZ77滑动窗口。

Absence of this extension parameter in an extension negotiation offer indicates that the client can decompress a message that the server built using context takeover.

扩展协商提供中缺少此扩展参数表示客户端可以解压缩服务器使用上下文接管生成的消息。

A server accepts an extension negotiation offer that includes the "server_no_context_takeover" extension parameter by including the "server_no_context_takeover" extension parameter in the corresponding extension negotiation response to send back to the client. The "server_no_context_takeover" extension parameter in an extension negotiation response has no value.

服务器通过在相应的扩展协商响应中包含“server\u no\u context\u takeover”扩展参数以发送回客户端,从而接受包含“server\u no\u context\u takeover”扩展参数的扩展协商要约。扩展协商响应中的“server\u no\u context\u takeover”扩展参数没有值。

It is RECOMMENDED that a server supports the "server_no_context_takeover" extension parameter in an extension negotiation offer.

建议服务器在扩展协商报价中支持“server\u no\u context\u takeover”扩展参数。

A server MAY include the "server_no_context_takeover" extension parameter in an extension negotiation response even if the extension negotiation offer being accepted by the extension negotiation response didn't include the "server_no_context_takeover" extension parameter.

服务器可能会在扩展协商响应中包含“server\u no\u context\u takeover”扩展参数,即使扩展协商响应接受的扩展协商要约不包含“server\u no\u context\u takeover”扩展参数。

7.1.1.2. The "client_no_context_takeover" Extension Parameter
7.1.1.2. “client\u no\u context\u takeover”扩展参数

A client MAY include the "client_no_context_takeover" extension parameter in an extension negotiation offer. This extension parameter has no value. By including this extension parameter in an extension negotiation offer, a client informs the peer server of a hint that even if the server doesn't include the "client_no_context_takeover" extension parameter in the corresponding extension negotiation response to the offer, the client is not going to use context takeover.

客户机可以在扩展协商报价中包含“client\u no\u context\u takeover”扩展参数。此扩展参数没有值。通过在扩展协商报价中包含此扩展参数,客户机将通知对等服务器一个提示,即即使服务器在报价的相应扩展协商响应中不包含“client_no_context_takeover”扩展参数,客户机也不会使用context takeover。

A server MAY include the "client_no_context_takeover" extension parameter in an extension negotiation response. If the received extension negotiation offer includes the "client_no_context_takeover" extension parameter, the server may either ignore the parameter or use the parameter to avoid taking over the LZ77 sliding window unnecessarily by including the "client_no_context_takeover" extension parameter in the corresponding extension negotiation response to the offer. The "client_no_context_takeover" extension parameter in an extension negotiation response has no value. By including the "client_no_context_takeover" extension parameter in an extension negotiation response, a server prevents the peer client from using context takeover. This reduces the amount of memory that the server has to reserve for the connection.

服务器可以在扩展协商响应中包含“client\u no\u context\u takeover”扩展参数。如果收到的扩展协商要约包括“客户端无上下文接管”扩展参数,则服务器可以忽略该参数,或者使用该参数通过包括“客户端无上下文接管”来避免不必要地接管LZ77滑动窗口对应报价的延期谈判响应中的延期参数。扩展协商响应中的“client\u no\u context\u takeover”扩展参数没有值。通过在扩展协商响应中包含“client\u no\u context\u takeover”扩展参数,服务器可以防止对等客户端使用context takeover。这将减少服务器为连接保留的内存量。

Absence of this extension parameter in an extension negotiation response indicates that the server can decompress messages built by the client using context takeover.

扩展协商响应中缺少此扩展参数表示服务器可以使用上下文接管解压缩客户端生成的消息。

A client MUST support the "client_no_context_takeover" extension parameter in an extension negotiation response.

客户端必须在扩展协商响应中支持“client\u no\u context\u takeover”扩展参数。

7.1.2. Limiting the LZ77 Sliding Window Size
7.1.2. 限制LZ77滑动窗口的大小
7.1.2.1. The "server_max_window_bits" Extension Parameter
7.1.2.1. “服务器\u最大\u窗口\u位”扩展参数

A client MAY include the "server_max_window_bits" extension parameter in an extension negotiation offer. This parameter has a decimal integer value without leading zeroes between 8 to 15, inclusive, indicating the base-2 logarithm of the LZ77 sliding window size, and MUST conform to the ABNF below.

客户机可以在扩展协商报价中包含“服务器最大窗口位”扩展参数。此参数有一个十进制整数值,前导零不在8到15之间(含8到15),表示LZ77滑动窗口大小的以2为底的对数,并且必须符合下面的ABNF。

       server-max-window-bits = 1*DIGIT
        
       server-max-window-bits = 1*DIGIT
        

By including this parameter in an extension negotiation offer, a client limits the LZ77 sliding window size that the server will use to compress messages. If the peer server uses a small LZ77 sliding window to compress messages, the client can reduce the memory needed for the LZ77 sliding window.

通过在扩展协商提供中包含此参数,客户机限制服务器用于压缩消息的LZ77滑动窗口大小。如果对等服务器使用一个小的LZ77滑动窗口来压缩消息,客户端可以减少LZ77滑动窗口所需的内存。

A server declines an extension negotiation offer with this parameter if the server doesn't support it.

如果服务器不支持此参数,则服务器将拒绝扩展协商提供。

Absence of this parameter in an extension negotiation offer indicates that the client can receive messages compressed using an LZ77 sliding window of up to 32,768 bytes.

扩展协商提供中缺少此参数表示客户端可以接收使用最多32768字节的LZ77滑动窗口压缩的消息。

A server accepts an extension negotiation offer with this parameter by including the "server_max_window_bits" extension parameter in the extension negotiation response to send back to the client with the same or smaller value as the offer. The "server_max_window_bits" extension parameter in an extension negotiation response has a decimal integer value without leading zeroes between 8 to 15, inclusive, indicating the base-2 logarithm of the LZ77 sliding window size, and MUST conform to the ABNF below.

服务器通过在扩展协商响应中包含“server\u max\u window\u bits”扩展参数来接受带有此参数的扩展协商要约,并以与要约相同或更小的值发送回客户端。扩展协商响应中的“server_max_window_bits”扩展参数具有十进制整数值,前导零不在8到15之间(含8到15),表示LZ77滑动窗口大小的以2为底对数,并且必须符合以下ABNF。

       server-max-window-bits = 1*DIGIT
        
       server-max-window-bits = 1*DIGIT
        

A server MAY include the "server_max_window_bits" extension parameter in an extension negotiation response even if the extension negotiation offer being accepted by the response didn't include the "server_max_window_bits" extension parameter.

服务器可以在扩展协商响应中包含“server\u max\u window\u bits”扩展参数,即使响应接受的扩展协商要约不包含“server\u max\u window\u bits”扩展参数。

7.1.2.2. The "client_max_window_bits" Extension Parameter
7.1.2.2. “客户端\u最大\u窗口\u位”扩展参数

A client MAY include the "client_max_window_bits" extension parameter in an extension negotiation offer. This parameter has no value or a decimal integer value without leading zeroes between 8 to 15

客户机可以在扩展协商报价中包括“客户机\u最大\u窗口\u位”扩展参数。此参数没有值或十进制整数值,前导零在8到15之间

inclusive indicating the base-2 logarithm of the LZ77 sliding window size. If a value is specified for this parameter, the value MUST conform to the ABNF below.

包括表示LZ77滑动窗口大小的以2为底的对数。如果为此参数指定了值,则该值必须符合以下ABNF。

       client-max-window-bits = 1*DIGIT
        
       client-max-window-bits = 1*DIGIT
        

By including this parameter in an offer, a client informs the peer server that the client supports the "client_max_window_bits" extension parameter in an extension negotiation response and, optionally, a hint by attaching a value to the parameter. If the "client_max_window_bits" extension parameter in an extension negotiation offer has a value, the parameter also informs the peer server of a hint that even if the server doesn't include the "client_max_window_bits" extension parameter in the corresponding extension negotiation response with a value greater than the one in the extension negotiation offer or if the server doesn't include the extension parameter at all, the client is not going to use an LZ77 sliding window size greater than the size specified by the value in the extension negotiation offer to compress messages.

通过将此参数包括在报价中,客户机通知对等服务器,该客户机支持扩展协商响应中的“client_max_window_bits”扩展参数,并且(可选)通过向参数附加值来提供提示。如果扩展协商报价中的“client_max_window_bits”扩展参数有一个值,则该参数还会通知对等服务器一个提示,即即使服务器不包含“client_max_window_bits”相应扩展协商响应中的扩展参数,其值大于扩展协商报价中的值,或者如果服务器根本不包含扩展参数,客户端不会使用LZ77滑动窗口大小大于扩展协商提供中的值指定的大小来压缩消息。

If a received extension negotiation offer has the "client_max_window_bits" extension parameter, the server MAY include the "client_max_window_bits" extension parameter in the corresponding extension negotiation response to the offer. If the "client_max_window_bits" extension parameter in a received extension negotiation offer has a value, the server may either ignore this value or use this value to avoid allocating an unnecessarily big LZ77 sliding window by including the "client_max_window_bits" extension parameter in the corresponding extension negotiation response to the offer with a value equal to or smaller than the received value. The "client_max_window_bits" extension parameter in an extension negotiation response has a decimal integer value without leading zeroes between 8 to 15 inclusive indicating the base-2 logarithm of the LZ77 sliding window size and MUST conform to the ABNF below.

如果接收到的扩展协商要约具有“client_max_window_bits”扩展参数,则服务器可以在对该要约的相应扩展协商响应中包括“client_max_window_bits”扩展参数。如果接收到的扩展协商要约中的“客户端最大窗口位”扩展参数有一个值,服务器可以忽略该值,或者使用该值通过包括“客户端最大窗口位”来避免分配不必要的大LZ77滑动窗口对应于报价的扩展协商响应中的扩展参数,其值等于或小于收到的值。扩展协商响应中的“client_max_window_bits”扩展参数具有十进制整数值,前导零不在8到15之间(含8到15),表示LZ77滑动窗口大小的基数2对数,并且必须符合以下ABNF。

       client-max-window-bits = 1*DIGIT
        
       client-max-window-bits = 1*DIGIT
        

By including this extension parameter in an extension negotiation response, a server limits the LZ77 sliding window size that the client uses to compress messages. This reduces the amount of memory for the decompression context that the server has to reserve for the connection.

通过在扩展协商响应中包含此扩展参数,服务器限制客户端用于压缩消息的LZ77滑动窗口大小。这减少了服务器为连接保留的解压缩上下文的内存量。

If a received extension negotiation offer doesn't have the "client_max_window_bits" extension parameter, the corresponding extension negotiation response to the offer MUST NOT include the "client_max_window_bits" extension parameter.

如果收到的扩展谈判报价没有“客户端最大窗口位”扩展参数,则对应于报价的扩展谈判响应不得包含“客户端最大窗口位”扩展参数。

Absence of this extension parameter in an extension negotiation response indicates that the server can receive messages compressed using an LZ77 sliding window of up to 32,768 bytes.

扩展协商响应中缺少此扩展参数表示服务器可以接收使用最多32768字节的LZ77滑动窗口压缩的消息。

7.1.3. Examples
7.1.3. 例子

The simplest "Sec-WebSocket-Extensions" header in a client's opening handshake to offer use of the "permessage-deflate" extension looks like this:

客户端开始握手时提供使用“permessage deflate”扩展的最简单的“Sec WebSocket Extensions”头如下所示:

Sec-WebSocket-Extensions: permessage-deflate

Sec WebSocket扩展:permessage deflate

Since the "client_max_window_bits" extension parameter is not included in this extension negotiation offer, the server must not accept the offer with an extension negotiation response that includes the "client_max_window_bits" extension parameter. The simplest "Sec-WebSocket-Extensions" header in a server's opening handshake to accept use of the "permessage-deflate" extension is the same:

由于“客户端最大窗口位”扩展参数未包含在此扩展协商报价中,服务器不得接受包含“客户端最大窗口位”扩展参数的扩展协商响应报价。服务器开始握手以接受使用“permessage deflate”扩展时,最简单的“Sec WebSocket Extensions”头是相同的:

Sec-WebSocket-Extensions: permessage-deflate

Sec WebSocket扩展:permessage deflate

The following extension negotiation offer sent by a client is asking the server to use an LZ77 sliding window with a size of 1,024 bytes or less and declaring that the client supports the "client_max_window_bits" extension parameter in an extension negotiation response.

以下由客户端发送的扩展协商提议要求服务器使用大小为1024字节或更小的LZ77滑动窗口,并声明客户端在扩展协商响应中支持“client_max_window_bits”扩展参数。

Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits; server_max_window_bits=10

Sec WebSocket扩展:permessage deflate;客户端\u最大\u窗口\u位;服务器\u最大\u窗口\u位=10

This extension negotiation offer might be rejected by the server because the server doesn't support the "server_max_window_bits" extension parameter in an extension negotiation offer. This is fine if the client cannot receive messages compressed using a larger sliding window size, but if the client just prefers using a small window but wants to fall back to the "permessage-deflate" without the "server_max_window_bits" extension parameter, the client can make an offer with the fallback option like this:

此扩展协商提供可能被服务器拒绝,因为服务器不支持扩展协商提供中的“服务器\u最大\u窗口\u位”扩展参数。如果客户端无法接收使用较大滑动窗口大小压缩的消息,这是可以接受的,但是如果客户端只是喜欢使用小窗口,但希望返回到没有“server_max_window_bits”扩展参数的“permessage deflate”,则客户端可以使用如下回退选项提供:

       Sec-WebSocket-Extensions:
           permessage-deflate;
           client_max_window_bits; server_max_window_bits=10,
           permessage-deflate;
           client_max_window_bits
        
       Sec-WebSocket-Extensions:
           permessage-deflate;
           client_max_window_bits; server_max_window_bits=10,
           permessage-deflate;
           client_max_window_bits
        

The server can accept "permessage-deflate" by picking any supported one from the listed offers. To accept the first option, for example, the server may send back a response as follows:

服务器可以通过从列出的服务中选择任何支持的服务来接受“permessage deflate”。例如,要接受第一个选项,服务器可以按如下方式发回响应:

Sec-WebSocket-Extensions: permessage-deflate; server_max_window_bits=10

Sec WebSocket扩展:permessage deflate;服务器\u最大\u窗口\u位=10

To accept the second option, for example, the server may send back a response as follows:

例如,要接受第二个选项,服务器可以按如下方式发回响应:

Sec-WebSocket-Extensions: permessage-deflate

Sec WebSocket扩展:permessage deflate

7.2. Message Payload Transformation
7.2. 消息负载转换
7.2.1. Compression
7.2.1. 压缩

An endpoint uses the following algorithm to compress a message.

端点使用以下算法压缩消息。

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

1. 使用DEFLATE压缩消息有效负载的所有八位字节。

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

2. 如果生成的数据没有以没有压缩的空DEFLATE块结束(“BTYPE”位设置为00),则在末尾附加一个没有压缩的空DEFLATE块。

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

3. 从尾端移除4个八位字节(即0x00 0x00 0xff 0xff)。在此步骤之后,压缩数据的最后一个八位字节包含(可能是部分)DEFLATE报头位,“BTYPE”位设置为00。

When using DEFLATE in the first step above:

在上述第一步中使用DEFLATE时:

o An endpoint MAY use multiple DEFLATE blocks to compress one message.

o 端点可以使用多个DEFLATE块压缩一条消息。

o An endpoint MAY use DEFLATE blocks of any type.

o 端点可以使用任何类型的放气块。

o An endpoint MAY use both DEFLATE blocks with the "BFINAL" bit set to 0 and DEFLATE blocks with the "BFINAL" bit set to 1.

o 端点可以使用“BFINAL”位设置为0的放气块和“BFINAL”位设置为1的放气块。

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

o 当“BFINAL”位设置为1的任何DEFLATE块未在字节边界处结束时,端点必须添加最小填充位0以使其在字节边界处结束。下一个DEFLATE块跟随填充的数据(如果有)。

An endpoint fragments a compressed message by splitting the result of running this algorithm. Even when only part of the payload is available, a fragment can be built by compressing the available data

端点通过分割运行此算法的结果来分割压缩消息。即使只有部分有效负载可用,也可以通过压缩可用数据来构建片段

and choosing the block type appropriately so that the end of the resulting compressed data is aligned at a byte boundary. Note that for non-final fragments, the removal of 0x00 0x00 0xff 0xff MUST NOT be done.

以及适当地选择块类型,使得得到的压缩数据的末端在字节边界处对齐。请注意,对于非最终片段,不得删除0x00 0x00 0xff 0xff。

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

端点不得使用长度超过32768字节的LZ77滑动窗口来压缩要发送的消息。

If the "agreed parameters" contain the "client_no_context_takeover" extension parameter, the client MUST start compressing each new message with an empty LZ77 sliding window. Otherwise, the client MAY take over the LZ77 sliding window used to build the last compressed message. Note that even if the client has included the "client_no_context_takeover" extension parameter in its offer, the client MAY take over the LZ77 sliding window used to build the last compressed message if the "agreed parameters" don't contain the "client_no_context_takeover" extension parameter. The client-to-server "client_no_context_takeover" extension parameter is just a hint for the server to build an extension negotiation response.

如果“商定的参数”包含“client\u no\u context\u takeover”扩展参数,则客户端必须使用空的LZ77滑动窗口开始压缩每个新消息。否则,客户端可能会接管用于构建最后一条压缩消息的LZ77滑动窗口。请注意,即使客户机在其报价中包含了“client_no_context_takeover”扩展参数,如果“agreed parameters”不包含“client_no_context_takeover”扩展参数,客户机也可以接管用于构建最后一条压缩消息的LZ77滑动窗口。客户端到服务器的“client\u no\u context\u takeover”扩展参数只是提示服务器构建扩展协商响应。

If the "agreed parameters" contain the "server_no_context_takeover" extension parameter, the server MUST start compressing each new message with an empty LZ77 sliding window. Otherwise, the server MAY take over the LZ77 sliding window used to build the last compressed message.

如果“商定参数”包含“服务器\u无\u上下文\u接管”扩展参数,则服务器必须使用空LZ77滑动窗口开始压缩每个新消息。否则,服务器可能会接管用于生成最后一条压缩消息的LZ77滑动窗口。

If the "agreed parameters" contain the "client_max_window_bits" extension parameter with a value of w, the client MUST NOT use an LZ77 sliding window longer than the w-th power of 2 bytes to compress messages to send. Note that even if the client has included in its offer the "client_max_window_bits" extension parameter with a value smaller than one in the "agreed parameters", the client MAY use an LZ77 sliding window with any size to compress messages to send as long as the size conforms to the "agreed parameters". The client-to-server "client_max_window_bits" extension parameter is just a hint for the server to build an extension negotiation response.

如果“商定参数”包含值为w的“客户端\u最大\u窗口\u位”扩展参数,则客户端不得使用长度超过2字节w次方的LZ77滑动窗口来压缩要发送的消息。请注意,即使客户机在其报价中包含了“客户机最大窗口位”扩展参数,其值小于“约定参数”中的值,只要大小符合“约定参数”,客户机也可以使用任意大小的LZ77滑动窗口来压缩要发送的消息。客户端到服务器的“client\u max\u window\u bits”扩展参数只是提示服务器构建扩展协商响应。

If the "agreed parameters" contain the "server_max_window_bits" extension parameter with a value of w, the server MUST NOT use an LZ77 sliding window longer than the w-th power of 2 bytes to compress messages to send.

如果“商定参数”包含值为w的“服务器最大窗口位”扩展参数,则服务器不得使用长度超过2字节w次方的LZ77滑动窗口来压缩要发送的消息。

7.2.2. Decompression
7.2.2. 减压

An endpoint uses the following algorithm to decompress a message.

端点使用以下算法解压缩消息。

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

1. 将0x00 0x00 0xff 0xff的4个八位字节附加到消息有效负载的末尾。

2. Decompress the resulting data using DEFLATE.

2. 使用DEFLATE解压缩生成的数据。

If the "agreed parameters" contain the "server_no_context_takeover" extension parameter, the client MAY decompress each new message with an empty LZ77 sliding window. Otherwise, the client MUST decompress each new message using the LZ77 sliding window used to process the last compressed message.

如果“商定参数”包含“服务器\u无\u上下文\u接管”扩展参数,则客户端可以使用空LZ77滑动窗口解压缩每个新消息。否则,客户端必须使用LZ77滑动窗口来解压缩每个新消息,该窗口用于处理最后一条压缩消息。

If the "agreed parameters" contain the "client_no_context_takeover" extension parameter, the server MAY decompress each new message with an empty LZ77 sliding window. Otherwise, the server MUST decompress each new message using the LZ77 sliding window used to process the last compressed message. Note that even if the client has included the "client_no_context_takeover" extension parameter in its offer, the server MUST decompress each new message using the LZ77 sliding window used to process the last compressed message if the "agreed parameters" don't contain the "client_no_context_takeover" extension parameter. The client-to-server "client_no_context_takeover" extension parameter is just a hint for the server to build an extension negotiation response.

如果“商定的参数”包含“client\u no\u context\u takeover”扩展参数,则服务器可以使用空的LZ77滑动窗口解压缩每个新消息。否则,服务器必须使用LZ77滑动窗口解压每条新消息,该窗口用于处理最后一条压缩消息。请注意,即使客户机在其报价中包含了“client\u no\u context\u takeover”扩展参数,如果“agreed parameters”不包含“client\u no\u context\u takeover”扩展参数,服务器也必须使用LZ77滑动窗口对每个新消息进行解压缩,LZ77滑动窗口用于处理最后的压缩消息。客户端到服务器的“client\u no\u context\u takeover”扩展参数只是提示服务器构建扩展协商响应。

If the "agreed parameters" contain the "server_max_window_bits" extension parameter with a value of w, the client MAY reduce the size of 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.

如果“商定参数”包含值为w的“服务器最大窗口位”扩展参数,则客户端可减小其LZ77滑动窗口的大小,以将接收到的消息解压缩至2字节的w次方。否则,客户端必须使用32768字节的LZ77滑动窗口来解压缩接收到的消息。

If the "agreed parameters" contain the "client_max_window_bits" extension parameter with a value of w, the server MAY reduce the size of 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. Note that even if the client has included in its offer the "client_max_window_bits" extension parameter with a value smaller than one in the "agreed parameters", the client MUST use an LZ77 sliding window of a size that conforms the "agreed parameters" to compress messages to send. The client-to-server "client_max_window_bits" extension parameter is just a hint for the server to build an extension negotiation response.

如果“商定参数”包含值为w的“客户端最大窗口位”扩展参数,则服务器可减小其LZ77滑动窗口的大小,以将接收到的消息解压缩至2字节的w次方。否则,服务器必须使用32768字节的LZ77滑动窗口来解压缩接收到的消息。请注意,即使客户机在其报价中包含了值小于“商定参数”中的值的“客户机最大窗口位”扩展参数,客户机也必须使用大小符合“商定参数”的LZ77滑动窗口来压缩要发送的消息。客户端到服务器的“client\u max\u window\u bits”扩展参数只是提示服务器构建扩展协商响应。

7.2.3. Examples
7.2.3. 例子

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

本节介绍“permessagedeflate”扩展如何转换消息的示例。

7.2.3.1. A Message Compressed Using One Compressed DEFLATE Block
7.2.3.1. 使用一个压缩的DEFLATE块压缩的消息

Suppose that an endpoint sends a text message "Hello". If the endpoint uses one compressed DEFLATE block (compressed with fixed Huffman code and the "BFINAL" bit not set) to compress the message, the endpoint obtains the compressed data to use for the message payload as follows.

假设端点发送一条文本消息“Hello”。如果端点使用一个压缩的DEFLATE块(使用固定的哈夫曼代码压缩,且未设置“BFINAL”位)来压缩消息,则端点将获得压缩数据以用于消息有效负载,如下所示。

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

端点将“Hello”压缩到一个压缩的DEFLATE块中,并使用空的DEFLATE块将结果数据刷新到字节数组中,而不进行压缩:

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

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

By stripping 0x00 0x00 0xff 0xff from the tail end, the endpoint gets the data to use for the message payload:

通过从后端剥离0x00 0x00 0xff 0xff,端点获取用于消息有效负载的数据:

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

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

Suppose that the endpoint sends this compressed message without fragmentation. The endpoint builds one frame by putting all of the compressed data in the payload data portion of the frame:

假设端点发送此压缩消息时没有碎片。端点通过将所有压缩数据放入帧的有效负载数据部分来构建一个帧:

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

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

The first 2 octets (0xc1 0x07) are the WebSocket frame header (FIN=1, RSV1=1, RSV2=0, RSV3=0, opcode=text, MASK=0, Payload length=7). The following figure shows what value is set in each field of the WebSocket frame header.

前两个八位字节(0xc1 0x07)是WebSocket帧头(FIN=1,RSV1=1,RSV2=0,RSV3=0,操作码=text,掩码=0,有效负载长度=7)。下图显示了在WebSocket框架标题的每个字段中设置的值。

        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      |
       +-+-+-+-+-------+-+-------------+
        
        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 message with fragmentation. The endpoint splits the compressed data into fragments and builds frames for each fragment. For example, if the fragments are 3 and 4 octets, the first frame is:

假设端点发送带有碎片的压缩消息。端点将压缩数据拆分为片段,并为每个片段构建帧。例如,如果片段为3和4个八位字节,则第一帧为:

0x41 0x03 0xf2 0x48 0xcd

0x41 0x03 0xf2 0x48 0xcd

and the second frame is:

第二帧是:

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

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

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

请注意,RSV1位仅在第一帧上设置。

7.2.3.2. Sharing LZ77 Sliding Window
7.2.3.2. 共享LZ77滑动窗口

Suppose that a client has sent a message "Hello" as a compressed message and will send the same message "Hello" again as a compressed message.

假设客户机已将消息“Hello”作为压缩消息发送,并将再次将相同的消息“Hello”作为压缩消息发送。

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

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

The above is the payload of the first message that the client has sent. If the "agreed parameters" contain the "client_no_context_takeover" extension parameter, the client compresses the payload of the next message into the same bytes (if the client uses the same "BTYPE" value and "BFINAL" value). So, the payload of the second message will be:

上面是客户端发送的第一条消息的有效负载。如果“商定的参数”包含“client\u no\u context\u takeover”扩展参数,则客户端将下一条消息的有效负载压缩为相同的字节(如果客户端使用相同的“BTYPE”值和“BFINAL”值)。因此,第二条消息的有效载荷将是:

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

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

If the "agreed parameters" did not contain the "client_no_context_takeover" extension parameter, the client can compress the payload of the next message into fewer bytes by referencing the history in the LZ77 sliding window. So, the payload of the second message will be:

如果“商定的参数”不包含“客户端\u否\u上下文\u接管”扩展参数,则客户端可以通过引用LZ77滑动窗口中的历史记录,将下一条消息的有效负载压缩为更少的字节。因此,第二条消息的有效载荷将是:

0xf2 0x00 0x11 0x00 0x00

0xf2 0x00 0x11 0x00 0x00

So, 2 bytes are saved in total.

因此,总共保存了2个字节。

Note that even if some uncompressed messages (with the RSV1 bit unset) are inserted between the two "Hello" messages, they don't affect the LZ77 sliding window.

请注意,即使在两条“Hello”消息之间插入了一些未压缩的消息(RSV1位未设置),它们也不会影响LZ77滑动窗口。

7.2.3.3. Using a DEFLATE Block with No Compression
7.2.3.3. 使用无压缩的放气块

A DEFLATE block with no compression may be used.

可使用无压缩的放气块。

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

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

This is a frame constituting a text message "Hello" built using a DEFLATE block with no compression. The first 2 octets (0xc1 0x0b) are the WebSocket frame header (FIN=1, RSV1=1, RSV2=0, RSV3=0, opcode=text, MASK=0, Payload length=7). Note that the RSV1 bit is set for this message (only on the first fragment if the message is fragmented) because the RSV1 bit is set when DEFLATE is applied to the message, including the case when only DEFLATE blocks with no compression are used. The 3rd to 13th octets consist of the payload data containing "Hello" compressed using a DEFLATE block with no compression.

这是一个构成文本消息“Hello”的框架,它使用DEFLATE块构建,没有压缩。前两个八位字节(0xc1 0x0b)是WebSocket帧头(FIN=1,RSV1=1,RSV2=0,RSV3=0,操作码=text,掩码=0,有效负载长度=7)。请注意,RSV1位是为此消息设置的(如果消息是分段的,则仅在第一个片段上设置),因为RSV1位是在对消息应用DEFLATE时设置的,包括仅使用DEFLATE块而不使用压缩的情况。第3到第13个八位字节由包含“Hello”的有效载荷数据组成,该数据使用无压缩的DEFLATE块进行压缩。

7.2.3.4. Using a DEFLATE Block with "BFINAL" Set to 1
7.2.3.4. 使用“BFINAL”设置为1的放气块

On platforms on which the flush method using an empty DEFLATE block with no compression is not available, implementors can choose to flush data using DEFLATE blocks with "BFINAL" set to 1.

在无法使用无压缩的空DEFLATE块刷新方法的平台上,实现者可以选择使用“BFINAL”设置为1的DEFLATE块刷新数据。

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

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

This is the payload of a message containing "Hello" compressed using a DEFLATE block with "BFINAL" set to 1. The first 7 octets constitute a DEFLATE block with "BFINAL" set to 1 and "BTYPE" set to 01 containing "Hello". The last 1 octet (0x00) contains the header bits with "BFINAL" set to 0 and "BTYPE" set to 00, and 5 padding bits of 0. This octet is necessary to allow the payload to be decompressed in the same manner as messages flushed using DEFLATE blocks with "BFINAL" unset.

这是包含“Hello”的消息的有效负载,使用DEFLATE块将“BFINAL”设置为1进行压缩。前7个八位字节构成一个放气块,“BFINAL”设置为1,“BTYPE”设置为01,包含“Hello”。最后1个八位字节(0x00)包含“BFINAL”设置为0、“BTYPE”设置为00的标头位,以及5个0的填充位。此八位组是必要的,以允许有效负载以与使用“BFINAL”unset的DEFLATE块刷新消息相同的方式进行解压缩。

7.2.3.5. Two DEFLATE Blocks in One Message
7.2.3.5. 一条消息中有两个放气块

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

一条消息中可使用两个或多个放气块。

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

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) constitute one DEFLATE block with "BFINAL" set to 0 and "BTYPE" set to 01 containing "He". The rest of the 4th octet contains the header bits with "BFINAL" set to 0 and "BTYPE" set to 00, and the 3 padding bits of 0. Together with the following 4 octets (0x00 0x00 0xff 0xff), the header bits constitute an empty DEFLATE block with no compression. A DEFLATE block containing "llo" follows the empty DEFLATE block.

前3个八位字节(0xf2 0x48 0x05)和第4个八位字节(0x00)的最低有效两位构成一个放气块,“BFINAL”设置为0,“BTYPE”设置为01,其中包含“He”。第四个八位字节的其余部分包含“BFINAL”设置为0,“BTYPE”设置为00的头位,以及0的3个填充位。与以下4个八位字节(0x00 0x00 0xff 0xff)一起,报头位构成一个无压缩的空放气块。包含“llo”的放气块位于空放气块之后。

7.2.3.6. Generating an Empty Fragment
7.2.3.6. 生成空片段

Suppose that an endpoint is sending data of unknown size. The endpoint may encounter the end-of-data signal from the data source when its buffer for uncompressed data is empty. In such a case, the endpoint just needs to send the last fragment with the FIN bit set to 1 and the payload set to the DEFLATE block(s), which contains 0 bytes of data. If the compression library being used doesn't generate any data when its buffer is empty, an empty uncompressed DEFLATE block can be built and used for this purpose as follows:

假设端点正在发送大小未知的数据。当未压缩数据的缓冲区为空时,端点可能会遇到来自数据源的数据结束信号。在这种情况下,端点只需要将FIN位设置为1的最后一个片段和有效负载设置为包含0字节数据的DEFLATE块。如果正在使用的压缩库在其缓冲区为空时未生成任何数据,则可以构建空的未压缩DEFLATE块,并将其用于此目的,如下所示:

0x00

0x00

The single octet 0x00 contains the header bits with "BFINAL" set to 0 and "BTYPE" set to 00, and 5 padding bits of 0.

单个八位字节0x00包含“BFINAL”设置为0,“BTYPE”设置为00的头位,以及5个0的填充位。

7.3. Implementation Notes
7.3. 实施说明

On most common software development platforms, the DEFLATE compression library provides a method for aligning compressed data to byte boundaries using an empty DEFLATE block with no compression. For example, zlib [zlib] does this when "Z_SYNC_FLUSH" is passed to the deflate function.

在大多数常见的软件开发平台上,DEFLATE压缩库提供了一种方法,使用空DEFLATE块将压缩数据与字节边界对齐,而不进行压缩。例如,zlib[zlib]在将“Z_SYNC_FLUSH”传递给deflate函数时执行此操作。

Some platforms may only provide methods to output and process compressed data with a zlib header and an Adler-32 checksum. On such platforms, developers need to write stub code to remove and complement the zlib and Adler-32 checksum by themselves.

一些平台可能只提供使用zlib报头和Adler-32校验和输出和处理压缩数据的方法。在这样的平台上,开发人员需要编写存根代码来删除和补充zlib和Adler-32校验和。

To obtain a useful compression ratio, an LZ77 sliding window size of 1,024 or more is RECOMMENDED.

为了获得有用的压缩比,建议使用1024或更大的LZ77滑动窗口。

If a side disallows context takeover, its endpoint can easily figure out whether or not a certain message will be shorter if compressed. Otherwise, it's not easy to know whether future messages will benefit from having a certain message compressed. Implementors may employ some heuristics to determine this.

如果一方不允许上下文接管,它的端点可以很容易地判断某条消息在压缩后是否会变短。否则,很难知道将来的消息是否会从压缩某个消息中获益。实现者可以使用一些启发式方法来确定这一点。

8. Security Considerations
8. 安全考虑

There is a known exploit when history-based compression is combined with a secure transport [CRIME]. Implementors should pay attention to this point when integrating this extension with other extensions or protocols.

当基于历史的压缩与安全传输[犯罪]相结合时,存在已知的漏洞。在将此扩展与其他扩展或协议集成时,实现者应该注意这一点。

9. IANA Considerations
9. IANA考虑
9.1. Registration of the "permessage-deflate" WebSocket Extension Name
9.1. 注册“permessage deflate”WebSocket扩展名

IANA has registered the following WebSocket extension name in the "WebSocket Extension Name Registry" defined in [RFC6455].

IANA已在[RFC6455]中定义的“WebSocket扩展名注册表”中注册了以下WebSocket扩展名。

Extension Identifier permessage-deflate

扩展标识符permessage deflate

Extension Common Name WebSocket Per-Message Deflate

扩展公共名称WebSocket每封邮件Deflate

Extension Definition This document.

此文档的扩展定义。

Known Incompatible Extensions None

已知的不兼容扩展无

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

WebSocket开始握手中的“Sec WebSocket Extensions”标题中使用了“permessage deflate”扩展名,以协商使用“permessage deflate”扩展名。

9.2. Registration of the "Per-Message Compressed" WebSocket Framing Header Bit

9.2. “每消息压缩”WebSocket帧头位的注册

IANA has registered the following WebSocket framing header bit in the "WebSocket Framing Header Bits Registry" defined in [RFC6455].

IANA已在[RFC6455]中定义的“WebSocket帧头位注册表”中注册了以下WebSocket帧头位。

Value RSV1

值RSV1

Description The "Per-Message Compressed" bit, which indicates whether or not the message is compressed. RSV1 is set for compressed messages and unset for uncompressed messages.

说明“每条消息压缩”位,指示消息是否压缩。RSV1为压缩消息设置,未压缩消息未设置。

Reference Section 6 of this document.

参考本文件第6节。

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

在数据消息的第一个片段上使用“每消息压缩”帧报头位,以指示消息的有效负载是否由PMCE压缩。

10. References
10. 工具书类
10.1. Normative References
10.1. 规范性引用文件

[CRIME] Rizzo, J. and T. Duong, "The CRIME attack", EKOparty Security Conference, September 2012.

[犯罪]Rizzo,J.和T.Duong,“犯罪袭击”,EKOparty安全会议,2012年9月。

[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, DOI 10.1109/TIT.1977.1055714, May 1977, <https://www.cs.duke.edu/courses/spring03/cps296.5/papers/ ziv_lempel_1977_universal_algorithm.pdf>.

[LZ77]Ziv,J.和A.Lempel,“顺序数据压缩的通用算法”,IEEE信息论学报,第23卷,第3期,337-343页,DOI 10.1109/TIT.1977.1055714,1977年5月<https://www.cs.duke.edu/courses/spring03/cps296.5/papers/ ziv_lempel_1977_universal_algorithm.pdf>。

[RFC1951] Deutsch, P., "DEFLATE Compressed Data Format Specification version 1.3", RFC 1951, DOI 10.17487/RFC1951, May 1996, <http://www.rfc-editor.org/info/rfc1951>.

[RFC1951]Deutsch,P.,“DEFLATE压缩数据格式规范1.3版”,RFC 1951,DOI 10.17487/RFC1951,1996年5月<http://www.rfc-editor.org/info/rfc1951>.

[RFC2119] Bradner, S., "Key words for use in RFCs to Indicate Requirement Levels", BCP 14, RFC 2119, DOI 10.17487/RFC2119, March 1997, <http://www.rfc-editor.org/info/rfc2119>.

[RFC2119]Bradner,S.,“RFC中用于表示需求水平的关键词”,BCP 14,RFC 2119,DOI 10.17487/RFC2119,1997年3月<http://www.rfc-editor.org/info/rfc2119>.

[RFC5234] Crocker, D., Ed. and P. Overell, "Augmented BNF for Syntax Specifications: ABNF", STD 68, RFC 5234, DOI 10.17487/RFC5234, January 2008, <http://www.rfc-editor.org/info/rfc5234>.

[RFC5234]Crocker,D.,Ed.和P.Overell,“语法规范的扩充BNF:ABNF”,STD 68,RFC 5234,DOI 10.17487/RFC5234,2008年1月<http://www.rfc-editor.org/info/rfc5234>.

[RFC6455] Fette, I. and A. Melnikov, "The WebSocket Protocol", RFC 6455, DOI 10.17487/RFC6455, December 2011, <http://www.rfc-editor.org/info/rfc6455>.

[RFC6455]Fette,I.和A.Melnikov,“WebSocket协议”,RFC 6455,DOI 10.17487/RFC6455,2011年12月<http://www.rfc-editor.org/info/rfc6455>.

10.2. Informative References
10.2. 资料性引用

[RFC1979] Woods, J., "PPP Deflate Protocol", RFC 1979, DOI 10.17487/RFC1979, August 1996, <http://www.rfc-editor.org/info/rfc1979>.

[RFC1979]Woods,J.,“PPP平减协议”,RFC 1979,DOI 10.17487/RFC1979,1996年8月<http://www.rfc-editor.org/info/rfc1979>.

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

[zlib]盖利,J.和M.阿德勒,“zlib”<http://www.zlib.net/>.

Acknowledgements

致谢

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

特别感谢Patrick McManus,他为WebSocket协议编写了基于DEFLATE的压缩扩展的初始规范,我在编写本规范时提到了该规范。

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

感谢以下参与HyBi工作组讨论并提出想法和/或提供详细评论的人士(名单可能不完整):亚当·赖斯、亚历山大·菲利浦、阿列克谢·梅尔尼科夫、阿曼·朱苏波夫、比约恩·霍尔曼、布赖恩·麦凯维、达里奥·克里维利、格雷格·威尔金斯、伊纳基·巴兹·卡斯蒂略、杰米·洛基尔、约阿金·埃尔德费尔特、,约翰·A·坦普林、朱利安·雷什克、石桥健一、马克·诺丁汉、彼得·索森、罗伯托·佩恩、萨尔瓦托·洛雷托、西蒙娜·博尔德、托比亚斯·奥伯斯坦和平野优塔卡。请注意,上面列出的人并不一定赞同这项工作的最终结果。

Author's Address

作者地址

Takeshi Yoshino Google, Inc.

吉野武史谷歌公司。

   Email: tyoshino@google.com
        
   Email: tyoshino@google.com