-
Notifications
You must be signed in to change notification settings - Fork 436
/
Copy pathmiddleware.py
101 lines (78 loc) · 3.35 KB
/
middleware.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# Copyright The IETF Trust 2007-2020, All Rights Reserved
# -*- coding: utf-8 -*-
from django.db import connection
from django.db.utils import OperationalError
from django.shortcuts import render
from django.http import HttpResponsePermanentRedirect
from ietf.utils.log import log, exc_parts
from ietf.utils.mail import log_smtp_exception
import re
import smtplib
import unicodedata
def sql_log_middleware(get_response):
def sql_log(request):
response = get_response(request)
for q in connection.queries:
if re.match("(update|insert)", q["sql"], re.IGNORECASE):
log(q["sql"])
return response
return sql_log
class SMTPExceptionMiddleware(object):
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
return self.get_response(request)
def process_exception(self, request, exception):
if isinstance(exception, smtplib.SMTPException):
(extype, value, tb) = log_smtp_exception(exception)
return render(
request,
"email_failed.html",
{"exception": extype, "args": value, "traceback": "".join(tb)},
)
return None
class Utf8ExceptionMiddleware(object):
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
return self.get_response(request)
def process_exception(self, request, exception):
if isinstance(exception, OperationalError):
extype, e, tb = exc_parts()
if e.args[0] == 1366:
log("Database 4-byte utf8 exception: %s: %s" % (extype, e))
return render(
request,
"utf8_4byte_failed.html",
{"exception": extype, "args": e.args, "traceback": "".join(tb)},
)
return None
def redirect_trailing_period_middleware(get_response):
def redirect_trailing_period(request):
response = get_response(request)
if response.status_code == 404 and request.path.endswith("."):
return HttpResponsePermanentRedirect(request.path.rstrip("."))
return response
return redirect_trailing_period
def unicode_nfkc_normalization_middleware(get_response):
def unicode_nfkc_normalization(request):
"""Do Unicode NFKC normalization to turn ligatures into individual characters.
This was prompted by somebody actually requesting an url for /wg/ipfix/charter
where the 'fi' was composed of an \\ufb01 ligature...
There are probably other elements of a request which may need this normalization
too, but let's put that in as it comes up, rather than guess ahead.
"""
request.META["PATH_INFO"] = unicodedata.normalize(
"NFKC", request.META["PATH_INFO"]
)
request.path_info = unicodedata.normalize("NFKC", request.path_info)
response = get_response(request)
return response
return unicode_nfkc_normalization
def is_authenticated_header_middleware(get_response):
"""Middleware to add an is-authenticated header to the response"""
def add_header(request):
response = get_response(request)
response["X-Datatracker-Is-Authenticated"] = "yes" if request.user.is_authenticated else "no"
return response
return add_header