middleware: add HTTP pass through header in maintenance middleware (#65096)
This commit is contained in:
parent
905ccd9ac8
commit
c2efde053c
|
@ -19,9 +19,14 @@ from django.http import HttpResponse
|
|||
from django.utils.translation import ugettext as _
|
||||
|
||||
|
||||
def pass_through(remote_addr):
|
||||
pass_through_ips = getattr(settings, 'MAINTENANCE_PASS_THROUGH_IPS', [])
|
||||
if remote_addr in pass_through_ips:
|
||||
def pass_through(request):
|
||||
remote_addr = request.META.get('REMOTE_ADDR')
|
||||
if remote_addr:
|
||||
pass_through_ips = getattr(settings, 'MAINTENANCE_PASS_THROUGH_IPS', [])
|
||||
if remote_addr in pass_through_ips:
|
||||
return True
|
||||
pass_through_header = getattr(settings, 'MAINTENANCE_PASS_THROUGH_HEADER', '')
|
||||
if pass_through_header and pass_through_header in request.headers:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
@ -32,9 +37,7 @@ class MaintenanceMiddleware:
|
|||
|
||||
def __call__(self, request):
|
||||
maintenance_mode = getattr(settings, 'MAINTENANCE_MODE', None)
|
||||
if maintenance_mode:
|
||||
remote_addr = request.META.get('REMOTE_ADDR')
|
||||
if not (remote_addr and pass_through(remote_addr)):
|
||||
maintenance_msg = _('The site is under maintenance')
|
||||
return HttpResponse('<h1>%s</h1>' % maintenance_msg, status=503)
|
||||
if maintenance_mode and not pass_through(request):
|
||||
maintenance_msg = _('The site is under maintenance')
|
||||
return HttpResponse('<h1>%s</h1>' % maintenance_msg, status=503)
|
||||
return self.get_response(request)
|
||||
|
|
|
@ -17,3 +17,7 @@ def test_maintenance_middleware(app, admin_user, db, monkeypatch, settings):
|
|||
|
||||
settings.MAINTENANCE_PASS_THROUGH_IPS = []
|
||||
resp = app.get('/', status=503)
|
||||
|
||||
settings.MAINTENANCE_PASS_THROUGH_HEADER = 'X-Entrouvert'
|
||||
resp = app.get('/', headers={'X-Entrouvert': 'yes'})
|
||||
assert resp.status_code == 200
|
||||
|
|
Loading…
Reference in New Issue