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(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) 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(env) 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 |
# 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.gets }) Marcopolo.log req_hash.to_a.map {|o| o.join(': ') }.join("\n") + "\n" end |
#rawlog_response(env) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/marcopolo/middleware.rb', line 47 def rawlog_response(env) 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 |