This repository has been archived by the owner on Aug 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
client: add auto_decode option: decode automatically deflate or gzip …
…encoded response
- Loading branch information
Showing
9 changed files
with
150 additions
and
17 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
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,51 @@ | ||
module Plum | ||
module Decoders | ||
class Base | ||
def decode(chunk) | ||
chunk | ||
end | ||
|
||
def finish | ||
end | ||
end | ||
|
||
# `deflate` is not just deflate, wrapped by zlib format (RFC 1950) | ||
class Deflate < Base | ||
def initialize | ||
@inflate = Zlib::Inflate.new(Zlib::MAX_WBITS) | ||
end | ||
|
||
def decode(chunk) | ||
@inflate.inflate(chunk) | ||
rescue Zlib::Error => e | ||
raise DecoderError.new("failed to decode chunk", e) | ||
end | ||
|
||
def finish | ||
@inflate.finish | ||
rescue Zlib::Error => e | ||
raise DecoderError.new("failed to finalize", e) | ||
end | ||
end | ||
|
||
class GZip < Base | ||
def initialize | ||
@stream = Zlib::Inflate.new(Zlib::MAX_WBITS + 16) | ||
end | ||
|
||
def decode(chunk) | ||
@stream.inflate(chunk) | ||
rescue Zlib::Error => e | ||
raise DecoderError.new("failed to decode chunk", e) | ||
end | ||
|
||
def finish | ||
@stream.finish | ||
rescue Zlib::Error => e | ||
raise DecoderError.new("failed to finalize", e) | ||
end | ||
end | ||
|
||
DECODERS = { "gzip" => GZip, "deflate" => Deflate }.freeze | ||
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
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,54 @@ | ||
require "test_helper" | ||
|
||
using Plum::BinaryString | ||
class DecodersTest < Minitest::Test | ||
def test_base_decode | ||
decoder = Decoders::Base.new | ||
assert_equal("abc", decoder.decode("abc")) | ||
end | ||
|
||
def test_base_finish | ||
decoder = Decoders::Base.new | ||
decoder.finish | ||
end | ||
|
||
def test_deflate_decode | ||
decoder = Decoders::Deflate.new | ||
assert_equal("hello", decoder.decode("\x78\x9c\xcb\x48\xcd\xc9\xc9\x07\x00\x06\x2c\x02\x15")) | ||
end | ||
|
||
def test_deflate_decode_error | ||
decoder = Decoders::Deflate.new | ||
assert_raises(DecoderError) { | ||
decoder.decode("\x79\x9c\xcb\x48\xcd\xc9\xc9\x07\x00\x06\x2c\x02\x15") | ||
} | ||
end | ||
|
||
def test_deflate_finish_error | ||
decoder = Decoders::Deflate.new | ||
decoder.decode("\x78\x9c\xcb\x48\xcd\xc9\xc9\x07\x00\x06\x2c\x02") | ||
assert_raises(DecoderError) { | ||
decoder.finish | ||
} | ||
end | ||
|
||
def test_gzip_decode | ||
decoder = Decoders::GZip.new | ||
assert_equal("hello", decoder.decode("\x1f\x8b\x08\x00\x1a\x96\xe0\x4c\x00\x03\xcb\x48\xcd\xc9\xc9\x07\x00\x86\xa6\x10\x36\x05\x00\x00\x00")) | ||
end | ||
|
||
def test_gzip_decode_error | ||
decoder = Decoders::GZip.new | ||
assert_raises(DecoderError) { | ||
decoder.decode("\x2f\x8b\x08\x00\x1a\x96\xe0\x4c\x00\x03\xcb\x48\xcd\xc9\xc9\x07\x00\x86\xa6\x10\x36\x05\x00\x00\x00") | ||
} | ||
end | ||
|
||
def test_gzip_finish_error | ||
decoder = Decoders::GZip.new | ||
decoder.decode("\x1f\x8b\x08\x00\x1a\x96") | ||
assert_raises(DecoderError) { | ||
decoder.finish | ||
} | ||
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