File: transfer_encoding.rb

package info (click to toggle)
ruby-protocol-http 0.55.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 840 kB
  • sloc: ruby: 6,904; makefile: 4
file content (78 lines) | stat: -rw-r--r-- 2,681 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
# 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"
				
				# Initializes the transfer encoding header with the given value. The value is split into distinct entries and converted to lowercase for normalization.
				#
				# @parameter value [String | Nil] the raw header value containing transfer encodings separated by commas.
				def initialize(value = nil)
					super(value&.downcase)
				end
				
				# Adds one or more comma-separated values to the transfer encoding header. The values are converted to lowercase for normalization.
				#
				# @parameter value [String] the value or values to add, 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