Skip to content

Commit

Permalink
Don't fail with invalid charset names
Browse files Browse the repository at this point in the history
  • Loading branch information
kazjote committed Oct 6, 2015
1 parent dc6ca9d commit e5ef02f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/patron/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,20 @@ def marshal_load(data)
def determine_charset(header_data, body)
header_data.match(charset_regex) || (body && body.match(charset_regex))

$1
charset = $1
validate_charset(charset) ? charset : nil
end

def charset_regex
/(?:charset|encoding)="?([a-z0-9-]+)"?/i
end

def validate_charset(charset)
charset && Encoding.find(charset) && true
rescue ArgumentError
false
end

def convert_to_default_encoding!(str)
if str.respond_to?(:encode) && Encoding.default_internal
str.force_encoding(charset).encode!(Encoding.default_internal)
Expand Down
26 changes: 26 additions & 0 deletions spec/response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,30 @@
it "should be able to serialize and deserialize itself" do
expect(Marshal.load(Marshal.dump(@request))).to eql(@request)
end

it "should encode body in the internal charset" do
allow(Encoding).to receive(:default_internal).and_return("UTF-8")

greek_encoding = Encoding.find("ISO-8859-7")
utf_encoding = Encoding.find("UTF-8")

headers = "HTTP/1.1 200 OK \r\nContent-Type: text/css\r\n"
body = "charset=ISO-8859-7 Ππ".encode(greek_encoding) # Greek alphabet

response = Patron::Response.new("url", "status", 0, headers, body, nil)

expect(response.body.encoding).to eql(utf_encoding)
end

it "should fallback to default charset when header or body charset is not valid" do
allow(Encoding).to receive(:default_internal).and_return("UTF-8")

encoding = Encoding.find("UTF-8")
headers = "HTTP/1.1 200 OK \r\nContent-Type: text/css\r\n"
body = "charset=invalid"

response = Patron::Response.new("url", "status", 0, headers, body, "UTF-8")

expect(response.body.encoding).to eql(encoding)
end
end

2 comments on commit e5ef02f

@christoph-buente
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 @kazjote

@toland Can we release this?

@toland
Copy link
Owner

@toland toland commented on e5ef02f Mar 14, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Please sign in to comment.