def __init__(self, wsgi, environ, start_response): self.wsgi = wsgi self.environ = environ self.connection = wsgi_request(self.environ).cache.connection self.debug = self.connection._loop.get_debug() self.start_response = start_response self.future = create_future()
def _call(self, pool, environ, start_response): if pool: wsgi_input = environ['wsgi.input'] if wsgi_input and not isinstance(wsgi_input, GreenStream): environ['wsgi.input'] = GreenStream(wsgi_input) request = wsgi_request(environ) path = request.path try: self.app.on_request(data=request) hnd = self.router.resolve(path, request.method) if hnd: request.cache.set('app_handler', hnd.router) request.cache.set('urlargs', hnd.urlargs) response = self.wait(hnd.handler(request)) else: raise Http404 except Exception as exc: response = handle_wsgi_error(environ, exc) try: self.app.fire_event('on_response', data=(request, response)) except Exception as exc: response = handle_wsgi_error(environ, exc) response.start(environ, start_response) return response
def request(self, environ, start_response=None): # Inject self as the authentication backend request = wsgi_request(environ) cache = request.cache cache.auth_backend = self cache.user = self.anonymous(request) return self._execute_backend_method('request', request)
def response(self, response): request = wsgi_request(response.environ) if request.cache.x_count > 0: request.cache.x_count -= 1 else: loop = get_event_loop() runtime = loop.time() - request.cache.x_runtime request.response['X-Runtime'] = '%.6f' % runtime
def ok(environ, start_response): request = wsgi_request(environ) request.cache.logger = MagicMock() response_headers = [ ('Content-Length', '0') ] start_response('200 OK', response_headers) return iter([])
def __call__(self, environ, start_response): request = wsgi_request(environ) path = request.path match = self.route.match(path[1:]) if match is not None: query = request.get('QUERY_STRING', '') path = urljoin(self.url, match.pop('__remaining__', '')) if query: path = '%s?%s' % (path, query) return self._call(request, path, start_response)
def response(self, response): request = wsgi_request(response.environ) session = request.cache.session if session: if response.can_set_cookies(): key = request.config["SESSION_COOKIE_NAME"] session_key = request.cookies.get(key) id = session.id if not session_key or session_key.value != id: response.set_cookie(key, value=str(id), httponly=True, expires=session.expiry) return session_store(request).save(session) return response
def response(self, response): request = wsgi_request(response.environ) session = request.cache.session if session: if response.can_set_cookies(): key = request.config['SESSION_COOKIE_NAME'] session_key = request.cookies.get(key) id = session.id if not session_key or session_key.value != id: response.set_cookie(key, value=str(id), httponly=True, expires=session.expiry) return session_store(request).save(session) return response
async def test_wsgi_request(url=None, method=None, headers=None, **kwargs): """Create a valid WSGI request :param url: optional path :param method: optional HTTP method, defaults to GET :param headers: optional list-alike collection of headers :return: :class:`~.WsgiRequest` """ cli = HttpWsgiClient(ok, headers=headers) url = url or '/' if not urlparse(url).scheme: url = 'http://www.example.com/%s' % ( url[1:] if url.startswith('/') else url) method = method or 'get' response = await cli.request(method, url, **kwargs) return wsgi_request(response.server_side.request.environ)
async def test_wsgi_request(url=None, method=None, headers=None, **kwargs): """Create a valid WSGI request :param url: optional path :param method: optional HTTP method, defaults to GET :param headers: optional list-alike collection of headers :return: :class:`~.WsgiRequest` """ cli = HttpWsgiClient(ok, headers=headers) url = url or '/' if not urlparse(url).scheme: url = 'http://www.example.com/%s' % (url[1:] if url.startswith('/') else url) method = method or 'get' response = await cli.request(method, url, **kwargs) return wsgi_request(response.server_side.request.environ)
def setup(self, environ): '''Called once only to setup the WSGI application handler. Check :ref:`lazy wsgi handler <wsgi-lazy-handler>` section for further information. ''' request = wsgi_request(environ) cfg = request.cache.cfg loop = request.cache._loop self.store = create_store(cfg.data_store, loop=loop) pubsub = self.store.pubsub(protocol=Protocol()) channel = '%s_webchat' % self.name ensure_future(pubsub.subscribe(channel), loop=loop) return WsgiHandler([Router('/', get=self.home_page), WebSocket('/message', Chat(pubsub, channel)), Router('/rpc', post=Rpc(pubsub, channel), response_content_types=JSON_CONTENT_TYPES)], [AsyncResponseMiddleware, GZipMiddleware(min_length=20)])
def setup(self, environ): '''Called once only to setup the WSGI application handler. Check :ref:`lazy wsgi handler <wsgi-lazy-handler>` section for further information. ''' request = wsgi_request(environ) cfg = request.cache.cfg loop = request.cache._loop self.store = create_store(cfg.data_store, loop=loop) pubsub = self.store.pubsub(protocol=Protocol()) channel = '%s_webchat' % self.name ensure_future(pubsub.subscribe(channel), loop=loop) return WsgiHandler([ Router('/', get=self.home_page), WebSocket('/message', Chat(pubsub, channel)), Router('/rpc', post=Rpc(pubsub, channel), response_content_types=JSON_CONTENT_TYPES) ], [AsyncResponseMiddleware, GZipMiddleware(min_length=20)])
def _(self, r, *args, **kwargs): if wsgi_request(r.environ).cache.skip_session_backend: return return method(self, r, *args, **kwargs)
def ok(environ, start_response): request = wsgi_request(environ) request.cache.logger = MagicMock() response_headers = [('Content-Length', '0')] start_response('200 OK', response_headers) return iter([])
def wsgi_request(self, response): return wsgi_request(response.server_side.request.environ)