rack-traffic-logger 0.1.1 → 0.1.2

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: f26f03dcdb7e324d22fe2d1eef5c171d2b0a2a7e
4
- data.tar.gz: 4dc100276db42b05d2d57673ec9a633de4ecef8a
3
+ metadata.gz: 735bee50aea81ceaff58c19569eb8dbac38806a1
4
+ data.tar.gz: 2ae588f2b1990d712692d5bff1bf96687d581a71
5
5
  SHA512:
6
- metadata.gz: b03a20d01b1c64ff07d84c450c21e4ecdba97626c0c892cc6c1a331a569c4ab10e64cb3a2e9e47f68c2e408b3c828fb5f01ed0e428670c69a49c1486fa721663
7
- data.tar.gz: 6281b3c7a221db00a86e1d4eb6178b75fc53bac6e506922a742580cb823922f06dc7ab04f8a3a35593ff757091d05258f08b0150a10d42bb301c3a916606bc8c
6
+ metadata.gz: c9c2680d8fe14afd6f9b3afa9b744fa6debac3235dca0232095fef4da816aecb05958ce1d7c8c1738187ca1411884cc54b473d229e4325cb1c2a4738cff74441
7
+ data.tar.gz: adab23dfc915298bb226fee7079086f1e85a555c8a517882ad1c3495910f508d8ec6c163d314a1c1dc2c28764d95ca3c3cfabc5b45dedefa1e73ecc27ae48179
@@ -4,6 +4,7 @@ require_relative 'traffic_logger/header_hash'
4
4
 
5
5
  require 'forwardable'
6
6
  require 'rack/nulllogger'
7
+ require 'json'
7
8
 
8
9
  module Rack
9
10
  class TrafficLogger
@@ -14,7 +15,9 @@ module Rack
14
15
  request_bodies: {type: [TrueClass, FalseClass], default: true},
15
16
  response_headers: {type: [TrueClass, FalseClass], default: true},
16
17
  response_bodies: {type: [TrueClass, FalseClass], default: true},
17
- colors: {type: [TrueClass, FalseClass], default: false}
18
+ colors: {type: [TrueClass, FalseClass], default: false},
19
+ prevent_compression: {type: [TrueClass, FalseClass], default: false},
20
+ pretty_print: {type: [TrueClass, FalseClass], default: false}
18
21
  }
19
22
 
20
23
  PUBLIC_ATTRIBUTES.each do |attr, props|
@@ -42,6 +45,7 @@ module Rack
42
45
  end
43
46
 
44
47
  def call(env)
48
+ env.delete 'HTTP_ACCEPT_ENCODING' if prevent_compression
45
49
  log_request! env
46
50
  @app.call(env).tap { |response| log_response! env, response }
47
51
  end
@@ -105,7 +109,11 @@ module Rack
105
109
  if response_bodies
106
110
  body = response[2]
107
111
  body = ::File.open(body.path, 'rb') { |f| f.read } if body.respond_to? :path
108
- body = body.tap(&:rewind).read if body.respond_to? :read
112
+ if body.respond_to? :read
113
+ stream = body
114
+ body = stream.tap(&:rewind).read
115
+ stream.rewind
116
+ end
109
117
  body = body.join if body.respond_to? :join
110
118
  log_body! body,
111
119
  type: headers['Content-Type'],
@@ -115,7 +123,9 @@ module Rack
115
123
  end
116
124
 
117
125
  def log_body!(body, type: nil, encoding: nil)
118
- body = "<#BINARY #{body.bytes.length} bytes>" if body =~ /[^[:print:]]/
126
+ body = Zlib::GzipReader.new(StringIO.new body).read if encoding == 'gzip'
127
+ body = JSON.pretty_generate(JSON.parse body) if type[/[^;]+/] == 'application/json' && pretty_print
128
+ body = "<#BINARY #{body.bytes.length} bytes>" if body =~ /[^[:print:]\r\n\t]/
119
129
  info body
120
130
  end
121
131
 
@@ -6,7 +6,17 @@ module Rack
6
6
  class Echo
7
7
  def call(env)
8
8
  begin
9
- [200, {'Content-Type' => 'application/json'}, [JSON.parse(env['rack.input'].tap(&:rewind).read)]]
9
+ body = JSON.parse(env['rack.input'].tap(&:rewind).read).to_json
10
+ headers = {'Content-Type' => 'application/json'}
11
+ if env['HTTP_ACCEPT_ENCODING'] =~ /\bgzip\b/
12
+ zipped = StringIO.new('w')
13
+ writer = Zlib::GzipWriter.new(zipped)
14
+ writer.write body
15
+ writer.close
16
+ body = zipped.string
17
+ headers['Content-Encoding'] = 'gzip'
18
+ end
19
+ [200, headers, [body]]
10
20
  rescue JSON::ParserError => error
11
21
  [
12
22
  500,
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  class TrafficLogger
3
- VERSION = '0.1.1'
3
+ VERSION = '0.1.2'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-traffic-logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Neil E. Pearson