Class: Protocol::HTTP::Header::TE

Inherits:
Split
  • Object
show all
Defined in:
lib/protocol/http/header/te.rb

Overview

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.

Defined Under Namespace

Classes: TransferCoding

Constant Summary collapse

ParseError =
Class.new(Error)
TOKEN =

Transfer encoding token pattern

/[!#$%&'*+\-.0-9A-Z^_`a-z|~]+/
QVALUE =

Quality value pattern (0.0 to 1.0)

/0(\.[0-9]{0,3})?|1(\.[0]{0,3})?/
TRANSFER_CODING =

Pattern for parsing transfer encoding with optional quality factor

/\A(?<name>#{TOKEN})(\s*;\s*q=(?<q>#{QVALUE}))?\z/
CHUNKED =

The chunked transfer encoding

"chunked"
GZIP =

The gzip transfer encoding

"gzip"
DEFLATE =

The deflate transfer encoding

"deflate"
COMPRESS =

The compress transfer encoding

"compress"
IDENTITY =

The identity transfer encoding

"identity"
TRAILERS =

The trailers pseudo-encoding indicates willingness to accept trailer fields

"trailers"

Constants inherited from Split

Split::COMMA

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Split

#initialize, #to_s

Constructor Details

This class inherits a constructor from Protocol::HTTP::Header::Split

Class Method Details

.coerce(value) ⇒ Object

Coerces a value into a parsed header object.



87
88
89
90
91
92
93
94
# File 'lib/protocol/http/header/te.rb', line 87

def self.coerce(value)
	case value
	when Array
		self.new(value.map(&:downcase))
	else
		self.parse(value.to_s)
	end
end

.parse(value) ⇒ Object

Parses a raw header value.



79
80
81
# File 'lib/protocol/http/header/te.rb', line 79

def self.parse(value)
	self.new(value.downcase.split(COMMA))
end

.trailer?Boolean

Whether this header is acceptable in HTTP trailers. TE headers negotiate transfer encodings and must not appear in trailers.

Returns:

  • (Boolean)


158
159
160
# File 'lib/protocol/http/header/te.rb', line 158

def self.trailer?
	false
end

Instance Method Details

#<<(value) ⇒ Object

Adds one or more comma-separated values to the TE header. The values are converted to lowercase for normalization.



99
100
101
# File 'lib/protocol/http/header/te.rb', line 99

def << value
	super(value.downcase)
end

#chunked?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/protocol/http/header/te.rb', line 126

def chunked?
	self.any?{|value| value.start_with?(CHUNKED)}
end

#compress?Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/protocol/http/header/te.rb', line 141

def compress?
	self.any?{|value| value.start_with?(COMPRESS)}
end

#deflate?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/protocol/http/header/te.rb', line 136

def deflate?
	self.any?{|value| value.start_with?(DEFLATE)}
end

#gzip?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/protocol/http/header/te.rb', line 131

def gzip?
	self.any?{|value| value.start_with?(GZIP)}
end

#identity?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/protocol/http/header/te.rb', line 146

def identity?
	self.any?{|value| value.start_with?(IDENTITY)}
end

#preferred_transfer_codingsObject

Parse the te header and order transfer codings by preference.

Transfer codings with equal quality factors retain their original relative order.



121
122
123
# File 'lib/protocol/http/header/te.rb', line 121

def preferred_transfer_codings
	sort_by_quality_factor(transfer_codings)
end

#trailers?Boolean

Returns:

  • (Boolean)


151
152
153
# File 'lib/protocol/http/header/te.rb', line 151

def trailers?
	self.any?{|value| value.start_with?(TRAILERS)}
end

#transfer_codingsObject

Parse the te header value into a list of transfer codings with quality factors.



106
107
108
109
110
111
112
113
114
# File 'lib/protocol/http/header/te.rb', line 106

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