lib/ruby_wolf/handler.rb in ruby_wolf-0.2.0 vs lib/ruby_wolf/handler.rb in ruby_wolf-0.3.0
- old
+ new
@@ -13,63 +13,61 @@
@response = ''
end
def process
prepare_rack_env
- parse_request
generate_response
callback.call(response) if callback
response
end
private
def prepare_rack_env
- @env = ENV.to_h
- @env.delete 'HTTP_CONTENT_LENGTH'
- url_scheme = %w(yes on 1).include?(ENV[::Rack::HTTPS]) ? 'https' : 'http'
- @env.update(
- ::Rack::RACK_VERSION => ::Rack::VERSION,
- ::Rack::RACK_INPUT => STDIN,
- ::Rack::RACK_ERRORS => STDERR,
- ::Rack::RACK_MULTITHREAD => false,
- ::Rack::RACK_MULTIPROCESS => true,
- ::Rack::RACK_RUNONCE => true,
- ::Rack::RACK_URL_SCHEME => url_scheme,
- ::Rack::SERVER_PROTOCOL => 'HTTP/1.1'
- )
- end
+ @env = ENV.to_h.merge(
+ 'rack.version' => ::Rack::VERSION,
+ 'rack.errors' => STDERR,
+ 'rack.multithread' => false,
+ 'rack.multiprocess' => true,
+ 'rack.runonce' => true,
+ 'rack.url_scheme' => ENV['HTTPS'] ? 'https' : 'http',
- def parse_request
- parser = Http::Parser.new
- parser.on_headers_complete = proc do
- env[::Rack::HTTP_VERSION] = parser.http_version
- env[::Rack::REQUEST_METHOD] = parser.http_method
+ 'REQUEST_METHOD' => connection.method,
+ 'REQUEST_PATH' => connection.path,
+ 'PATH_INFO' => connection.path,
+ 'QUERY_STRING' => connection.query,
- uri = URI.parse(parser.request_url)
- env[::Rack::REQUEST_PATH] = uri.path
- env[::Rack::QUERY_STRING] = uri.query
- end
- parser << connection.read_data
+ 'SERVER_PROTOCOL' => 'HTTP/1.1',
+ 'SERVER_NAME' => 'Ruby Wolf',
+ 'HTTP_VERSION' => 'HTTP/1.1',
+ 'HTTP_HOST' => connection.headers['Host'],
+ 'HTTP_USER_AGENT' => connection.headers['User-Agent'],
+ 'HTTP_ACCEPT' => connection.headers['Accept'],
+ 'CONTENT_LENGTH' => connection.headers['Content-Length'],
+ 'CONTENT_TYPE' => connection.headers['Content-Type'],
+
+ 'rack.input' => StringIO.new(connection.read_chunk)
+ )
+
RubyWolf.log(
[
- "HTTP/#{env[::Rack::HTTP_VERSION].join('.')}",
- env[::Rack::REQUEST_METHOD],
- "#{env[::Rack::REQUEST_PATH]}?#{env[::Rack::QUERY_STRING]}"
+ 'HTTP/1.1',
+ connection.method,
+ "#{connection.path}?#{connection.query}"
].join(' ')
)
end
def generate_response
status, headers, body = app.call(env)
RubyWolf.log(
- "Response #{env[::Rack::SERVER_PROTOCOL]} #{status}"
+ "Response HTTP/1.1 #{status}"
)
compose_response(status, headers, body)
end
def compose_response(status, headers, body)
- @response += "#{env[::Rack::SERVER_PROTOCOL]} #{status} #{RubyWolf::CRLF}"
+ @response += "HTTP/1.1 #{status} #{RubyWolf::CRLF}"
headers.each do |key, value|
@response += "#{key}: #{value}#{RubyWolf::CRLF}"
end
@response += RubyWolf::CRLF