def status(self, request): request.response.content_type = 'text/html' msg = request.url_data.get('message', 'test error') raise HttpException(msg, status=request.urlargs['status'])
def _response(self, environ): exc_info = None response = None done = False alive = self.cfg.keep_alive or 15 while not done: done = True try: if exc_info is None: if 'SERVER_NAME' not in environ: raise HttpException(status=400) response = self.wsgi_callable(environ, self.start_response) if isfuture(response): response = yield from wait_for(response, alive) else: response = handle_wsgi_error(environ, exc_info) if isfuture(response): response = yield from wait_for(response, alive) # if exc_info: self.start_response(response.status, response.get_headers(), exc_info) # # Do the actual writing loop = self._loop start = loop.time() for chunk in response: if isfuture(chunk): chunk = yield from wait_for(chunk, alive) start = loop.time() result = self.write(chunk) if isfuture(result): yield from wait_for(result, alive) start = loop.time() else: time_in_loop = loop.time() - start if time_in_loop > MAX_TIME_IN_LOOP: self.logger.debug( 'Released the event loop after %.3f seconds', time_in_loop) yield None start = loop.time() # # make sure we write headers and last chunk if needed self.write(b'', True) except IOError: # client disconnected, end this connection self.finished() except Exception: if wsgi_request(environ).cache.handle_wsgi_error: self.keep_alive = False self._write_headers() self.connection.close() self.finished() else: done = False exc_info = sys.exc_info() else: if not self.keep_alive: self.connection.close() self.finished() log_wsgi_info(self.logger.info, environ, self.status) finally: if hasattr(response, 'close'): try: response.close() except Exception: self.logger.exception( 'Error while closing wsgi iterator')
def fail(self): if self.waiting_expect(): raise HttpException(status=417)
def raise_large_body_error(limit): raise HttpException( "Request content length too large. Limit is %s" % convert_bytes(limit), status=LARGE_BODY_CODE )