Skip to content
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
3 changes: 2 additions & 1 deletion .mailmap
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Dan Olson <[email protected]>
Dan Olson <[email protected]>
Thomas Morgan <[email protected]>
23 changes: 22 additions & 1 deletion lib/protocol/http/header/etags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,42 @@

# Released under the MIT License.
# Copyright, 2020-2023, by Samuel Williams.
# Copyright, 2023, by Thomas Morgan.

require_relative 'split'

module Protocol
module HTTP
module Header
# This implementation is not strictly correct according to the RFC-specified format.
class ETags < Split
def wildcard?
self.include?('*')
end

# This implementation is not strictly correct according to the RFC-specified format.
def match?(etag)
wildcard? || self.include?(etag)
end

# Useful with If-Match
def strong_match?(etag)
wildcard? || (!weak_tag?(etag) && self.include?(etag))
end

# Useful with If-None-Match
def weak_match?(etag)
wildcard? || self.include?(etag) || self.include?(opposite_tag(etag))
end

private

def opposite_tag(etag)
weak_tag?(etag) ? etag[2..-1] : "W/#{etag}"
end

def weak_tag?(tag)
tag&.start_with? 'W/'
end
end
end
end
Expand Down
1 change: 1 addition & 0 deletions license.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Copyright, 2020-2023, by Bruno Sutic.
Copyright, 2022, by Herrick Fang.
Copyright, 2022, by Dan Olson.
Copyright, 2023, by Genki Takiuchi.
Copyright, 2023, by Thomas Morgan.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
31 changes: 27 additions & 4 deletions test/protocol/http/header/etags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# Released under the MIT License.
# Copyright, 2020-2023, by Samuel Williams.
# Copyright, 2023, by Thomas Morgan.

require 'protocol/http/header/etags'

Expand All @@ -14,21 +15,43 @@
end

it "matches anything" do
expect(header).to be(:match?, "anything")
expect(header).to be(:match?, '"anything"')
end
end

with "abcd" do
with '"abcd"' do
it "is not a wildcard" do
expect(header).not.to be(:wildcard?)
end

it "matches itself" do
expect(header).to be(:match?, "abcd")
expect(header).to be(:match?, '"abcd"')
end

it "strongly matches only another strong etag" do
expect(header).to be(:strong_match?, '"abcd"')
expect(header).not.to be(:strong_match?, 'W/"abcd"')
end

it "weakly matches both weak and strong etags" do
expect(header).to be(:weak_match?, '"abcd"')
expect(header).to be(:weak_match?, 'W/"abcd"')
end

it "does not match anything else" do
expect(header).not.to be(:match?, "anything else")
expect(header).not.to be(:match?, '"anything else"')
end
end

with 'W/"abcd"' do
it "never strongly matches" do
expect(header).not.to be(:strong_match?, '"abcd"')
expect(header).not.to be(:strong_match?, 'W/"abcd"')
end

it "weakly matches both weak and strong etags" do
expect(header).to be(:weak_match?, '"abcd"')
expect(header).to be(:weak_match?, 'W/"abcd"')
end
end
end