summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorSenthil Kumaran <orsenthil@gmail.com>2010-11-21 14:36:14 (GMT)
committerSenthil Kumaran <orsenthil@gmail.com>2010-11-21 14:36:14 (GMT)
commite4dad4f8e239be6d9d1fd3e5398584a7d2a14edb (patch)
treeff6526bd9de4fe65ec484ca7f5e0fd029b858897 /Lib
parenta73dc9d5e8ba2320a5aebf8c24ab665ccdfed5f8 (diff)
downloadcpython-e4dad4f8e239be6d9d1fd3e5398584a7d2a14edb.zip
cpython-e4dad4f8e239be6d9d1fd3e5398584a7d2a14edb.tar.gz
cpython-e4dad4f8e239be6d9d1fd3e5398584a7d2a14edb.tar.bz2
Fix issue3709 - BaseHTTPRequestHandler will buffer the headers and write only on end_headers call.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/http/server.py9
-rw-r--r--Lib/test/test_httpservers.py43
2 files changed, 50 insertions, 2 deletions
diff --git a/Lib/http/server.py b/Lib/http/server.py
index 8a6ba70..5ebfd25 100644
--- a/Lib/http/server.py
+++ b/Lib/http/server.py
@@ -443,7 +443,10 @@ class BaseHTTPRequestHandler(socketserver.StreamRequestHandler):
def send_header(self, keyword, value):
"""Send a MIME header."""
if self.request_version != 'HTTP/0.9':
- self.wfile.write(("%s: %s\r\n" % (keyword, value)).encode('ASCII', 'strict'))
+ if not hasattr(self, '_headers_buffer'):
+ self._headers_buffer = []
+ self._headers_buffer.append(
+ ("%s: %s\r\n" % (keyword, value)).encode('ASCII', 'strict'))
if keyword.lower() == 'connection':
if value.lower() == 'close':
@@ -454,7 +457,9 @@ class BaseHTTPRequestHandler(socketserver.StreamRequestHandler):
def end_headers(self):
"""Send the blank line ending the MIME headers."""
if self.request_version != 'HTTP/0.9':
- self.wfile.write(b"\r\n")
+ self._headers_buffer.append(b"\r\n")
+ self.wfile.write(b"".join(self._headers_buffer))
+ self._headers_buffer = []
def log_request(self, code='-', size='-'):
"""Log an accepted request.
diff --git a/Lib/test/test_httpservers.py b/Lib/test/test_httpservers.py
index daf37b2..86ce912 100644
--- a/Lib/test/test_httpservers.py
+++ b/Lib/test/test_httpservers.py
@@ -511,6 +511,49 @@ class BaseHTTPRequestHandlerTestCase(unittest.TestCase):
self.verify_get_called()
self.assertEqual(result[-1], b'<html><body>Data</body></html>\r\n')
+ def test_header_buffering(self):
+
+ def _readAndReseek(f):
+ pos = f.tell()
+ f.seek(0)
+ data = f.read()
+ f.seek(pos)
+ return data
+
+ input = BytesIO(b'GET / HTTP/1.1\r\n\r\n')
+ output = BytesIO()
+ self.handler.rfile = input
+ self.handler.wfile = output
+ self.handler.request_version = 'HTTP/1.1'
+
+ self.handler.send_header('Foo', 'foo')
+ self.handler.send_header('bar', 'bar')
+ self.assertEqual(_readAndReseek(output), b'')
+ self.handler.end_headers()
+ self.assertEqual(_readAndReseek(output),
+ b'Foo: foo\r\nbar: bar\r\n\r\n')
+
+ def test_header_unbuffered_when_continue(self):
+
+ def _readAndReseek(f):
+ pos = f.tell()
+ f.seek(0)
+ data = f.read()
+ f.seek(pos)
+ return data
+
+ input = BytesIO(b'GET / HTTP/1.1\r\nExpect: 100-continue\r\n\r\n')
+ output = BytesIO()
+ self.handler.rfile = input
+ self.handler.wfile = output
+ self.handler.request_version = 'HTTP/1.1'
+
+ self.handler.handle_one_request()
+ self.assertNotEqual(_readAndReseek(output), b'')
+ result = _readAndReseek(output).split(b'\r\n')
+ self.assertEqual(result[0], b'HTTP/1.1 100 Continue')
+ self.assertEqual(result[1], b'HTTP/1.1 200 OK')
+
def test_with_continue_rejected(self):
usual_handler = self.handler # Save to avoid breaking any subsequent tests.
self.handler = RejectingSocketlessRequestHandler()