lookout-rack-utils 3.0.1.11 → 3.1.0.12

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NjU0ZmVkMThlN2IxNThhZjY4YzNjMTc2NmI2ZWJlM2ZjODMzNWQxYg==
4
+ ZmMzMzI1NjY3NDYxZmZhYjc4ZGZhMzVmODFjNTQ0NjlmNWIzMTQ5ZA==
5
5
  data.tar.gz: !binary |-
6
- Y2JkNmUzZjY5Y2Q5MTRhMmYyOWM2ZjdjNTRiOGM0ZGI1MDk3ZmY1Yw==
6
+ YzJiOTE1MTJhMDU0N2I5Y2M2ODUzN2MxNzYzYzQ0NjI0MTg5YjU5Ng==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NTcwODkzN2ZkOGI4YTg0NDIxZTM3MjQzOGNkNjI0OGEyMjc4ODJmMjFiNWFi
10
- NGQzY2FkN2M0OWJmNDNmZGQ3MDgwYjJhMTlhY2M5YmY5MzAzOTlhYTY2YTYz
11
- NTg1ZDQwMzFjOTMyNzQ3N2VhNTIyNzg0MTBmYWJiMDBlMjAyMDY=
9
+ ZTJmYzY3NWM0NzBjMzk5MjdmYTkxYmMyZGEyYjAwOWNhYjM4OTAxYTgwMTM1
10
+ M2FjZTIwM2NmMTNhZjg4Mjk3YzI2NWMxYTM5YjljNmY0ZTMzNDFmZmVhZjU3
11
+ NmNkZWNjODk3Y2QyOTI5YmM4YjYwMzY3ZGMzYjFiMmNhMGM0YTM=
12
12
  data.tar.gz: !binary |-
13
- MDdkOTczM2VmNjBiNWU2MGM4ZTNhYWRhMzgyMmY5N2RmYzFiZjk0MGIzODRm
14
- N2QzNTBmMzA3MmVlMTljMDAzNzYyMDdmYmFkN2NhY2JjNzNjNjlkYjJhZWQx
15
- MjU5MGZkNzQ3MjhhYWIyYmQ0Mjg2OTRiZWU3YWIxYWVkM2Q1OTM=
13
+ MDkzODg2YTkyMzM1MTNiZDIyYjg3OWVlMzNkMjQ2NWIzMjc0OWVlMTgxNWMy
14
+ MjZiNDExNjQwZTAzNWZhMTNmNjE4NGM3ZDJlY2VjY2YxNjFjOGE0NGEzMWY5
15
+ YzJmMmJmNWQ2OGExYTJlNGZiMTliNDU3NzU5YWU4MWVmMGIwMjQ=
@@ -1,6 +1,10 @@
1
+ require 'zlib'
2
+
1
3
  module Lookout::Rack::Utils
2
4
  module Request
3
5
  ILLEGAL_CHARS_REGEX = /[<>]/
6
+ HTTP_CONTENT_ENCODING_KEY = "HTTP_CONTENT_ENCODING".freeze
7
+ CONTENT_ENCODING_GZIPPED = "gzip".freeze
4
8
 
5
9
  # Return the raw, unprocessed request body
6
10
  #
@@ -11,11 +15,32 @@ module Lookout::Rack::Utils
11
15
  return request.body.read
12
16
  end
13
17
 
14
- # Process and parse the request body as JSON
18
+ # Return the body, which is gunzipped only if the appropriate header is set on the request
15
19
  #
16
20
  # Will halt and create a a 400 status code if there is something wrong with
17
21
  # the body
18
22
  #
23
+ # @return [String]
24
+ def gunzipped_body
25
+ body = raw_body
26
+ if request.env[HTTP_CONTENT_ENCODING_KEY] != CONTENT_ENCODING_GZIPPED
27
+ return body
28
+ else
29
+ begin
30
+ return gunzip(body)
31
+ rescue Zlib::Error
32
+ if defined?(Lookout::Rack::Utils::Log)
33
+ Lookout::Rack::Utils::Log.instance.warn "Unzipping error when decompressing request body (#{body})"
34
+ end
35
+ halt 400, "{}"
36
+ end
37
+ end
38
+ end
39
+
40
+ # Process and parse the request body as JSON
41
+ #
42
+ # Will halt and create a a 400 status code if there is something wrong with
43
+ # the body
19
44
  def body_as_json
20
45
  body = raw_body
21
46
 
@@ -31,5 +56,24 @@ module Lookout::Rack::Utils
31
56
  halt 400, "{}"
32
57
  end
33
58
  end
59
+
60
+ private
61
+
62
+ # Decompresses data
63
+ #
64
+ # @raise [Zlib::Error] if malformed data is provided
65
+ # @return [String]
66
+ def gunzip(data)
67
+ # We set the window bits to MAX_WBITS + 32 to enable zlib and gzip decoding
68
+ # with automatic header detection.
69
+ zstream = Zlib::Inflate.new(Zlib::MAX_WBITS+32)
70
+ begin
71
+ decompressed = zstream.inflate(data)
72
+ ensure
73
+ zstream.close
74
+ end
75
+ decompressed
76
+ end
77
+
34
78
  end
35
79
  end
@@ -1,7 +1,7 @@
1
1
  module Lookout
2
2
  module Rack
3
3
  module Utils
4
- VERSION = "3.0.1"
4
+ VERSION = "3.1.0"
5
5
  end
6
6
  end
7
7
  end
@@ -0,0 +1,48 @@
1
+ require 'spec_helper'
2
+ require 'lookout/rack/utils/request'
3
+ require 'zlib'
4
+
5
+ class TestHelper
6
+ attr_accessor :request
7
+
8
+ include Lookout::Rack::Utils::Request
9
+
10
+ def initialize
11
+ end
12
+ end
13
+
14
+
15
+ describe Lookout::Rack::Utils::Request do
16
+ let(:helper) { TestHelper.new }
17
+ let(:sample_data) {'i am groot'}
18
+ let(:zipped_sample_data){Zlib::Deflate.deflate(sample_data)}
19
+
20
+ describe '#gunzipped_body' do
21
+
22
+ before :each do
23
+ helper.request = Object.new
24
+ helper.request.stub(:env).and_return({'HTTP_CONTENT_ENCODING' => 'gzip'})
25
+ helper.request.stub(:body).and_return(double)
26
+ helper.request.body.stub(:rewind).and_return(double)
27
+ end
28
+
29
+ it 'should unzip data zipped data properly' do
30
+ helper.request.body.stub(:read).and_return(zipped_sample_data)
31
+ expect(helper.gunzipped_body).to eq(sample_data)
32
+ end
33
+
34
+ it 'should do nothing if encoding is not set' do
35
+ helper.request.stub(:env).and_return({})
36
+ helper.request.body.stub(:read).and_return(zipped_sample_data)
37
+ expect(helper.gunzipped_body).to eq(zipped_sample_data)
38
+ end
39
+
40
+ it 'should halt and throw and 400 when we have badly encoded data' do
41
+ allow_any_instance_of(Lookout::Rack::Utils::Log).to receive(:warn)
42
+ helper.request.body.stub(:read).and_return(sample_data)
43
+ expect(helper).to receive(:halt).with(400, "{}")
44
+ helper.gunzipped_body
45
+ end
46
+ end
47
+
48
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lookout-rack-utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.1.11
4
+ version: 3.1.0.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ian Smith
@@ -216,6 +216,7 @@ files:
216
216
  - spec/graphite_spec.rb
217
217
  - spec/i18n_spec.rb
218
218
  - spec/log_spec.rb
219
+ - spec/request_spec.rb
219
220
  - spec/spec_helper.rb
220
221
  - spec/subroute_spec.rb
221
222
  - spec/support/routehelpers.rb
@@ -248,6 +249,7 @@ test_files:
248
249
  - spec/graphite_spec.rb
249
250
  - spec/i18n_spec.rb
250
251
  - spec/log_spec.rb
252
+ - spec/request_spec.rb
251
253
  - spec/spec_helper.rb
252
254
  - spec/subroute_spec.rb
253
255
  - spec/support/routehelpers.rb