Skip to content
Open
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
13 changes: 13 additions & 0 deletions lib/protocol/http/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,18 @@ def initialize(key)
# @attribute [String] key The header key that was duplicated.
attr :key
end

# Raised when an invalid trailer header is encountered in headers.
class InvalidTrailerError < Error
include BadRequest

# @parameter key [String] The trailer key that is invalid.
def initialize(key)
super("Invalid trailer key: #{key.inspect}")
end

# @attribute [String] key The trailer key that is invalid.
attr :key
end
end
end
6 changes: 4 additions & 2 deletions lib/protocol/http/headers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ def delete(key)
if policy = @policy[key]
# Check if we're adding to trailers and this header is allowed:
if trailer && !policy.trailer?
return false
raise InvalidTrailerError, key
end

if current_value = hash[key]
Expand All @@ -418,7 +418,7 @@ def delete(key)
else
# By default, headers are not allowed in trailers:
if trailer
return false
raise InvalidTrailerError, key
end

if hash.key?(key)
Expand All @@ -431,6 +431,8 @@ def delete(key)

# Compute a hash table of headers, where the keys are normalized to lower case and the values are normalized according to the policy for that header.
#
# This will enforce policy rules, such as merging multiple headers into arrays, or raising errors for duplicate headers.
#
# @returns [Hash] A hash table of `{key, value}` pairs.
def to_h
unless @indexed
Expand Down
2 changes: 1 addition & 1 deletion test/protocol/http/headers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@
it "can't add a #{key.inspect} header in the trailer", unique: key do
trailer = headers.trailer!
headers.add(key, "example")
expect(headers).not.to be(:include?, key)
expect{headers.to_h}.to raise_exception(Protocol::HTTP::InvalidTrailerError)
end
end
end
Expand Down
Loading