Skip to content

Add support for constructing headers with tail marker. #89

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions lib/protocol/http/headers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,15 @@ def self.[] headers
# Initialize the headers with the specified fields.
#
# @parameter fields [Array] An array of `[key, value]` pairs.
# @parameter indexed [Hash] A hash table of normalized headers, if available.
def initialize(fields = [], indexed = nil)
# @parameter tail [Integer | Nil] The index of the trailer start in the @fields array.
def initialize(fields = [], tail = nil, indexed: nil)
@fields = fields
@indexed = indexed

# Marks where trailer start in the @fields array.
@tail = nil
# Marks where trailer start in the @fields array:
@tail = tail

# The cached index of headers:
@indexed = nil
end

# Initialize a copy of the headers.
Expand All @@ -86,8 +88,8 @@ def initialize_dup(other)
# Clear all headers.
def clear
@fields.clear
@indexed = nil
@tail = nil
@indexed = nil
end

# Flatten trailer into the headers, in-place.
Expand All @@ -108,6 +110,9 @@ def flatten
# @attribute [Array] An array of `[key, value]` pairs.
attr :fields

# @attribute [Integer | Nil] The index where trailers begin.
attr :tail

# @returns [Array] The fields of the headers.
def to_a
@fields
Expand Down
1 change: 1 addition & 0 deletions releases.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

- Add `Protocol::HTTP::Headers#to_a` method that returns the fields array, providing compatibility with standard Ruby array conversion pattern.
- Expose `tail` in `Headers.new` so that trailers can be accurately reproduced.

## v0.51.0

Expand Down
25 changes: 19 additions & 6 deletions test/protocol/http/headers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,25 @@
let(:fields) do
[
["Content-Type", "text/html"],
["connection", "Keep-Alive"],
["Set-Cookie", "hello=world"],
["Accept", "*/*"],
["set-cookie", "foo=bar"],
["connection", "Keep-Alive"]
]
end

let(:headers) {subject[fields]}

with ".new" do
it "can construct headers with trailers" do
headers = subject.new(fields, 4)
expect(headers).to be(:trailer?)
expect(headers.trailer.to_a).to be == [
["set-cookie", "foo=bar"],
]
end
end

with ".[]" do
it "can be constructed from frozen array" do
self.fields.freeze
Expand All @@ -29,13 +39,14 @@

with "#keys" do
it "should return keys" do
expect(headers.keys).to be == ["content-type", "set-cookie", "accept", "connection"]
expect(headers.keys).to be == ["content-type", "connection", "set-cookie", "accept"]
end
end

with "#trailer?" do
it "should not be a trailer" do
expect(headers).not.to be(:trailer?)
expect(headers.tail).to be_nil
end
end

Expand Down Expand Up @@ -269,8 +280,10 @@
with "#trailer!" do
it "can add trailer" do
headers.add("trailer", "etag")
count = headers.fields.size

trailer = headers.trailer!
expect(headers.tail).to be == count

headers.add("etag", "abcd")

Expand Down Expand Up @@ -345,8 +358,8 @@
describe Protocol::HTTP::Headers::Merged do
let(:merged) do
Protocol::HTTP::Headers::Merged.new(
Protocol::HTTP::Headers.new("content-type" => "text/html"),
Protocol::HTTP::Headers.new("content-encoding" => "gzip")
Protocol::HTTP::Headers["content-type" => "text/html"],
Protocol::HTTP::Headers["content-encoding" => "gzip"]
)
end

Expand Down Expand Up @@ -382,8 +395,8 @@
with "non-normalized case" do
let(:merged) do
Protocol::HTTP::Headers::Merged.new(
Protocol::HTTP::Headers.new("Content-Type" => "text/html"),
Protocol::HTTP::Headers.new("Content-Encoding" => "gzip")
Protocol::HTTP::Headers["Content-Type" => "text/html"],
Protocol::HTTP::Headers["Content-Encoding" => "gzip"]
)
end

Expand Down
Loading