One mechanism to extend HTTP is to introduce new HTTP upgrade tokens; see
Section 16.7 of [
HTTP]. In HTTP/1.x, these tokens are used via the Upgrade mechanism; see
Section 7.8 of [
HTTP]. In HTTP/2 and HTTP/3, these tokens are used via the Extended CONNECT mechanism; see [
EXT-CONNECT2] and [
EXT-CONNECT3].
This specification introduces the Capsule Protocol. The Capsule Protocol is a sequence of type-length-value tuples that definitions of new HTTP upgrade tokens can choose to use. It allows endpoints to reliably communicate request-related information end-to-end on HTTP request streams, even in the presence of HTTP intermediaries. The Capsule Protocol can be used to exchange HTTP Datagrams, which is necessary when HTTP is running over a transport that does not support the QUIC DATAGRAM frame. The Capsule Protocol can also be used to communicate reliable and bidirectional control messages associated with a datagram-based protocol even when HTTP/3 Datagrams are in use.
This specification defines the "data stream" of an HTTP request as the bidirectional stream of bytes that follows the header section of the request message and the final response message that is either successful (i.e., 2xx) or upgraded (i.e., 101).
In HTTP/1.x, the data stream consists of all bytes on the connection that follow the blank line that concludes either the request header section or the final response header section. As a result, only the last HTTP request on an HTTP/1.x connection can start the Capsule Protocol.
In HTTP/2 and HTTP/3, the data stream of a given HTTP request consists of all bytes sent in DATA frames with the corresponding stream ID.
The concept of a data stream is particularly relevant for methods such as CONNECT, where there is no HTTP message content after the headers.
Data streams can be prioritized using any means suited to stream or request prioritization. For example, see
Section 11 of [
PRIORITY].
Data streams are subject to the flow control mechanisms of the underlying layers; examples include HTTP/2 stream flow control, HTTP/2 connection flow control, and TCP flow control.
Definitions of new HTTP upgrade tokens can state that their associated request's data stream uses the Capsule Protocol. If they do so, the contents of the associated request's data stream uses the following format:
Capsule Protocol {
Capsule (..) ...,
}
Capsule {
Capsule Type (i),
Capsule Length (i),
Capsule Value (..),
}
-
Capsule Type:
-
A variable-length integer indicating the type of the capsule. An IANA registry is used to manage the assignment of Capsule Types; see Section 5.4.
-
Capsule Length:
-
The length, in bytes, of the Capsule Value field, which follows this field, encoded as a variable-length integer. Note that this field can have a value of zero.
-
Capsule Value:
-
The payload of this Capsule. Its semantics are determined by the value of the Capsule Type field.
An intermediary can identify the use of the Capsule Protocol either through the presence of the Capsule-Protocol header field (
Section 3.4) or by understanding the chosen HTTP Upgrade token.
Because new protocols or extensions might define new Capsule Types, intermediaries that wish to allow for future extensibility
SHOULD forward Capsules without modification unless the definition of the Capsule Type in use specifies additional intermediary processing. One such Capsule Type is the DATAGRAM Capsule; see
Section 3.5. In particular, intermediaries
SHOULD forward Capsules with an unknown Capsule Type without modification.
Endpoints that receive a Capsule with an unknown Capsule Type
MUST silently drop that Capsule and skip over it to parse the next Capsule.
By virtue of the definition of the data stream:
-
The Capsule Protocol is not in use unless the response includes a 2xx (Successful) or 101 (Switching Protocols) status code.
-
When the Capsule Protocol is in use, the associated HTTP request and response do not carry HTTP content. A future extension MAY define a new Capsule Type to carry HTTP content.
The Capsule Protocol only applies to definitions of new HTTP upgrade tokens; thus, in HTTP/2 and HTTP/3, it can only be used with the CONNECT method. Therefore, once both endpoints agree to use the Capsule Protocol, the frame usage requirements of the stream change as specified in
Section 8.5 of [
HTTP/2] and
Section 4.4 of [
HTTP/3].
The Capsule Protocol
MUST NOT be used with messages that contain Content-Length, Content-Type, or Transfer-Encoding header fields. Additionally, HTTP status codes 204 (No Content), 205 (Reset Content), and 206 (Partial Content)
MUST NOT be sent on responses that use the Capsule Protocol. A receiver that observes a violation of these requirements
MUST treat the HTTP message as malformed.
When processing Capsules, a receiver might be tempted to accumulate the full length of the Capsule Value field in the data stream before handling it. This approach
SHOULD be avoided because it can consume flow control in underlying layers, and that might lead to deadlocks if the Capsule data exhausts the flow control window.
When a receiver encounters an error processing the Capsule Protocol, the receiver
MUST treat it as if it had received a malformed or incomplete HTTP message. For HTTP/3, the handling of malformed messages is described in
Section 4.1.2 of [
HTTP/3]. For HTTP/2, the handling of malformed messages is described in
Section 8.1.1 of [
HTTP/2]. For HTTP/1.x, the handling of incomplete messages is described in
Section 8 of [
HTTP/1.1].
Each Capsule's payload
MUST contain exactly the fields identified in its description. A Capsule payload that contains additional bytes after the identified fields or a Capsule payload that terminates before the end of the identified fields
MUST be treated as it if were a malformed or incomplete message. In particular, redundant length encodings
MUST be verified to be self-consistent.
If the receive side of a stream carrying Capsules is terminated cleanly (for example, in HTTP/3 this is defined as receiving a QUIC STREAM frame with the FIN bit set) and the last Capsule on the stream was truncated, this
MUST be treated as if it were a malformed or incomplete message.
The "Capsule-Protocol" header field is an Item Structured Field; see
Section 3.3 of [
STRUCTURED-FIELDS]. Its value
MUST be a Boolean; any other value type
MUST be handled as if the field were not present by recipients (for example, if this field is included multiple times, its type will become a List and the field will be ignored). This document does not define any parameters for the Capsule-Protocol header field value, but future documents might define parameters. Receivers
MUST ignore unknown parameters.
Endpoints indicate that the Capsule Protocol is in use on a data stream by sending a Capsule-Protocol header field with a true value. A Capsule-Protocol header field with a false value has the same semantics as when the header is not present.
Intermediaries
MAY use this header field to allow processing of HTTP Datagrams for unknown HTTP upgrade tokens. Note that this is only possible for HTTP Upgrade or Extended CONNECT.
The Capsule-Protocol header field
MUST NOT be used on HTTP responses with a status code that is both different from 101 (Switching Protocols) and outside the 2xx (Successful) range.
When using the Capsule Protocol, HTTP endpoints
SHOULD send the Capsule-Protocol header field to simplify intermediary processing. Definitions of new HTTP upgrade tokens that use the Capsule Protocol
MAY alter this recommendation.
This document defines the DATAGRAM (0x00) Capsule Type. This Capsule allows HTTP Datagrams to be sent on a stream using the Capsule Protocol. This is particularly useful when HTTP is running over a transport that does not support the QUIC DATAGRAM frame.
Datagram Capsule {
Type (i) = 0x00,
Length (i),
HTTP Datagram Payload (..),
}
-
HTTP Datagram Payload:
-
The payload of the datagram, whose semantics are defined by the extension that is using HTTP Datagrams. Note that this field can be empty.
HTTP Datagrams sent using the DATAGRAM Capsule have the same semantics as those sent in QUIC DATAGRAM frames. In particular, the restrictions on when it is allowed to send an HTTP Datagram and how to process them (from
Section 2.1) also apply to HTTP Datagrams sent and received using the DATAGRAM Capsule.
An intermediary can re-encode HTTP Datagrams as it forwards them. In other words, an intermediary
MAY send a DATAGRAM Capsule to forward an HTTP Datagram that was received in a QUIC DATAGRAM frame and vice versa. Intermediaries
MUST NOT perform this re-encoding unless they have identified the use of the Capsule Protocol on the corresponding request stream; see
Section 3.2.
Note that while DATAGRAM Capsules, which are sent on a stream, are reliably delivered in order, intermediaries can re-encode DATAGRAM Capsules into QUIC DATAGRAM frames when forwarding messages, which could result in loss or reordering.
If an intermediary receives an HTTP Datagram in a QUIC DATAGRAM frame and is forwarding it on a connection that supports QUIC DATAGRAM frames, the intermediary
SHOULD NOT convert that HTTP Datagram to a DATAGRAM Capsule. If the HTTP Datagram is too large to fit in a DATAGRAM frame (for example, because the Path MTU (PMTU) of that QUIC connection is too low or if the maximum UDP payload size advertised on that connection is too low), the intermediary
SHOULD drop the HTTP Datagram instead of converting it to a DATAGRAM Capsule. This preserves the end-to-end unreliability characteristic that methods such as Datagram Packetization Layer PMTU Discovery (DPLPMTUD) depend on [
DPLPMTUD]. An intermediary that converts QUIC DATAGRAM frames to DATAGRAM Capsules allows HTTP Datagrams to be arbitrarily large without suffering any loss. This can misrepresent the true path properties, defeating methods such as DPLPMTUD.
While DATAGRAM Capsules can theoretically carry a payload of length 2
62-1, most HTTP extensions that use HTTP Datagrams will have their own limits on what datagram payload sizes are practical. Implementations
SHOULD take those limits into account when parsing DATAGRAM Capsules. If an incoming DATAGRAM Capsule has a length that is known to be so large as to not be usable, the implementation
SHOULD discard the Capsule without buffering its contents into memory.
Since QUIC DATAGRAM frames are required to fit within a QUIC packet, implementations that re-encode DATAGRAM Capsules into QUIC DATAGRAM frames might be tempted to accumulate the entire Capsule in the stream before re-encoding it. This
SHOULD be avoided, because it can cause flow control problems; see
Section 3.2.
Note that it is possible for an HTTP extension to use HTTP Datagrams without using the Capsule Protocol. For example, if an HTTP extension that uses HTTP Datagrams is only defined over transports that support QUIC DATAGRAM frames, it might not need a stream encoding. Additionally, HTTP extensions can use HTTP Datagrams with their own data stream protocol. However, new HTTP extensions that wish to use HTTP Datagrams
SHOULD use the Capsule Protocol, as failing to do so will make it harder for the HTTP extension to support versions of HTTP other than HTTP/3 and will prevent interoperability with intermediaries that only support the Capsule Protocol.