fast-priority 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 +44 -0
- data/README.md +107 -0
- data/lib/fast/priority/errors.rb +47 -0
- data/lib/fast/priority/field.rb +271 -0
- data/lib/fast/priority/h2.rb +123 -0
- data/lib/fast/priority/h3.rb +161 -0
- data/lib/fast/priority/parameters.rb +217 -0
- data/lib/fast/priority/version.rb +7 -0
- data/lib/fast/priority.rb +67 -0
- metadata +73 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: cd22d96a54ee71dc7863dc8f3f37f05a40f4c59c84074ae4d5c3a87b8b6f3fc7
|
|
4
|
+
data.tar.gz: 1443479c7c0e6b6576781b6752948751abe88996932d4d85b02bb840d301de23
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 02f7f391fdd9a09172540b6e894f3568cb0897b86eb6159c9e71ebfe1701126012174491e591218f8e51f4fd7979bd048cca92a3a100691b1833bfc276fbb2d2
|
|
7
|
+
data.tar.gz: 63565b8386cccbfe07da0878fa79eb5c34ef91231a57fe61277dc4cc8764ce122538797b32df70c6f8c9966923bf5d6da9cd2e0b6605e00973e6255cddb8d5e0
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.0 (2026-11-26)
|
|
4
|
+
|
|
5
|
+
- Field: `Fast::Priority.parse` / `parse!` / `valid?` — the RFC 9218 §4
|
|
6
|
+
recipient semantics over the Priority header field, an RFC 9651
|
|
7
|
+
Dictionary consumed through fast-sfv: the whole-field malformed and
|
|
8
|
+
absent cases answer the §4 defaults (urgency 3, incremental false,
|
|
9
|
+
no member held), the member-level MUST-ignore filter drops
|
|
10
|
+
out-of-range and wrong-typed `u`/`i` while unknown parameters pass
|
|
11
|
+
through as parsed, the strict parse wraps the Dictionary failure
|
|
12
|
+
with its message and byte offset and points §4 semantic errors at
|
|
13
|
+
the effective member's value, and `valid?` mirrors it with a
|
|
14
|
+
boolean.
|
|
15
|
+
- Value object: `Fast::Priority::Parameters` — the frozen holder of
|
|
16
|
+
the effective members with `urgency`/`incremental`/`unknown`/
|
|
17
|
+
`members` readers, the canonical `to_s`, member-absence semantics,
|
|
18
|
+
construction validation, canonical-wire-form equality, and the §8
|
|
19
|
+
merge helper (each known parameter the other holds wins, its
|
|
20
|
+
omissions keep self's).
|
|
21
|
+
- Frames: `Fast::Priority::H2` and `Fast::Priority::H3` — the §7.1 and
|
|
22
|
+
§7.2 PRIORITY_UPDATE payload codecs, byte-exact in both directions
|
|
23
|
+
(the Reserved bit masked on h2 receipt, the RFC 9000 §16 minimal
|
|
24
|
+
varint on h3 encode and the Appendix A.1 algorithm on decode, any
|
|
25
|
+
form), with the §7 field-value parse failure surfaced on the decode
|
|
26
|
+
result for the caller's connection-error MAY.
|
|
27
|
+
- Interop: the gem's own bundle (`gems.rb`) wires protocol-http2 0.29.2
|
|
28
|
+
as a test-group peer staging offline from the gitignored root cache,
|
|
29
|
+
exercising both directions against their framing stack (the h3 side
|
|
30
|
+
has no usable Ruby peer — the ecosystem's HTTP/3 gems are native
|
|
31
|
+
extensions without a frame API — so it is verified against the RFC
|
|
32
|
+
vectors alone).
|
|
33
|
+
- Scaffold: the gemspec (one runtime dependency, fast-sfv), the
|
|
34
|
+
README front door, the allocation budgets for the eight paths, and
|
|
35
|
+
this changelog. The documentation pass and this 0.1.0 release land
|
|
36
|
+
together (docs are part of the product — the family's release rule).
|
|
37
|
+
- Docs: the diátaxis docs tree under `docs/` (tutorial, four how-to
|
|
38
|
+
guides, two reference pages, three explanations), the README front
|
|
39
|
+
door with the documentation index, and the both-mode performance page
|
|
40
|
+
from one measurement session (interpreter and YJIT side by side, the
|
|
41
|
+
benchmark kit alongside). Every fenced ruby example was executed as
|
|
42
|
+
printed and every RFC citation checked against the RFC text by the
|
|
43
|
+
pipeline's independent review. The gemspec metadata completes with
|
|
44
|
+
`documentation_uri` pointing at the docs tree.
|
data/README.md
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# fast-priority
|
|
2
|
+
|
|
3
|
+
fast-priority is the RFC 9218 Extensible Priorities implementation for Ruby: the Priority HTTP field and the HTTP/2/HTTP/3 PRIORITY_UPDATE frame payloads, built on fast-sfv.
|
|
4
|
+
|
|
5
|
+
## What it is
|
|
6
|
+
|
|
7
|
+
Implements RFC 9218: the Priority header field (§4–§5) as an RFC 9651 Dictionary consumer — urgency (`u`), incremental (`i`), unknown-parameter passthrough — plus the PRIORITY_UPDATE frame payload codecs for HTTP/2 (§7.1) and HTTP/3 (§7.2) (`lib/fast/priority.rb:18-21,46-53`).
|
|
8
|
+
|
|
9
|
+
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 the frame encoders are always strict; results are frozen value objects; one runtime dependency (fast-sfv) (`lib/fast/priority.rb:24-45`; `fast-priority.gemspec:47`).
|
|
10
|
+
|
|
11
|
+
Pure Ruby, byte-exact binary codecs, no connection or stream state (`lib/fast/priority.rb:55,64`).
|
|
12
|
+
|
|
13
|
+
MIT license; source at `https://github.com/jetpks/fast-http-fields` (`fast-priority.gemspec:19-20`).
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
gem install fast-priority
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
In a Gemfile:
|
|
22
|
+
|
|
23
|
+
```ruby
|
|
24
|
+
gem "fast-priority"
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Require path is `fast/priority`:
|
|
28
|
+
|
|
29
|
+
```ruby
|
|
30
|
+
require "fast/priority"
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Requires Ruby >= 3.4 and RubyGems >= 3.5.3; the only runtime requirement is fast-sfv (`fast-priority.gemspec:22-23,47`).
|
|
34
|
+
|
|
35
|
+
## Quickstart
|
|
36
|
+
|
|
37
|
+
Parse a Priority field and read it:
|
|
38
|
+
|
|
39
|
+
```ruby
|
|
40
|
+
require "fast/priority"
|
|
41
|
+
|
|
42
|
+
priority = Fast::Priority.parse("u=5, i")
|
|
43
|
+
priority.urgency # => 5
|
|
44
|
+
priority.incremental # => true
|
|
45
|
+
priority.to_s # => "u=5, i"
|
|
46
|
+
|
|
47
|
+
Fast::Priority.parse("!!").urgency # => 3
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
The malformed line never raises — a recipient that cannot read the field answers the §4 defaults (urgency 3, incremental false).
|
|
51
|
+
|
|
52
|
+
An HTTP/2 PRIORITY_UPDATE payload, both directions:
|
|
53
|
+
|
|
54
|
+
```ruby
|
|
55
|
+
require "fast/priority"
|
|
56
|
+
|
|
57
|
+
payload = Fast::Priority::H2.encode_payload(5, Fast::Priority.parse("u=1, i"))
|
|
58
|
+
payload.unpack1("H*") # => "00000005753d312c2069"
|
|
59
|
+
|
|
60
|
+
frame = Fast::Priority::H2.decode_payload(payload)
|
|
61
|
+
frame.stream_id # => 5
|
|
62
|
+
frame.field_value # => "u=1, i"
|
|
63
|
+
frame.error # => nil
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Next: the [tutorial](docs/tutorials/priority-field.md) for the guided path.
|
|
67
|
+
|
|
68
|
+
## The recipient defaults
|
|
69
|
+
|
|
70
|
+
- A server that receives no priority parameters acts as if the defaults were specified: urgency 3, incremental false (§4; RFC 9218 §4.1 "The default is 3" at `rfc9218.txt:350`; §4.2 at 383).
|
|
71
|
+
- A malformed whole field answers the same defaults — the recipient ignores what it cannot read — while a parsed field is filtered member by member: out-of-range or wrong-typed `u`/`i` is ignored, unknown parameters pass through untouched (§4's MUST-ignore, `rfc9218.txt:340-344`).
|
|
72
|
+
- A member's absence is visible on the value object rather than folded into a default (§7's omission-is-default signal and §8's response omission semantics, `rfc9218.txt:523-524,694-696`).
|
|
73
|
+
|
|
74
|
+
See [Handle malformed or absent Priority](docs/how-to-guides/handle-malformed-priority.md) and [Why malformed input answers the defaults](docs/explanation/defaults-on-malformed.md).
|
|
75
|
+
|
|
76
|
+
## Frame payloads, not frames
|
|
77
|
+
|
|
78
|
+
- The h2 and h3 codecs start and end at the PRIORITY_UPDATE payload: the 4-byte Reserved+Prioritized Stream ID word plus the field value for HTTP/2, the QUIC varint Prioritized Element ID plus the field value for HTTP/3 (`lib/fast/priority/h2.rb:16-22`; `lib/fast/priority/h3.rb:11-17`).
|
|
79
|
+
- Frame headers, stream-state rules, control-stream placement, and SETTINGS_NO_RFC7540_PRIORITIES are the transport's: this gem keeps no connection or stream state (`lib/fast/priority.rb:56-58`).
|
|
80
|
+
|
|
81
|
+
See [Frame payload codecs](docs/reference/frames.md) and [How the priority scheme binds to HTTP/2 and HTTP/3](docs/explanation/h2-h3-bindings.md).
|
|
82
|
+
|
|
83
|
+
## Documentation
|
|
84
|
+
|
|
85
|
+
Current release 0.1.0; changelog at [CHANGELOG.md](CHANGELOG.md).
|
|
86
|
+
|
|
87
|
+
### Tutorial
|
|
88
|
+
|
|
89
|
+
- [Your first Priority field](docs/tutorials/priority-field.md) — One guided path: parse a request's priority, read it, meet the unknown passthrough, build and emit a value, meet the defaults and the strict error.
|
|
90
|
+
|
|
91
|
+
### How-to guides
|
|
92
|
+
|
|
93
|
+
- [Parse request priority in Rack](docs/how-to-guides/parse-request-priority.md) — Read a Priority header in a Rack-shaped env, parse it, handle absence and multiple lines, know what to schedule on.
|
|
94
|
+
- [Emit Priority from a server](docs/how-to-guides/emit-priority-from-a-server.md) — Build a Parameters, emit the canonical field, merge client and server signals per §8, set the response header.
|
|
95
|
+
- [Reprioritize with PRIORITY_UPDATE frames](docs/how-to-guides/reprioritize-with-priority-update.md) — Build, send, and decode h2 PRIORITY_UPDATE payloads with protocol-http2; the h3 side with hand-built payloads.
|
|
96
|
+
- [Handle malformed or absent Priority](docs/how-to-guides/handle-malformed-priority.md) — What malformed and absent input answer, member-level ignoring, the strict view, and the frame payload's error signal.
|
|
97
|
+
|
|
98
|
+
### Reference
|
|
99
|
+
|
|
100
|
+
- [API reference](docs/reference/api.md) — The complete field surface: every method, class, error, input/output shape, with signatures and return types.
|
|
101
|
+
- [Frame payload codecs](docs/reference/frames.md) — The frame payload codecs: constants, byte layouts, the Frame results, the varint, the error contract, and how the codecs are verified.
|
|
102
|
+
|
|
103
|
+
### Explanation
|
|
104
|
+
|
|
105
|
+
- [Why malformed input answers the defaults](docs/explanation/defaults-on-malformed.md) — Why §4's recipient contract answers defaults on malformed input, why absence stays visible, and the three-failure taxonomy.
|
|
106
|
+
- [How the priority scheme binds to HTTP/2 and HTTP/3](docs/explanation/h2-h3-bindings.md) — Why the scheme exists (replacing RFC 7540 priorities), the SETTINGS_NO_RFC7540_PRIORITIES story, end-to-end vs hop-by-hop, and the transport-owned boundary.
|
|
107
|
+
- [Performance and allocation budgets](docs/explanation/performance.md) — How the paths stay allocation-lean; the both-mode benchmark tables and the allocation budgets, with methodology and caveats.
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Fast
|
|
4
|
+
module Priority
|
|
5
|
+
# The family root: every Fast::Priority failure descends from
|
|
6
|
+
# Error, so a caller can guard the whole surface with one rescue.
|
|
7
|
+
class Error < StandardError; end
|
|
8
|
+
|
|
9
|
+
# A priority signal this gem cannot read. Two shapes meet here:
|
|
10
|
+
# the underlying RFC 9651 Dictionary failed to parse — the failure
|
|
11
|
+
# is wrapped with its message and byte offset preserved — or, under
|
|
12
|
+
# the strict parse, a member broke a RFC 9218 §4 parameter rule
|
|
13
|
+
# ("u" not an Integer 0..7, "i" not a Boolean), and #offset is the
|
|
14
|
+
# byte position of that member's VALUE in the combined field value.
|
|
15
|
+
# The frame payload codecs use the same class for payloads too
|
|
16
|
+
# short to carry their fixed prefix. A wrong input type is a caller
|
|
17
|
+
# bug, not field content: it raises plain TypeError and never this
|
|
18
|
+
# class. The lenient parse never raises it — §4's recipients are
|
|
19
|
+
# told to ignore what they cannot read.
|
|
20
|
+
class ParseError < Error
|
|
21
|
+
attr_reader :offset
|
|
22
|
+
|
|
23
|
+
def initialize(message, offset)
|
|
24
|
+
super(message)
|
|
25
|
+
@offset = offset
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# A value cannot go on the wire: the Parameters under construction,
|
|
30
|
+
# or the stream or element identifier handed to a frame encoder,
|
|
31
|
+
# breaks a bound the RFC states (§4.1's Integer 0..7, §4.2's
|
|
32
|
+
# Boolean, §7.1's 31-bit stream identifier, §7.2's 62-bit element
|
|
33
|
+
# identifier). #offset is the byte position in the output built so
|
|
34
|
+
# far — 0, because the check runs before anything is emitted; the
|
|
35
|
+
# whole emit path raises this class and never TypeError: on emit
|
|
36
|
+
# the wrongness is in the value being sent, which is field content,
|
|
37
|
+
# not a caller protocol bug.
|
|
38
|
+
class SerializeError < Error
|
|
39
|
+
attr_reader :offset
|
|
40
|
+
|
|
41
|
+
def initialize(message, offset)
|
|
42
|
+
super(message)
|
|
43
|
+
@offset = offset
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Fast
|
|
4
|
+
module Priority
|
|
5
|
+
# The strict parse's positioning walk: where each known member's
|
|
6
|
+
# VALUE sits in the combined field value — the byte offset a §4
|
|
7
|
+
# semantic ParseError points at. The field value already parsed
|
|
8
|
+
# when this runs, so the walk only tracks the boundaries: members
|
|
9
|
+
# split on commas at inner-list depth zero outside quoted strings
|
|
10
|
+
# (RFC 9651 §4.2's separators), a quoted string's commas and
|
|
11
|
+
# parens do not count (§4.2.5's escapes are skipped two bytes), and
|
|
12
|
+
# a member without "=" — the implicit Boolean true — offsets at its
|
|
13
|
+
# key's end, where its value would be written.
|
|
14
|
+
class Scan
|
|
15
|
+
# A 256-entry byte class: true for every byte of +spec+ — the
|
|
16
|
+
# byte-table shape Fast::SFV's own scanners walk with.
|
|
17
|
+
def self.byte_class(spec)
|
|
18
|
+
table = Array.new(256, false)
|
|
19
|
+
spec.each_byte { |byte| table[byte] = true }
|
|
20
|
+
table.freeze
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# A Dictionary key's bytes (RFC 9651 §4.2.3.3: lcalpha, DIGIT,
|
|
24
|
+
# "_", "-", "." and "*"). The first-byte restriction never
|
|
25
|
+
# matters here — the field value already parsed.
|
|
26
|
+
KEY = byte_class("abcdefghijklmnopqrstuvwxyz0123456789_-.*")
|
|
27
|
+
# The optional whitespace around a member separator (§4.2.1).
|
|
28
|
+
OWS = byte_class(" \t")
|
|
29
|
+
|
|
30
|
+
COMMA = 0x2C
|
|
31
|
+
EQUALS = 0x3D
|
|
32
|
+
LPAREN = 0x28
|
|
33
|
+
RPAREN = 0x29
|
|
34
|
+
DQUOTE = 0x22
|
|
35
|
+
BACKSLASH = 0x5C
|
|
36
|
+
# The two keys this gem interprets (RFC 9218 §4.1 and §4.2).
|
|
37
|
+
U_KEY = 0x75
|
|
38
|
+
I_KEY = 0x69
|
|
39
|
+
|
|
40
|
+
def initialize(combined)
|
|
41
|
+
@combined = combined
|
|
42
|
+
@size = combined.bytesize
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# One walk answers the offsets of the "u" and "i" members'
|
|
46
|
+
# values — the LAST one seen for each, because Fast::SFV applies
|
|
47
|
+
# §4.2's last-wins before the check reads these offsets, so the
|
|
48
|
+
# effective member is the one its value came from.
|
|
49
|
+
def offsets
|
|
50
|
+
@offsets = {}
|
|
51
|
+
index = 0
|
|
52
|
+
index = member_at(index) while index < @size
|
|
53
|
+
@offsets
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
private
|
|
57
|
+
|
|
58
|
+
# Walks one member from +index+ and answers the index the next
|
|
59
|
+
# member starts at: leading OWS, the key, the value — its offset
|
|
60
|
+
# recorded when the member is "u" or "i" — then the separator.
|
|
61
|
+
# A member that is not followed by a comma ends the walk.
|
|
62
|
+
def member_at(index)
|
|
63
|
+
index += 1 while index < @size && OWS[@combined.getbyte(index)]
|
|
64
|
+
key_start = index
|
|
65
|
+
index += 1 while index < @size && KEY[@combined.getbyte(index)]
|
|
66
|
+
key_end = index
|
|
67
|
+
value_offset = index
|
|
68
|
+
if @combined.getbyte(index) == EQUALS
|
|
69
|
+
index += 1
|
|
70
|
+
value_offset = index
|
|
71
|
+
index = value_end(index)
|
|
72
|
+
end
|
|
73
|
+
record(key_start, key_end - key_start, value_offset)
|
|
74
|
+
index += 1 while index < @size && OWS[@combined.getbyte(index)]
|
|
75
|
+
return @size unless index < @size && @combined.getbyte(index) == COMMA
|
|
76
|
+
|
|
77
|
+
index + 1
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# Records a known member's value offset: a one-byte key that
|
|
81
|
+
# spells "u" or "i" (RFC 9218's two registered single-character
|
|
82
|
+
# parameters — anything longer is an unknown member).
|
|
83
|
+
def record(key_start, key_size, value_offset)
|
|
84
|
+
return unless key_size == 1
|
|
85
|
+
|
|
86
|
+
case @combined.getbyte(key_start)
|
|
87
|
+
when U_KEY then @offsets["u"] = value_offset
|
|
88
|
+
when I_KEY then @offsets["i"] = value_offset
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# The index just past the member VALUE starting at +index+:
|
|
93
|
+
# quoted strings (§4.2.5 — a DQUOTE opens, a backslash escapes
|
|
94
|
+
# the next byte, a DQUOTE closes) and inner-list depth
|
|
95
|
+
# (§4.2.1.1's parens) are tracked, and a comma at depth zero
|
|
96
|
+
# outside a string — the §4.2 member boundary — stops the walk.
|
|
97
|
+
def value_end(index)
|
|
98
|
+
depth = 0
|
|
99
|
+
quoted = false
|
|
100
|
+
while index < @size
|
|
101
|
+
byte = @combined.getbyte(index)
|
|
102
|
+
if quoted
|
|
103
|
+
index += byte == BACKSLASH ? 2 : 1
|
|
104
|
+
quoted = false if byte == DQUOTE
|
|
105
|
+
else
|
|
106
|
+
case byte
|
|
107
|
+
when DQUOTE then quoted = true
|
|
108
|
+
when LPAREN then depth += 1
|
|
109
|
+
when RPAREN then depth -= 1
|
|
110
|
+
when COMMA then return index if depth.zero?
|
|
111
|
+
end
|
|
112
|
+
index += 1
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
index
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
private_constant :Scan
|
|
119
|
+
|
|
120
|
+
# The RFC 9218 §4 recipient semantics over one field value: one
|
|
121
|
+
# RFC 9651 Dictionary parse through Fast::SFV ("Receivers parse the
|
|
122
|
+
# Dictionary as described in Section 4.2 of [STRUCTURED-FIELDS]"),
|
|
123
|
+
# the §4 member filter over what parsed, and — when the whole field
|
|
124
|
+
# was malformed — the wrapped failure the frame decoders surface as
|
|
125
|
+
# §7's connection-error signal. The lenient answer never raises on
|
|
126
|
+
# field content: the filter drops what §4's MUST-ignore names, and
|
|
127
|
+
# the failure becomes the defaults.
|
|
128
|
+
class Field
|
|
129
|
+
# The held members when the whole field was malformed: none —
|
|
130
|
+
# the §4 defaults, the same members an absent field answers.
|
|
131
|
+
EMPTY_MEMBERS = {}.freeze
|
|
132
|
+
|
|
133
|
+
def initialize(input)
|
|
134
|
+
@error = nil
|
|
135
|
+
begin
|
|
136
|
+
@members = filter(SFV.parse_dict(input))
|
|
137
|
+
rescue SFV::ParseError => e
|
|
138
|
+
@members = EMPTY_MEMBERS
|
|
139
|
+
@error = ParseError.new(e.message, e.offset)
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
# The wrapped RFC 9651 failure, nil when the field parsed. This
|
|
144
|
+
# is §7's signal for the CALLER: "Failure to parse the Priority
|
|
145
|
+
# Field Value MAY be treated as a connection error" — the caller
|
|
146
|
+
# owns that MAY.
|
|
147
|
+
attr_reader :error
|
|
148
|
+
|
|
149
|
+
# The §4 view of the field: the Parameters the recipient reads.
|
|
150
|
+
def parameters
|
|
151
|
+
Parameters.send(:from_members, @members)
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
private
|
|
155
|
+
|
|
156
|
+
# §4's filter over a parsed Dictionary: "unknown priority
|
|
157
|
+
# parameters, priority parameters with out-of-range values, or
|
|
158
|
+
# values of unexpected types MUST be ignored" — unknown members
|
|
159
|
+
# pass through as parsed, an out-of-range or wrong-typed "u" or
|
|
160
|
+
# "i" is dropped (never re-emitted), the rest are kept. The
|
|
161
|
+
# parsed Hash is fresh and frozen, so it IS the members Hash when
|
|
162
|
+
# nothing is ignored; a filtered copy is built only when a member
|
|
163
|
+
# drops.
|
|
164
|
+
def filter(dict)
|
|
165
|
+
return dict unless dict.any? { |key, value| ignored?(key, value) }
|
|
166
|
+
|
|
167
|
+
dict.reject { |key, value| ignored?(key, value) }.freeze
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
# §4's ignore test for one member: "u" survives only as an Item
|
|
171
|
+
# holding an Integer 0..7 (§4.1), "i" only as an Item holding a
|
|
172
|
+
# Boolean (§4.2); every other member is unknown-parameter
|
|
173
|
+
# passthrough (§4.3's extension design) and always kept.
|
|
174
|
+
def ignored?(key, value)
|
|
175
|
+
case key
|
|
176
|
+
when "u"
|
|
177
|
+
!(value.is_a?(Item) && value.value.is_a?(Integer) && value.value.between?(0, 7))
|
|
178
|
+
when "i"
|
|
179
|
+
!(value.is_a?(Item) && (value.value.equal?(true) || value.value.equal?(false)))
|
|
180
|
+
else
|
|
181
|
+
false
|
|
182
|
+
end
|
|
183
|
+
end
|
|
184
|
+
end
|
|
185
|
+
private_constant :Field
|
|
186
|
+
|
|
187
|
+
module_function
|
|
188
|
+
|
|
189
|
+
# RFC 9218 §4, the recipient semantics — never raises on field
|
|
190
|
+
# content. A malformed or absent field answers the defaults object
|
|
191
|
+
# (urgency 3, incremental false, no member held, to_s ""); a parsed
|
|
192
|
+
# Dictionary is filtered member by member (§4's MUST-ignore on the
|
|
193
|
+
# "u"/"i" members, unknown members kept as parsed). A String is one
|
|
194
|
+
# field value; an Array is the field's lines, combined with ", "
|
|
195
|
+
# (RFC 9651 §4.2). A non-String, non-Array input is a caller bug
|
|
196
|
+
# and raises TypeError.
|
|
197
|
+
def parse(input)
|
|
198
|
+
Field.new(input).parameters
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
# Strict truth. The RFC 9651 Dictionary failure is wrapped with its
|
|
202
|
+
# message and byte offset preserved; then §4 inverted on the
|
|
203
|
+
# effective (last-wins) members — a held "u" that is not an Item
|
|
204
|
+
# with an Integer 0..7 (§4.1), or a held "i" that is not an Item
|
|
205
|
+
# with a Boolean (§4.2), raises ParseError pointing at the member's
|
|
206
|
+
# VALUE in the combined field value (a member without "=", the
|
|
207
|
+
# implicit Boolean true, at its key's end). Unknown members are
|
|
208
|
+
# valid input (§4.3) and pass through untouched. TypeError for a
|
|
209
|
+
# wrong input type, as parse.
|
|
210
|
+
def parse!(input)
|
|
211
|
+
dict = SFV.parse_dict(input)
|
|
212
|
+
check_members!(dict, combined(input))
|
|
213
|
+
Parameters.send(:from_members, dict)
|
|
214
|
+
rescue SFV::ParseError => e
|
|
215
|
+
raise ParseError.new(e.message, e.offset)
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
# The boolean mirror: true exactly when parse! returns — false for
|
|
219
|
+
# malformed content, for §4 semantic violations, and for a wrong
|
|
220
|
+
# input type alike (a caller bug is not field content either).
|
|
221
|
+
def valid?(input)
|
|
222
|
+
parse!(input)
|
|
223
|
+
true
|
|
224
|
+
rescue TypeError, ParseError
|
|
225
|
+
false
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
# The field value the scan positions into: the lines joined with
|
|
229
|
+
# ", " — the same RFC 9651 §4.2 combining Fast::SFV.parse_dict
|
|
230
|
+
# applies — a String as given.
|
|
231
|
+
def combined(input)
|
|
232
|
+
input.is_a?(Array) ? input.join(", ") : input
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
# §4 inverted on the effective members: the two known parameters
|
|
236
|
+
# checked, each error pointed at the value the walk found.
|
|
237
|
+
def check_members!(dict, field_value)
|
|
238
|
+
offsets = Scan.new(field_value).offsets
|
|
239
|
+
urgency = dict["u"]
|
|
240
|
+
check_urgency!(urgency, offsets["u"]) if urgency
|
|
241
|
+
incremental = dict["i"]
|
|
242
|
+
check_incremental!(incremental, offsets["i"]) if incremental
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
# §4.1, inverted: a held "u" member is an Item carrying an Integer
|
|
246
|
+
# 0..7.
|
|
247
|
+
def check_urgency!(member, offset)
|
|
248
|
+
return if member.is_a?(Item) && member.value.is_a?(Integer) && member.value.between?(0, 7)
|
|
249
|
+
|
|
250
|
+
raise ParseError.new("urgency is an Integer 0..7 (got #{shown(member)})", offset)
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
# §4.2, inverted: a held "i" member is an Item carrying a Boolean
|
|
254
|
+
# — one of the two literals, the identity check the emit path's own
|
|
255
|
+
# short-form test uses, so nothing allocates per member.
|
|
256
|
+
def check_incremental!(member, offset)
|
|
257
|
+
return if member.is_a?(Item) && (member.value.equal?(true) || member.value.equal?(false))
|
|
258
|
+
|
|
259
|
+
raise ParseError.new("incremental is a Boolean (got #{shown(member)})", offset)
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
# What a semantic error shows of the offending member: the bare
|
|
263
|
+
# value when it is an Item (out of range, or an alien type), the
|
|
264
|
+
# member's class when it is not an Item at all — an inner list.
|
|
265
|
+
def shown(member)
|
|
266
|
+
member.is_a?(Item) ? member.value : member.class
|
|
267
|
+
end
|
|
268
|
+
|
|
269
|
+
private_class_method :combined, :check_members!, :check_urgency!, :check_incremental!, :shown
|
|
270
|
+
end
|
|
271
|
+
end
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Fast
|
|
4
|
+
module Priority
|
|
5
|
+
# RFC 9218 §7.1: the HTTP/2 PRIORITY_UPDATE frame payload codec.
|
|
6
|
+
# FRAME_TYPE is 0x10, the frame header's type byte — the constant a
|
|
7
|
+
# caller (or an interop peer's frame registry) cites.
|
|
8
|
+
#
|
|
9
|
+
# The payload is Figure 1's tail: the Reserved(1)+Prioritized
|
|
10
|
+
# Stream ID(31) as one big-endian 32-bit word — the Reserved bit
|
|
11
|
+
# written 0 on send and masked off on receipt, leaving §7.1's
|
|
12
|
+
# "A 31-bit stream identifier for the stream that is the target of
|
|
13
|
+
# the priority update" — then the Priority Field Value, "the
|
|
14
|
+
# priority update value in ASCII text, ... the same representation
|
|
15
|
+
# as the Priority header field value".
|
|
16
|
+
#
|
|
17
|
+
# The frame header is the transport's: the 9-byte Length, Type,
|
|
18
|
+
# Unused Flags, and the header Stream Identifier §7.1 says MUST be
|
|
19
|
+
# zero. The stream-state rules are the caller's too, and they are
|
|
20
|
+
# connection policy this gem keeps no state for: a prioritized
|
|
21
|
+
# stream ID of 0x0 is a connection error of type PROTOCOL_ERROR;
|
|
22
|
+
# the open, half-closed (local) or idle expectations for request
|
|
23
|
+
# streams, the reserved (remote) or half-closed (local) ones for
|
|
24
|
+
# push streams, and the servers-MUST-NOT-send rule are all §7.1.
|
|
25
|
+
# The SETTINGS_NO_RFC7540_PRIORITIES setting (§2.1, 0x9) is the h2
|
|
26
|
+
# peer's way to disable RFC 7540 priority signals: sent in the
|
|
27
|
+
# first SETTINGS frame, value 0 or 1, unchanged after; a client
|
|
28
|
+
# value of 1 means the client ignores h2 priority signals, a
|
|
29
|
+
# server value that the server will. A SETTINGS frame belongs to
|
|
30
|
+
# the connection, so it is documentation here, never behavior.
|
|
31
|
+
module H2
|
|
32
|
+
# §7.1: "The HTTP/2 PRIORITY_UPDATE frame (type=0x10)".
|
|
33
|
+
FRAME_TYPE = 0x10
|
|
34
|
+
# §7.1: "Prioritized Stream ID: A 31-bit stream identifier" —
|
|
35
|
+
# the field's bound, and the mask that strips Figure 1's Reserved
|
|
36
|
+
# bit off the received word.
|
|
37
|
+
MAX_STREAM_ID = 0x7FFF_FFFF
|
|
38
|
+
# The payload's fixed prefix: the 4-byte Reserved+ID word.
|
|
39
|
+
PREFIX_SIZE = 4
|
|
40
|
+
|
|
41
|
+
# The decode result. #stream_id is the 31-bit prioritized stream
|
|
42
|
+
# identifier (the Reserved bit masked off); #field_value the raw
|
|
43
|
+
# Priority Field Value, ASCII-8BIT — the payload's bytes whatever
|
|
44
|
+
# tag the input String carried; #priority the §4 recipient parse
|
|
45
|
+
# of the field value; #error §7's signal for the CALLER: "Failure
|
|
46
|
+
# to parse the Priority Field Value MAY be treated as a
|
|
47
|
+
# connection error. In HTTP/2, the error is of type
|
|
48
|
+
# PROTOCOL_ERROR" — the wrapped RFC 9651 Dictionary failure, nil
|
|
49
|
+
# when the value parsed. A parsed-but-ignored member is not a
|
|
50
|
+
# failure (§4's MUST-ignore governs), so #priority is the
|
|
51
|
+
# defaults only when #error is set. Frozen, like every family
|
|
52
|
+
# result.
|
|
53
|
+
class Frame
|
|
54
|
+
attr_reader :stream_id, :field_value, :priority, :error
|
|
55
|
+
|
|
56
|
+
def initialize(stream_id, field_value, priority, error)
|
|
57
|
+
@stream_id = stream_id
|
|
58
|
+
@field_value = field_value
|
|
59
|
+
@priority = priority
|
|
60
|
+
@error = error
|
|
61
|
+
freeze
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
module_function
|
|
66
|
+
|
|
67
|
+
# The §7.1 payload as an ASCII-8BIT String: the 4-byte word —
|
|
68
|
+
# Reserved bit 0 — then the Parameters' canonical to_s. Strict,
|
|
69
|
+
# like every emit: a value that is not a Parameters (a bare
|
|
70
|
+
# String, say) raises SerializeError, because no unvalidated
|
|
71
|
+
# value reaches the wire, and so does an identifier outside the
|
|
72
|
+
# 31-bit field.
|
|
73
|
+
def encode_payload(stream_id, priority)
|
|
74
|
+
check_priority(priority)
|
|
75
|
+
check_stream_id(stream_id)
|
|
76
|
+
([stream_id].pack("N") << priority.to_s)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# The §7.1 payload decoded: the 31-bit stream identifier with the
|
|
80
|
+
# Reserved bit masked off, the raw field value, the §4 parse of
|
|
81
|
+
# it, and the §7 error. Never raises on field content; TypeError
|
|
82
|
+
# for a non-String payload; a payload shorter than the 4-byte
|
|
83
|
+
# word raises ParseError at the payload's byte size — where the
|
|
84
|
+
# scan stopped.
|
|
85
|
+
def decode_payload(payload)
|
|
86
|
+
check_payload(payload)
|
|
87
|
+
size = payload.bytesize
|
|
88
|
+
raise ParseError.new("the payload carries a 4-byte prioritized stream ID word", size) if size < PREFIX_SIZE
|
|
89
|
+
|
|
90
|
+
stream_id = payload.unpack1("N") & MAX_STREAM_ID
|
|
91
|
+
field_value = payload.byteslice(PREFIX_SIZE, size - PREFIX_SIZE)
|
|
92
|
+
field_value.force_encoding(Encoding::ASCII_8BIT)
|
|
93
|
+
field = Field.new(field_value)
|
|
94
|
+
Frame.new(stream_id, field_value, field.parameters, field.error)
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# The emit path never raises TypeError: the wrongness is in the
|
|
98
|
+
# value being sent.
|
|
99
|
+
def check_priority(priority)
|
|
100
|
+
return if priority.is_a?(Parameters)
|
|
101
|
+
|
|
102
|
+
raise SerializeError.new("a Fast::Priority::Parameters is required (got #{priority.class})", 0)
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# §7.1's field bound.
|
|
106
|
+
def check_stream_id(stream_id)
|
|
107
|
+
return if stream_id.is_a?(Integer) && stream_id.between?(0, MAX_STREAM_ID)
|
|
108
|
+
|
|
109
|
+
raise SerializeError.new("the prioritized stream ID is an Integer 0..0x7FFFFFFF (got #{stream_id.inspect})", 0)
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# The payload is a binary String read bytewise; anything else is
|
|
113
|
+
# a caller bug.
|
|
114
|
+
def check_payload(payload)
|
|
115
|
+
return if payload.is_a?(String)
|
|
116
|
+
|
|
117
|
+
raise TypeError, "a binary payload String is required (got #{payload.class})"
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
private_class_method :check_priority, :check_stream_id, :check_payload
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
end
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Fast
|
|
4
|
+
module Priority
|
|
5
|
+
# RFC 9218 §7.2: the HTTP/3 PRIORITY_UPDATE frame payload codec.
|
|
6
|
+
# REQUEST_TYPE (0xF0700, request streams) and PUSH_TYPE (0xF0701,
|
|
7
|
+
# push streams) are the frame header's type field — the constants a
|
|
8
|
+
# caller cites. The type is itself a QUIC variable-length integer
|
|
9
|
+
# on the wire (0xF0700 = 984576, a 4-byte form, spelled 8f 07 00),
|
|
10
|
+
# and it lives in the frame header, never in this payload.
|
|
11
|
+
#
|
|
12
|
+
# The payload is Figure 2's tail: the Prioritized Element ID — "The
|
|
13
|
+
# stream ID or push ID that is the target of the priority update" —
|
|
14
|
+
# as a QUIC variable-length integer (RFC 9000 §16), then the
|
|
15
|
+
# Priority Field Value, "the priority update value in ASCII text
|
|
16
|
+
# that uses the same representation as that of the Priority header
|
|
17
|
+
# field value".
|
|
18
|
+
#
|
|
19
|
+
# The frame header and placement are the transport's: the type and
|
|
20
|
+
# length varints, and the client control stream the frame MUST be
|
|
21
|
+
# sent on (receiving it on another stream is a connection error of
|
|
22
|
+
# type H3_FRAME_UNEXPECTED). The reference rules are the caller's
|
|
23
|
+
# too, and they are connection policy this gem keeps no state for:
|
|
24
|
+
# the request-stream variant (0xF0700) must reference a request
|
|
25
|
+
# stream and the push-stream variant (0xF0701) a promised push
|
|
26
|
+
# stream, a violation being a connection error of type H3_ID_ERROR
|
|
27
|
+
# (§7.2).
|
|
28
|
+
module H3
|
|
29
|
+
# §7.2: "PRIORITY_UPDATE with a frame type of 0xF0700 is used
|
|
30
|
+
# for request streams".
|
|
31
|
+
REQUEST_TYPE = 0xF0700
|
|
32
|
+
# §7.2: "PRIORITY_UPDATE with a frame type of 0xF0701 is used
|
|
33
|
+
# for push streams".
|
|
34
|
+
PUSH_TYPE = 0xF0701
|
|
35
|
+
# RFC 9000 §16, Table 4: the 8-byte form's 62 usable bits —
|
|
36
|
+
# the Prioritized Element ID's whole range.
|
|
37
|
+
MAX_ELEMENT_ID = (1 << 62) - 1
|
|
38
|
+
# The first byte's low 6 bits — the value bits every form
|
|
39
|
+
# carries in its first byte (§16).
|
|
40
|
+
VARINT_MASK = 0x3F
|
|
41
|
+
|
|
42
|
+
# The decode result. #element_id is the Prioritized Element ID
|
|
43
|
+
# (the stream ID or push ID that is the target); #field_value the
|
|
44
|
+
# raw Priority Field Value, ASCII-8BIT — the payload's bytes
|
|
45
|
+
# whatever tag the input String carried; #priority the §4
|
|
46
|
+
# recipient parse of the field value; #error §7's signal for the
|
|
47
|
+
# CALLER: "Failure to parse the Priority Field Value MAY be
|
|
48
|
+
# treated as a connection error. In HTTP/3, the error is of type
|
|
49
|
+
# H3_GENERAL_PROTOCOL_ERROR" — the wrapped RFC 9651 Dictionary
|
|
50
|
+
# failure, nil when the value parsed. A parsed-but-ignored member
|
|
51
|
+
# is not a failure (§4's MUST-ignore governs), so #priority is
|
|
52
|
+
# the defaults only when #error is set. Frozen, like every family
|
|
53
|
+
# result.
|
|
54
|
+
class Frame
|
|
55
|
+
attr_reader :element_id, :field_value, :priority, :error
|
|
56
|
+
|
|
57
|
+
def initialize(element_id, field_value, priority, error)
|
|
58
|
+
@element_id = element_id
|
|
59
|
+
@field_value = field_value
|
|
60
|
+
@priority = priority
|
|
61
|
+
@error = error
|
|
62
|
+
freeze
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
module_function
|
|
67
|
+
|
|
68
|
+
# The §7.2 payload as an ASCII-8BIT String: the Prioritized
|
|
69
|
+
# Element ID as its minimal QUIC varint, then the Parameters'
|
|
70
|
+
# canonical to_s. Strict, like every emit: a value that is not a
|
|
71
|
+
# Parameters raises SerializeError, because no unvalidated value
|
|
72
|
+
# reaches the wire, and so does an identifier beyond the 62-bit
|
|
73
|
+
# range.
|
|
74
|
+
def encode_payload(element_id, priority)
|
|
75
|
+
check_priority(priority)
|
|
76
|
+
check_element_id(element_id)
|
|
77
|
+
(varint(element_id) << priority.to_s)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# The §7.2 payload decoded: the Prioritized Element ID in
|
|
81
|
+
# whatever varint form it arrived, the raw field value, the §4
|
|
82
|
+
# parse of it, and the §7 error. Never raises on field content;
|
|
83
|
+
# TypeError for a non-String payload; an empty payload, or one
|
|
84
|
+
# whose varint prefix promises more bytes than it carries,
|
|
85
|
+
# raises ParseError at the payload's byte size — where the scan
|
|
86
|
+
# stopped.
|
|
87
|
+
def decode_payload(payload)
|
|
88
|
+
check_payload(payload)
|
|
89
|
+
raise ParseError.new("the payload carries a Prioritized Element ID varint", 0) if payload.empty?
|
|
90
|
+
|
|
91
|
+
length = 1 << (payload.getbyte(0) >> 6)
|
|
92
|
+
size = payload.bytesize
|
|
93
|
+
if size < length
|
|
94
|
+
raise ParseError.new("the varint prefix promises #{length} bytes and the payload carries #{size}", size)
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
element_id = varint_value(payload, length)
|
|
98
|
+
field_value = payload.byteslice(length, size - length)
|
|
99
|
+
field_value.force_encoding(Encoding::ASCII_8BIT)
|
|
100
|
+
field = Field.new(field_value)
|
|
101
|
+
Frame.new(element_id, field_value, field.parameters, field.error)
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
# RFC 9000 §16: the minimal form — the smallest of the four
|
|
105
|
+
# lengths that holds the value, the length's 2-MSB prefix OR'd
|
|
106
|
+
# into the first byte, the value in network byte order. ("Values
|
|
107
|
+
# do not need to be encoded on the minimum number of bytes
|
|
108
|
+
# necessary, with the sole exception of the Frame Type field" —
|
|
109
|
+
# the Prioritized Element ID is not that field, but this encoder
|
|
110
|
+
# still emits the minimum.)
|
|
111
|
+
def varint(value)
|
|
112
|
+
case value
|
|
113
|
+
when 0..0x3F then [value].pack("C")
|
|
114
|
+
when 0..0x3FFF then [value | 0x4000].pack("n")
|
|
115
|
+
when 0..0x3FFF_FFFF then [value | 0x8000_0000].pack("N")
|
|
116
|
+
else [value | 0xC000_0000_0000_0000].pack("Q>")
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# RFC 9000 Appendix A.1's ReadVarint fold: the first byte's low 6
|
|
121
|
+
# bits, then each remaining byte of the +length+ the prefix
|
|
122
|
+
# promised, in network byte order. ANY of the four forms decodes,
|
|
123
|
+
# minimal or not — §16's sole must-be-minimal exception is the
|
|
124
|
+
# Frame Type field, and the Prioritized Element ID is not one.
|
|
125
|
+
def varint_value(payload, length)
|
|
126
|
+
value = payload.getbyte(0) & VARINT_MASK
|
|
127
|
+
index = 1
|
|
128
|
+
while index < length
|
|
129
|
+
value = (value << 8) | payload.getbyte(index)
|
|
130
|
+
index += 1
|
|
131
|
+
end
|
|
132
|
+
value
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
# The emit path never raises TypeError: the wrongness is in the
|
|
136
|
+
# value being sent.
|
|
137
|
+
def check_priority(priority)
|
|
138
|
+
return if priority.is_a?(Parameters)
|
|
139
|
+
|
|
140
|
+
raise SerializeError.new("a Fast::Priority::Parameters is required (got #{priority.class})", 0)
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
# §16 Table 4's whole range.
|
|
144
|
+
def check_element_id(element_id)
|
|
145
|
+
return if element_id.is_a?(Integer) && element_id.between?(0, MAX_ELEMENT_ID)
|
|
146
|
+
|
|
147
|
+
raise SerializeError.new("the prioritized element ID is an Integer 0..2^62-1 (got #{element_id.inspect})", 0)
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
# The payload is a binary String read bytewise; anything else is
|
|
151
|
+
# a caller bug.
|
|
152
|
+
def check_payload(payload)
|
|
153
|
+
return if payload.is_a?(String)
|
|
154
|
+
|
|
155
|
+
raise TypeError, "a binary payload String is required (got #{payload.class})"
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
private_class_method :check_priority, :check_element_id, :check_payload, :varint, :varint_value
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
end
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Fast
|
|
4
|
+
module Priority
|
|
5
|
+
# The RFC 9651 value shapes a Priority field holds: its members are
|
|
6
|
+
# Fast::SFV Items and InnerLists. Aliased here — private to this
|
|
7
|
+
# gem — so the filter, the strict check and the construction walk
|
|
8
|
+
# read them by the short name.
|
|
9
|
+
Item = SFV::Item
|
|
10
|
+
InnerList = SFV::InnerList
|
|
11
|
+
private_constant :Item, :InnerList
|
|
12
|
+
|
|
13
|
+
# Items this gem builds carry no parameters, so they all share the
|
|
14
|
+
# one frozen empty Hash — the parse path's own shape.
|
|
15
|
+
EMPTY_PARAMS = {}.freeze
|
|
16
|
+
private_constant :EMPTY_PARAMS
|
|
17
|
+
|
|
18
|
+
# The empty unknown view, shared by every signal with no unknown
|
|
19
|
+
# members.
|
|
20
|
+
EMPTY_UNKNOWN = {}.freeze
|
|
21
|
+
private_constant :EMPTY_UNKNOWN
|
|
22
|
+
|
|
23
|
+
# The frozen value object behind every parse and every emit: the
|
|
24
|
+
# EFFECTIVE members of a priority signal — "u" and "i" as bare
|
|
25
|
+
# Items when they are present, the unknown members exactly as
|
|
26
|
+
# parsed (§4.3's extension design) — with the readers a recipient
|
|
27
|
+
# reads and the canonical wire form a sender sends.
|
|
28
|
+
#
|
|
29
|
+
# A member's ABSENCE is visible, never folded into a value:
|
|
30
|
+
# #urgency and #incremental answer the §4 defaults (3 and false)
|
|
31
|
+
# for an absent member while #to_s omits it, because RFC 9218
|
|
32
|
+
# turns on the difference — §7: "Omitting a priority parameter is
|
|
33
|
+
# a signal to use its default value", and §8: "The absence of a
|
|
34
|
+
# priority parameter in an HTTP response indicates the server's
|
|
35
|
+
# disinterest in changing the client-provided value".
|
|
36
|
+
class Parameters
|
|
37
|
+
# RFC 9218 §4.1: "The default is 3."
|
|
38
|
+
DEFAULT_URGENCY = 3
|
|
39
|
+
# The two keys this gem interprets (§4.1 and §4.2) — frozen at the
|
|
40
|
+
# class level so the member walks allocate nothing per member.
|
|
41
|
+
KNOWN_KEYS = %w[u i].freeze
|
|
42
|
+
|
|
43
|
+
# The held members: "u"/"i" as bare Items when present, the
|
|
44
|
+
# unknown members as parsed. Frozen and ordered — the parse's
|
|
45
|
+
# order, a merge's order, or u-then-i-then-unknowns from the
|
|
46
|
+
# keywords.
|
|
47
|
+
attr_reader :members
|
|
48
|
+
|
|
49
|
+
# Builds a signal from its three §4 parameters. Every keyword is
|
|
50
|
+
# optional: an omitted (or nil) one means the member is ABSENT —
|
|
51
|
+
# the reader still answers the §4 default and #to_s omits the
|
|
52
|
+
# member. Strict, like every emit: urgency must be an Integer
|
|
53
|
+
# 0..7 (§4.1), incremental a Boolean (§4.2), and unknown a Hash
|
|
54
|
+
# of Fast::SFV Items and InnerLists under String keys other than
|
|
55
|
+
# "u" and "i" — anything else raises SerializeError at
|
|
56
|
+
# construction, so no unvalidated value ever reaches the wire.
|
|
57
|
+
def initialize(urgency: nil, incremental: nil, unknown: nil)
|
|
58
|
+
check_urgency(urgency)
|
|
59
|
+
check_incremental(incremental)
|
|
60
|
+
members = {}
|
|
61
|
+
members["u"] = Item.new(urgency, EMPTY_PARAMS) unless urgency.nil?
|
|
62
|
+
members["i"] = Item.new(incremental, EMPTY_PARAMS) unless incremental.nil?
|
|
63
|
+
add_unknown(members, unknown)
|
|
64
|
+
hold(members)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# RFC 9218 §4.1: the urgency (u) parameter — an Integer 0..7, in
|
|
68
|
+
# descending order of priority. The default is 3, answered when
|
|
69
|
+
# the member is absent.
|
|
70
|
+
def urgency
|
|
71
|
+
member_value("u", DEFAULT_URGENCY)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# RFC 9218 §4.2: the incremental (i) parameter — a Boolean: can
|
|
75
|
+
# the response be processed incrementally. The default is false.
|
|
76
|
+
def incremental
|
|
77
|
+
member_value("i", false)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# The members this gem does not interpret — every key other than
|
|
81
|
+
# "u" and "i", values as parsed, §4's unknown-parameter
|
|
82
|
+
# passthrough. Frozen, empty for a signal with none.
|
|
83
|
+
attr_reader :unknown
|
|
84
|
+
|
|
85
|
+
# The canonical wire form: the RFC 9651 §4.1.2 Dictionary
|
|
86
|
+
# serialization of the held members, "" when none are held — a
|
|
87
|
+
# sender omits the field entirely, the same reason
|
|
88
|
+
# Fast::SFV.serialize_dict answers nil for the empty Dictionary.
|
|
89
|
+
def to_s
|
|
90
|
+
SFV.serialize_dict(@members) || ""
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# RFC 9218 §8: "No guidance is provided for merging priorities;
|
|
94
|
+
# this is left as an implementation decision." This is the §8
|
|
95
|
+
# example's rule, exposed as a helper: every parameter OTHER
|
|
96
|
+
# holds wins (the example prefers the server-provided urgency),
|
|
97
|
+
# every parameter other omits keeps self's, and the unknown
|
|
98
|
+
# members union with other's winning collisions. Member order is
|
|
99
|
+
# self's, then other's new keys. Answers a NEW frozen Parameters
|
|
100
|
+
# with both inputs untouched — a caller with a different policy
|
|
101
|
+
# writes their own merge over #members.
|
|
102
|
+
def merge(other)
|
|
103
|
+
raise TypeError, "a Fast::Priority::Parameters is required (got #{other.class})" unless other.is_a?(Parameters)
|
|
104
|
+
|
|
105
|
+
Parameters.send(:from_members, @members.merge(other.members))
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# Canonical-wire-form equality: two signals are the same when
|
|
109
|
+
# they serialize the same, whatever objects hold the members.
|
|
110
|
+
def ==(other)
|
|
111
|
+
other.is_a?(Parameters) && to_s == other.to_s
|
|
112
|
+
end
|
|
113
|
+
alias eql? ==
|
|
114
|
+
|
|
115
|
+
# Consistent with eql? — the canonical form's hash.
|
|
116
|
+
def hash
|
|
117
|
+
to_s.hash
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def inspect
|
|
121
|
+
wire = to_s
|
|
122
|
+
wire.empty? ? "#<#{self.class.name}>" : "#<#{self.class.name} #{wire}>"
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# The parse and merge paths hold their members already — SFV
|
|
126
|
+
# Items and InnerLists the §4 filter or the §8 merge produced —
|
|
127
|
+
# in a shape .new's keywords cannot express: a parsed "u=3;q=1"
|
|
128
|
+
# keeps its parameters, and member order is the field's, not
|
|
129
|
+
# u-then-i. Private to this gem's own paths.
|
|
130
|
+
def self.from_members(members)
|
|
131
|
+
parameters = allocate
|
|
132
|
+
parameters.send(:hold, members)
|
|
133
|
+
parameters
|
|
134
|
+
end
|
|
135
|
+
private_class_method :from_members
|
|
136
|
+
|
|
137
|
+
private
|
|
138
|
+
|
|
139
|
+
# The one place the held state is written, then frozen: the
|
|
140
|
+
# members Hash as given, and the unknown view derived from it.
|
|
141
|
+
def hold(members)
|
|
142
|
+
@members = members.freeze
|
|
143
|
+
@unknown = unknown_view(@members)
|
|
144
|
+
freeze
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
# A §4 reader: the member's bare value when held, the RFC's
|
|
148
|
+
# default when absent.
|
|
149
|
+
def member_value(key, default)
|
|
150
|
+
member = @members[key]
|
|
151
|
+
member ? member.value : default
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
# The passthrough view: the members minus the two this gem
|
|
155
|
+
# interprets. The empty view is shared — a signal with no
|
|
156
|
+
# unknown members costs no Hash.
|
|
157
|
+
def unknown_view(members)
|
|
158
|
+
unknown = nil
|
|
159
|
+
members.each do |key, value|
|
|
160
|
+
next if KNOWN_KEYS.include?(key)
|
|
161
|
+
|
|
162
|
+
unknown ||= {}
|
|
163
|
+
unknown[key] = value
|
|
164
|
+
end
|
|
165
|
+
unknown ? unknown.freeze : EMPTY_UNKNOWN
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
# §4.1's bound at the boundary, before any member is built.
|
|
169
|
+
def check_urgency(urgency)
|
|
170
|
+
return if urgency.nil? || (urgency.is_a?(Integer) && urgency.between?(0, 7))
|
|
171
|
+
|
|
172
|
+
raise SerializeError.new("urgency is an Integer 0..7 (got #{urgency.inspect})", 0)
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
# §4.2's bound at the boundary, before any member is built.
|
|
176
|
+
def check_incremental(incremental)
|
|
177
|
+
return if incremental.nil? || incremental == true || incremental == false
|
|
178
|
+
|
|
179
|
+
raise SerializeError.new("incremental is a Boolean (got #{incremental.inspect})", 0)
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
# The unknown members join after "u" and "i", in insertion
|
|
183
|
+
# order, each validated as it is walked.
|
|
184
|
+
def add_unknown(members, unknown)
|
|
185
|
+
return if unknown.nil?
|
|
186
|
+
raise SerializeError.new("unknown members are a Hash (got #{unknown.class})", 0) unless unknown.is_a?(Hash)
|
|
187
|
+
|
|
188
|
+
unknown.each do |key, value|
|
|
189
|
+
check_unknown_key(key)
|
|
190
|
+
check_unknown_value(value)
|
|
191
|
+
members[key] = value
|
|
192
|
+
end
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
# The two keys this gem interprets are set through the keywords;
|
|
196
|
+
# every other member's key is a String, the wire form of a
|
|
197
|
+
# Dictionary key.
|
|
198
|
+
def check_unknown_key(key)
|
|
199
|
+
return if key.is_a?(String) && !KNOWN_KEYS.include?(key)
|
|
200
|
+
|
|
201
|
+
raise SerializeError.new(
|
|
202
|
+
"an unknown member's key is a String other than u and i, set through the keywords (got #{key.inspect})", 0
|
|
203
|
+
)
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
# The wire shapes Fast::SFV serializes: an Item or an InnerList,
|
|
207
|
+
# nothing else.
|
|
208
|
+
def check_unknown_value(value)
|
|
209
|
+
return if value.is_a?(Item) || value.is_a?(InnerList)
|
|
210
|
+
|
|
211
|
+
raise SerializeError.new(
|
|
212
|
+
"an unknown member's value is a Fast::SFV Item or InnerList (got #{value.class})", 0
|
|
213
|
+
)
|
|
214
|
+
end
|
|
215
|
+
end
|
|
216
|
+
end
|
|
217
|
+
end
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# The substrate: RFC 9218's priority parameters ride in an RFC 9651
|
|
4
|
+
# Dictionary, so this gem is a fast-sfv consumer, never a parser of
|
|
5
|
+
# its own.
|
|
6
|
+
require "fast/sfv"
|
|
7
|
+
|
|
8
|
+
require_relative "priority/version"
|
|
9
|
+
require_relative "priority/errors"
|
|
10
|
+
require_relative "priority/parameters"
|
|
11
|
+
require_relative "priority/field"
|
|
12
|
+
require_relative "priority/h2"
|
|
13
|
+
require_relative "priority/h3"
|
|
14
|
+
|
|
15
|
+
module Fast
|
|
16
|
+
# RFC 9218 Extensible Priorities: the Priority HTTP header field and
|
|
17
|
+
# the HTTP/2/HTTP/3 PRIORITY_UPDATE frame payloads.
|
|
18
|
+
#
|
|
19
|
+
# The Priority field is a Dictionary (§5: "The Priority HTTP header
|
|
20
|
+
# field is a Dictionary that carries priority parameters"), so
|
|
21
|
+
# Fast::Priority.parse hands the field value to Fast::SFV.parse_dict
|
|
22
|
+
# and then applies §4's recipient semantics on what parsed.
|
|
23
|
+
#
|
|
24
|
+
# parse is the recipient's view and never raises on field content: a
|
|
25
|
+
# malformed or absent field answers the §4 defaults (urgency 3,
|
|
26
|
+
# incremental false, no member held), and a parsed Dictionary is
|
|
27
|
+
# filtered member by member — §4: "unknown priority parameters,
|
|
28
|
+
# priority parameters with out-of-range values, or values of
|
|
29
|
+
# unexpected types MUST be ignored" — so an out-of-range or
|
|
30
|
+
# wrong-typed "u" or "i" is dropped, unknown members pass through as
|
|
31
|
+
# parsed, and what survives is held. A String is one field value; an
|
|
32
|
+
# Array is the field's lines, combined with ", " (RFC 9651 §4.2). A
|
|
33
|
+
# wrong input type is a caller bug and raises TypeError.
|
|
34
|
+
#
|
|
35
|
+
# parse! is strict truth: the Dictionary failure wrapped with its
|
|
36
|
+
# message and byte offset, then §4 inverted on the effective
|
|
37
|
+
# (last-wins) members — a held "u" that is not an Integer 0..7 (§4.1)
|
|
38
|
+
# or a held "i" that is not a Boolean (§4.2) raises a ParseError
|
|
39
|
+
# pointing at the member's value. valid? answers the same question
|
|
40
|
+
# with a boolean and never raises.
|
|
41
|
+
#
|
|
42
|
+
# The answer is a frozen Fast::Priority::Parameters: urgency,
|
|
43
|
+
# incremental, the unknown members, the held members, the canonical
|
|
44
|
+
# to_s ("" when nothing is held — a sender omits the field), the §8
|
|
45
|
+
# merge helper, and canonical-wire-form equality. A member's ABSENCE
|
|
46
|
+
# is visible, not folded into a default: §7 ("Omitting a priority
|
|
47
|
+
# parameter is a signal to use its default value") and §8 ("The
|
|
48
|
+
# absence of a priority parameter in an HTTP response indicates the
|
|
49
|
+
# server's disinterest in changing the client-provided value") both
|
|
50
|
+
# turn on the difference. to_s and the frame encoders are ALWAYS
|
|
51
|
+
# strict, in every family gem: a sender never puts garbage on the
|
|
52
|
+
# wire.
|
|
53
|
+
#
|
|
54
|
+
# Fast::Priority::H2 and Fast::Priority::H3 are the PRIORITY_UPDATE
|
|
55
|
+
# frame payload codecs (§7.1 and §7.2): byte-exact encode and decode
|
|
56
|
+
# of the payload after the frame header — the h2 Reserved(1)+
|
|
57
|
+
# Prioritized Stream ID(31) word, the h3 QUIC variable-length
|
|
58
|
+
# Prioritized Element ID — plus the Priority Field Value. A field
|
|
59
|
+
# value that fails the Dictionary parse is surfaced on the result's
|
|
60
|
+
# #error ("Failure to parse the Priority Field Value MAY be treated
|
|
61
|
+
# as a connection error" — the caller owns that MAY); a
|
|
62
|
+
# parsed-but-ignored member is not a failure. The frame headers, the
|
|
63
|
+
# stream-state rules and every SETTINGS decision are the
|
|
64
|
+
# transport's: this gem keeps no connection or stream state.
|
|
65
|
+
module Priority
|
|
66
|
+
end
|
|
67
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: fast-priority
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Eric Jacobs
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: fast-sfv
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '0'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - ">="
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '0'
|
|
26
|
+
description: |
|
|
27
|
+
Fast::Priority implements RFC 9218 Extensible Priorities — the
|
|
28
|
+
Priority HTTP header field as an RFC 9651 Dictionary consumer with
|
|
29
|
+
the recipient semantics (urgency, incremental, unknown-parameter
|
|
30
|
+
passthrough), plus the HTTP/2 and HTTP/3 PRIORITY_UPDATE frame
|
|
31
|
+
payload codecs. Built on fast-sfv, frozen value objects, strict
|
|
32
|
+
emit, one runtime dependency.
|
|
33
|
+
executables: []
|
|
34
|
+
extensions: []
|
|
35
|
+
extra_rdoc_files: []
|
|
36
|
+
files:
|
|
37
|
+
- CHANGELOG.md
|
|
38
|
+
- README.md
|
|
39
|
+
- lib/fast/priority.rb
|
|
40
|
+
- lib/fast/priority/errors.rb
|
|
41
|
+
- lib/fast/priority/field.rb
|
|
42
|
+
- lib/fast/priority/h2.rb
|
|
43
|
+
- lib/fast/priority/h3.rb
|
|
44
|
+
- lib/fast/priority/parameters.rb
|
|
45
|
+
- lib/fast/priority/version.rb
|
|
46
|
+
homepage: https://github.com/jetpks/fast-http-fields
|
|
47
|
+
licenses:
|
|
48
|
+
- MIT
|
|
49
|
+
metadata:
|
|
50
|
+
homepage_uri: https://github.com/jetpks/fast-http-fields
|
|
51
|
+
source_code_uri: https://github.com/jetpks/fast-http-fields
|
|
52
|
+
changelog_uri: https://github.com/jetpks/fast-http-fields/blob/main/gems/priority/CHANGELOG.md
|
|
53
|
+
documentation_uri: https://github.com/jetpks/fast-http-fields/tree/main/gems/priority/docs
|
|
54
|
+
rubygems_mfa_required: 'true'
|
|
55
|
+
rdoc_options: []
|
|
56
|
+
require_paths:
|
|
57
|
+
- lib
|
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
59
|
+
requirements:
|
|
60
|
+
- - ">="
|
|
61
|
+
- !ruby/object:Gem::Version
|
|
62
|
+
version: '3.4'
|
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
64
|
+
requirements:
|
|
65
|
+
- - ">="
|
|
66
|
+
- !ruby/object:Gem::Version
|
|
67
|
+
version: 3.5.3
|
|
68
|
+
requirements: []
|
|
69
|
+
rubygems_version: 4.0.20
|
|
70
|
+
specification_version: 4
|
|
71
|
+
summary: 'RFC 9218 Extensible Priorities: the Priority field and the h2/h3 PRIORITY_UPDATE
|
|
72
|
+
frame payloads.'
|
|
73
|
+
test_files: []
|