degzipper 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +3 -0
- data/CHANGELOG.md +6 -0
- data/README.md +1 -1
- data/degzipper.gemspec +1 -0
- data/lib/degzipper/middleware.rb +20 -13
- data/lib/degzipper/version.rb +1 -1
- data/spec/degzipper_spec.rb +59 -42
- metadata +19 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9a36b7f29e14fe05b8873d292452bf787cde984f
|
4
|
+
data.tar.gz: d18190258ac989ccff637b97f0b6b2fbd3ac8883
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3651b710bd5659f4f986f4e43ccaeae217854f18eeef3f6b22e69a54d025c1abaadaa0bc34a9a7fdc06a1d7cf2729717c3578a8451e1d59111e001b48b1cf39b
|
7
|
+
data.tar.gz: 625046d22854e92f3fb59e7f2b1476631b889906805796b196eb3eb24f5c9e79cea053eea14bc9d420488cda9447da83e5333a93a2bf4dcd6fe91174a924813d
|
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# Degzipper
|
2
2
|
Rack middleware to inflate incoming Gzip data from HTTP requests.
|
3
3
|
|
4
|
-
[![Code Climate](https://codeclimate.com/github/andrhamm/degzipper.png)](https://codeclimate.com/github/andrhamm/degzipper)
|
4
|
+
[![Build Status](https://travis-ci.org/andrhamm/degzipper.svg)](https://travis-ci.org/andrhamm/degzipper) [![Code Climate](https://codeclimate.com/github/andrhamm/degzipper.png)](https://codeclimate.com/github/andrhamm/degzipper)
|
5
5
|
|
6
6
|
## Getting Started
|
7
7
|
Degzipper is a Rack Middleware. That means that you can use it the same way that you use any other Rack middleware. For example, to use in a Sinatra application I might do this:
|
data/degzipper.gemspec
CHANGED
data/lib/degzipper/middleware.rb
CHANGED
@@ -4,31 +4,38 @@ module Degzipper
|
|
4
4
|
@app = app
|
5
5
|
end
|
6
6
|
|
7
|
-
def method_handled?(env)
|
8
|
-
!!(env['REQUEST_METHOD'] =~ /(POST|PUT|PATCH)/)
|
9
|
-
end
|
10
|
-
|
11
|
-
def encoding_handled?(env)
|
12
|
-
['gzip', 'deflate'].include? env['HTTP_CONTENT_ENCODING']
|
13
|
-
end
|
14
|
-
|
15
7
|
def call(env)
|
16
|
-
if method_handled?(env) && encoding_handled?(env)
|
8
|
+
if method_handled?(env['REQUEST_METHOD']) && encoding_handled?(env['HTTP_CONTENT_ENCODING'])
|
17
9
|
extracted = decode(env['rack.input'], env['HTTP_CONTENT_ENCODING'])
|
18
10
|
|
19
11
|
env.delete('HTTP_CONTENT_ENCODING')
|
20
12
|
env['CONTENT_LENGTH'] = extracted.bytesize
|
21
|
-
env['rack.input'] = StringIO.new(extracted)
|
13
|
+
env['rack.input'] = StringIO.new(extracted).set_encoding('utf-8')
|
22
14
|
end
|
23
15
|
|
24
|
-
|
25
|
-
|
16
|
+
@app.call(env)
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def method_handled?(method)
|
22
|
+
['POST', 'PUT', 'PATCH'].include? method
|
23
|
+
end
|
24
|
+
|
25
|
+
def encoding_handled?(encoding)
|
26
|
+
['gzip', 'zlib', 'deflate'].include? encoding
|
26
27
|
end
|
27
28
|
|
28
29
|
def decode(input, content_encoding)
|
29
30
|
case content_encoding
|
30
31
|
when 'gzip' then Zlib::GzipReader.new(input).read
|
31
|
-
when '
|
32
|
+
when 'zlib' then Zlib::Inflate.inflate(input.string)
|
33
|
+
when 'deflate'
|
34
|
+
stream = Zlib::Inflate.new(-Zlib::MAX_WBITS)
|
35
|
+
content = stream.inflate(input.string)
|
36
|
+
stream.finish
|
37
|
+
stream.close
|
38
|
+
content
|
32
39
|
end
|
33
40
|
end
|
34
41
|
end
|
data/lib/degzipper/version.rb
CHANGED
data/spec/degzipper_spec.rb
CHANGED
@@ -4,14 +4,15 @@ require 'rack'
|
|
4
4
|
require 'zlib'
|
5
5
|
|
6
6
|
RSpec.describe Degzipper::Middleware do
|
7
|
-
def
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
7
|
+
def make_request(input, encoding)
|
8
|
+
body = middleware.call(Rack::MockRequest.env_for(
|
9
|
+
'/',
|
10
|
+
method: 'POST',
|
11
|
+
input: input,
|
12
|
+
'HTTP_CONTENT_ENCODING' => encoding
|
13
|
+
))[2]
|
13
14
|
|
14
|
-
|
15
|
+
JSON.parse(body)
|
15
16
|
end
|
16
17
|
|
17
18
|
let(:middleware) do
|
@@ -24,57 +25,73 @@ RSpec.describe Degzipper::Middleware do
|
|
24
25
|
length: req.content_length.to_i
|
25
26
|
)
|
26
27
|
|
27
|
-
[200, {},
|
28
|
+
[200, {}, body]
|
28
29
|
end)
|
29
30
|
end
|
30
31
|
|
31
32
|
it 'passes through a non-gzipped request body' do
|
32
|
-
|
33
|
-
'/',
|
34
|
-
method: 'POST',
|
35
|
-
input: 'hello'
|
36
|
-
))
|
37
|
-
|
38
|
-
parsed_body = JSON.parse(body.first)
|
33
|
+
resp = make_request('hello', nil)
|
39
34
|
|
40
|
-
expect(
|
35
|
+
expect(resp).to eq(
|
41
36
|
'body' => 'hello',
|
42
37
|
'content_encoding' => nil,
|
43
38
|
'length' => 5
|
44
39
|
)
|
45
40
|
end
|
46
41
|
|
47
|
-
|
48
|
-
|
49
|
-
'
|
50
|
-
method: 'POST',
|
51
|
-
input: gzip('hello'),
|
52
|
-
'HTTP_CONTENT_ENCODING' => 'gzip'
|
53
|
-
))
|
42
|
+
shared_examples_for 'a compression' do |type|
|
43
|
+
it 'extracts a compressed request body' do
|
44
|
+
resp = make_request(compress('hello'), type)
|
54
45
|
|
55
|
-
|
46
|
+
expect(resp).to eq(
|
47
|
+
'body' => 'hello',
|
48
|
+
'content_encoding' => nil,
|
49
|
+
'length' => 5
|
50
|
+
)
|
51
|
+
end
|
56
52
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
53
|
+
it 'sets the correct content length for UTF-8 content' do
|
54
|
+
resp = make_request(compress('你好'), type)
|
55
|
+
|
56
|
+
expect(resp).to eq(
|
57
|
+
'body' => '你好',
|
58
|
+
'content_encoding' => nil,
|
59
|
+
'length' => 6
|
60
|
+
)
|
61
|
+
end
|
62
62
|
end
|
63
63
|
|
64
|
-
|
65
|
-
|
66
|
-
'/',
|
67
|
-
method: 'POST',
|
68
|
-
input: gzip('你好'),
|
69
|
-
'HTTP_CONTENT_ENCODING' => 'gzip'
|
70
|
-
))
|
64
|
+
context 'gzip' do
|
65
|
+
it_behaves_like 'a compression', 'gzip'
|
71
66
|
|
72
|
-
|
67
|
+
def compress(content)
|
68
|
+
string_io = StringIO.new
|
73
69
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
70
|
+
gz = Zlib::GzipWriter.new(string_io)
|
71
|
+
gz.write content
|
72
|
+
gz.close
|
73
|
+
|
74
|
+
string_io.string
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
|
79
|
+
context 'zlib' do
|
80
|
+
it_behaves_like 'a compression', 'zlib'
|
81
|
+
|
82
|
+
def compress(content)
|
83
|
+
Zlib::Deflate.deflate(content)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
context 'deflate' do
|
88
|
+
it_behaves_like 'a compression', 'deflate'
|
89
|
+
|
90
|
+
def compress(content)
|
91
|
+
stream = Zlib::Deflate.new(Zlib::DEFAULT_COMPRESSION, -Zlib::MAX_WBITS)
|
92
|
+
result = stream.deflate(content, Zlib::FINISH)
|
93
|
+
stream.close
|
94
|
+
result
|
95
|
+
end
|
79
96
|
end
|
80
97
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: degzipper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Hammond
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-
|
12
|
+
date: 2015-06-29 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -53,6 +53,20 @@ dependencies:
|
|
53
53
|
- - ">="
|
54
54
|
- !ruby/object:Gem::Version
|
55
55
|
version: '0'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: rack
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
56
70
|
description: Rack middleware to inflate incoming Gzip data from HTTP requests.
|
57
71
|
email:
|
58
72
|
- andrew@evertrue.com
|
@@ -62,6 +76,8 @@ extra_rdoc_files: []
|
|
62
76
|
files:
|
63
77
|
- ".gitignore"
|
64
78
|
- ".rspec"
|
79
|
+
- ".travis.yml"
|
80
|
+
- CHANGELOG.md
|
65
81
|
- Gemfile
|
66
82
|
- LICENSE.txt
|
67
83
|
- README.md
|
@@ -92,7 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
92
108
|
version: '0'
|
93
109
|
requirements: []
|
94
110
|
rubyforge_project:
|
95
|
-
rubygems_version: 2.
|
111
|
+
rubygems_version: 2.4.8
|
96
112
|
signing_key:
|
97
113
|
specification_version: 4
|
98
114
|
summary: Rack middleware to inflate incoming Gzip data from HTTP requests.
|