Skip to content

Commit

Permalink
Return the last instance of u=.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Dec 4, 2024
1 parent 9c84310 commit d03cf24
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
8 changes: 5 additions & 3 deletions lib/protocol/http/header/priority.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ def << value
# The default urgency level if not specified.
DEFAULT_URGENCY = 3

# Returns the urgency level if specified. 0 is the highest priority, and 7 is the lowest.
# The urgency level, if specified using `u=`. 0 is the highest priority, and 7 is the lowest.
#
# Note that when duplicate Dictionary keys are encountered, all but the last instance are ignored.
#
# @returns [Integer | Nil] the urgency level if specified, or `nil` if not present.
def urgency(default = DEFAULT_URGENCY)
if value = self.find { |value| value.start_with?("u=") }
if value = self.reverse_find{|value| value.start_with?("u=")}
_, level = value.split("=", 2)
return level.to_i
return Integer(level)
end

return default
Expand Down
10 changes: 10 additions & 0 deletions lib/protocol/http/header/split.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ def << value
def to_s
join(",")
end

protected

def reverse_find(&block)
reverse_each do |value|
return value if block.call(value)
end

return nil
end
end
end
end
Expand Down
5 changes: 2 additions & 3 deletions test/protocol/http/header/priority.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,9 @@
end

with "u=2, u=5" do
it "prioritizes the first urgency directive" do
it "prioritizes the last urgency directive" do
expect(header).to have_attributes(
# First occurrence takes precedence
urgency: be == 2,
urgency: be == 5,
)
end
end
Expand Down

0 comments on commit d03cf24

Please sign in to comment.