fast-proxy-status 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,199 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Fast
4
+ class ProxyStatus
5
+ # The extra parameters entries that §2.3 spells "None" — one shared
6
+ # frozen Hash, the shape every registry entry carries.
7
+ NONE = {}.freeze
8
+ private_constant :NONE
9
+
10
+ # RFC 9209 §2.3 — the 32 proxy error types this document defines,
11
+ # pinned as frozen data in document order. A gem that pins its RFC's
12
+ # registry at publication passes unknown values through, so a future
13
+ # registration never breaks an old gem version. Each entry is a
14
+ # frozen Hash of the three fields the registry's template names:
15
+ # "recommended_status" (an Integer — two entries carry the RFC's own
16
+ # prose instead: http_request_error's "The applicable 4xx status
17
+ # code" and proxy_internal_response's "The most appropriate status
18
+ # code for the response"), "response_only" (§2.1.1: "Proxy error
19
+ # types that are registered with a 'Response only generated by
20
+ # intermediaries' value of 'true' indicate that they can only occur
21
+ # in responses generated by the intermediary. If the value is
22
+ # 'false', the response might be generated by the intermediary or an
23
+ # inbound server.") and "extra_parameters" (the name => Structured
24
+ # Type table the entry defines — §2.1.1: "Their use, like all
25
+ # parameters, is optional. As a result, if an extra parameter is used
26
+ # with a proxy error type for which it is not defined, it will be
27
+ # ignored.").
28
+ #
29
+ # The registry is ADVISORY data and never gates a parse or an emit:
30
+ # an error type it does not carry parses, emits and round-trips
31
+ # untouched, and recommended_status is the status code a sender
32
+ # SHOULD set ("When generating an HTTP response containing the error,
33
+ # its HTTP status code SHOULD be set to the recommended HTTP status
34
+ # code"), never a bound the library enforces.
35
+ ERRORS = {
36
+ "dns_timeout" => { "recommended_status" => 504, "response_only" => true, "extra_parameters" => NONE }.freeze,
37
+ "dns_error" => {
38
+ "recommended_status" => 502, "response_only" => true,
39
+ "extra_parameters" => { "rcode" => "String", "info-code" => "Integer" }.freeze
40
+ }.freeze,
41
+ "destination_not_found" => {
42
+ "recommended_status" => 500, "response_only" => true,
43
+ "extra_parameters" => NONE
44
+ }.freeze,
45
+ "destination_unavailable" => {
46
+ "recommended_status" => 503, "response_only" => true,
47
+ "extra_parameters" => NONE
48
+ }.freeze,
49
+ "destination_ip_prohibited" => {
50
+ "recommended_status" => 502, "response_only" => true,
51
+ "extra_parameters" => NONE
52
+ }.freeze,
53
+ "destination_ip_unroutable" => {
54
+ "recommended_status" => 502, "response_only" => true,
55
+ "extra_parameters" => NONE
56
+ }.freeze,
57
+ "connection_refused" => {
58
+ "recommended_status" => 502, "response_only" => true,
59
+ "extra_parameters" => NONE
60
+ }.freeze,
61
+ "connection_terminated" => {
62
+ "recommended_status" => 502, "response_only" => false,
63
+ "extra_parameters" => NONE
64
+ }.freeze,
65
+ "connection_timeout" => {
66
+ "recommended_status" => 504, "response_only" => true,
67
+ "extra_parameters" => NONE
68
+ }.freeze,
69
+ "connection_read_timeout" => {
70
+ "recommended_status" => 504, "response_only" => false,
71
+ "extra_parameters" => NONE
72
+ }.freeze,
73
+ "connection_write_timeout" => {
74
+ "recommended_status" => 504, "response_only" => false,
75
+ "extra_parameters" => NONE
76
+ }.freeze,
77
+ "connection_limit_reached" => {
78
+ "recommended_status" => 503, "response_only" => true,
79
+ "extra_parameters" => NONE
80
+ }.freeze,
81
+ "tls_protocol_error" => {
82
+ "recommended_status" => 502, "response_only" => false,
83
+ "extra_parameters" => NONE
84
+ }.freeze,
85
+ "tls_certificate_error" => {
86
+ "recommended_status" => 502, "response_only" => true,
87
+ "extra_parameters" => NONE
88
+ }.freeze,
89
+ "tls_alert_received" => {
90
+ "recommended_status" => 502, "response_only" => false,
91
+ "extra_parameters" => { "alert-id" => "Integer", "alert-message" => "Token or String" }.freeze
92
+ }.freeze,
93
+ "http_request_error" => {
94
+ "recommended_status" => "The applicable 4xx status code", "response_only" => true,
95
+ "extra_parameters" => { "status-code" => "Integer", "status-phrase" => "String" }.freeze
96
+ }.freeze,
97
+ "http_request_denied" => {
98
+ "recommended_status" => 403, "response_only" => true,
99
+ "extra_parameters" => NONE
100
+ }.freeze,
101
+ "http_response_incomplete" => {
102
+ "recommended_status" => 502, "response_only" => false,
103
+ "extra_parameters" => NONE
104
+ }.freeze,
105
+ "http_response_header_section_size" => {
106
+ "recommended_status" => 502, "response_only" => false,
107
+ "extra_parameters" => { "header-section-size" => "Integer" }.freeze
108
+ }.freeze,
109
+ "http_response_header_size" => {
110
+ "recommended_status" => 502, "response_only" => false,
111
+ "extra_parameters" => { "header-name" => "String", "header-size" => "Integer" }.freeze
112
+ }.freeze,
113
+ "http_response_body_size" => {
114
+ "recommended_status" => 502, "response_only" => false,
115
+ "extra_parameters" => { "body-size" => "Integer" }.freeze
116
+ }.freeze,
117
+ "http_response_trailer_section_size" => {
118
+ "recommended_status" => 502, "response_only" => false,
119
+ "extra_parameters" => { "trailer-section-size" => "Integer" }.freeze
120
+ }.freeze,
121
+ "http_response_trailer_size" => {
122
+ "recommended_status" => 502, "response_only" => false,
123
+ "extra_parameters" => { "trailer-name" => "String", "trailer-size" => "Integer" }.freeze
124
+ }.freeze,
125
+ "http_response_transfer_coding" => {
126
+ "recommended_status" => 502, "response_only" => false,
127
+ "extra_parameters" => { "coding" => "Token" }.freeze
128
+ }.freeze,
129
+ "http_response_content_coding" => {
130
+ "recommended_status" => 502, "response_only" => false,
131
+ "extra_parameters" => { "coding" => "Token" }.freeze
132
+ }.freeze,
133
+ "http_response_timeout" => {
134
+ "recommended_status" => 504, "response_only" => false,
135
+ "extra_parameters" => NONE
136
+ }.freeze,
137
+ "http_upgrade_failed" => {
138
+ "recommended_status" => 502, "response_only" => true,
139
+ "extra_parameters" => NONE
140
+ }.freeze,
141
+ "http_protocol_error" => {
142
+ "recommended_status" => 502, "response_only" => false,
143
+ "extra_parameters" => NONE
144
+ }.freeze,
145
+ "proxy_internal_response" => {
146
+ "recommended_status" => "The most appropriate status code for the response", "response_only" => true,
147
+ "extra_parameters" => NONE
148
+ }.freeze,
149
+ "proxy_internal_error" => {
150
+ "recommended_status" => 500, "response_only" => true,
151
+ "extra_parameters" => NONE
152
+ }.freeze,
153
+ "proxy_configuration_error" => {
154
+ "recommended_status" => 500, "response_only" => true,
155
+ "extra_parameters" => NONE
156
+ }.freeze,
157
+ "proxy_loop_detected" => {
158
+ "recommended_status" => 502, "response_only" => true,
159
+ "extra_parameters" => NONE
160
+ }.freeze
161
+ }.freeze
162
+
163
+ class << self
164
+ # Is +name+ one of §2.3's 32 proxy error types? An unknown name
165
+ # answers false, and passes through the field untouched either way.
166
+ def registered?(name)
167
+ ERRORS.key?(name)
168
+ end
169
+
170
+ # §2.3's Recommended HTTP Status Code for +name+ — an Integer, or
171
+ # the RFC's own prose for the two entries whose recommendation is
172
+ # not a number (http_request_error, proxy_internal_response) — or
173
+ # nil for a name the registry does not carry. Advisory: §2.1.1
174
+ # makes it a SHOULD for the sender, never a bound enforced here.
175
+ def recommended_status(name)
176
+ entry = ERRORS[name]
177
+ entry && entry["recommended_status"]
178
+ end
179
+
180
+ # §2.3's "Response Only Generated by Intermediaries" flag for
181
+ # +name+ — false for an unknown name: a type the registry does not
182
+ # carry is not known to be response-only.
183
+ def response_only?(name)
184
+ entry = ERRORS[name]
185
+ entry ? entry["response_only"] : false
186
+ end
187
+
188
+ # §2.3's Extra Parameters table for +name+ — a frozen Hash of
189
+ # name => Structured Type, {} where the entry says "None" — or nil
190
+ # for an unknown name. Advisory data: a parameter used with a type
191
+ # it is not defined for "will be ignored" (§2.1.1), so the table
192
+ # never gates anything.
193
+ def extra_parameters(name)
194
+ entry = ERRORS[name]
195
+ entry && entry["extra_parameters"]
196
+ end
197
+ end
198
+ end
199
+ end
@@ -0,0 +1,161 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Fast
4
+ class ProxyStatus
5
+ # The shared List engine (Fast::ProxyStatus::ListField): this field
6
+ # includes it and answers its three private class hooks below —
7
+ # member_class (the Hop), empty_value (the shared frozen empty
8
+ # value) and field_label (the word the member errors carry). What
9
+ # stays in this file is Proxy-Status's own: §2's trailer-promotion
10
+ # machinery with its Promotion answer, the hops reader that names
11
+ # the engine's members ivar, and the shared empty value.
12
+ include ListField
13
+
14
+ class << self
15
+ # §2's trailer-promotion algorithm, spec-exact, over two field
16
+ # arguments read as parse reads them (a String, an Array of
17
+ # lines, a Fast::ProxyStatus used as held, or nil; a malformed
18
+ # field is treated as absent). For each trailer member in order:
19
+ # the first (leftmost) header member whose identifier equals it —
20
+ # "comparing the String or Token character by character without
21
+ # consideration of parameters", which Ruby String content
22
+ # equality IS (a Token equals the String that spells it) — is
23
+ # REPLACED with the trailer member "in its entirety, including
24
+ # any parameters" and the trailer member CONSUMED (the only
25
+ # reading under which step 2's "Remove the Proxy-Status trailer
26
+ # field if empty" is not vacuous); an unmatched member stays.
27
+ # Replacements are sequential, each trailer member matching
28
+ # against the header as it stands. Answers a frozen Promotion:
29
+ # #header nil exactly when the header input was nil (the
30
+ # algorithm only replaces, never adds), #trailer nil exactly when
31
+ # no members remain (absent, unreadable, or every member
32
+ # promoted). TypeError for a wrong input type.
33
+ def promote(header, trailer)
34
+ header_hops = held_hops(header)
35
+ replaced = header_hops&.dup
36
+ remaining = unmatched(replaced, held_hops(trailer))
37
+ Promotion.new(
38
+ header_hops.nil? ? nil : from_members(replaced),
39
+ remaining.empty? ? nil : from_members(remaining)
40
+ )
41
+ end
42
+
43
+ private
44
+
45
+ # The engine's member-class hook: §2's member.
46
+ def member_class
47
+ Hop
48
+ end
49
+
50
+ # The engine's empty-value hook: the shared frozen empty value.
51
+ def empty_value
52
+ EMPTY
53
+ end
54
+
55
+ # The engine's field-label hook: the word the member errors
56
+ # carry.
57
+ def field_label
58
+ "Proxy-Status"
59
+ end
60
+
61
+ # A field argument as received, the hops form: nil for the absent
62
+ # field, an existing value's hops (no re-parse), a String or
63
+ # Array of lines parsed leniently. TypeError for anything else.
64
+ def held_hops(input)
65
+ return nil if input.nil?
66
+ return input.hops if input.is_a?(ProxyStatus)
67
+
68
+ parse(input).hops
69
+ end
70
+
71
+ # §2 step 1's walk: for each trailer member in order, the
72
+ # leftmost match in +replaced+ — the header's private copy, nil
73
+ # when there is no header field (step 1.2's "no matching
74
+ # header_member is found") — is replaced in its entirety and the
75
+ # member consumed; an unmatched member stays. Answers the
76
+ # remaining trailer members in order; +replaced+ is mutated in
77
+ # place (a private copy — the held values are never touched).
78
+ def unmatched(replaced, trailer_hops)
79
+ remaining = []
80
+ (trailer_hops || EMPTY.hops).each do |member|
81
+ index = replaced && leftmost(replaced, member)
82
+ if index
83
+ replaced[index] = member
84
+ else
85
+ remaining << member
86
+ end
87
+ end
88
+ remaining
89
+ end
90
+
91
+ # §2 step 1.1: the FIRST (leftmost) member of the header as it
92
+ # stands whose identifier equals +member+'s — String content
93
+ # equality on the values only, parameters ignored. Answers the
94
+ # index, or nil when there is no match. Manual and
95
+ # allocation-free.
96
+ def leftmost(replaced, member)
97
+ value = member.value
98
+ index = 0
99
+ while index < replaced.size
100
+ return index if replaced[index].value == value
101
+
102
+ index += 1
103
+ end
104
+ nil
105
+ end
106
+ end
107
+
108
+ # §2's trailer-promotion answer: the promoted header field value
109
+ # and the trailer field that remains. #header is nil exactly when
110
+ # the header input was nil (the algorithm only replaces, never
111
+ # adds); #trailer is nil exactly when no trailer members remain —
112
+ # §2 step 2's "Remove the Proxy-Status trailer field if empty",
113
+ # the members a promotion consumed. Frozen, with class-checked
114
+ # equality on the pair.
115
+ class Promotion
116
+ # The promoted header value, or nil when there was no header
117
+ # field.
118
+ attr_reader :header
119
+
120
+ # The trailer value that remains — the unmatched members — or
121
+ # nil when nothing remains.
122
+ attr_reader :trailer
123
+
124
+ def initialize(header, trailer)
125
+ @header = header
126
+ @trailer = trailer
127
+ freeze
128
+ end
129
+
130
+ def ==(other)
131
+ other.instance_of?(self.class) && header == other.header && trailer == other.trailer
132
+ end
133
+ alias eql? ==
134
+
135
+ # Consistent with eql? — the pair's hash.
136
+ def hash
137
+ header.hash ^ trailer.hash
138
+ end
139
+
140
+ def inspect
141
+ "#<#{self.class.name} header=#{header.inspect}, trailer=#{trailer.inspect}>"
142
+ end
143
+ end
144
+
145
+ # The held members: the frozen Array of Hops in wire order — §2's
146
+ # order, the first member closest to the origin server, the last
147
+ # closest to the user agent. The engine's members ivar, named.
148
+ def hops
149
+ @members
150
+ end
151
+
152
+ # The shared frozen empty value: the answer an absent, malformed
153
+ # or constraint-violating field parses to — one object, stable
154
+ # identity. Defined at the end of the class body because the
155
+ # constant's initializer calls new, and initialize and the engine's
156
+ # hooks are defined above; the valid empty List ("") parses to a
157
+ # FRESH empty value, equal by the canonical wire form.
158
+ EMPTY = new([])
159
+ private_constant :EMPTY
160
+ end
161
+ end