Class: CompressedRequests

Inherits:
Object
  • Object
show all
Defined in:
lib/logstash/util/http_compressed_requests.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ CompressedRequests

Returns a new instance of CompressedRequests.



2
3
4
# File 'lib/logstash/util/http_compressed_requests.rb', line 2

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/logstash/util/http_compressed_requests.rb', line 14

def call(env)
  if method_handled?(env) && encoding_handled?(env)
    begin
      extracted = decode(env['rack.input'], env['HTTP_CONTENT_ENCODING'])
    rescue Zlib::Error
      return [400, {'Content-Type' => 'text/plain'}, ["Failed to decompress body"]]
    end

    env.delete('HTTP_CONTENT_ENCODING')
    env['CONTENT_LENGTH'] = extracted.bytesize
    env['rack.input'] = StringIO.new(extracted)
  end

  @app.call(env)
end

#decode(input, content_encoding) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/logstash/util/http_compressed_requests.rb', line 30

def decode(input, content_encoding)
  case content_encoding
    when 'gzip' then
      Zlib::GzipReader.new(input).read
    when 'deflate' then
      Zlib::Inflate.inflate(input.read)
  end
end

#encoding_handled?(env) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/logstash/util/http_compressed_requests.rb', line 10

def encoding_handled?(env)
  ['gzip', 'deflate'].include? env['HTTP_CONTENT_ENCODING']
end

#method_handled?(env) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/logstash/util/http_compressed_requests.rb', line 6

def method_handled?(env)
  !!(env['REQUEST_METHOD'] =~ /(POST|PUT)/)
end