def test_vary_with_original_update_cache_middleware(self): """ Mainly to demonstrate the need to remove the Vary: Cookie header during caching. Same basic test as test_vary() but with django's UpdateCacheMiddleware instead of PatchedVaryUpdateCacheMiddleware. This does not get a cache hit if the cookies are not the same. """ request = self.factory.get('/') request.method = 'GET' request.COOKIES = {'test': 'foo'} request.META['HTTP_COOKIE'] = 'test=foo' response = HttpResponse() patch_vary_headers(response, ['Cookie']) response.set_cookie('test', 'foo') SessionMiddleware().process_request(request) AuthenticationMiddleware().process_request(request) cache_hit = FetchFromCacheMiddleware().process_request(request) self.assertEqual(cache_hit, None) response = UpdateCacheMiddleware().process_response(request, response) cache_hit = FetchFromCacheMiddleware().process_request(request) self.assertTrue(isinstance(cache_hit, HttpResponse)) new_request = self.factory.get('/') new_request.method = 'GET' # note: not using cookies here. this demonstrates that cookies don't # affect the cache key cache_hit = FetchFromCacheMiddleware().process_request(new_request) self.assertEqual(cache_hit, None)
def test_vary(self): """ Ensure caching works even when cookies are present and `Vary: Cookie` is on. """ request = self.factory.get('/test/vary') request.COOKIES = {'test': 'foo'} request.META['HTTP_COOKIE'] = 'test=foo' response = HttpResponse() patch_vary_headers(response, ['Cookie']) response.set_cookie('test', 'foo') SessionMiddleware().process_request(request) AuthenticationMiddleware().process_request(request) cache_hit = FetchFromCacheMiddleware().process_request(request) self.assertTrue(cache_hit is None) response = PatchedVaryUpdateCacheMiddleware().process_response(request, response) cache_hit = FetchFromCacheMiddleware().process_request(request) self.assertTrue(isinstance(cache_hit, HttpResponse)) new_request = self.factory.get('/test/vary') # note: not using cookies here. this demonstrates that cookies don't # affect the cache key cache_hit = FetchFromCacheMiddleware().process_request(new_request) self.assertTrue(isinstance(cache_hit, HttpResponse))
def get_result(request): url_match = resolve(request.path) module = importlib.import_module(url_match.func.__module__) view = getattr(module, url_match.func.__name__)() request = copy.deepcopy(request) SessionMiddleware().process_request(request) AuthenticationMiddleware().process_request(request) view.request = request return view.get_result(request, *url_match.args, **url_match.kwargs)
def __init__(self): print print "Starting SOCKETIO Application" print "=" * 20 print namespaces = load_namespaces() if namespaces: self._my_namespaces = namespaces self.django_wsgi = WSGIHandler() super(Application, self).__init__() self.middleware = [SessionMiddleware(), AuthenticationMiddleware()]
def test_no_vary(self): """ Ensure basic caching works. """ request = self.factory.get('/test/no-vary') response = HttpResponse() SessionMiddleware().process_request(request) AuthenticationMiddleware().process_request(request) cache_hit = FetchFromCacheMiddleware().process_request(request) self.assertEqual(cache_hit, None) response = PatchedVaryUpdateCacheMiddleware().process_response(request, response) cache_hit = FetchFromCacheMiddleware().process_request(request) self.assertTrue(isinstance(cache_hit, HttpResponse))
def forward_request(client, backend, auth_func): headers_complete = False body_part = None authenticated = False headers = StringIO() if not headers_complete: # Accumulate headers, then parse them. while True: data = client.recv(1024) logger.debug(data) if not data: break header_part, header_end, body_part = data.partition('\r\n\r\n') headers.write(header_part) if header_end: headers.write(header_end) # Build a Django flavor HttpRequest from the raw headers request = DjangoizedHttpRequest(StringIO(headers.getvalue()), client) # Run it through the middleware we need SessionMiddleware().process_request(request) AuthenticationMiddleware().process_request(request) authenticated = auth_func(request) break if authenticated: backend.sendall(headers.getvalue()) if body_part: backend.sendall(body_part) while True: data = client.recv(1024) if not data: break backend.sendall(data) else: # Close backend connection. backend.sendall("") # Respond to client with forbidden body = u"403 Forbidden" response = u"\r\n".join(("HTTP/1.1 403 Forbidden", "Content-Type: text/plain; charset=utf-8", "Content-Length: %s" % len(body), "Connection: close", "\r\n", body)) client.sendall(response) # Close client connection. client.sendall("")