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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c3fae1db9f80d7bfeb0a7dfa6d071ca0c0c02d16709f20cffa330932ab996893
4
+ data.tar.gz: 2510fa940d7734d5c05d07304c75c5a3a7efe627ec3b404ea9a34c0ea51b4160
5
+ SHA512:
6
+ metadata.gz: 3c82a2fc88b0bc7dbeb8d200cd5dd368fb21aa8b2b31aa46a548f4daadac54924b54b76665cf5536c026d1acc4fed68162e86bb85f1e209184fc342dbf8e35ee
7
+ data.tar.gz: '07978c3b2b06615a101136a1f4ee3799bc38011abe2e349fa07fe079d86c76e15f5d6eeefdea29e675ac0a39aa884beee7662b5540252724be501f280e7ca74a'
data/CHANGELOG.md ADDED
@@ -0,0 +1,95 @@
1
+ # Changelog
2
+
3
+ ## 0.1.0 (2026-11-26)
4
+
5
+ - Fields: `Fast::ProxyStatus` — the RFC 9209 Proxy-Status field as a
6
+ frozen value object over fast-sfv: a List of parameterized members,
7
+ one per intermediary that handled the response, the first closest to
8
+ the origin server, with the family mechanics — `parse` with the
9
+ diagnostic leniency (a malformed field, or any member that is not a
10
+ String or Token, is the entire field ignored per RFC 9651 §2.2, the
11
+ shared frozen empty value; unknown and wrong-typed §2.1 parameters
12
+ preserved in the data and ignored semantically, so §2.1.5's own
13
+ quoted-string error example parses), `parse!` positioned strict truth
14
+ (the wrapped List failure, then the member and §2.1 parameter
15
+ constraints, the first offender in List order raised at the
16
+ offending value's byte offset), `valid?` the boolean mirror, strict
17
+ construction, canonical `to_s`, class-checked equality.
18
+ - Fields: `Fast::CacheStatus` — the RFC 9211 Cache-Status field on the
19
+ same List engine: a List of parameterized members, one per cache
20
+ that handled the request, the first closest to the origin server,
21
+ with the eight §2.1-§2.8 parameters as typed readers on the `Cache`
22
+ member value object (hit, fwd, fwd-status, ttl, stored, collapsed,
23
+ key, detail — false is a value, never an absence), `hit_and_fwd?`
24
+ surfacing §2.1's exclusivity SHOULD without ever gating anything,
25
+ §2.2's fwd value set pinned as frozen data
26
+ (`Fast::CacheStatus::FWD`) and validated at construction only
27
+ (an unregistered Token parses and round-trips untouched), the
28
+ parameters table pinned as frozen data
29
+ (`Fast::CacheStatus::PARAMETERS`), and `append` (§2's
30
+ append-yourself — this cache's member appended as the LAST, a new
31
+ frozen value).
32
+ - Fields: `Fast::TargetedCacheControl` — the RFC 9213 targeted
33
+ cache-control fields as a Dictionary value object (the RFC 9530
34
+ digest shape): §2.1's type-inference rules read on parse (valueless
35
+ → Boolean true, quoted → String, token → Token/Integer/Decimal by
36
+ content) and enforced on emit ("Implementations MUST NOT generate
37
+ values that violate these inferred constraints"), the five
38
+ §2.2 MUST-implement directives pinned as frozen data
39
+ (`Fast::TargetedCacheControl::DIRECTIVES`) with unknown directives
40
+ passing through, `FIELD_NAME` ("CDN-Cache-Control", the registered
41
+ instance, and no "-Cache-Control" suffix matcher — §2.4's MUST NOT),
42
+ and the leniency split: an empty or malformed field is ignored
43
+ entirely (§2.1's MUST) while a violating directive is
44
+ preserved-but-flagged — `#violating` answers the SHOULD-NOT-consume
45
+ set, the value stays in the data and round-trips.
46
+ - Engine: `Fast::ProxyStatus::ListField` — the List-of-members
47
+ machinery the two List fields share, extracted from Proxy-Status
48
+ behavior-invisibly (every offset pin, allocation ceiling and test
49
+ of the first half held exactly): the class-level one-walk/
50
+ two-policy parse machinery, the instance surface over the members
51
+ ivar, the positioning `Scan`, and the `Member` base the two member
52
+ classes build on. The RFC 9213 field is a Dictionary and does not
53
+ ride it.
54
+ - `Fast::ProxyStatus::Hop` — the member value object, an
55
+ `Fast::SFV::Item` subclass: the raw `#[]` and `#params` beside the
56
+ five §2.1 typed readers (`error`, `next-hop`, `next-protocol`,
57
+ `received-status`, `details`), the strict construction boundary (the
58
+ identifier a plain String or a Token — the class is the wire-type
59
+ tag; the five §2.1 parameters type-checked; unknown parameters any
60
+ bare Structured Fields type; value and params frozen in place), and
61
+ the shared constraint surface the parse walk and the construction
62
+ read.
63
+ - Operations: `append` (§2's append-yourself — the upstream value
64
+ parsed leniently, this intermediary's member appended as the LAST,
65
+ a new frozen value) and `promote` (§2's trailer-promotion algorithm
66
+ spec-exact — the leftmost match by value characters across the
67
+ String and Token forms, replacement in its entirety, consumed
68
+ trailer members, the trailer removed when nothing remains —
69
+ answering a frozen `Fast::ProxyStatus::Promotion`).
70
+ - Registry: `Fast::ProxyStatus::ERRORS` — §2.3's 32 proxy error types
71
+ as frozen data in document order (recommended statuses, response-only
72
+ flags, extra parameters; the two prose statuses verbatim), with
73
+ `registered?`/`recommended_status`/`response_only?`/
74
+ `extra_parameters` lookups and unknown passthrough; the registry
75
+ never gates a parse or an emit.
76
+ - Budgets: the allocation tests for the ten Proxy-Status paths
77
+ (parse, the malformed fall, the member-violation fall, strict
78
+ parse, `valid?`, Hop construction, append on a held value, append
79
+ over a received String, promote, emit), extended with the ten
80
+ Cache-Status paths and the seven TargetedCacheControl paths
81
+ (including the flagged parse — the leniency split's own price),
82
+ measured and frozen in the file's comment.
83
+ - Scaffold: the gemspec (one runtime dependency, fast-sfv), the README
84
+ front door, and this changelog. The documentation pass and this 0.1.0
85
+ release land together — docs are part of the product, the family's
86
+ release rule.
87
+ - Docs: the diátaxis docs tree under `docs/` (tutorial covering all
88
+ three modules, four how-to guides, five reference pages — the three
89
+ APIs and both registries — four explanations), the README front door
90
+ with the three-module quickstart and the documentation index, and
91
+ the both-mode performance page from one measurement session
92
+ (interpreter and YJIT side by side, the benchmark kit alongside).
93
+ Every fenced ruby example was executed as printed and every RFC
94
+ citation checked against the RFC text by the pipeline's independent
95
+ review.
data/README.md ADDED
@@ -0,0 +1,225 @@
1
+ # fast-proxy-status
2
+
3
+ fast-proxy-status is the RFC 9209, 9211 and 9213 diagnostics-field implementation for Ruby: Proxy-Status, Cache-Status and Targeted Cache Control, built on fast-sfv.
4
+
5
+ ## What it is
6
+
7
+ `Fast::ProxyStatus` implements RFC 9209 §2 — "Its value is a List (see Section 3.1 of [STRUCTURED-FIELDS]). Each member of the List represents an intermediary that has handled the response. The first member represents the intermediary closest to the origin server, and the last member represents the intermediary closest to the user agent" — each member a String or Token carrying §2.1's five parameters as typed readers (`error`, `next-hop`, `next-protocol`, `received-status`, `details`), with §2.3's proxy error type registry pinned as frozen data (`Fast::ProxyStatus::ERRORS`), unknown error types passing through untouched.
8
+
9
+ `Fast::CacheStatus` implements RFC 9211 §2 — the same List shape, one member per cache, "the first member represents the cache closest to the origin server, and the last member represents the cache closest to the user (possibly including the user agent's cache itself if it appends a value)" — with the eight §2.1-§2.8 parameters as typed readers (`hit`, `fwd`, `fwd-status`, `ttl`, `stored`, `collapsed`, `key`, `detail`) and §2.2's fwd value set pinned as frozen data (`Fast::CacheStatus::FWD`).
10
+
11
+ `Fast::TargetedCacheControl` implements RFC 9213 — the targeted cache-control fields as a Dictionary of cache directives with §2.1's type-inference rules, the known-directive table pinned as frozen data (`Fast::TargetedCacheControl::DIRECTIVES`), and a violating directive preserved-but-flagged on parse.
12
+
13
+ The shared mechanics: the lenient parse (`parse`) answers a recipient's view and never raises on field content; the strict parse (`parse!`) tells the truth with byte offsets; `to_s` and construction are always strict; results are frozen value objects; one runtime dependency (fast-sfv), no stdlib, no middleware, no connection state. The two List fields share one engine, `Fast::ProxyStatus::ListField`.
14
+
15
+ Pure Ruby; MIT license; source at `https://github.com/jetpks/fast-http-fields`.
16
+
17
+ ## Installation
18
+
19
+ ```sh
20
+ gem install fast-proxy-status
21
+ ```
22
+
23
+ In a Gemfile:
24
+
25
+ ```ruby
26
+ gem "fast-proxy-status"
27
+ ```
28
+
29
+ ```ruby
30
+ require "fast/proxy_status"
31
+ ```
32
+
33
+ Requires Ruby >= 3.4 and RubyGems >= 3.5.3; the only runtime requirement is fast-sfv.
34
+
35
+ ## Quickstart: Proxy-Status
36
+
37
+ Read a response's Proxy-Status and walk the chain of intermediaries:
38
+
39
+ ```ruby
40
+ require "fast/proxy_status"
41
+
42
+ field = Fast::ProxyStatus.parse("revproxy1.example.net, ExampleCDN")
43
+ field.first.value == "revproxy1.example.net" # => true
44
+ field.last.value == "ExampleCDN" # => true
45
+ ```
46
+
47
+ `first` is the intermediary closest to the origin server and `last` the one closest to the user agent; the member values are `Fast::SFV::Token`s — a String that equals it spells it.
48
+
49
+ The error lookup, §2.1.1's 504 example and the registry behind it:
50
+
51
+ ```ruby
52
+ field = Fast::ProxyStatus.parse("ExampleCDN; error=connection_timeout")
53
+ field.first.error # => #<Fast::SFV::Token "connection_timeout">
54
+ Fast::ProxyStatus.recommended_status("connection_timeout") # => 504
55
+ Fast::ProxyStatus.response_only?("connection_timeout") # => true
56
+ ```
57
+
58
+ A wrong-typed parameter is diagnostic data, never a field failure (§2.1.5's own quoted-string error example parses):
59
+
60
+ ```ruby
61
+ field = Fast::ProxyStatus.parse(%q{proxy.example.net; error="http_protocol_error"})
62
+ field.first.error # => nil
63
+ field.first["error"] # => "http_protocol_error"
64
+ ```
65
+
66
+ See the [tutorial](docs/tutorials/diagnostics-fields.md) for the guided path.
67
+
68
+ ## Cache-Status: how the caches handled the response
69
+
70
+ RFC 9211 §3's three-layer example — three field lines, combined per RFC 9651 §4.2 (a List concatenates):
71
+
72
+ ```ruby
73
+ field = Fast::CacheStatus.parse(
74
+ ["ReverseProxyCache; hit",
75
+ "ForwardProxyCache; fwd=uri-miss; collapsed; stored",
76
+ "BrowserCache; fwd=uri-miss"]
77
+ )
78
+ field.size # => 3
79
+ field.to_s # => "ReverseProxyCache;hit, ForwardProxyCache;fwd=uri-miss;collapsed;stored, BrowserCache;fwd=uri-miss"
80
+ ```
81
+
82
+ The three-line example's members read through the typed readers — `false` is a value, never an absence (§3's own `collapsed=?0` example):
83
+
84
+ ```ruby
85
+ field = Fast::CacheStatus.parse("ExampleCache; fwd=uri-miss; collapsed=?0")
86
+ field.first.fwd # => #<Fast::SFV::Token "uri-miss">
87
+ field.first.collapsed # => false
88
+ field.first.ttl # => nil
89
+ ```
90
+
91
+ The hit/fwd exclusivity is §2.1's SHOULD — "hit" and "fwd" are exclusive; only one of them should appear on each list member" — surfaced, never gated: `hit_and_fwd?` answers true exactly when a member carries both, and the field still parses, validates and round-trips:
92
+
93
+ ```ruby
94
+ field = Fast::CacheStatus.parse("ExampleCache; hit; fwd=miss")
95
+ field.first.hit_and_fwd? # => true
96
+ ```
97
+
98
+ The fwd value set is §2.2's own: `Fast::CacheStatus::FWD` pins the eight defined reasons, from most specific to least. The set is validated on emit only — §2.2 defines the values, it does not MUST them on a recipient — so an unregistered Token parses and round-trips untouched, while `Cache.new` refuses one:
99
+
100
+ ```ruby
101
+ Fast::CacheStatus.parse("ExampleCache; fwd=network-miss").first.fwd
102
+ # => #<Fast::SFV::Token "network-miss">
103
+
104
+ Fast::CacheStatus::FWD
105
+ # => ["bypass", "method", "uri-miss", "vary-miss", "miss", "request", "stale", "partial"]
106
+ ```
107
+
108
+ A cache appends itself as the LAST member — §2's "caches SHOULD preserve the existing field value" — the received members kept in order, the answer a new frozen value:
109
+
110
+ ```ruby
111
+ field = Fast::CacheStatus.append(
112
+ 'OriginCache; hit; ttl=1100, "CDN Company Here"; hit; ttl=545',
113
+ Fast::SFV::Token.new("MyCache"),
114
+ { "fwd" => Fast::SFV::Token.new("miss") }
115
+ )
116
+ field.to_s
117
+ # => "OriginCache;hit;ttl=1100, \"CDN Company Here\";hit;ttl=545, MyCache;fwd=miss"
118
+ ```
119
+
120
+ See the [monitoring how-to](docs/how-to-guides/read-cache-status-for-monitoring.md) and the [Cache-Status reference](docs/reference/cache-status.md).
121
+
122
+ ## Targeted Cache Control: directives aimed at one class of cache
123
+
124
+ RFC 9213's targeted fields are Dictionaries of cache directives — "a targeted field is an HTTP response header field that has the same semantics as the Cache-Control response header field ([HTTP-CACHING], Section 5.2). However, it has a distinct field name that indicates the target for its cache directives" — their values mapped into Structured Fields types by §2.1. The registered instance is `Fast::TargetedCacheControl::FIELD_NAME` ("CDN-Cache-Control", §3), and the five directives §2.2 MUSTs are pinned as frozen data in `DIRECTIVES` with unknown directives passing through untouched:
125
+
126
+ ```ruby
127
+ field = Fast::TargetedCacheControl.parse("max-age=600")
128
+ field["max-age"] # => 600
129
+ field.to_s # => "max-age=600"
130
+
131
+ inference = Fast::TargetedCacheControl.parse(%q{no-store, private="Foo, Bar", x-thing=foo, y=1.5})
132
+ inference["no-store"] # => true
133
+ inference["private"] # => "Foo, Bar"
134
+ inference["x-thing"] # => #<Fast::SFV::Token "foo">
135
+ inference["y"] # => 1.5
136
+ ```
137
+
138
+ The leniency is §2.1's own split, and it is the module's design core: an empty field or a parsing error is the entire field ignored — "that field MUST be ignored by the cache" — while a directive whose value violates the inferred constraints is PRESERVED and FLAGGED, because "Implementations SHOULD NOT consume values that violate these inferred constraints": `#violating` answers the SHOULD-NOT-consume set, the value stays in the data, and the field still round-trips:
139
+
140
+ ```ruby
141
+ field = Fast::TargetedCacheControl.parse("max-age=60.5")
142
+ field["max-age"] # => 60.5
143
+ field.violating # => ["max-age"]
144
+ field.to_s # => "max-age=60.5"
145
+ ```
146
+
147
+ Emit is always strict — §2.1: "Implementations MUST NOT generate values that violate these inferred constraints" — every value one of the five inferred types, a known directive carrying its section's type, keys Structured Fields keys. §2.1's digit-initial rule holds structurally: a plain String always emits quoted, and a Token's own grammar refuses the form at its construction, so a string whose first character is not alphabetic or `*` can never be mistaken for another type:
148
+
149
+ ```ruby
150
+ Fast::TargetedCacheControl.new({ "max-age" => 600, "no-store" => true }).to_s
151
+ # => "max-age=600, no-store"
152
+
153
+ Fast::TargetedCacheControl.new({ "x-custom" => "9abc" }).to_s # => "x-custom=\"9abc\""
154
+ ```
155
+
156
+ See the [emitting how-to](docs/how-to-guides/emit-cdn-cache-control.md) and the [Targeted Cache Control reference](docs/reference/targeted-cache-control.md).
157
+
158
+ ## The guarantees
159
+
160
+ `parse` never raises on field content. A field whose syntax is not a Structured Field Value, or whose List carries a member that is not a String or Token, is treated as absent — the entire field ignored (RFC 9651 §2.2: "When field-specific constraints are violated, the entire field is also ignored, unless the field definition defines other handling requirements"), the same shared frozen empty value an absent field gives. Parameters never fail the field: unknown parameters and known parameters of a type the field's own sections do not allow are preserved in the member's data and ignored semantically — the diagnostic leniency a debugging field demands. The TargetedCacheControl split is the one exception that is not a failure: a violating directive is preserved and flagged, the rest of the field stays readable. `parse!` is the strict truth: the first offender in List or Dictionary order raises `Fast::ProxyStatus::ParseError` (or the field's own, descending from it) positioned at the offending value's byte offset; `valid?` mirrors it with a boolean. `to_s` and construction are always strict: a sender never puts garbage on the wire, and the canonical form re-emits round-trip (the RFC's `; hit=` spacing canonicalizes to `;hit=`).
161
+
162
+ The value objects are frozen: the field, its `hops`/`caches` Array or `members` Hash, each member (an `Fast::SFV::Item` — the raw `#[]` and `#params` beside the typed readers), every parameter Hash and value String. A String is one field value; an Array is the field's lines, combined per RFC 9651 §4.2 — "parsers MUST combine all field lines in the same section (header or trailer) that case-insensitively match the field name into one comma-separated field-value" — a List concatenates (there is no last-wins); a Dictionary keeps the last value under the first position.
163
+
164
+ See [Why diagnostics preserve unknown members](docs/explanation/diagnostic-leniency.md).
165
+
166
+ ## Append yourself, the intermediary's operation
167
+
168
+ §2's "When adding a value to the Proxy-Status field, intermediaries SHOULD preserve the existing members of the field to allow debugging of the entire chain of intermediaries handling the request unless explicitly configured to remove them". `append` answers a new frozen value with this intermediary's member as the LAST — closest to the user agent — and re-emits the whole list canonically:
169
+
170
+ ```ruby
171
+ field = Fast::ProxyStatus.append("SomeOtherProxy", Fast::SFV::Token.new("ThisProxy"))
172
+ field.to_s # => "SomeOtherProxy, ThisProxy"
173
+ ```
174
+
175
+ The upstream value is parsed leniently — nil, a String, an Array of lines, or a held `Fast::ProxyStatus`; a malformed upstream field starts a fresh list rather than failing the response. `TypeError` for a wrong input type, `Fast::ProxyStatus::SerializeError` for a wrong hop or parameter. `Fast::CacheStatus.append` is the same operation for a cache, with §2.2's fwd set validated at the construction boundary.
176
+
177
+ See [Append your proxy to Proxy-Status](docs/how-to-guides/append-yourself-to-proxy-status.md).
178
+
179
+ ## The trailer story: append, then promote
180
+
181
+ §2 lets an intermediary send Proxy-Status as a trailer field when it has also generated a header field with the same member — "an intermediary MUST NOT send Proxy-Status as a trailer field unless it has also generated a Proxy-Status header field with the same member (although potentially different parameters) in that message" — the proxy appends itself to the header, then appends its parameters to the trailer. A client MAY promote the trailer into the header — `promote` is §2's algorithm, spec-exact:
182
+
183
+ ```ruby
184
+ promoted = Fast::ProxyStatus.promote("SomeOtherProxy, ThisProxy", "ThisProxy; error=read_timeout")
185
+ promoted.header.to_s # => "SomeOtherProxy, ThisProxy;error=read_timeout"
186
+ promoted.trailer # => nil
187
+ ```
188
+
189
+ Each trailer member replaces the leftmost header member that spells the same identifier — String or Token, character by character, parameters ignored — in its entirety; an unmatched member stays in the trailer, and the trailer is removed when nothing remains. The answer is a frozen `Fast::ProxyStatus::Promotion`: `header` is nil exactly when there was no header field, `trailer` exactly when nothing remains.
190
+
191
+ See [Promote a Proxy-Status trailer into the header](docs/how-to-guides/promote-a-proxy-status-trailer.md).
192
+
193
+ ## The registry as data
194
+
195
+ `Fast::ProxyStatus::ERRORS` carries §2.3's 32 proxy error types in document order, each with its recommended HTTP status code (an Integer — `http_request_error` and `proxy_internal_response` carry the RFC's own prose), its response-only flag, and its extra parameters. The lookups `registered?`, `recommended_status`, `response_only?` and `extra_parameters` answer unknown names with nil/false. The registry never gates a parse or an emit: an unknown error type parses, emits and round-trips untouched — future registrations never break old gem versions. The same rule holds for the two newer tables: `Fast::CacheStatus::PARAMETERS`/`FWD` and `Fast::TargetedCacheControl::DIRECTIVES` never gate a parse.
196
+
197
+ See [The proxy error type registry](docs/reference/proxy-error-types.md), [The cache directive table](docs/reference/cache-directives.md), and [Why diagnostics preserve unknown members](docs/explanation/diagnostic-leniency.md).
198
+
199
+ ## The error hierarchy
200
+
201
+ `Fast::ProxyStatus::Error` is the gem root, and the two newer modules descend from it — `Fast::CacheStatus::Error` and `Fast::TargetedCacheControl::Error`, each nesting its own `ParseError`/`SerializeError` — so one rescue guards the whole gem, both with `#offset`. The wrongness of a parse defect or a value that cannot go on the wire is field content; a wrong input type is a caller bug and raises plain `TypeError`.
202
+
203
+ See the three [reference pages](docs/reference/proxy-status.md).
204
+
205
+ ## Documentation
206
+
207
+ - Tutorial: [Your first diagnostics fields](docs/tutorials/diagnostics-fields.md) — one guided path across all three modules.
208
+ - How-to guides:
209
+ - [Append your proxy to Proxy-Status](docs/how-to-guides/append-yourself-to-proxy-status.md) — the intermediary's operation.
210
+ - [Promote a Proxy-Status trailer into the header](docs/how-to-guides/promote-a-proxy-status-trailer.md) — the client's operation.
211
+ - [Read Cache-Status for monitoring](docs/how-to-guides/read-cache-status-for-monitoring.md) — combine field lines and read the typed parameters.
212
+ - [Emit CDN-Cache-Control](docs/how-to-guides/emit-cdn-cache-control.md) — construct the Dictionary and handle the leniency.
213
+ - Reference:
214
+ - [Proxy-Status API reference](docs/reference/proxy-status.md) — the complete `Fast::ProxyStatus` surface.
215
+ - [Cache-Status API reference](docs/reference/cache-status.md) — the complete `Fast::CacheStatus` surface.
216
+ - [Targeted Cache Control API reference](docs/reference/targeted-cache-control.md) — the complete `Fast::TargetedCacheControl` surface.
217
+ - [The proxy error type registry](docs/reference/proxy-error-types.md) — RFC 9209 §2.3's 32 entries as data.
218
+ - [The cache directive table](docs/reference/cache-directives.md) — the five §2.2 MUST-implement directives.
219
+ - Explanation:
220
+ - [Why diagnostics preserve unknown members](docs/explanation/diagnostic-leniency.md) — the RFCs' leniency rules.
221
+ - [Targeted cache control and the age penalty](docs/explanation/age-penalty.md) — RFC 9213 §2.3's freshness interaction.
222
+ - [The Targeted Cache Control boundary](docs/explanation/the-targeted-cache-control-boundary.md) — what this class does not parse.
223
+ - [Performance and allocation budgets](docs/explanation/performance.md) — how the paths stay allocation-lean.
224
+
225
+ Current release 0.1.0; changelog at [CHANGELOG.md](CHANGELOG.md).