diff options
author | Senthil Kumaran <senthil@uthcode.com> | 2011-05-09 15:25:02 (GMT) |
---|---|---|
committer | Senthil Kumaran <senthil@uthcode.com> | 2011-05-09 15:25:02 (GMT) |
commit | c7ae19b6a7cf819d5646f2c88c5dd6e2c244939d (patch) | |
tree | f57fd821f8b18ada6e40a25e141d3cfa1eddda2a /Lib/http | |
parent | 87cf220972c9cb400ddcd577962883dcc5dca51a (diff) | |
download | cpython-c7ae19b6a7cf819d5646f2c88c5dd6e2c244939d.zip cpython-c7ae19b6a7cf819d5646f2c88c5dd6e2c244939d.tar.gz cpython-c7ae19b6a7cf819d5646f2c88c5dd6e2c244939d.tar.bz2 |
Issue #3709: a flush_headers method to BaseHTTPRequestHandler which manages the
sending of headers to output stream and flushing the internal headers buffer.
Patch contribution by Andrew Schaaf
Diffstat (limited to 'Lib/http')
-rw-r--r-- | Lib/http/server.py | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/Lib/http/server.py b/Lib/http/server.py index 6aacbbd..1d193f8 100644 --- a/Lib/http/server.py +++ b/Lib/http/server.py @@ -355,6 +355,7 @@ class BaseHTTPRequestHandler(socketserver.StreamRequestHandler): """ self.send_response_only(100) + self.flush_headers() return True def handle_one_request(self): @@ -432,7 +433,8 @@ class BaseHTTPRequestHandler(socketserver.StreamRequestHandler): self.wfile.write(content.encode('UTF-8', 'replace')) def send_response(self, code, message=None): - """Send the response header and log the response code. + """Add the response header to the headers buffer and log the + response code. Also send two standard headers with the server software version and the current date. @@ -451,11 +453,14 @@ class BaseHTTPRequestHandler(socketserver.StreamRequestHandler): else: message = '' if self.request_version != 'HTTP/0.9': - self.wfile.write(("%s %d %s\r\n" % - (self.protocol_version, code, message)).encode('latin-1', 'strict')) + if not hasattr(self, '_headers_buffer'): + self._headers_buffer = [] + self._headers_buffer.append(("%s %d %s\r\n" % + (self.protocol_version, code, message)).encode( + 'latin-1', 'strict')) def send_header(self, keyword, value): - """Send a MIME header.""" + """Send a MIME header to the headers buffer.""" if self.request_version != 'HTTP/0.9': if not hasattr(self, '_headers_buffer'): self._headers_buffer = [] @@ -472,6 +477,10 @@ class BaseHTTPRequestHandler(socketserver.StreamRequestHandler): """Send the blank line ending the MIME headers.""" if self.request_version != 'HTTP/0.9': self._headers_buffer.append(b"\r\n") + self.flush_headers() + + def flush_headers(self): + if hasattr(self, '_headers_buffer'): self.wfile.write(b"".join(self._headers_buffer)) self._headers_buffer = [] @@ -1081,6 +1090,7 @@ class CGIHTTPRequestHandler(SimpleHTTPRequestHandler): env.setdefault(k, "") self.send_response(200, "Script output follows") + self.flush_headers() decoded_query = query.replace('+', ' ') |