rack-traffic-logger 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2ca028e1a99d48ccc15d12bbe7173a46b1b10c8c
4
+ data.tar.gz: 59f69d7fd8ea9ba939d58f9620f68fb7fade8747
5
+ SHA512:
6
+ metadata.gz: b404dc7d0092b9c5e3945ac38b992e14eb3cbfa2a204cfb6c60c495ae57235be3cca7631a0174914af25a2fd05ab037612523f59b5ff40aba16469d6e9125364
7
+ data.tar.gz: 3838f643eae3525c4ca23e3a9602634b8f41958f268f2673627c673de07e844ba558faa9e4e8c9c98b9744c47c9d58e5ebe2fc2964643df418e8a23896fa51b3
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Neil E. Pearson
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # Rack::TrafficLogger
2
+
3
+ TODO: Write a gem description
@@ -0,0 +1,116 @@
1
+ require_relative 'traffic_logger/version'
2
+ require_relative 'traffic_logger/logger'
3
+
4
+ require 'forwardable'
5
+ require 'rack/nulllogger'
6
+
7
+ module Rack
8
+ class TrafficLogger
9
+ extend Forwardable
10
+
11
+ PUBLIC_ATTRIBUTES = {
12
+ request_headers: {type: [TrueClass, FalseClass], default: true},
13
+ request_bodies: {type: [TrueClass, FalseClass], default: true},
14
+ response_headers: {type: [TrueClass, FalseClass], default: true},
15
+ response_bodies: {type: [TrueClass, FalseClass], default: true},
16
+ colors: {type: [TrueClass, FalseClass], default: false}
17
+ }
18
+
19
+ PUBLIC_ATTRIBUTES.each do |attr, props|
20
+ type = props[:type]
21
+ type = [type] unless Array === type
22
+ define_method(attr) { @options[attr] }
23
+ define_method :"#{attr}=" do |value|
24
+ raise "Expected one of [#{type.map(&:name).join ' | '}], got #{value.class.name}" unless type.find { |t| t === value }
25
+ @options[attr] = value
26
+ end
27
+ end
28
+
29
+ delegate %i(info debug warn error fatal) => :@logger
30
+
31
+ def initialize(app, logger = nil, options = {})
32
+ Raise "Expected a Hash, but got #{options.class.name}" unless Hash === options
33
+ @app = app
34
+ case logger
35
+ when nil, false then logger = Logger.new(STDOUT)
36
+ when String, IO then logger = Logger.new(logger)
37
+ else logger = Rack::NullLogger.new(nil) unless logger.respond_to? :debug
38
+ end
39
+ @logger = logger
40
+ @options = self.class.default_options.merge(options)
41
+ end
42
+
43
+ def call(env)
44
+ log_request! env
45
+ @app.call(env).tap { |response| log_response! env, response }
46
+ end
47
+
48
+ private
49
+
50
+ def self.default_options
51
+ @default_options ||= PUBLIC_ATTRIBUTES.map { |k, v| [k, v[:default]] }.to_h
52
+ end
53
+
54
+ REQUEST_TEMPLATES = {
55
+ true => "%s %s%s HTTP/%s",
56
+ false => "%s %s%s HTTP/%s"
57
+ }
58
+
59
+ def log_request!(env)
60
+ debug REQUEST_TEMPLATES[colors] % [
61
+ env['REQUEST_METHOD'],
62
+ env['PATH_INFO'],
63
+ (q = env['QUERY_STRING']).empty? ? '' : "?#{q}",
64
+ env['HTTP_VERSION'] || '1.1'
65
+ ]
66
+ log_headers! env_request_headers(env) if request_headers
67
+ input = env['rack.input']
68
+ if request_bodies && input
69
+ log_body! input.read
70
+ input.rewind
71
+ end
72
+ end
73
+
74
+ RESPONSE_TEMPLATES = {
75
+ true => "HTTP/%s %s %s",
76
+ false => "HTTP/%s %s %s"
77
+ }
78
+
79
+ def log_response!(env, response)
80
+ debug RESPONSE_TEMPLATES[colors] % [
81
+ env['HTTP_VERSION'] || '1.1',
82
+ code = response[0],
83
+ Rack::Utils::HTTP_STATUS_CODES[code]
84
+ ]
85
+ log_headers! response[1] if response_headers
86
+ log_body! response[2].join if response_bodies
87
+ end
88
+
89
+ def log_body!(body)
90
+ info body
91
+ end
92
+
93
+ HEADER_TEMPLATES = {
94
+ true => "%s: %s\n",
95
+ false => "%s: %s\n"
96
+ }
97
+
98
+ def log_headers!(headers)
99
+ info headers.map { |k, v| HEADER_TEMPLATES[colors] % [k, v] }.join
100
+ end
101
+
102
+ def env_request_headers(env)
103
+ env.select do |k, _|
104
+ k =~ /^(CONTENT|HTTP)_/
105
+ end.map do |(k, v)|
106
+ [
107
+ k.sub(/^HTTP_/, '').split(/[_ -]/).map do |word|
108
+ word[0].upcase << word[1..-1].downcase
109
+ end.join('-'),
110
+ v
111
+ ]
112
+ end.to_h
113
+ end
114
+
115
+ end
116
+ end
@@ -0,0 +1,12 @@
1
+ require_relative '../traffic_logger'
2
+ require 'json'
3
+
4
+ module Rack
5
+ class TrafficLogger
6
+ class Echo
7
+ def call(env)
8
+ JSON.parse env['rack.input'].tap(&:rewind).read
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,9 @@
1
+ require 'logger'
2
+
3
+ module Rack
4
+ class TrafficLogger
5
+ class Logger < ::Logger
6
+
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ class TrafficLogger
3
+ VERSION = '0.0.1'
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-traffic-logger
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Neil E. Pearson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1'
27
+ description: Rack Middleware for logging raw incoming/outgoing HTTP traffic
28
+ email:
29
+ - neil@helium.net.au
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - LICENSE.txt
35
+ - README.md
36
+ - lib/rack/traffic_logger.rb
37
+ - lib/rack/traffic_logger/echo.rb
38
+ - lib/rack/traffic_logger/logger.rb
39
+ - lib/rack/traffic_logger/version.rb
40
+ homepage: https://github.com/hx/rack-traffic-logger
41
+ licenses:
42
+ - MIT
43
+ metadata: {}
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubyforge_project:
60
+ rubygems_version: 2.2.2
61
+ signing_key:
62
+ specification_version: 4
63
+ summary: Rack Raw HTTP Traffic Logger
64
+ test_files: []
65
+ has_rdoc: