Changeset View
Changeset View
Standalone View
Standalone View
swh/web/common/middlewares.py
# Copyright (C) 2018-2019 The Software Heritage developers | # Copyright (C) 2018-2019 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU Affero General Public License version 3, or any later version | # License: GNU Affero General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
from django.middleware.cache import FetchFromCacheMiddleware | |||||
from htmlmin import minify | from htmlmin import minify | ||||
import sentry_sdk | import sentry_sdk | ||||
from swh.web.common.utils import prettify_html | from swh.web.common.utils import prettify_html | ||||
class HtmlPrettifyMiddleware(object): | class HtmlPrettifyMiddleware(object): | ||||
""" | """ | ||||
▲ Show 20 Lines • Show All 50 Lines • ▼ Show 20 Lines | def __call__(self, request): | ||||
resp = self.get_response(request) | resp = self.get_response(request) | ||||
if 'RateLimit-Limit' in request.META: | if 'RateLimit-Limit' in request.META: | ||||
resp['X-RateLimit-Limit'] = request.META['RateLimit-Limit'] | resp['X-RateLimit-Limit'] = request.META['RateLimit-Limit'] | ||||
if 'RateLimit-Remaining' in request.META: | if 'RateLimit-Remaining' in request.META: | ||||
resp['X-RateLimit-Remaining'] = request.META['RateLimit-Remaining'] | resp['X-RateLimit-Remaining'] = request.META['RateLimit-Remaining'] | ||||
if 'RateLimit-Reset' in request.META: | if 'RateLimit-Reset' in request.META: | ||||
resp['X-RateLimit-Reset'] = request.META['RateLimit-Reset'] | resp['X-RateLimit-Reset'] = request.META['RateLimit-Reset'] | ||||
return resp | return resp | ||||
class AnonymousFetchFromCacheMiddleware(FetchFromCacheMiddleware): | |||||
""" | |||||
Middleware extending django.middleware.cache.FetchFromCacheMiddleware | |||||
behavior by do not caching requests for authenticated users. | |||||
""" | |||||
def __init__(self, get_response=None): | |||||
super().__init__(get_response) | |||||
def process_request(self, request): | |||||
# Don't cache requests from authenticated users. | |||||
if request.method in ('GET', 'HEAD') and request.user.is_authenticated: | |||||
request._cache_update_cache = False | |||||
request._not_cached = True | |||||
return None | |||||
return super().process_request(request) |