-
-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathbackports.py
More file actions
27 lines (21 loc) · 846 Bytes
/
backports.py
File metadata and controls
27 lines (21 loc) · 846 Bytes
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
"""OpenAPI core contrib django backports module"""
class HttpHeaders(dict):
HTTP_PREFIX = 'HTTP_'
# PEP 333 gives two headers which aren't prepended with HTTP_.
UNPREFIXED_HEADERS = {'CONTENT_TYPE', 'CONTENT_LENGTH'}
def __init__(self, environ):
headers = {}
for header, value in list(environ.items()):
name = self.parse_header_name(header)
if name:
headers[name] = value
super().__init__(headers)
@classmethod
def parse_header_name(cls, header):
if header.startswith(cls.HTTP_PREFIX):
header = header[len(cls.HTTP_PREFIX):]
elif header not in cls.UNPREFIXED_HEADERS:
return None
return header.replace('_', '-').title()
def request_current_scheme_host(req):
return f'{req.scheme}://{req.get_host()}'