Skip to content

Instantly share code, notes, and snippets.

@jasl
Created July 6, 2013 10:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasl/5939460 to your computer and use it in GitHub Desktop.
Save jasl/5939460 to your computer and use it in GitHub Desktop.
A monkey patch to fix a issue on decompress response. A typical scenario is omnioauth do oauth through proxy.
class Net::HTTPResponse
class Inflater
##
# Finishes the inflate stream.
def finish
begin
@inflate.finish
rescue Zlib::DataError
# No luck with Zlib decompression. Let's try with raw deflate,
# like some broken web servers do.
# taken by rest-client lib/restclient/request.rb L243-247
@inflate = Zlib::Inflate.new(-Zlib::MAX_WBITS)
retry
end
end
##
# Returns a Net::ReadAdapter that inflates each read chunk into +dest+.
#
# This allows a large response body to be inflated without storing the
# entire body in memory.
def inflate_adapter(dest)
block = proc do |compressed_chunk|
begin
@inflate.inflate(compressed_chunk) do |chunk|
dest << chunk
end
rescue Zlib::DataError
# No luck with Zlib decompression. Let's try with raw deflate,
# like some broken web servers do.
# taken by rest-client lib/restclient/request.rb L243-247
@inflate = Zlib::Inflate.new(-Zlib::MAX_WBITS)
retry
end
end
Net::ReadAdapter.new(block)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment