Commit a79f98c8 authored by Denis Bilenko's avatar Denis Bilenko

pywsgi: do not use a fileobject to send the response; socket.sendall seems good enough

add 'wfile' as a deprecated property
parent 912b2356
...@@ -41,16 +41,16 @@ def format_date_time(timestamp): ...@@ -41,16 +41,16 @@ def format_date_time(timestamp):
class Input(object): class Input(object):
def __init__(self, rfile, content_length, wfile=None, chunked_input=False): def __init__(self, rfile, content_length, socket=None, chunked_input=False):
self.rfile = rfile self.rfile = rfile
self.content_length = content_length self.content_length = content_length
self.wfile = wfile self.socket = socket
self.position = 0 self.position = 0
self.chunked_input = chunked_input self.chunked_input = chunked_input
self.chunk_length = -1 self.chunk_length = -1
def _discard(self): def _discard(self):
if self.wfile is None and (self.position < (self.content_length or 0) or self.chunked_input): if self.socket is None and (self.position < (self.content_length or 0) or self.chunked_input):
# ## Read and discard body # ## Read and discard body
while 1: while 1:
d = self.read(16384) d = self.read(16384)
...@@ -58,9 +58,9 @@ class Input(object): ...@@ -58,9 +58,9 @@ class Input(object):
break break
def _send_100_continue(self): def _send_100_continue(self):
if self.wfile is not None: if self.socket is not None:
self.wfile.write(_CONTINUE_RESPONSE) self.socket.sendall(_CONTINUE_RESPONSE)
self.wfile = None self.sendall = None
def _do_read(self, reader, length=None): def _do_read(self, reader, length=None):
self._send_100_continue() self._send_100_continue()
...@@ -149,12 +149,22 @@ class WSGIHandler(object): ...@@ -149,12 +149,22 @@ class WSGIHandler(object):
protocol_version = 'HTTP/1.1' protocol_version = 'HTTP/1.1'
MessageClass = mimetools.Message MessageClass = mimetools.Message
def __init__(self, socket, address, server): def __init__(self, socket, address, server, rfile=None):
self.socket = socket self.socket = socket
self.client_address = address self.client_address = address
self.server = server self.server = server
if rfile is None:
self.rfile = socket.makefile('rb', -1) self.rfile = socket.makefile('rb', -1)
self.wfile = socket.makefile('wb', 0) else:
self.rfile = rfile
@property
def wfile(self):
# DEPRECATED, UNTESTED, TO BE REMOVED
wfile = getattr(self, '_wfile', None)
if wfile is None:
wfile = self._wfile = self.socket.makefile('wb', 0)
return wfile
def handle(self): def handle(self):
try: try:
...@@ -167,7 +177,7 @@ class WSGIHandler(object): ...@@ -167,7 +177,7 @@ class WSGIHandler(object):
if result is True: if result is True:
continue continue
self.status, response_body = result self.status, response_body = result
self.wfile.write(response_body) self.socket.sendall(response_body)
if self.time_finish == 0: if self.time_finish == 0:
self.time_finish = time.time() self.time_finish = time.time()
self.log_request() self.log_request()
...@@ -181,7 +191,7 @@ class WSGIHandler(object): ...@@ -181,7 +191,7 @@ class WSGIHandler(object):
pass pass
self.__dict__.pop('socket', None) self.__dict__.pop('socket', None)
self.__dict__.pop('rfile', None) self.__dict__.pop('rfile', None)
self.__dict__.pop('wfile', None) self.__dict__.pop('_wfile', None) # XXX remove once wfile property is gone
def _check_http_version(self): def _check_http_version(self):
version = self.request_version version = self.request_version
...@@ -353,8 +363,9 @@ class WSGIHandler(object): ...@@ -353,8 +363,9 @@ class WSGIHandler(object):
else: else:
towrite.append(data) towrite.append(data)
self.wfile.writelines(towrite) msg = ''.join(towrite)
self.response_length += sum(len(x) for x in towrite) self.socket.sendall(msg)
self.response_length += len(msg)
def start_response(self, status, headers, exc_info=None): def start_response(self, status, headers, exc_info=None):
if exc_info: if exc_info:
...@@ -400,7 +411,7 @@ class WSGIHandler(object): ...@@ -400,7 +411,7 @@ class WSGIHandler(object):
if self.status and not self.headers_sent: if self.status and not self.headers_sent:
self.write('') self.write('')
if self.response_use_chunked: if self.response_use_chunked:
self.wfile.writelines('0\r\n\r\n') self.socket.sendall('0\r\n\r\n')
self.response_length += 5 self.response_length += 5
def run_application(self): def run_application(self):
...@@ -483,11 +494,11 @@ class WSGIHandler(object): ...@@ -483,11 +494,11 @@ class WSGIHandler(object):
env[key] = value env[key] = value
if env.get('HTTP_EXPECT') == '100-continue': if env.get('HTTP_EXPECT') == '100-continue':
wfile = self.wfile socket = self.socket
else: else:
wfile = None socket = None
chunked = env.get('HTTP_TRANSFER_ENCODING', '').lower() == 'chunked' chunked = env.get('HTTP_TRANSFER_ENCODING', '').lower() == 'chunked'
self.wsgi_input = Input(self.rfile, self.content_length, wfile=wfile, chunked_input=chunked) self.wsgi_input = Input(self.rfile, self.content_length, socket=socket, chunked_input=chunked)
env['wsgi.input'] = self.wsgi_input env['wsgi.input'] = self.wsgi_input
return env return env
......
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