From 24ae9ad893a873d569b9dd383439eaa6d5a1452a Mon Sep 17 00:00:00 2001 From: Garret Alfert Date: Wed, 3 Jan 2018 22:06:03 +0100 Subject: [PATCH] Allow for weak ETAGs with leading quote The remoteStorage client currently surrounds all ETAGs with quotes, so instead of W\"etag" it looks like "W\"etag". --- lib/remote_storage/swift.rb | 8 ++++---- spec/swift/app_spec.rb | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/lib/remote_storage/swift.rb b/lib/remote_storage/swift.rb index fb83f2f..73e05f3 100644 --- a/lib/remote_storage/swift.rb +++ b/lib/remote_storage/swift.rb @@ -52,7 +52,7 @@ module RemoteStorage set_response_headers(res) - none_match = (server.env["HTTP_IF_NONE_MATCH"] || "").gsub(/^W\//, "").split(",").map(&:strip) + none_match = (server.env["HTTP_IF_NONE_MATCH"] || "").gsub(/^"?W\//, "").split(",").map(&:strip) server.halt 304 if none_match.include? %Q("#{res.headers[:etag]}") return res.body @@ -71,7 +71,7 @@ module RemoteStorage server.headers["Content-Type"] = "application/ld+json" - none_match = (server.env["HTTP_IF_NONE_MATCH"] || "").gsub(/^W\//, "").split(",").map(&:strip) + none_match = (server.env["HTTP_IF_NONE_MATCH"] || "").gsub(/^"?W\//, "").split(",").map(&:strip) if etag server.halt 304 if none_match.include? %Q("#{etag}") @@ -139,7 +139,7 @@ module RemoteStorage url = url_for_key(user, directory, key) if required_match = server.env["HTTP_IF_MATCH"] - unless required_match.gsub(/^W\//, "") == %Q("#{existing_metadata["e"]}") + unless required_match.gsub(/^"?W\//, "") == %Q("#{existing_metadata["e"]}") server.halt 412, "Precondition Failed" end end @@ -187,7 +187,7 @@ module RemoteStorage existing_metadata = redis.hgetall "rs:m:#{user}:#{directory}/#{key}" if required_match = server.env["HTTP_IF_MATCH"] - unless required_match.gsub(/^W\//, "") == %Q("#{existing_metadata["e"]}") + unless required_match.gsub(/^"?W\//, "") == %Q("#{existing_metadata["e"]}") server.halt 412, "Precondition Failed" end end diff --git a/spec/swift/app_spec.rb b/spec/swift/app_spec.rb index 8cfe170..60d7fda 100644 --- a/spec/swift/app_spec.rb +++ b/spec/swift/app_spec.rb @@ -265,6 +265,22 @@ describe "App" do last_response.headers["Etag"].must_equal "\"newetag\"" end + it "allows the request if the header contains a weak ETAG with leading quote matching the current ETag" do + header "If-Match", "\"W/\"oldetag\"" + + put_stub = OpenStruct.new(headers: { + etag: "newetag", + last_modified: "Fri, 04 Mar 2016 12:20:18 GMT" + }) + + RestClient.stub :put, put_stub do + put "/phil/food/aguacate", "aye" + end + + last_response.status.must_equal 200 + last_response.headers["Etag"].must_equal "\"newetag\"" + end + it "fails the request if the header does not match the current ETag" do header "If-Match", "someotheretag"