lib/reel/rack/server.rb in reel-rack-0.2.1 vs lib/reel/rack/server.rb in reel-rack-0.2.2
- old
+ new
@@ -38,11 +38,13 @@
options = {
:method => request.method,
:input => request.body.to_s,
"REMOTE_ADDR" => request.remote_addr
}.merge(convert_headers(request.headers))
-
+
+ normalize_env(options)
+
status, headers, body = app.call ::Rack::MockRequest.env_for(request.url, options)
if body.respond_to? :each
# If Content-Length was specified we can send the response all at once
if headers.keys.detect { |h| h =~ CONTENT_LENGTH_HEADER }
@@ -74,9 +76,29 @@
[header, value]
else
['HTTP_' + header, value]
end
}]
+ end
+
+ # Copied from lib/puma/server.rb
+ def normalize_env(env)
+ if host = env["HTTP_HOST"]
+ if colon = host.index(":")
+ env["SERVER_NAME"] = host[0, colon]
+ env["SERVER_PORT"] = host[colon+1, host.bytesize]
+ else
+ env["SERVER_NAME"] = host
+ env["SERVER_PORT"] = default_server_port(env)
+ end
+ else
+ env["SERVER_NAME"] = "localhost"
+ env["SERVER_PORT"] = default_server_port(env)
+ end
+ end
+
+ def default_server_port(env)
+ env['HTTP_X_FORWARDED_PROTO'] == 'https' ? 443 : 80
end
def status_symbol(status)
if status.is_a?(Fixnum)
Http::Response::STATUS_CODES[status].downcase.gsub(/\s|-/, '_').to_sym