File: transfer_encoding.rb

package info (click to toggle)
ruby-protocol-http 0.59.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 864 kB
  • sloc: ruby: 7,612; makefile: 4
file content (93 lines) | stat: -rw-r--r-- 3,013 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2025, by Samuel Williams.

require_relative "split"

module Protocol
	module HTTP
		module Header
			# The `transfer-encoding` header indicates the encoding transformations that have been applied to the message body.
			#
			# The `transfer-encoding` header is used to specify the form of encoding used to safely transfer the message body between the sender and receiver.
			class TransferEncoding < Split
				# The `chunked` transfer encoding allows a server to send data of unknown length by breaking it into chunks.
				CHUNKED = "chunked"
				
				# The `gzip` transfer encoding compresses the message body using the gzip algorithm.
				GZIP = "gzip"
				
				# The `deflate` transfer encoding compresses the message body using the deflate algorithm.
				DEFLATE = "deflate"
				
				# The `compress` transfer encoding compresses the message body using the compress algorithm.
				COMPRESS = "compress"
				
				# The `identity` transfer encoding indicates no transformation has been applied.
				IDENTITY = "identity"
				
				# Parses a raw header value.
				#
				# @parameter value [String] a raw header value containing comma-separated encodings.
				# @returns [TransferEncoding] a new instance with normalized (lowercase) encodings.
				def self.parse(value)
					self.new(value.downcase.split(COMMA))
				end
				
				# Coerces a value into a parsed header object.
				#
				# @parameter value [String | Array] the value to coerce.
				# @returns [TransferEncoding] a parsed header object with normalized values.
				def self.coerce(value)
					case value
					when Array
						self.new(value.map(&:downcase))
					else
						self.parse(value.to_s)
					end
				end
				
				# Adds one or more comma-separated values to the transfer encoding header. The values are converted to lowercase for normalization.
				#
				# @parameter value [String] a raw header value containing one or more values separated by commas.
				def << value
					super(value.downcase)
				end
				
				# @returns [Boolean] whether the `chunked` encoding is present.
				def chunked?
					self.include?(CHUNKED)
				end
				
				# @returns [Boolean] whether the `gzip` encoding is present.
				def gzip?
					self.include?(GZIP)
				end
				
				# @returns [Boolean] whether the `deflate` encoding is present.
				def deflate?
					self.include?(DEFLATE)
				end
				
				# @returns [Boolean] whether the `compress` encoding is present.
				def compress?
					self.include?(COMPRESS)
				end
				
				# @returns [Boolean] whether the `identity` encoding is present.
				def identity?
					self.include?(IDENTITY)
				end
				
				# Whether this header is acceptable in HTTP trailers.
				# Transfer-Encoding headers control message framing and must not appear in trailers.
				# @returns [Boolean] `false`, as Transfer-Encoding headers are hop-by-hop and must precede the message body.
				def self.trailer?
					false
				end
			end
		end
	end
end