Ticket #343: large_response.patch
| File large_response.patch, 1.6 KB (added by gawel, 3 years ago) |
|---|
-
wsgiproxy/exactproxy.py
9 9 'transfer-encoding', 10 10 ) 11 11 12 BLOCK_SIZE = 4096 * 16 13 12 14 def filter_paste_httpserver_proxy(app): 13 15 """ 14 16 Maps the ``paste.httpserver`` proxy environment keys to … … 95 97 status = '%s %s' % (res.status, res.reason) 96 98 start_response(status, headers_out) 97 99 length = res.getheader('content-length') 98 # @@: This shouldn't really read in all the content at once99 100 if length is not None: 100 body = res.read(int(length)) 101 length = int(length) 102 if length > BLOCK_SIZE: 103 return iter(_BodyIter(conn, res, length)) 104 body = res.read(length) 101 105 else: 102 106 body = res.read() 103 107 conn.close() 104 108 return [body] 105 109 110 class _BodyIter(object): 111 """ 112 Allow to iter over large response body and close the connection when all 113 data is received. 114 """ 115 116 def __init__(self, conn, res, length): 117 self.conn = conn 118 self.res = res 119 self.length = length 120 121 def __iter__(self): 122 return self 123 124 def next(self): 125 if self.length > 0: 126 if self.length > BLOCK_SIZE: 127 data = self.res.read(BLOCK_SIZE) 128 self.length -= BLOCK_SIZE 129 else: 130 data = self.res.read(self.length) 131 self.length = 0 132 return data 133 self.conn.close() 134 raise StopIteration() 135 106 136 def parse_headers(message): 107 137 """ 108 138 Turn a Message object into a list of WSGI-style headers.
