Skip to content

Commit 925deae

Browse files
authored
fix: Binary HTTP format parses quoted tokens according to RFC 7230 section 3.2.6 (#53)
Signed-off-by: Daniel Azuma <[email protected]>
1 parent 14d2ffa commit 925deae

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

lib/cloud_events/http_binding.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ def encode_binary_content event
254254
# @return [String] Resulting decoded string in UTF-8.
255255
#
256256
def percent_decode str
257+
str = str.gsub(/"((?:[^"\\]|\\.)*)"/) { Regexp.last_match(1).gsub(/\\(.)/, '\1') }
257258
decoded_str = str.gsub(/%[0-9a-fA-F]{2}/) { |m| [m[1..-1].to_i(16)].pack "C" }
258259
decoded_str.force_encoding ::Encoding::UTF_8
259260
end
@@ -271,7 +272,7 @@ def percent_encode str
271272
arr = []
272273
utf_str = str.to_s.encode ::Encoding::UTF_8
273274
utf_str.each_byte do |byte|
274-
if byte >= 33 && byte <= 126 && byte != 37
275+
if byte >= 33 && byte <= 126 && byte != 34 && byte != 37
275276
arr << byte
276277
else
277278
hi = byte / 16

test/test_http_binding.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
let(:my_source_string) { "/my_source" }
1515
let(:my_source) { URI.parse my_source_string }
1616
let(:my_type) { "my_type" }
17-
let(:weird_type) { "¡Hola!\n100% 😀 " }
18-
let(:encoded_weird_type) { "%C2%A1Hola!%0A100%25%20%F0%9F%98%80%20" }
17+
let(:weird_type) { "¡Hola!\n\"100%\" 😀 " }
18+
let(:encoded_weird_type) { "%C2%A1Hola!%0A%22100%25%22%20%F0%9F%98%80%20" }
19+
let(:quoted_type) { "Hello Ruby world this\"is\\a1string okay" }
20+
let(:encoded_quoted_type) { "Hello%20\"Ruby%20world\"%20\"this\\\"is\\\\a\\1string\"%20okay" }
1921
let(:spec_version) { "1.0" }
2022
let(:my_simple_data) { "12345" }
2123
let(:my_content_type_string) { "text/plain; charset=us-ascii" }
@@ -62,6 +64,11 @@
6264
assert_equal weird_type, str
6365
end
6466

67+
it "percent-decodes a string with quoted tokens" do
68+
str = http_binding.percent_decode encoded_quoted_type
69+
assert_equal quoted_type, str
70+
end
71+
6572
it "decodes a structured rack env and re-encodes as batch" do
6673
env = {
6774
"rack.input" => StringIO.new(my_json_struct_encoded),

0 commit comments

Comments
 (0)