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.
- checksums.yaml +7 -0
- data/CHANGELOG.md +95 -0
- data/README.md +225 -0
- data/lib/fast/proxy_status/cache_status.rb +382 -0
- data/lib/fast/proxy_status/errors.rb +54 -0
- data/lib/fast/proxy_status/hop.rb +135 -0
- data/lib/fast/proxy_status/list_field.rb +654 -0
- data/lib/fast/proxy_status/registry.rb +199 -0
- data/lib/fast/proxy_status/status.rb +161 -0
- data/lib/fast/proxy_status/targeted_cache_control.rb +642 -0
- data/lib/fast/proxy_status/version.rb +7 -0
- data/lib/fast/proxy_status.rb +96 -0
- metadata +91 -0
|
@@ -0,0 +1,382 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Fast
|
|
4
|
+
# RFC 9211 Cache-Status: the HTTP response field that "indicates how
|
|
5
|
+
# caches have handled that response and its corresponding request"
|
|
6
|
+
# (§2). "Its value is a List. Each member of the List represents a
|
|
7
|
+
# cache that has handled the request. The first member represents the
|
|
8
|
+
# cache closest to the origin server, and the last member represents
|
|
9
|
+
# the cache closest to the user (possibly including the user agent's
|
|
10
|
+
# cache itself if it appends a value)" (§2) — the same List shape
|
|
11
|
+
# Proxy-Status spells, so the class includes the shared
|
|
12
|
+
# Fast::ProxyStatus::ListField engine and rides its one-walk/
|
|
13
|
+
# two-policy parse, its positioning Scan, its frozen value object and
|
|
14
|
+
# its append-yourself operation, answering the engine's hooks with
|
|
15
|
+
# this field's member class (the Cache), empty value and label. Error,
|
|
16
|
+
# ParseError, SerializeError, the Cache member class, the PARAMETERS
|
|
17
|
+
# and FWD tables and EMPTY nest inside it.
|
|
18
|
+
#
|
|
19
|
+
# parse is the recipient's view and never raises on field content,
|
|
20
|
+
# with the diagnostic leniency the List engine spells: a field whose
|
|
21
|
+
# syntax is not a Structured Field Value, or whose List carries any
|
|
22
|
+
# member that is not a String or Token, is the ENTIRE field ignored
|
|
23
|
+
# (RFC 9651 §2.2 — RFC 9211 defines no other handling for members),
|
|
24
|
+
# answering the shared frozen empty value, the same answer an absent
|
|
25
|
+
# field gives; while parameters NEVER fail the field — unknown
|
|
26
|
+
# parameters and known parameters of a type §2.1-§2.8 does not allow
|
|
27
|
+
# are preserved in the Cache's data (#[], #params) and ignored
|
|
28
|
+
# semantically (the typed readers answer nil). A String is one field
|
|
29
|
+
# value; an Array is the field's lines, combined with ", "
|
|
30
|
+
# (RFC 9651 §4.2) — a List has no last-wins, the members
|
|
31
|
+
# concatenate. A wrong input type is a caller bug and raises
|
|
32
|
+
# TypeError.
|
|
33
|
+
#
|
|
34
|
+
# parse! is strict truth: the List failure wrapped with its message
|
|
35
|
+
# and byte offset, then the member constraint and §2.1-§2.8's
|
|
36
|
+
# parameter constraints, the first offender in List order raised
|
|
37
|
+
# positioned — the member's identifier offset, the parameter's VALUE
|
|
38
|
+
# offset (just past the "="; the key's end for the valueless form).
|
|
39
|
+
# valid? answers the same question with a boolean and never raises.
|
|
40
|
+
#
|
|
41
|
+
# The answer is a frozen value object: caches (the Cache members in
|
|
42
|
+
# wire order), first/last naming §2's two ends, the eight typed
|
|
43
|
+
# readers on each Cache (hit, fwd, fwd_status, ttl, stored,
|
|
44
|
+
# collapsed, key, detail — nil when the parameter is absent or
|
|
45
|
+
# wrong-typed) beside the raw #[] and #params, hit_and_fwd? surfacing
|
|
46
|
+
# §2.1's exclusivity SHOULD, the canonical to_s ("" when nothing is
|
|
47
|
+
# held), and class-checked canonical equality.
|
|
48
|
+
#
|
|
49
|
+
# to_s and construction are ALWAYS strict, in every family gem: a
|
|
50
|
+
# sender never puts garbage on the wire — the identifier is a plain
|
|
51
|
+
# String (emits quoted) or a Fast::SFV::Token (emits bare), the class
|
|
52
|
+
# the wire-type tag; the eight parameters carry their sections' types;
|
|
53
|
+
# and fwd is validated against §2.2's defined set on emit ONLY:
|
|
54
|
+
# §2.2 defines the values, it does not MUST them on a recipient, and
|
|
55
|
+
# a future registration must not break an old gem version — an
|
|
56
|
+
# unregistered fwd Token parses and round-trips untouched.
|
|
57
|
+
#
|
|
58
|
+
# append is §2's append-yourself: "caches SHOULD preserve the
|
|
59
|
+
# existing field value" — the received members in order, this cache's
|
|
60
|
+
# member appended as the LAST (closest to the user), a new frozen
|
|
61
|
+
# value.
|
|
62
|
+
class CacheStatus
|
|
63
|
+
# RFC 9651 value shapes aliased here, private to this class, so the
|
|
64
|
+
# checks and the readers read them by the short name.
|
|
65
|
+
Token = SFV::Token
|
|
66
|
+
private_constant :Token
|
|
67
|
+
|
|
68
|
+
# The Byte Sequence tag: a String tagged ASCII-8BIT IS the Byte
|
|
69
|
+
# Sequence on the wire (RFC 9651 §3.3.5 — the encoding is the
|
|
70
|
+
# wire-type tag), so the key parameter's constraint reads the same
|
|
71
|
+
# fact, and a UTF-8 String would emit as a quoted sf-string.
|
|
72
|
+
BINARY = Encoding::ASCII_8BIT
|
|
73
|
+
private_constant :BINARY
|
|
74
|
+
|
|
75
|
+
# The family root, one rescue for the whole gem: every
|
|
76
|
+
# Fast::CacheStatus failure descends from Fast::ProxyStatus::Error,
|
|
77
|
+
# so a caller can guard this class and its siblings with one
|
|
78
|
+
# rescue. This class is never raised in its own right — every
|
|
79
|
+
# failure here is either a parse defect or a serialization defect.
|
|
80
|
+
# A wrong input type is a caller bug, not field content: it raises
|
|
81
|
+
# plain TypeError and never this class.
|
|
82
|
+
class Error < ProxyStatus::Error; end
|
|
83
|
+
|
|
84
|
+
# A Cache-Status field this gem cannot read. Two shapes meet here:
|
|
85
|
+
# the underlying RFC 9651 List failed to parse — the failure is
|
|
86
|
+
# wrapped with its message and byte offset preserved — or, under the
|
|
87
|
+
# strict parse, a member broke RFC 9211 §2's constraint (a member
|
|
88
|
+
# that is not a String or Token) or a §2.1-§2.8 parameter broke its
|
|
89
|
+
# section's type, and #offset is the byte position of the offending
|
|
90
|
+
# member's identifier or parameter VALUE in the combined field value
|
|
91
|
+
# (a member's, the member's first non-OWS byte; a parameter's, just
|
|
92
|
+
# past the "=" — the key's end for the valueless form, the implicit
|
|
93
|
+
# Boolean true). The lenient parse never raises it: a recipient that
|
|
94
|
+
# cannot read the field ignores it entirely (RFC 9651 §2.2).
|
|
95
|
+
class ParseError < Error
|
|
96
|
+
attr_reader :offset
|
|
97
|
+
|
|
98
|
+
def initialize(message, offset)
|
|
99
|
+
super(message)
|
|
100
|
+
@offset = offset
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
# A value cannot go on the wire: the field under construction breaks
|
|
105
|
+
# a bound RFC 9211 states — a member identifier that is not a plain
|
|
106
|
+
# String or a Fast::SFV::Token (a String tagged ASCII-8BIT would
|
|
107
|
+
# emit as a Byte Sequence), a §2.1-§2.8 parameter of a type its
|
|
108
|
+
# section does not allow, an unknown parameter of a non-bare type, a
|
|
109
|
+
# parameter key outside the Structured Fields key grammar, a fwd
|
|
110
|
+
# Token outside §2.2's defined set, or members that are not Caches.
|
|
111
|
+
# #offset is the byte position in the output built so far — 0,
|
|
112
|
+
# because the check runs before anything is emitted; the whole emit
|
|
113
|
+
# path raises this class and never TypeError: on emit the wrongness
|
|
114
|
+
# is in the value being sent, which is field content, not a caller
|
|
115
|
+
# protocol bug.
|
|
116
|
+
class SerializeError < Error
|
|
117
|
+
attr_reader :offset
|
|
118
|
+
|
|
119
|
+
def initialize(message, offset)
|
|
120
|
+
super(message)
|
|
121
|
+
@offset = offset
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# §2.1-§2.8's eight parameters, key => the type its own section
|
|
126
|
+
# states, worded as the sections word them — the wording both error
|
|
127
|
+
# families carry. Frozen public data, its own lookup (a Hash IS its
|
|
128
|
+
# registry's read); the table never gates a parse: an unknown
|
|
129
|
+
# parameter parses, is preserved and round-trips (§4's registration
|
|
130
|
+
# path — a future registration must not break an old gem version).
|
|
131
|
+
PARAMETERS = {
|
|
132
|
+
"hit" => "a Boolean", # §2.1
|
|
133
|
+
"fwd" => "a Token", # §2.2
|
|
134
|
+
"fwd-status" => "an Integer", # §2.3
|
|
135
|
+
"ttl" => "an Integer", # §2.4
|
|
136
|
+
"stored" => "a Boolean", # §2.5
|
|
137
|
+
"collapsed" => "a Boolean", # §2.6
|
|
138
|
+
"key" => "a String", # §2.7
|
|
139
|
+
"detail" => "a String or Token" # §2.8
|
|
140
|
+
}.freeze
|
|
141
|
+
|
|
142
|
+
# §2.2's fwd values, "from most specific to least" — the document
|
|
143
|
+
# order the section spells them in. Frozen public data; the
|
|
144
|
+
# emit-side enum check reads it, the parse side never does (§2.2
|
|
145
|
+
# defines the values, it does not MUST them on a recipient).
|
|
146
|
+
FWD = [
|
|
147
|
+
"bypass", # the cache was configured to not handle this request
|
|
148
|
+
"method", # the request method's semantics require forwarding
|
|
149
|
+
"uri-miss", # no stored response matched the request URI
|
|
150
|
+
"vary-miss", # a stored URI match could not select a response
|
|
151
|
+
"miss", # no usable stored response (uri-miss/vary-miss indistinct)
|
|
152
|
+
"request", # a fresh response was selected, request semantics refused it
|
|
153
|
+
"stale", # a response was selected but it was stale
|
|
154
|
+
"partial" # a partial response lacked the requested ranges
|
|
155
|
+
].freeze
|
|
156
|
+
|
|
157
|
+
# The shared List engine, as Proxy-Status includes it: the
|
|
158
|
+
# class-level parse/parse!/valid?/append, the instance surface over
|
|
159
|
+
# the members ivar, the construction boundary — all reading this
|
|
160
|
+
# class's member class, empty value and label through the private
|
|
161
|
+
# hooks below.
|
|
162
|
+
include ProxyStatus::ListField
|
|
163
|
+
|
|
164
|
+
# §2's member: one cache's entry in the List — "Each List member
|
|
165
|
+
# identifies the cache that inserted it, and this identifier MUST
|
|
166
|
+
# be a String or Token" (§2) — the substrate's Item IS the member
|
|
167
|
+
# shape, so the subclass of the shared member base
|
|
168
|
+
# (Fast::ProxyStatus::ListField::Member) inherits #value, #params,
|
|
169
|
+
# the raw #[] reader and the canonical strict #to_s, and serializes
|
|
170
|
+
# inside Fast::SFV.serialize_list unchanged, while §2.1-§2.8's
|
|
171
|
+
# parameter rules, the eight typed readers, the exclusivity
|
|
172
|
+
# surface and §2.2's emit-side enum check stay here, the field's
|
|
173
|
+
# own table. A Cache never equals a Hop, whatever their value and
|
|
174
|
+
# parameters — the class is the member's identity.
|
|
175
|
+
class Cache < ProxyStatus::ListField::Member
|
|
176
|
+
class << self
|
|
177
|
+
# §2.1-§2.8's constraint for one parameter: true when +value+
|
|
178
|
+
# carries the type the parameter's section allows, and always
|
|
179
|
+
# true for an unknown key (§4's registration path — an unknown
|
|
180
|
+
# parameter is preserved and ignored).
|
|
181
|
+
def param_ok?(key, value)
|
|
182
|
+
case key
|
|
183
|
+
when "hit", "stored", "collapsed" then boolean_value?(value)
|
|
184
|
+
when "fwd" then value.is_a?(Token)
|
|
185
|
+
when "fwd-status", "ttl" then value.is_a?(Integer)
|
|
186
|
+
when "key" then plain_string?(value)
|
|
187
|
+
when "detail" then identifier_value?(value)
|
|
188
|
+
else true
|
|
189
|
+
end
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
# The type one of §2.1-§2.8's parameters allows, worded as its
|
|
193
|
+
# section words it — nil for an unknown key. The wording the
|
|
194
|
+
# ParseError and the SerializeError both carry.
|
|
195
|
+
def param_rule(key)
|
|
196
|
+
PARAMETERS[key]
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
# A Boolean parameter value — §2.1, §2.5 and §2.6's readers and
|
|
200
|
+
# constraints share the shape: false is a value, never an
|
|
201
|
+
# absence (§3's "collapsed=?0" example), so the check reads the
|
|
202
|
+
# class, not the truth.
|
|
203
|
+
def boolean_value?(value)
|
|
204
|
+
value.is_a?(TrueClass) || value.is_a?(FalseClass)
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
private
|
|
208
|
+
|
|
209
|
+
# The member base's error hook: Cache-Status's SerializeError
|
|
210
|
+
# nests in the field class, not the member class.
|
|
211
|
+
def serialize_error(message)
|
|
212
|
+
SerializeError.new(message, 0)
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
# §2.7: "The value of 'key' is a String" — a plain String, not
|
|
216
|
+
# a Token (a Token is a different wire type) and not a Byte
|
|
217
|
+
# Sequence.
|
|
218
|
+
def plain_string?(value)
|
|
219
|
+
!value.is_a?(Token) && identifier_value?(value)
|
|
220
|
+
end
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
# Strict construction — a sender never puts garbage on the wire.
|
|
224
|
+
# +value+ is the cache's identifier: a plain String (emits
|
|
225
|
+
# quoted) or a Fast::SFV::Token (emits bare) — the class is the
|
|
226
|
+
# wire-type tag; a String tagged ASCII-8BIT is refused (it would
|
|
227
|
+
# emit as a Byte Sequence). +params+ is a Hash of §2.1-§2.8
|
|
228
|
+
# parameters under Structured Fields keys: the eight carry their
|
|
229
|
+
# sections' types, any other parameter any bare Structured Fields
|
|
230
|
+
# type. The value String and the params Hash are frozen IN PLACE
|
|
231
|
+
# as part of the value object — pass copies if the buffers are
|
|
232
|
+
# reused. Anything else raises SerializeError naming the
|
|
233
|
+
# parameter and the type. The base's checks run first, then
|
|
234
|
+
# §2.2's emit-side enum check below — the only field-specific
|
|
235
|
+
# rule construction adds.
|
|
236
|
+
def initialize(value, params = {})
|
|
237
|
+
super
|
|
238
|
+
check_fwd!
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
# §2.1: "The value of 'hit' is a Boolean that, when true,
|
|
242
|
+
# indicates that the request was satisfied by the cache; that is,
|
|
243
|
+
# it was not forwarded, and the response was obtained from the
|
|
244
|
+
# cache." true, false, or nil — false is a value, never an
|
|
245
|
+
# absence, exactly as §3's "collapsed=?0" example spells for its
|
|
246
|
+
# own Boolean.
|
|
247
|
+
def hit
|
|
248
|
+
value = @params["hit"]
|
|
249
|
+
Cache.boolean_value?(value) ? value : nil
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
# §2.2: "'fwd', when present, indicates that the request went
|
|
253
|
+
# forward towards the origin; its value is a Token that indicates
|
|
254
|
+
# why." The Token — a String that equals it spells it — or nil
|
|
255
|
+
# when the parameter is absent or arrived as another type. Any
|
|
256
|
+
# Token parses (§2.2 defines the values, it does not MUST them on
|
|
257
|
+
# a recipient); construction validates the set.
|
|
258
|
+
def fwd
|
|
259
|
+
value = @params["fwd"]
|
|
260
|
+
value.is_a?(Token) ? value : nil
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
# §2.3: "The value of 'fwd-status' is an Integer that indicates
|
|
264
|
+
# which status code the next-hop server returned in response to
|
|
265
|
+
# the forwarded request." The Integer or nil. The section's "If
|
|
266
|
+
# fwd-status is not present but the fwd parameter is, it defaults
|
|
267
|
+
# to the status code sent in the response" is a response-level
|
|
268
|
+
# fact this field object never computes — the reader answers the
|
|
269
|
+
# field only, the caller owns the response.
|
|
270
|
+
def fwd_status
|
|
271
|
+
value = @params["fwd-status"]
|
|
272
|
+
value.is_a?(Integer) ? value : nil
|
|
273
|
+
end
|
|
274
|
+
|
|
275
|
+
# §2.4: "The value of 'ttl' is an Integer that indicates the
|
|
276
|
+
# response's remaining freshness lifetime ... It may be negative,
|
|
277
|
+
# to indicate staleness." The Integer — either sign, §3's
|
|
278
|
+
# "ttl=-412" example — or nil.
|
|
279
|
+
def ttl
|
|
280
|
+
value = @params["ttl"]
|
|
281
|
+
value.is_a?(Integer) ? value : nil
|
|
282
|
+
end
|
|
283
|
+
|
|
284
|
+
# §2.5: "The value of 'stored' is a Boolean that indicates whether
|
|
285
|
+
# the cache stored the response ... a true value indicates that it
|
|
286
|
+
# did." true, false, or nil.
|
|
287
|
+
def stored
|
|
288
|
+
value = @params["stored"]
|
|
289
|
+
Cache.boolean_value?(value) ? value : nil
|
|
290
|
+
end
|
|
291
|
+
|
|
292
|
+
# §2.6: "The value of 'collapsed' is a Boolean that indicates
|
|
293
|
+
# whether this request was collapsed together with one or more
|
|
294
|
+
# other forward requests." true, false, or nil — §3's
|
|
295
|
+
# "collapsed=?0" example: the collapse was attempted and failed,
|
|
296
|
+
# a value, not an absence.
|
|
297
|
+
def collapsed
|
|
298
|
+
value = @params["collapsed"]
|
|
299
|
+
Cache.boolean_value?(value) ? value : nil
|
|
300
|
+
end
|
|
301
|
+
|
|
302
|
+
# §2.7: "The value of 'key' is a String that conveys a
|
|
303
|
+
# representation of the cache key used for the response." The
|
|
304
|
+
# plain String — a Token is a different wire type — or nil.
|
|
305
|
+
def key
|
|
306
|
+
value = @params["key"]
|
|
307
|
+
self.class.send(:plain_string?, value) ? value : nil
|
|
308
|
+
end
|
|
309
|
+
|
|
310
|
+
# §2.8: "The value of 'detail' is either a String or a Token that
|
|
311
|
+
# allows implementations to convey additional information not
|
|
312
|
+
# captured in other parameters" — §2's own example carries a
|
|
313
|
+
# Token ("ExampleCache; hit; detail=MEMORY"). The String, the
|
|
314
|
+
# Token, or nil.
|
|
315
|
+
def detail
|
|
316
|
+
value = @params["detail"]
|
|
317
|
+
self.class.identifier_value?(value) ? value : nil
|
|
318
|
+
end
|
|
319
|
+
|
|
320
|
+
# §2.1's exclusivity SHOULD, surfaced: "'hit' and 'fwd' are
|
|
321
|
+
# exclusive; only one of them should appear on each list member."
|
|
322
|
+
# true exactly when both readers answer (false is a present hit),
|
|
323
|
+
# and never a gate on a parse, a strict parse or an emit — a
|
|
324
|
+
# SHOULD is sender advice, and the value object reports what the
|
|
325
|
+
# sender sent.
|
|
326
|
+
def hit_and_fwd?
|
|
327
|
+
!hit.nil? && !fwd.nil?
|
|
328
|
+
end
|
|
329
|
+
|
|
330
|
+
private
|
|
331
|
+
|
|
332
|
+
# §2.2's defined values, the emit-side-only check: a Token the
|
|
333
|
+
# set does not carry cannot go on the wire from this gem, but it
|
|
334
|
+
# PARSES and round-trips untouched — §2.2 defines the values, it
|
|
335
|
+
# does not MUST them on a recipient, and a future registration
|
|
336
|
+
# must not break an old gem version.
|
|
337
|
+
def check_fwd!
|
|
338
|
+
value = @params["fwd"]
|
|
339
|
+
return unless value.is_a?(Token)
|
|
340
|
+
return if FWD.include?(value)
|
|
341
|
+
|
|
342
|
+
raise SerializeError.new("fwd: the value is a Token of the 2.2 set (#{FWD.join(', ')})", 0)
|
|
343
|
+
end
|
|
344
|
+
end
|
|
345
|
+
|
|
346
|
+
class << self
|
|
347
|
+
private
|
|
348
|
+
|
|
349
|
+
# The engine's member-class hook: §2's member.
|
|
350
|
+
def member_class
|
|
351
|
+
Cache
|
|
352
|
+
end
|
|
353
|
+
|
|
354
|
+
# The engine's empty-value hook: the shared frozen empty value.
|
|
355
|
+
def empty_value
|
|
356
|
+
EMPTY
|
|
357
|
+
end
|
|
358
|
+
|
|
359
|
+
# The engine's field-label hook: the word the member errors
|
|
360
|
+
# carry.
|
|
361
|
+
def field_label
|
|
362
|
+
"Cache-Status"
|
|
363
|
+
end
|
|
364
|
+
end
|
|
365
|
+
|
|
366
|
+
# The held members: the frozen Array of Caches in wire order — §2's
|
|
367
|
+
# order, the first member closest to the origin server, the last
|
|
368
|
+
# closest to the user. The engine's members ivar, named.
|
|
369
|
+
def caches
|
|
370
|
+
@members
|
|
371
|
+
end
|
|
372
|
+
|
|
373
|
+
# The shared frozen empty value: the answer an absent, malformed
|
|
374
|
+
# or constraint-violating field parses to — one object, stable
|
|
375
|
+
# identity. Defined at the end of the class body because the
|
|
376
|
+
# constant's initializer calls new, and initialize and the engine's
|
|
377
|
+
# hooks are defined above; the valid empty List ("") parses to a
|
|
378
|
+
# FRESH empty value, equal by the canonical wire form.
|
|
379
|
+
EMPTY = new([])
|
|
380
|
+
private_constant :EMPTY
|
|
381
|
+
end
|
|
382
|
+
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Fast
|
|
4
|
+
class ProxyStatus
|
|
5
|
+
# The family root: every Fast::ProxyStatus failure descends from
|
|
6
|
+
# Error, so a caller can guard the whole surface with one rescue.
|
|
7
|
+
# This class is never raised in its own right — every failure here
|
|
8
|
+
# is either a parse defect or a serialization defect — but the
|
|
9
|
+
# family shape keeps the base so one rescue guards them all. A
|
|
10
|
+
# wrong input type is a caller bug, not field content: it raises
|
|
11
|
+
# plain TypeError and never this class.
|
|
12
|
+
class Error < StandardError; end
|
|
13
|
+
|
|
14
|
+
# A Proxy-Status field this gem cannot read. Two shapes meet here:
|
|
15
|
+
# the underlying RFC 9651 List failed to parse — the failure is
|
|
16
|
+
# wrapped with its message and byte offset preserved — or, under the
|
|
17
|
+
# strict parse, a member broke RFC 9209 §2's constraint (a member
|
|
18
|
+
# that is not a String or Token) or a §2.1 parameter broke its
|
|
19
|
+
# section's type, and #offset is the byte position of the offending
|
|
20
|
+
# member's identifier or parameter VALUE in the combined field value
|
|
21
|
+
# (a member's, the member's first non-OWS byte; a parameter's, just
|
|
22
|
+
# past the "=" — the key's end for the valueless form, the implicit
|
|
23
|
+
# Boolean true). The lenient parse never raises it: a recipient that
|
|
24
|
+
# cannot read the field ignores it entirely (RFC 9651 §2.2).
|
|
25
|
+
class ParseError < Error
|
|
26
|
+
attr_reader :offset
|
|
27
|
+
|
|
28
|
+
def initialize(message, offset)
|
|
29
|
+
super(message)
|
|
30
|
+
@offset = offset
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# A value cannot go on the wire: the field under construction breaks
|
|
35
|
+
# a bound RFC 9209 states — a member identifier that is not a plain
|
|
36
|
+
# String or a Fast::SFV::Token (a String tagged ASCII-8BIT would
|
|
37
|
+
# emit as a Byte Sequence), a §2.1 parameter of a type its section
|
|
38
|
+
# does not allow, an unknown parameter of a non-bare type, a
|
|
39
|
+
# parameter key outside the Structured Fields key grammar, or
|
|
40
|
+
# members that are not Hops. #offset is the byte position in the
|
|
41
|
+
# output built so far — 0, because the check runs before anything is
|
|
42
|
+
# emitted; the whole emit path raises this class and never TypeError:
|
|
43
|
+
# on emit the wrongness is in the value being sent, which is field
|
|
44
|
+
# content, not a caller protocol bug.
|
|
45
|
+
class SerializeError < Error
|
|
46
|
+
attr_reader :offset
|
|
47
|
+
|
|
48
|
+
def initialize(message, offset)
|
|
49
|
+
super(message)
|
|
50
|
+
@offset = offset
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Fast
|
|
4
|
+
class ProxyStatus
|
|
5
|
+
# RFC 9209 §2's member: one intermediary's entry in the List — the
|
|
6
|
+
# substrate's Item IS the member shape ("Each member of the List
|
|
7
|
+
# identifies the intermediary that inserted the value and MUST have
|
|
8
|
+
# a type of either String or Token"; parameters "convey additional
|
|
9
|
+
# information about that intermediary's handling of the response and
|
|
10
|
+
# its associated request; see Section 2.1"), so the subclass of the
|
|
11
|
+
# shared member base (Fast::ProxyStatus::ListField::Member) inherits
|
|
12
|
+
# #value, #params, the raw #[] reader and the canonical strict #to_s,
|
|
13
|
+
# and serializes inside Fast::SFV.serialize_list unchanged — while
|
|
14
|
+
# §2.1's five parameter rules, the five typed readers and §2.1.3's
|
|
15
|
+
# Token-or-Byte-Sequence rule stay here, the field's own table. The
|
|
16
|
+
# member constraint, the key grammar and the generic construction
|
|
17
|
+
# boundary are the base's (RFC 9211 §2, Cache-Status's member,
|
|
18
|
+
# spells the identical identifier constraint); the shared
|
|
19
|
+
# constraint surface the parse walk and the construction read is
|
|
20
|
+
# one surface across both List fields, so the lenient parse, the
|
|
21
|
+
# strict parse and the emit cannot drift.
|
|
22
|
+
class Hop < ListField::Member
|
|
23
|
+
# §2.1's five parameters, key => the type its own section states,
|
|
24
|
+
# worded as the sections word them — the wording both error
|
|
25
|
+
# families carry. An unknown key answers nil from param_rule and
|
|
26
|
+
# true from param_ok?: "Unrecognised parameters MUST be ignored"
|
|
27
|
+
# (§2.1) — and accepts any bare Structured Fields type at
|
|
28
|
+
# construction.
|
|
29
|
+
PARAM_RULES = {
|
|
30
|
+
"error" => "a Token", # §2.1.1
|
|
31
|
+
"next-hop" => "a String or Token", # §2.1.2
|
|
32
|
+
"next-protocol" => "a Token or Byte Sequence", # §2.1.3
|
|
33
|
+
"received-status" => "an Integer", # §2.1.4
|
|
34
|
+
"details" => "a String" # §2.1.5
|
|
35
|
+
}.freeze
|
|
36
|
+
private_constant :PARAM_RULES
|
|
37
|
+
|
|
38
|
+
class << self
|
|
39
|
+
# §2.1's constraint for one parameter: true when +value+ carries
|
|
40
|
+
# the type the parameter's section allows, and always true for
|
|
41
|
+
# an unknown key ("Unrecognised parameters MUST be ignored",
|
|
42
|
+
# §2.1).
|
|
43
|
+
def param_ok?(key, value)
|
|
44
|
+
case key
|
|
45
|
+
when "error" then value.is_a?(Token)
|
|
46
|
+
when "next-hop" then identifier_value?(value)
|
|
47
|
+
when "next-protocol" then byte_value?(value)
|
|
48
|
+
when "received-status" then value.is_a?(Integer)
|
|
49
|
+
when "details" then plain_string?(value)
|
|
50
|
+
else true
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# The type one of §2.1's parameters allows, worded as its
|
|
55
|
+
# section words it — nil for an unknown key. The wording the
|
|
56
|
+
# ParseError and the SerializeError both carry.
|
|
57
|
+
def param_rule(key)
|
|
58
|
+
PARAM_RULES[key]
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# The identifier constraint under the name this field's public
|
|
62
|
+
# surface froze — the base's identifier_value? answers the same
|
|
63
|
+
# String-or-Token rule for the shared machinery.
|
|
64
|
+
def hop_value?(value)
|
|
65
|
+
identifier_value?(value)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# §2.1.3: "The value MUST be either a Token or Byte Sequence" —
|
|
69
|
+
# a Token, or a String tagged ASCII-8BIT (the Byte Sequence
|
|
70
|
+
# tag).
|
|
71
|
+
def byte_value?(value)
|
|
72
|
+
value.is_a?(Token) || (value.is_a?(String) && value.encoding == BINARY)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
private
|
|
76
|
+
|
|
77
|
+
# The member base's error hook: §2.1's SerializeError nests in
|
|
78
|
+
# the field class, not the member class.
|
|
79
|
+
def serialize_error(message)
|
|
80
|
+
SerializeError.new(message, 0)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# §2.1.5: "The details parameter's value is a String" — a plain
|
|
84
|
+
# String, not a Token (a Token is a different wire type) and not
|
|
85
|
+
# a Byte Sequence.
|
|
86
|
+
def plain_string?(value)
|
|
87
|
+
!value.is_a?(Token) && identifier_value?(value)
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# §2.1.1: "The error parameter's value is a Token that is a proxy
|
|
92
|
+
# error type." The Token — a String that equals it spells it — or
|
|
93
|
+
# nil when the parameter is absent or arrived as another type.
|
|
94
|
+
def error
|
|
95
|
+
value = @params["error"]
|
|
96
|
+
value.is_a?(Token) ? value : nil
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# §2.1.2: "The next-hop parameter's value is a String or Token
|
|
100
|
+
# that identifies the intermediary or origin server selected."
|
|
101
|
+
# The String or Token, or nil for another type.
|
|
102
|
+
def next_hop
|
|
103
|
+
value = @params["next-hop"]
|
|
104
|
+
Hop.hop_value?(value) ? value : nil
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# §2.1.3: "The value MUST be either a Token or Byte Sequence
|
|
108
|
+
# representing a TLS ALPN Protocol ID." The Token or the binary
|
|
109
|
+
# String, or nil for another type. The section's "If the protocol
|
|
110
|
+
# identifier is able to be expressed as a Token using ASCII
|
|
111
|
+
# encoding, that form MUST be used" is sender advice on which
|
|
112
|
+
# form to emit, never a recipient's rejection of the Byte
|
|
113
|
+
# Sequence form.
|
|
114
|
+
def next_protocol
|
|
115
|
+
value = @params["next-protocol"]
|
|
116
|
+
Hop.byte_value?(value) ? value : nil
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# §2.1.4: "The value MUST be an Integer." The Integer — no range
|
|
120
|
+
# constraint: the section states the type only — or nil.
|
|
121
|
+
def received_status
|
|
122
|
+
value = @params["received-status"]
|
|
123
|
+
value.is_a?(Integer) ? value : nil
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
# §2.1.5: "The details parameter's value is a String containing
|
|
127
|
+
# additional information not captured anywhere else." The plain
|
|
128
|
+
# String — a Token is a different wire type — or nil.
|
|
129
|
+
def details
|
|
130
|
+
value = @params["details"]
|
|
131
|
+
self.class.send(:plain_string?, value) ? value : nil
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
end
|