Class: Marcopolo::Middleware
- Inherits:
-
Object
- Object
- Marcopolo::Middleware
- Defined in:
- lib/marcopolo/middleware.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app) ⇒ Middleware
constructor
A new instance of Middleware.
- #rawlog_request(env) ⇒ Object
- #rawlog_response(status, headers, response) ⇒ Object
- #request_body ⇒ Object
Constructor Details
#initialize(app) ⇒ Middleware
Returns a new instance of Middleware.
3 4 5 |
# File 'lib/marcopolo/middleware.rb', line 3 def initialize(app) @app = app end |
Instance Method Details
#call(env) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/marcopolo/middleware.rb', line 7 def call(env) @request = Rack::Request.new(env) allow = Marcopolo.allow(@request) if allow rawlog_request(env) else Marcopolo.log "Filtering request: #{@request.request_method} #{@request.url}" end status, headers, response = @app.call(env) rawlog_response(status, headers, response) if allow return [status, headers, response] end |
#rawlog_request(env) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/marcopolo/middleware.rb', line 25 def rawlog_request(env) req_headers = env.select {|k,v| k.start_with? 'HTTP_'} .collect {|pair| [pair[0].sub(/^HTTP_/, '').split('_').map(&:titleize).join('-'), pair[1]]} .sort req_hash = { "REQUEST" => "", "Remote Address" => @request.ip, "Request URL" => @request.url, "Request Method" => @request.request_method, "REQUEST HEADERS" => "" } req_headers.to_a.each {|i| req_hash["\t" + i.first] = i.last } req_hash.merge!({ "Request Body" => request_body }) Marcopolo.log req_hash.to_a.map {|o| o.join(': ') }.join("\n") + "\n" rescue => e Marcopolo.log "Failed to log request: #{e}" end |
#rawlog_response(status, headers, response) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/marcopolo/middleware.rb', line 55 def rawlog_response(status, headers, response) resp_hash = { "RESPONSE" => "", "Response Status" => status, "Response Headers" => "" } headers.to_a.each {|i| resp_hash["\t" + i.first] = i.last } response_body = response.respond_to?(:body) ? response.body : response resp_hash.merge!({ "Response Body" => response_body }) Marcopolo.log resp_hash.to_a.map {|o| o.join(': ') }.join("\n") + "\n" rescue => e Marcopolo.log "Failed to log response: #{e}" end |
#request_body ⇒ Object
49 50 51 52 53 |
# File 'lib/marcopolo/middleware.rb', line 49 def request_body @request.body.read ensure @request.body.rewind end |