rack-traffic-logger 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rack/traffic_logger.rb +13 -3
- data/lib/rack/traffic_logger/echo.rb +11 -1
- data/lib/rack/traffic_logger/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 735bee50aea81ceaff58c19569eb8dbac38806a1
|
4
|
+
data.tar.gz: 2ae588f2b1990d712692d5bff1bf96687d581a71
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c9c2680d8fe14afd6f9b3afa9b744fa6debac3235dca0232095fef4da816aecb05958ce1d7c8c1738187ca1411884cc54b473d229e4325cb1c2a4738cff74441
|
7
|
+
data.tar.gz: adab23dfc915298bb226fee7079086f1e85a555c8a517882ad1c3495910f508d8ec6c163d314a1c1dc2c28764d95ca3c3cfabc5b45dedefa1e73ecc27ae48179
|
data/lib/rack/traffic_logger.rb
CHANGED
@@ -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
|
-
|
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 =
|
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
|
-
|
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,
|