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(env) ⇒ 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) @status, @headers, @response = @app.call(env) if Marcopolo.allow(@request) begin rawlog(env) rescue => e Marcopolo.log "Failed to log request: #{e.message}" end else Marcopolo.log "Filtering request: #{@request.request_method} #{@request.url}" end return [@status, @headers, @response] end |
#rawlog(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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/marcopolo/middleware.rb', line 25 def rawlog(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.gets }) Marcopolo.log req_hash.to_a.map {|o| o.join(': ') }.join("\n") + "\n" 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" end |