Skip to content

Commit

Permalink
Support zstd decompression
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyshields committed Jun 26, 2022
1 parent 907accf commit 715f971
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
16 changes: 11 additions & 5 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,16 @@ If you explicitly set `Accept-Encoding`, there be dragons:
`Net::HTTP` will automatically decompress it, and will omit `Content-Encoding`
from your `HTTParty::Response` headers.

* For encodings `br` (Brotli) or `compress` (LZW), HTTParty will automatically
decompress if you include the `brotli` or `ruby-lzws` gems respectively into your project.
* For the following encodings, HTTParty will automatically decompress them if you include
the required gem into your project. Similar to above, if decompression succeeds,
`Content-Encoding` will be omitted from your `HTTParty::Response` headers.
**Warning:** Support for these encodings is experimental and not fully battle-tested.
Similar to above, if decompression succeeds, `Content-Encoding` will be omitted
from your `HTTParty::Response` headers.

| Encoding | Required Gem |
| --- | --- |
| `br` (Brotli) | [brotli](https://rubygems.org/gems/brotli) |
| `compress` (LZW) | [ruby-lzws](https://rubygems.org/gems/ruby-lzws) |
| `zstd` (Zstandard) | [zstd-ruby](https://rubygems.org/gems/zstd-ruby) |

* For other encodings, `HTTParty::Response#body` will return the raw uncompressed byte string,
and you'll need to inspect the `Content-Encoding` response header and decompress it yourself.
Expand All @@ -147,7 +152,8 @@ JSON.parse(res.body) # safe

require 'brotli'
require 'lzws'
res = HTTParty.get('https://example.com/test.json', headers: { 'Accept-Encoding' => 'br,compress' })
require 'zstd-ruby'
res = HTTParty.get('https://example.com/test.json', headers: { 'Accept-Encoding' => 'br,compress,zstd' })
JSON.parse(res.body)


Expand Down
12 changes: 11 additions & 1 deletion lib/httparty/decompressor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class Decompressor
'none' => :none,
'identity' => :none,
'br' => :brotli,
'compress' => :lzw
'compress' => :lzw,
'zstd' => :zstd
}.freeze

# The response body of the request
Expand Down Expand Up @@ -88,5 +89,14 @@ def lzw
nil
end
end

def zstd
return nil unless defined?(::Zstd)
begin
::Zstd.decompress(body)
rescue StandardError
nil
end
end
end
end
28 changes: 28 additions & 0 deletions spec/httparty/decompressor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,5 +131,33 @@
end
end
end

context 'when encoding is "zstd"' do
let(:encoding) { 'zstd' }

context 'when zstd-ruby gem not included' do
it_behaves_like 'returns nil'
end

context 'when zstd-ruby included' do
before do
dbl = double('Zstd')
expect(dbl).to receive(:decompress).with('body').and_return('foobar')
stub_const('Zstd', dbl)
end

it { expect(subject).to eq 'foobar' }
end

context 'when zstd raises error' do
before do
dbl = double('Zstd')
expect(dbl).to receive(:decompress).with('body') { raise RuntimeError.new('zstd error') }
stub_const('Zstd', dbl)
end

it { expect(subject).to eq nil }
end
end
end
end

0 comments on commit 715f971

Please sign in to comment.