/usr/share/pyshared/w3lib/http.py is in python-w3lib 1.5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | from base64 import urlsafe_b64encode
def headers_raw_to_dict(headers_raw):
"""
Convert raw headers (single multi-line string)
to the dictionary.
For example:
>>> headers_raw_to_dict("Content-type: text/html\\n\\rAccept: gzip\\n\\n")
{'Content-type': ['text/html'], 'Accept': ['gzip']}
Incorrect input:
>>> headers_raw_to_dict("Content-typt gzip\\n\\n")
{}
Argument is None:
>>> headers_raw_to_dict(None)
"""
if headers_raw is None:
return None
return dict([
(header_item[0].strip(), [header_item[1].strip()])
for header_item
in [
header.split(':', 1)
for header
in headers_raw.splitlines()]
if len(header_item) == 2])
def headers_dict_to_raw(headers_dict):
"""
Returns a raw HTTP headers representation of headers
For example:
>>> headers_dict_to_raw({'Content-type': 'text/html', 'Accept': 'gzip'})
'Content-type: text/html\\r\\nAccept: gzip'
>>> from twisted.python.util import InsensitiveDict
>>> td = InsensitiveDict({'Content-type': ['text/html'], 'Accept': ['gzip']})
>>> headers_dict_to_raw(td)
'Content-type: text/html\\r\\nAccept: gzip'
Argument is None:
>>> headers_dict_to_raw(None)
"""
if headers_dict is None:
return None
raw_lines = []
for key, value in headers_dict.items():
if isinstance(value, (str, unicode)):
raw_lines.append("%s: %s" % (key, value))
elif isinstance(value, (list, tuple)):
for v in value:
raw_lines.append("%s: %s" % (key, v))
return '\r\n'.join(raw_lines)
def basic_auth_header(username, password):
"""Return `Authorization` header for HTTP Basic Access Authentication (RFC 2617)"""
auth = "%s:%s" % (username, password)
if not isinstance(auth, bytes):
# XXX: RFC 2617 doesn't define encoding, but ISO-8859-1
# seems to be the most widely used encoding here. See also:
# http://greenbytes.de/tech/webdav/draft-ietf-httpauth-basicauth-enc-latest.html
auth = auth.encode('ISO-8859-1')
return 'Basic ' + urlsafe_b64encode(auth).decode('ascii')
|