def process_response(self, request, response): # It's not worth attempting to compress really short responses. if len(response.content) < 200: return response patch_vary_headers(response, ('Accept-Encoding', )) # Avoid gzipping if we've already got a content-encoding. if response.has_header('Content-Encoding'): return response # MSIE have issues with gzipped response of various content types. if "msie" in request.META.get('HTTP_USER_AGENT', '').lower(): ctype = response.get('Content-Type', '').lower() if not ctype.startswith("text/") or "javascript" in ctype: return response ae = request.META.get('HTTP_ACCEPT_ENCODING', '') if not re_accepts_gzip.search(ae): return response # Return the compressed content only if it's actually shorter. compressed_content = compress_string(response.content) if len(compressed_content) >= len(response.content): return response if response.has_header('ETag'): response['ETag'] = re.sub('"$', ';gzip"', response['ETag']) response.content = compressed_content response['Content-Encoding'] = 'gzip' response['Content-Length'] = str(len(response.content)) return response
def process_response(self, request, response): """ If request.session was modified, or if the configuration is to save the session every time, save the changes and set a session cookie. """ try: accessed = request.session.accessed modified = request.session.modified except AttributeError: pass else: if accessed: patch_vary_headers(response, ('Cookie', )) if modified or settings.SESSION_SAVE_EVERY_REQUEST: if request.session.get_expire_at_browser_close(): max_age = None expires = None else: max_age = request.session.get_expiry_age() expires_time = time.time() + max_age expires = cookie_date(expires_time) # Save the session data and refresh the client cookie. request.session.save() response.set_cookie(settings.SESSION_COOKIE_NAME, request.session.session_key, max_age=max_age, expires=expires, domain=settings.SESSION_COOKIE_DOMAIN, path=settings.SESSION_COOKIE_PATH, secure=settings.SESSION_COOKIE_SECURE or None, httponly=settings.SESSION_COOKIE_HTTPONLY or None) return response
def process_response(self, request, response): if getattr(response, 'csrf_processing_done', False): return response # If CSRF_COOKIE is unset, then CsrfViewMiddleware.process_view was # never called, probaby because a request middleware returned a response # (for example, contrib.auth redirecting to a login page). if request.META.get("CSRF_COOKIE") is None: return response if not request.META.get("CSRF_COOKIE_USED", False): return response # Set the CSRF cookie even if it's already set, so we renew # the expiry timer. response.set_cookie(settings.CSRF_COOKIE_NAME, request.META["CSRF_COOKIE"], max_age=60 * 60 * 24 * 7 * 52, domain=settings.CSRF_COOKIE_DOMAIN, path=settings.CSRF_COOKIE_PATH, secure=settings.CSRF_COOKIE_SECURE) # Content varies with the CSRF cookie, so set the Vary header. patch_vary_headers(response, ('Cookie', )) response.csrf_processing_done = True return response
def process_response(self, request, response): language = translation.get_language() if (response.status_code == 404 and not translation.get_language_from_path(request.path_info) and self.is_language_prefix_patterns_used()): urlconf = getattr(request, 'urlconf', None) language_path = '/%s%s' % (language, request.path_info) if settings.APPEND_SLASH and not language_path.endswith('/'): language_path = language_path + '/' if is_valid_path(language_path, urlconf): language_url = "%s://%s/%s%s" % ( request.is_secure() and 'https' or 'http', request.get_host(), language, request.get_full_path()) return HttpResponseRedirect(language_url) translation.deactivate() patch_vary_headers(response, ('Accept-Language', )) if 'Content-Language' not in response: response['Content-Language'] = language return response
def inner_func(*args, **kwargs): response = func(*args, **kwargs) patch_vary_headers(response, ('Cookie', )) return response
def inner_func(*args, **kwargs): response = func(*args, **kwargs) patch_vary_headers(response, headers) return response