Skip to content

Commit

Permalink
do not convert body encoding if it's not a text
Browse files Browse the repository at this point in the history
  • Loading branch information
Paxa committed Nov 11, 2011
1 parent 24d82b1 commit 188b362
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/patron/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,14 @@ def initialize(url, status, redirect_count, header_data, body, default_charset =

@charset = determine_charset(header_data, body) || default_charset

[url, header_data, body].each do |attr|
[url, header_data].each do |attr|
convert_to_default_encoding!(attr)
end

parse_headers(header_data)
if @headers["Content-Type"] && @headers["Content-Type"][0, 5] == "text/"
convert_to_default_encoding!(@body)
end
end

attr_reader :url, :status, :status_line, :redirect_count, :body, :headers, :charset
Expand Down
Binary file added pic.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions script/test_server
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ class RepetitiveHeaderServlet < HTTPServlet::AbstractServlet
end
end

class PictureServlet < HTTPServlet::AbstractServlet
def do_GET(req, res)
res['Content-Type'] = "image/png"
res.body = File.read("./pic.png")
end
end

class WrongContentLengthServlet < HTTPServlet::AbstractServlet
def do_GET(req, res)
res.keep_alive = false
Expand All @@ -122,6 +129,7 @@ server.mount("/test", TestServlet)
server.mount("/testpost", TestPostBodyServlet)
server.mount("/timeout", TimeoutServlet)
server.mount("/redirect", RedirectServlet)
server.mount("/picture", PictureServlet)
server.mount("/setcookie", SetCookieServlet)
server.mount("/repetitiveheader", RepetitiveHeaderServlet)
server.mount("/wrongcontentlength", WrongContentLengthServlet)
Expand Down
6 changes: 6 additions & 0 deletions spec/response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@
response.headers['Set-Cookie'].should == ["a=1","b=2"]
end

it "should works with non-text files" do
response = @session.get("/picture")
response.headers['Content-Type'].should == 'image/png'
response.body.encoding.should == Encoding::ASCII_8BIT
end

it "should not allow a default charset to be nil" do
Encoding.stub(:default_internal).and_return("UTF-8")
expect {
Expand Down

3 comments on commit 188b362

@Empact
Copy link

@Empact Empact commented on 188b362 Jan 17, 2012

Choose a reason for hiding this comment

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

I ran into this - how about a bug fix release?

@Paxa
Copy link
Contributor Author

@Paxa Paxa commented on 188b362 Jan 18, 2012

Choose a reason for hiding this comment

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

Sorry, what do you mean? If you found bug - can you provide more information about it?

@Empact
Copy link

@Empact Empact commented on 188b362 Jan 18, 2012

Choose a reason for hiding this comment

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

This commit is the fix for a bug I encountered, but I guess you're right that opening a bug is the right way to suggest a release.
Opened #53.

Please sign in to comment.