Commit a92e7730 authored by Denis Bilenko's avatar Denis Bilenko

wsgi.py: pulled Mike Barton's patches that fix double content-length bug

parent 62a74d35
...@@ -75,7 +75,7 @@ class Input(object): ...@@ -75,7 +75,7 @@ class Input(object):
if length is None and self.content_length is not None: if length is None and self.content_length is not None:
length = self.content_length - self.position length = self.content_length - self.position
if length and self.content_length is not None and length > self.content_length - self.position: if length and length > self.content_length - self.position:
length = self.content_length - self.position length = self.content_length - self.position
if not length: if not length:
return '' return ''
...@@ -177,8 +177,6 @@ class HttpProtocol(BaseHTTPServer.BaseHTTPRequestHandler): ...@@ -177,8 +177,6 @@ class HttpProtocol(BaseHTTPServer.BaseHTTPRequestHandler):
start = time.time() start = time.time()
headers_set = [] headers_set = []
headers_sent = [] headers_sent = []
# set of lowercase header names that were sent
header_dict = {}
wfile = self.wfile wfile = self.wfile
result = None result = None
...@@ -193,19 +191,18 @@ class HttpProtocol(BaseHTTPServer.BaseHTTPRequestHandler): ...@@ -193,19 +191,18 @@ class HttpProtocol(BaseHTTPServer.BaseHTTPRequestHandler):
elif not headers_sent: elif not headers_sent:
status, response_headers = headers_set status, response_headers = headers_set
headers_sent.append(1) headers_sent.append(1)
for k, v in response_headers: header_list = [header[0].lower() for header in response_headers]
header_dict[k.lower()] = k
towrite.append('%s %s\r\n' % (self.protocol_version, status)) towrite.append('%s %s\r\n' % (self.protocol_version, status))
for header in response_headers: for header in response_headers:
towrite.append('%s: %s\r\n' % header) towrite.append('%s: %s\r\n' % header)
# send Date header? # send Date header?
if 'date' not in header_dict: 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':
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_dict: elif 'content-length' not in header_list:
use_chunked[0] = True use_chunked[0] = True
towrite.append('Transfer-Encoding: chunked\r\n') towrite.append('Transfer-Encoding: chunked\r\n')
towrite.append('\r\n') towrite.append('\r\n')
...@@ -249,16 +246,19 @@ class HttpProtocol(BaseHTTPServer.BaseHTTPRequestHandler): ...@@ -249,16 +246,19 @@ class HttpProtocol(BaseHTTPServer.BaseHTTPRequestHandler):
try: try:
try: try:
result = self.application(self.environ, start_response) result = self.application(self.environ, start_response)
if not headers_sent and hasattr(result, '__len__'): if not headers_sent and hasattr(result, '__len__') and \
headers_set[1].append(('content-length', str(sum(map(len, result))))) 'Content-Length' not in [h for h, v in headers_set[1]]:
headers_set[1].append(('Content-Length', str(sum(map(len, result)))))
towrite = [] towrite = []
for data in result: for data in result:
if data: towrite.append(data)
write(data) if sum(map(len, towrite)) >= self.minimum_chunk_size:
if not headers_sent: write(''.join(towrite))
towrite = []
if towrite:
write(''.join(towrite))
if not headers_sent or use_chunked[0]:
write('') write('')
if use_chunked[0]:
wfile.write('0\r\n\r\n')
except Exception, e: except Exception, e:
self.close_connection = 1 self.close_connection = 1
exc = traceback.format_exc() exc = traceback.format_exc()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment