Changeset 1582
- Timestamp:
- 12/29/06 00:02:24
- Files:
-
- branches/cherrypy-2.x/cherrypy/_cpwsgi.py (modified) (4 diffs)
- branches/cherrypy-2.x/cherrypy/_cpwsgiserver.py (modified) (2 diffs)
- branches/cherrypy-2.x/cherrypy/filters/wsgiappfilter.py (modified) (1 diff)
- branches/cherrypy-2.x/cherrypy/test/helper.py (modified) (1 diff)
- branches/cherrypy-2.x/cherrypy/test/test.py (modified) (1 diff)
- branches/cherrypy-2.x/cherrypy/test/test_custom_filters.py (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/cherrypy-2.x/cherrypy/_cpwsgi.py
r1520 r1582 50 50 pass 51 51 52 class ResponseIter(object): 53 def __init__(self, request, body): 54 self.body = body 55 self.request = request 56 57 def __iter__(self): 58 if not self.body: 59 raise StopIteration 60 try: 61 for chunk in self.body: 62 # WSGI requires all data to be of type "str". This coercion should 63 # not take any time at all if chunk is already of type "str". 64 # If it's unicode, it could be a big performance hit (x ~500). 65 if not isinstance(chunk, str): 66 chunk = chunk.encode("ISO-8859-1") 67 yield chunk 68 except (KeyboardInterrupt, SystemExit), ex: 69 raise ex 70 except: 71 cherrypy.log(traceback=True) 72 s, h, b = _cputil.bareError() 73 # CherryPy test suite expects bareError body to be output, 74 # so don't call start_response (which, according to PEP 333, 75 # may raise its own error at that point). 76 for chunk in b: 77 # WSGI requires all data to be of type "str". This coercion should 78 # not take any time at all if chunk is already of type "str". 79 # If it's unicode, it could be a big performance hit (x ~500). 80 if not isinstance(chunk, str): 81 chunk = chunk.encode("ISO-8859-1") 82 yield chunk 83 84 def close(self): 85 try: 86 if self.request: 87 self.request.close() 88 except: 89 cherrypy.log(traceback=True) 90 self.request = None 52 91 53 92 def wsgiApp(environ, start_response): … … 87 126 except: 88 127 if cherrypy.config.get("server.throw_errors", False): 128 if request: 129 request.close() 130 request = None 89 131 raise 90 132 tb = _cputil.formatExc() … … 95 137 exc = sys.exc_info() 96 138 97 try: 98 start_response(s, h, exc) 99 for chunk in b: 100 # WSGI requires all data to be of type "str". This coercion should 101 # not take any time at all if chunk is already of type "str". 102 # If it's unicode, it could be a big performance hit (x ~500). 103 if not isinstance(chunk, str): 104 chunk = chunk.encode("ISO-8859-1") 105 yield chunk 106 if request: 107 request.close() 108 request = None 109 except (KeyboardInterrupt, SystemExit), ex: 110 try: 111 if request: 112 request.close() 113 except: 114 cherrypy.log(traceback=True) 115 request = None 116 raise ex 117 except: 118 cherrypy.log(traceback=True) 119 try: 120 if request: 121 request.close() 122 except: 123 cherrypy.log(traceback=True) 124 request = None 125 s, h, b = _cputil.bareError() 126 # CherryPy test suite expects bareError body to be output, 127 # so don't call start_response (which, according to PEP 333, 128 # may raise its own error at that point). 129 for chunk in b: 130 if not isinstance(chunk, str): 131 chunk = chunk.encode("ISO-8859-1") 132 yield chunk 139 start_response(s, h, exc) 140 return ResponseIter(request, b) 141 142 133 143 134 144 … … 283 293 self.ssl_private_key = conf("server.ssl_private_key") 284 294 295 branches/cherrypy-2.x/cherrypy/_cpwsgiserver.py
r1572 r1582 236 236 response = request.wsgi_app(request.environ, 237 237 request.start_response) 238 for line in response: 239 request.write(line) 240 if hasattr(response, "close"): 241 response.close() 238 try: 239 for line in response: 240 request.write(line) 241 finally: 242 if hasattr(response, "close"): 243 response.close() 242 244 except socket.error, e: 243 245 errno = e.args[0] … … 434 436 pass 435 437 438 branches/cherrypy-2.x/cherrypy/filters/wsgiappfilter.py
r1008 r1582 136 136 137 137 # run the wsgi app and have it set response.body 138 cherrypy.response.body = self.app(environ, start_response) 138 response = self.app(environ, start_response) 139 try: 140 cherrypy.response.body = response 141 finally: 142 if hasattr(response, "close"): 143 response.close() 139 144 140 145 # tell CP not to handle the request further branches/cherrypy-2.x/cherrypy/test/helper.py
r1567 r1582 31 31 32 32 mount_point = "" 33 scheme = "http" 33 34 34 35 def prefix(self): branches/cherrypy-2.x/cherrypy/test/test.py
r1569 r1582 159 159 160 160 --port=<int>: use a port other than the default (%s). 161 --1. 1: use HTTP/1.1 servers instead of default HTTP/1.0.161 --1.0: use HTTP/1.0 servers instead of default HTTP/1.1. 162 162 163 163 --cover: turn on the code-coverage tool. branches/cherrypy-2.x/cherrypy/test/test_custom_filters.py
r1017 r1582 1 1 """Test the various means of instantiating and invoking filters.""" 2 2 3 import time 3 4 import types 4 5 import test … … 103 104 raise ValueError() 104 105 106 def stream(self): 107 for i in xrange(100000000): 108 yield str(i) 109 105 110 def errinstream(self): 106 111 raise ValueError() … … 134 139 'stream_response': True, 135 140 }, 141 '/cpfilterlist/stream': { 142 'stream_response': True, 143 }, 136 144 '/cpfilterlist/err_in_onstart': { 137 145 # Because this isn't a dict, on_start_resource will error. … … 159 167 160 168 class FilterTests(helper.CPWebCase): 161 162 169 def testCPFilterList(self): 163 170 self.getPage("/cpfilterlist/") … … 184 191 # If this fails, then on_end_request isn't being called at all. 185 192 self.getPage("/cpfilterlist/ended/5") 193 self.assertBody("True") 194 195 # Test that on_end_request is called even if the client drops. 196 self.persistent = True 197 try: 198 conn = self.HTTP_CONN 199 conn.putrequest('GET', '/cpfilterlist/stream', skip_host=True) 200 conn.putheader('Host', self.HOST) 201 conn.endheaders() 202 # Skip the rest of the request and close the conn. This will 203 # cause the server's active socket to error, which *should* 204 # result in the request being aborted, and request.close being 205 # called all the way up the stack (including WSGI middleware), 206 # eventually calling our on_end_request hook. 207 finally: 208 self.persistent = False 209 time.sleep(0.1) 210 # on_end_request should have been called 211 self.getPage('/cpfilterlist/ended/7') 186 212 self.assertBody("True") 187 213

