Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set encoding of the response string, if specified by server. #278

Closed
wants to merge 1 commit into from

Conversation

dhamidi
Copy link

@dhamidi dhamidi commented Apr 13, 2014

The encoding of the response string is always reported as 'ASCII-8BIT',
even when the underlying byte sequence is valid UTF-8 and the server
advertises it as UTF-8 in the Content-Type header.

With this commit, the Content-Type header in the server response is
checked for a charset defintion and the response string encoding set
accordingly. If there is a mismatch between the advertised and the
actual encoding, the response encoding is left as 'ASCII-8BIT'.

The following irb session illustrates the problem:

irb(main):001:0> require 'restclient'
=> true
irb(main):002:0> response = RestClient.get 'http://www.goldenerunkelruebe.de'; response.code
=> 200
irb(main):003:0> response.encoding
=> #<Encoding:ASCII-8BIT>
irb(main):004:0> response.headers[:content_type]
=> "text/html; charset=UTF-8"

The encoding of the response string is always reported as 'ASCII-8BIT',
even when the underlying byte sequence is valid UTF-8 and the server
advertises it as UTF-8 in the Content-Type header.

With this commit, the Content-Type header in the server response is
checked for a charset defintion and the response string encoding set
accordingly.  If there is a mismatch between the advertised and the
actual encoding, the response encoding is left as 'ASCII-8BIT'.

The following irb session illustrates the problem:

```
irb(main):001:0> require 'restclient'
=> true
irb(main):002:0> response = RestClient.get 'http://www.goldenerunkelruebe.de'; response.code
=> 200
irb(main):003:0> response.encoding
=> #<Encoding:ASCII-8BIT>
irb(main):004:0> response.headers[:content_type]
=> "text/html; charset=UTF-8"
```
@ab
Copy link
Member

ab commented Apr 13, 2014

This looks pretty good, thanks! I think it should probably wait for the next minor release for backwards-compatibility, though.

@dhamidi
Copy link
Author

dhamidi commented Apr 15, 2014

Cool! Will I have to do anything for that or will you just merge it when it is time for the next minor release?

@ab ab added this to the 1.8.0 milestone Apr 24, 2014
@ab
Copy link
Member

ab commented Apr 24, 2014

I haven't had a chance to look too closely, but I think it should just be good to go. I've tagged it 1.8.0 so hopefully I won't forget about it, but feel free to bump as 1.8.0 gets closer. Right now I'm focusing on just getting a 1.7.0 release out to get some new momentum.

@dhamidi
Copy link
Author

dhamidi commented Apr 24, 2014

Okay, thanks for the heads-up!

@ab ab mentioned this pull request Nov 3, 2014
@ab ab self-assigned this Jan 28, 2015
@jure
Copy link

jure commented Feb 25, 2015

@chrismo did some additional research into how other gems do this here: lostisland/faraday#139

For reference:
EM-HTTP-Request igrigorik/em-http-request@65dc4f5
Patron toland/patron@2ddc3b8
HTTPClient nahi/httpclient@e5efea5

@ab ab modified the milestones: 2.0.0, 1.8.0 Mar 1, 2015
@ab ab closed this in #361 Mar 14, 2015
@ab
Copy link
Member

ab commented Mar 14, 2015

Thanks @jure for those references.

And thanks very much @dhamidi. Ended up adapting this somewhat for use. It should go out in the 2.0 release.

ab added a commit that referenced this pull request Mar 14, 2015
Including dhamidi for #278.
@jure
Copy link

jure commented Mar 14, 2015

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants