class TE < Split
- Inherits from
Split: #to_s, #sort_by_quality_factor
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.
Nested Classes and Modules
class TransferCodingA single transfer coding entry with optional quality factor
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
def self.parse(value)
Parses a raw header value.
Signature
-
parameter
valueString a raw header value containing comma-separated encodings.
-
returns
TE a new instance with normalized (lowercase) encodings.
Implementation
def self.parse(value)
self.new(value.downcase.split(COMMA))
end
def self.coerce(value)
Coerces a value into a parsed header object.
Signature
-
parameter
valueString | Array the value to coerce.
-
returns
TE a parsed header object with normalized values.
Implementation
def self.coerce(value)
case value
when Array
self.new(value.map(&:downcase))
else
self.parse(value.to_s)
end
end
def <<(value)
Adds one or more comma-separated values to the TE header. The values are converted to lowercase for normalization.
Signature
-
parameter
valueString a raw header value containing one or more values 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 preferred_transfer_codings
Parse the te header and order transfer codings by preference.
Transfer codings with equal quality factors retain their original relative order.
Signature
-
returns
Array(TransferCoding) the preferred transfer codings.
Implementation
def preferred_transfer_codings
sort_by_quality_factor(transfer_codings)
end
def chunked?
Signature
-
returns
Boolean whether the
chunkedencoding is accepted.
Implementation
def chunked?
self.any?{|value| value.start_with?(CHUNKED)}
end
def gzip?
Signature
-
returns
Boolean whether the
gzipencoding is accepted.
Implementation
def gzip?
self.any?{|value| value.start_with?(GZIP)}
end
def deflate?
Signature
-
returns
Boolean whether the
deflateencoding is accepted.
Implementation
def deflate?
self.any?{|value| value.start_with?(DEFLATE)}
end
def compress?
Signature
-
returns
Boolean whether the
compressencoding is accepted.
Implementation
def compress?
self.any?{|value| value.start_with?(COMPRESS)}
end
def identity?
Signature
-
returns
Boolean whether the
identityencoding 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