Patch for wsgi http/1.0 keepalive from Brian Brunswick. Added unit test to ensure this keeps working.

This commit is contained in:
Ryan Williams
2009-09-28 14:22:48 -07:00
parent 252dcad98e
commit f36908a01e
2 changed files with 25 additions and 5 deletions

View File

@@ -230,6 +230,10 @@ class HttpProtocol(BaseHTTPServer.BaseHTTPRequestHandler):
if 'date' not in header_list: if 'date' not in header_list:
towrite.append('Date: %s\r\n' % (format_date_time(time.time()),)) towrite.append('Date: %s\r\n' % (format_date_time(time.time()),))
if self.request_version == 'HTTP/1.0': if self.request_version == 'HTTP/1.0':
if self.headers.get('Connection', "").lower() == 'keep-alive':
towrite.append('Connection: keep-alive\r\n')
self.close_connection = 0
else:
towrite.append('Connection: close\r\n') towrite.append('Connection: close\r\n')
self.close_connection = 1 self.close_connection = 1
elif 'content-length' not in header_list: elif 'content-length' not in header_list:

View File

@@ -22,8 +22,8 @@
import cgi import cgi
import os import os
from tests import skipped from tests import skipped, LimitedTestCase
from unittest import TestCase, main from unittest import main
from eventlet import api from eventlet import api
from eventlet import util from eventlet import util
@@ -125,7 +125,7 @@ def read_http(sock):
return response_line, headers, body return response_line, headers, body
class TestHttpd(TestCase): class TestHttpd(LimitedTestCase):
mode = 'static' mode = 'static'
def setUp(self): def setUp(self):
self.logfile = StringIO() self.logfile = StringIO()
@@ -427,6 +427,22 @@ class TestHttpd(TestCase):
success = server_coro.wait() success = server_coro.wait()
self.assert_(success) self.assert_(success)
def test_018_http_10_keepalive(self):
# verify that if an http/1.0 client sends connection: keep-alive
# that we don't close the connection
sock = api.connect_tcp(
('127.0.0.1', 12346))
fd = sock.makeGreenFile()
fd.write('GET / HTTP/1.0\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n')
self.assert_('connection: keep-alive' in
fd.readuntil('\r\n\r\n').lower())
# repeat request to verify connection is actually still open
fd.write('GET / HTTP/1.0\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n')
self.assert_('connection: keep-alive' in
fd.readuntil('\r\n\r\n').lower())
if __name__ == '__main__': if __name__ == '__main__':
main() main()