lib/ruby_wolf/handler.rb in ruby_wolf-0.3.0 vs lib/ruby_wolf/handler.rb in ruby_wolf-0.3.1

- old
+ new

@@ -1,11 +1,11 @@ require 'http/parser' module RubyWolf class Handler - attr_reader :app, :env, :connection, :response, :callback + attr_reader :app, :env, :connection, :response, :callback, :body, :headers, :status def initialize(app, connection, &callback) @app = app @connection = connection @callback = callback @@ -45,29 +45,20 @@ 'CONTENT_LENGTH' => connection.headers['Content-Length'], 'CONTENT_TYPE' => connection.headers['Content-Type'], 'rack.input' => StringIO.new(connection.read_chunk) ) - - RubyWolf.log( - [ - 'HTTP/1.1', - connection.method, - "#{connection.path}?#{connection.query}" - ].join(' ') - ) + log_request end def generate_response - status, headers, body = app.call(env) - RubyWolf.log( - "Response HTTP/1.1 #{status}" - ) - compose_response(status, headers, body) + @status, @headers, @body = app.call(env) + compose_response + log_response end - def compose_response(status, headers, body) + def compose_response @response += "HTTP/1.1 #{status} #{RubyWolf::CRLF}" headers.each do |key, value| @response += "#{key}: #{value}#{RubyWolf::CRLF}" end @@ -75,8 +66,34 @@ body.each do |part| @response += part end ensure body.close if body.respond_to? :close + end + + private + + def log_request + RubyWolf.logger.info( + [ + 'HTTP/1.1', + connection.method, + request_path + ].join(' ') + ) + end + + def log_response + RubyWolf.logger.info( + "Response HTTP/1.1 #{status}" + ) + end + + def request_path + if connection.query + "#{connection.path}?#{connection.query}" + else + connection.path + end end end end