Skip to content

Commit

Permalink
added support gzip content encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
stavskiys committed Dec 19, 2013
1 parent 9b5054c commit cc040f7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/oauth2/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def status

# The HTTP response body
def body
response.body || ''
@body ||= preprocessing_of_body(response.body)
end

# Procs that, when called, will parse a response body according
Expand Down Expand Up @@ -82,6 +82,19 @@ def parser
return options[:parse].to_sym if PARSERS.key?(options[:parse])
CONTENT_TYPES[content_type]
end

private

def content_encoding
((response.headers.values_at('content-encoding', 'Content-Encoding').compact.first || '').split(';').first || '').strip
end

def preprocessing_of_body(resp_body)
return '' if resp_body.nil? || resp_body.empty?

content_encoding == 'gzip' ? Zlib::GzipReader.new(StringIO.new(resp_body)).read : resp_body
end

end
end

Expand Down
16 changes: 16 additions & 0 deletions spec/oauth2/response_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'helper'
require 'zlib'

describe OAuth2::Response do
describe '#initialize' do
Expand Down Expand Up @@ -88,4 +89,19 @@
OAuth2::Response.new(response).parsed.should == {"foo" => {"bar" => "baz"}}
end
end

describe 'when gzip content-encoding' do
let(:headers) {{ 'content-type' => 'application/json', 'content-encoding' => 'gzip' }}
let(:original_data) {{ 'data' => 'some test data' }}
let(:body) do
str_io = StringIO.new("w")
Zlib::GzipWriter.new(str_io).tap do |gz|
gz.write(MultiJson.dump(original_data))
gz.close
end
compressed = str_io.string
end
let(:response) { double('response', status: 200, headers: headers, body: body) }
specify { OAuth2::Response.new(response).parsed.should eq original_data }
end
end

4 comments on commit cc040f7

@stavskiys
Copy link
Owner Author

Choose a reason for hiding this comment

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

This commit should resolve issue: NilClass# (OmniAuth::Strategies::Facebook::NoAuthorizationCodeError) "must pass either a code parameter or a signed request (via signed_request parameter or a fbsr_XXX cookie)"

@marknadig
Copy link

Choose a reason for hiding this comment

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

Seems Net::HTTP will decode get automatically http://ruby-doc.org/stdlib-2.1.0/libdoc/net/http/rdoc/Net/HTTP.html#method-i-get.
And Faraday seems assume this will be done as well lostisland/faraday_middleware#84

Just curious why this is falling down so that decoding needs to be done here in oauth2.

@romanbsd
Copy link

Choose a reason for hiding this comment

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

My faraday middleware actually doesn't assume anything, because of the great variety and inconsistency between net/http versions. It just asks for compressed data and uncompresses it.

@romanbsd
Copy link

Choose a reason for hiding this comment

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

imho, it depends on the Ruby version. For 2.1.1 it's probably not needed (see discussion here: lostisland/faraday_middleware#84 )

Please sign in to comment.