Skip to content

Commit

Permalink
proper persistent connection behavior for HTTP 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
rkh authored and evanphx committed Oct 3, 2011
1 parent 3085156 commit 357b3cb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/puma/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,14 @@ def handle_request(env, client, body)
env["rack.input"] = body
env["rack.url_scheme"] = env["HTTPS"] ? "https" : "http"

keep_alive = env["HTTP_CONNECTION"] != "close"
if env['HTTP_VERSION'] == 'HTTP/1.1'
http_version = "HTTP/1.1 "
keep_alive = env["HTTP_CONNECTION"] != "close"
else
http_version = "HTTP/1.0 "
keep_alive = env["HTTP_CONNECTION"] == "Keep-Alive"
end

chunked = false

begin
Expand All @@ -231,7 +238,7 @@ def handle_request(env, client, body)
content_length = res_body[0].size
end

client.write "HTTP/1.1 "
client.write http_version
client.write status.to_s
client.write " "
client.write HTTP_STATUS_CODES[status]
Expand Down
18 changes: 18 additions & 0 deletions test/test_persistent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class TestPersistent < Test::Unit::TestCase
def setup
@valid_request = "GET / HTTP/1.1\r\nHost: test.com\r\nContent-Type: text/plain\r\n\r\n"
@close_request = "GET / HTTP/1.1\r\nHost: test.com\r\nContent-Type: text/plain\r\nConnection: close\r\n\r\n"
@http10_request = "GET / HTTP/1.0\r\nHost: test.com\r\nContent-Type: text/plain\r\n\r\n"
@keep_request = "GET / HTTP/1.0\r\nHost: test.com\r\nContent-Type: text/plain\r\nConnection: Keep-Alive\r\n\r\n"

@headers = { "X-Header" => "Works" }
@body = ["Hello"]
Expand Down Expand Up @@ -75,4 +77,20 @@ def test_client_close
assert_equal "Hello", @client.read(5)
end

def test_client_close
@client << @http10_request
sz = @body[0].size.to_s

assert_equal "HTTP/1.0 200 OK\r\nConnection: close\r\nContent-Length: #{sz}\r\nX-Header: Works\r\n\r\n", lines(5)
assert_equal "Hello", @client.read(5)
end

def test_one_with_keep_alive_header
@client << @keep_request
sz = @body[0].size.to_s

assert_equal "HTTP/1.0 200 OK\r\nContent-Length: #{sz}\r\nX-Header: Works\r\n\r\n", lines(4)
assert_equal "Hello", @client.read(5)
end

end

0 comments on commit 357b3cb

Please sign in to comment.