-
Notifications
You must be signed in to change notification settings - Fork 961
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #729 from tablecheck/fix-decompression
Better handling of Accept-Encoding / Content-Encoding decompression (fixes #562)
- Loading branch information
Showing
9 changed files
with
475 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# frozen_string_literal: true | ||
|
||
module HTTParty | ||
# Decompresses the response body based on the Content-Encoding header. | ||
# | ||
# Net::HTTP automatically decompresses Content-Encoding values "gzip" and "deflate". | ||
# This class will handle "br" (Brotli) and "compress" (LZW) if the requisite | ||
# gems are installed. Otherwise, it returns nil if the body data cannot be | ||
# decompressed. | ||
# | ||
# @abstract Read the HTTP Compression section for more information. | ||
class Decompressor | ||
|
||
# "gzip" and "deflate" are handled by Net::HTTP | ||
# hence they do not need to be handled by HTTParty | ||
SupportedEncodings = { | ||
'none' => :none, | ||
'identity' => :none, | ||
'br' => :brotli, | ||
'compress' => :lzw | ||
}.freeze | ||
|
||
# The response body of the request | ||
# @return [String] | ||
attr_reader :body | ||
|
||
# The Content-Encoding algorithm used to encode the body | ||
# @return [Symbol] e.g. :gzip | ||
attr_reader :encoding | ||
|
||
# @param [String] body - the response body of the request | ||
# @param [Symbol] encoding - the Content-Encoding algorithm used to encode the body | ||
def initialize(body, encoding) | ||
@body = body | ||
@encoding = encoding | ||
end | ||
|
||
# Perform decompression on the response body | ||
# @return [String] the decompressed body | ||
# @return [nil] when the response body is nil or cannot decompressed | ||
def decompress | ||
return nil if body.nil? | ||
return body if encoding.nil? || encoding.strip.empty? | ||
|
||
if supports_encoding? | ||
decompress_supported_encoding | ||
else | ||
nil | ||
end | ||
end | ||
|
||
protected | ||
|
||
def supports_encoding? | ||
SupportedEncodings.keys.include?(encoding) | ||
end | ||
|
||
def decompress_supported_encoding | ||
method = SupportedEncodings[encoding] | ||
if respond_to?(method, true) | ||
send(method) | ||
else | ||
raise NotImplementedError, "#{self.class.name} has not implemented a decompression method for #{encoding.inspect} encoding." | ||
end | ||
end | ||
|
||
def none | ||
body | ||
end | ||
|
||
def brotli | ||
return nil unless defined?(::Brotli) | ||
begin | ||
::Brotli.inflate(body) | ||
rescue StandardError | ||
nil | ||
end | ||
end | ||
|
||
def lzw | ||
begin | ||
if defined?(::LZWS::String) | ||
::LZWS::String.decompress(body) | ||
elsif defined?(::LZW::Simple) | ||
::LZW::Simple.new.decompress(body) | ||
end | ||
rescue StandardError | ||
nil | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.