class TE
The te
header indicates the transfer encodings the client is willing to accept. AKA accept-transfer-encoding
. How we ended up with te
instead of accept-transfer-encoding
is a mystery lost to time.
The te
header allows a client to indicate which transfer encodings it can handle, and in what order of preference using quality factors.
Definitions
TOKEN = /[!#$%&'*+\-.0-9A-Z^_`a-z|~]+/
Transfer encoding token pattern
QVALUE = /0(\.[0-9]{0,3})?|1(\.[0]{0,3})?/
Quality value pattern (0.0 to 1.0)
TRANSFER_CODING = /\A(?<name>#{TOKEN})(\s*;\s*q=(?<q>#{QVALUE}))?\z/
Pattern for parsing transfer encoding with optional quality factor
CHUNKED = "chunked"
The chunked
transfer encoding
GZIP = "gzip"
The gzip
transfer encoding
DEFLATE = "deflate"
The deflate
transfer encoding
COMPRESS = "compress"
The compress
transfer encoding
IDENTITY = "identity"
The identity
transfer encoding
TRAILERS = "trailers"
The trailers
pseudo-encoding indicates willingness to accept trailer fields
TransferCoding
A single transfer coding entry with optional quality factor
Implementation
TransferCoding = Struct.new(:name, :q) do
def quality_factor
(q || 1.0).to_f
end
def <=> other
other.quality_factor <=> self.quality_factor
end
def to_s
if q && q != 1.0
"#{name};q=#{q}"
else
name.to_s
end
end
end
def initialize(value = nil)
Initializes the TE header with the given value. The value is split into distinct entries and converted to lowercase for normalization.
Signature
-
parameter
value
String | Nil
the raw header value containing transfer encodings separated by commas.
Implementation
def initialize(value = nil)
super(value&.downcase)
end
def <<(value)
Adds one or more comma-separated values to the TE header. The values are converted to lowercase for normalization.
Signature
-
parameter
value
String
the value or values to add, separated by commas.
Implementation
def << value
super(value.downcase)
end
def transfer_codings
Parse the te
header value into a list of transfer codings with quality factors.
Signature
-
returns
Array(TransferCoding)
the list of transfer codings and their associated quality factors.
Implementation
def transfer_codings
self.map do |value|
if match = value.match(TRANSFER_CODING)
TransferCoding.new(match[:name], match[:q])
else
raise ParseError.new("Could not parse transfer coding: #{value.inspect}")
end
end
end
def chunked?
Signature
-
returns
Boolean
whether the
chunked
encoding is accepted.
Implementation
def chunked?
self.any? {|value| value.start_with?(CHUNKED)}
end
def gzip?
Signature
-
returns
Boolean
whether the
gzip
encoding is accepted.
Implementation
def gzip?
self.any? {|value| value.start_with?(GZIP)}
end
def deflate?
Signature
-
returns
Boolean
whether the
deflate
encoding is accepted.
Implementation
def deflate?
self.any? {|value| value.start_with?(DEFLATE)}
end
def compress?
Signature
-
returns
Boolean
whether the
compress
encoding is accepted.
Implementation
def compress?
self.any? {|value| value.start_with?(COMPRESS)}
end
def identity?
Signature
-
returns
Boolean
whether the
identity
encoding is accepted.
Implementation
def identity?
self.any? {|value| value.start_with?(IDENTITY)}
end
def trailers?
Signature
-
returns
Boolean
whether trailers are accepted.
Implementation
def trailers?
self.any? {|value| value.start_with?(TRAILERS)}
end
def self.trailer?
Whether this header is acceptable in HTTP trailers. TE headers negotiate transfer encodings and must not appear in trailers.
Signature
-
returns
Boolean
false
, as TE headers are hop-by-hop and control message framing.
Implementation
def self.trailer?
false
end