Skip to content

Commit d03cf24

Browse files
committed
Return the last instance of u=.
As per RFC8941: <https://www.rfc-editor.org/rfc/rfc8941#section-3.2>.
1 parent 9c84310 commit d03cf24

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

lib/protocol/http/header/priority.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@ def << value
2929
# The default urgency level if not specified.
3030
DEFAULT_URGENCY = 3
3131

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

4143
return default

lib/protocol/http/header/split.rb

+10
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ def << value
3939
def to_s
4040
join(",")
4141
end
42+
43+
protected
44+
45+
def reverse_find(&block)
46+
reverse_each do |value|
47+
return value if block.call(value)
48+
end
49+
50+
return nil
51+
end
4252
end
4353
end
4454
end

test/protocol/http/header/priority.rb

+2-3
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,9 @@
5555
end
5656

5757
with "u=2, u=5" do
58-
it "prioritizes the first urgency directive" do
58+
it "prioritizes the last urgency directive" do
5959
expect(header).to have_attributes(
60-
# First occurrence takes precedence
61-
urgency: be == 2,
60+
urgency: be == 5,
6261
)
6362
end
6463
end

0 commit comments

Comments
 (0)