degzipper 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1151434f8833470a8de8ba07d73bd3430c2330e9
4
- data.tar.gz: 51b24f05136a687187e2977a6bf7e589d118c836
3
+ metadata.gz: 9a36b7f29e14fe05b8873d292452bf787cde984f
4
+ data.tar.gz: d18190258ac989ccff637b97f0b6b2fbd3ac8883
5
5
  SHA512:
6
- metadata.gz: c7f93d6007d513925ab291f3757e502d7757d15126585116725d5ffa8e7b75c14f981d0802a2d1bdeb35d338525b425cbaf5b844b0c5d47f991822b4d631a21e
7
- data.tar.gz: 19149bcb8ac05e5470c0f35b13a626ad474dfeb18cdd754dc9e31bd599ee1866592e7f9bf3299e452a52ced7b5980525f700a85285c13472b675e78578e45fc6
6
+ metadata.gz: 3651b710bd5659f4f986f4e43ccaeae217854f18eeef3f6b22e69a54d025c1abaadaa0bc34a9a7fdc06a1d7cf2729717c3578a8451e1d59111e001b48b1cf39b
7
+ data.tar.gz: 625046d22854e92f3fb59e7f2b1476631b889906805796b196eb3eb24f5c9e79cea053eea14bc9d420488cda9447da83e5333a93a2bf4dcd6fe91174a924813d
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - "2.2.2"
@@ -0,0 +1,6 @@
1
+ # Change Log
2
+
3
+ ## [0.0.4] - 2015-06-29
4
+
5
+ ### Fixed
6
+ - [Fix no-utf encoding and add the `real` deflate implementation (#3)](https://github.com/andrhamm/degzipper/pull/3)
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:
@@ -21,4 +21,5 @@ Gem::Specification.new do |spec|
21
21
  spec.add_development_dependency "bundler", "~> 1.3"
22
22
  spec.add_development_dependency "rake"
23
23
  spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "rack"
24
25
  end
@@ -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
- status, headers, response = @app.call(env)
25
- return [status, headers, response]
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 'deflate' then Zlib::Inflate.inflate(input.read)
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
@@ -1,3 +1,3 @@
1
1
  module Degzipper
2
- VERSION = "0.0.3"
2
+ VERSION = '0.0.4'
3
3
  end
@@ -4,14 +4,15 @@ require 'rack'
4
4
  require 'zlib'
5
5
 
6
6
  RSpec.describe Degzipper::Middleware do
7
- def gzip(content)
8
- string_io = StringIO.new
9
-
10
- gz = Zlib::GzipWriter.new(string_io)
11
- gz.write content
12
- gz.close
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
- string_io.string
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, {}, [body]]
28
+ [200, {}, body]
28
29
  end)
29
30
  end
30
31
 
31
32
  it 'passes through a non-gzipped request body' do
32
- _, _, body = middleware.call(Rack::MockRequest.env_for(
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(parsed_body).to eq(
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
- it 'extracts a gzipped request body' do
48
- _, _, body = middleware.call(Rack::MockRequest.env_for(
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
- parsed_body = JSON.parse(body.first)
46
+ expect(resp).to eq(
47
+ 'body' => 'hello',
48
+ 'content_encoding' => nil,
49
+ 'length' => 5
50
+ )
51
+ end
56
52
 
57
- expect(parsed_body).to eq(
58
- 'body' => 'hello',
59
- 'content_encoding' => nil,
60
- 'length' => 5
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
- it 'sets the correct content length for UTF-8 content' do
65
- _, _, body = middleware.call(Rack::MockRequest.env_for(
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
- parsed_body = JSON.parse(body.first)
67
+ def compress(content)
68
+ string_io = StringIO.new
73
69
 
74
- expect(parsed_body).to eq(
75
- 'body' => '你好',
76
- 'content_encoding' => nil,
77
- 'length' => 6
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.3
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-05-11 00:00:00.000000000 Z
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.2.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.