2015-09-25 22:39:04 +00:00
|
|
|
from __future__ import absolute_import, print_function, division
|
|
|
|
|
2016-02-17 03:18:59 +00:00
|
|
|
import re
|
2015-09-25 22:39:04 +00:00
|
|
|
import warnings
|
|
|
|
|
|
|
|
import six
|
|
|
|
from six.moves import urllib
|
|
|
|
|
|
|
|
from netlib import utils
|
|
|
|
from netlib.http import cookies
|
|
|
|
from netlib.odict import ODict
|
|
|
|
from .. import encoding
|
|
|
|
from .headers import Headers
|
2015-09-26 22:49:41 +00:00
|
|
|
from .message import Message, _native, _always_bytes, MessageData
|
2015-09-25 22:39:04 +00:00
|
|
|
|
2016-02-17 03:18:59 +00:00
|
|
|
# This regex extracts & splits the host header into host and port.
|
|
|
|
# Handles the edge case of IPv6 addresses containing colons.
|
|
|
|
# https://bugzilla.mozilla.org/show_bug.cgi?id=45891
|
|
|
|
host_header_re = re.compile(r"^(?P<host>[^:]+|\[.+\])(?::(?P<port>\d+))?$")
|
2015-09-25 22:39:04 +00:00
|
|
|
|
2015-09-26 22:49:41 +00:00
|
|
|
class RequestData(MessageData):
|
2015-09-25 22:39:04 +00:00
|
|
|
def __init__(self, first_line_format, method, scheme, host, port, path, http_version, headers=None, content=None,
|
|
|
|
timestamp_start=None, timestamp_end=None):
|
2016-02-08 03:16:58 +00:00
|
|
|
if not isinstance(headers, Headers):
|
|
|
|
headers = Headers(headers)
|
2015-09-25 22:39:04 +00:00
|
|
|
|
|
|
|
self.first_line_format = first_line_format
|
|
|
|
self.method = method
|
|
|
|
self.scheme = scheme
|
|
|
|
self.host = host
|
|
|
|
self.port = port
|
|
|
|
self.path = path
|
|
|
|
self.http_version = http_version
|
|
|
|
self.headers = headers
|
|
|
|
self.content = content
|
|
|
|
self.timestamp_start = timestamp_start
|
|
|
|
self.timestamp_end = timestamp_end
|
|
|
|
|
|
|
|
|
|
|
|
class Request(Message):
|
|
|
|
"""
|
|
|
|
An HTTP request.
|
|
|
|
"""
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
data = RequestData(*args, **kwargs)
|
|
|
|
super(Request, self).__init__(data)
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
if self.host and self.port:
|
|
|
|
hostport = "{}:{}".format(self.host, self.port)
|
|
|
|
else:
|
|
|
|
hostport = ""
|
|
|
|
path = self.path or ""
|
2015-09-26 15:39:50 +00:00
|
|
|
return "Request({} {}{})".format(
|
2015-09-25 22:39:04 +00:00
|
|
|
self.method, hostport, path
|
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def first_line_format(self):
|
|
|
|
"""
|
|
|
|
HTTP request form as defined in `RFC7230 <https://tools.ietf.org/html/rfc7230#section-5.3>`_.
|
|
|
|
|
|
|
|
origin-form and asterisk-form are subsumed as "relative".
|
|
|
|
"""
|
|
|
|
return self.data.first_line_format
|
|
|
|
|
|
|
|
@first_line_format.setter
|
|
|
|
def first_line_format(self, first_line_format):
|
|
|
|
self.data.first_line_format = first_line_format
|
|
|
|
|
|
|
|
@property
|
|
|
|
def method(self):
|
|
|
|
"""
|
|
|
|
HTTP request method, e.g. "GET".
|
|
|
|
"""
|
2015-09-28 11:53:59 +00:00
|
|
|
return _native(self.data.method).upper()
|
2015-09-25 22:39:04 +00:00
|
|
|
|
|
|
|
@method.setter
|
|
|
|
def method(self, method):
|
|
|
|
self.data.method = _always_bytes(method)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def scheme(self):
|
|
|
|
"""
|
|
|
|
HTTP request scheme, which should be "http" or "https".
|
|
|
|
"""
|
|
|
|
return _native(self.data.scheme)
|
|
|
|
|
|
|
|
@scheme.setter
|
|
|
|
def scheme(self, scheme):
|
|
|
|
self.data.scheme = _always_bytes(scheme)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def host(self):
|
|
|
|
"""
|
2015-09-26 15:39:50 +00:00
|
|
|
Target host. This may be parsed from the raw request
|
|
|
|
(e.g. from a ``GET http://example.com/ HTTP/1.1`` request line)
|
2015-09-25 22:39:04 +00:00
|
|
|
or inferred from the proxy mode (e.g. an IP in transparent mode).
|
2015-11-01 17:20:00 +00:00
|
|
|
|
|
|
|
Setting the host attribute also updates the host header, if present.
|
2015-09-25 22:39:04 +00:00
|
|
|
"""
|
|
|
|
|
2015-09-26 18:07:11 +00:00
|
|
|
if six.PY2: # pragma: nocover
|
2015-09-25 22:39:04 +00:00
|
|
|
return self.data.host
|
|
|
|
|
|
|
|
if not self.data.host:
|
|
|
|
return self.data.host
|
|
|
|
try:
|
|
|
|
return self.data.host.decode("idna")
|
|
|
|
except UnicodeError:
|
|
|
|
return self.data.host.decode("utf8", "surrogateescape")
|
|
|
|
|
|
|
|
@host.setter
|
|
|
|
def host(self, host):
|
|
|
|
if isinstance(host, six.text_type):
|
|
|
|
try:
|
|
|
|
# There's no non-strict mode for IDNA encoding.
|
|
|
|
# We don't want this operation to fail though, so we try
|
|
|
|
# utf8 as a last resort.
|
|
|
|
host = host.encode("idna", "strict")
|
|
|
|
except UnicodeError:
|
|
|
|
host = host.encode("utf8", "surrogateescape")
|
|
|
|
|
|
|
|
self.data.host = host
|
|
|
|
|
|
|
|
# Update host header
|
|
|
|
if "host" in self.headers:
|
|
|
|
if host:
|
|
|
|
self.headers["host"] = host
|
|
|
|
else:
|
|
|
|
self.headers.pop("host")
|
|
|
|
|
|
|
|
@property
|
|
|
|
def port(self):
|
|
|
|
"""
|
|
|
|
Target port
|
|
|
|
"""
|
|
|
|
return self.data.port
|
|
|
|
|
|
|
|
@port.setter
|
|
|
|
def port(self, port):
|
|
|
|
self.data.port = port
|
|
|
|
|
|
|
|
@property
|
|
|
|
def path(self):
|
|
|
|
"""
|
|
|
|
HTTP request path, e.g. "/index.html".
|
|
|
|
Guaranteed to start with a slash.
|
|
|
|
"""
|
2016-03-20 22:22:50 +00:00
|
|
|
if self.data.path is None:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return _native(self.data.path)
|
2015-09-25 22:39:04 +00:00
|
|
|
|
|
|
|
@path.setter
|
|
|
|
def path(self, path):
|
|
|
|
self.data.path = _always_bytes(path)
|
|
|
|
|
2015-09-26 15:39:50 +00:00
|
|
|
@property
|
|
|
|
def url(self):
|
|
|
|
"""
|
|
|
|
The URL string, constructed from the request's URL components
|
|
|
|
"""
|
|
|
|
return utils.unparse_url(self.scheme, self.host, self.port, self.path)
|
|
|
|
|
|
|
|
@url.setter
|
|
|
|
def url(self, url):
|
|
|
|
self.scheme, self.host, self.port, self.path = utils.parse_url(url)
|
|
|
|
|
2016-02-17 03:18:59 +00:00
|
|
|
def _parse_host_header(self):
|
|
|
|
"""Extract the host and port from Host header"""
|
|
|
|
if "host" not in self.headers:
|
|
|
|
return None, None
|
|
|
|
host, port = self.headers["host"], None
|
|
|
|
m = host_header_re.match(host)
|
|
|
|
if m:
|
|
|
|
host = m.group("host").strip("[]")
|
|
|
|
if m.group("port"):
|
|
|
|
port = int(m.group("port"))
|
|
|
|
return host, port
|
|
|
|
|
2015-09-26 15:39:50 +00:00
|
|
|
@property
|
|
|
|
def pretty_host(self):
|
|
|
|
"""
|
|
|
|
Similar to :py:attr:`host`, but using the Host headers as an additional preferred data source.
|
|
|
|
This is useful in transparent mode where :py:attr:`host` is only an IP address,
|
|
|
|
but may not reflect the actual destination as the Host header could be spoofed.
|
|
|
|
"""
|
2016-02-18 01:31:52 +00:00
|
|
|
host, port = self._parse_host_header()
|
|
|
|
if not host:
|
|
|
|
return self.host
|
|
|
|
if not port:
|
|
|
|
port = 443 if self.scheme == 'https' else 80
|
2016-02-18 14:39:28 +00:00
|
|
|
# Prefer the original address if host header has an unexpected form
|
2016-02-18 01:31:52 +00:00
|
|
|
return host if port == self.port else self.host
|
2015-09-26 15:39:50 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def pretty_url(self):
|
|
|
|
"""
|
|
|
|
Like :py:attr:`url`, but using :py:attr:`pretty_host` instead of :py:attr:`host`.
|
|
|
|
"""
|
|
|
|
if self.first_line_format == "authority":
|
2016-02-18 14:39:28 +00:00
|
|
|
return "%s:%d" % (self.pretty_host, self.port)
|
|
|
|
return utils.unparse_url(self.scheme, self.pretty_host, self.port, self.path)
|
2015-09-26 15:39:50 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def query(self):
|
|
|
|
"""
|
|
|
|
The request query string as an :py:class:`ODict` object.
|
|
|
|
None, if there is no query.
|
|
|
|
"""
|
|
|
|
_, _, _, _, query, _ = urllib.parse.urlparse(self.url)
|
|
|
|
if query:
|
|
|
|
return ODict(utils.urldecode(query))
|
|
|
|
return None
|
|
|
|
|
|
|
|
@query.setter
|
|
|
|
def query(self, odict):
|
|
|
|
query = utils.urlencode(odict.lst)
|
|
|
|
scheme, netloc, path, params, _, fragment = urllib.parse.urlparse(self.url)
|
2016-02-16 00:21:20 +00:00
|
|
|
_, _, _, self.path = utils.parse_url(
|
|
|
|
urllib.parse.urlunparse([scheme, netloc, path, params, query, fragment]))
|
2015-09-26 15:39:50 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def cookies(self):
|
|
|
|
"""
|
|
|
|
The request cookies.
|
|
|
|
An empty :py:class:`ODict` object if the cookie monster ate them all.
|
|
|
|
"""
|
|
|
|
ret = ODict()
|
|
|
|
for i in self.headers.get_all("Cookie"):
|
|
|
|
ret.extend(cookies.parse_cookie_header(i))
|
|
|
|
return ret
|
|
|
|
|
|
|
|
@cookies.setter
|
|
|
|
def cookies(self, odict):
|
|
|
|
self.headers["cookie"] = cookies.format_cookie_header(odict)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def path_components(self):
|
|
|
|
"""
|
|
|
|
The URL's path components as a list of strings.
|
|
|
|
Components are unquoted.
|
|
|
|
"""
|
|
|
|
_, _, path, _, _, _ = urllib.parse.urlparse(self.url)
|
|
|
|
return [urllib.parse.unquote(i) for i in path.split("/") if i]
|
|
|
|
|
|
|
|
@path_components.setter
|
|
|
|
def path_components(self, components):
|
|
|
|
components = map(lambda x: urllib.parse.quote(x, safe=""), components)
|
|
|
|
path = "/" + "/".join(components)
|
|
|
|
scheme, netloc, _, params, query, fragment = urllib.parse.urlparse(self.url)
|
2016-02-16 00:21:20 +00:00
|
|
|
_, _, _, self.path = utils.parse_url(
|
|
|
|
urllib.parse.urlunparse([scheme, netloc, path, params, query, fragment]))
|
2015-09-26 15:39:50 +00:00
|
|
|
|
2015-09-25 22:39:04 +00:00
|
|
|
def anticache(self):
|
|
|
|
"""
|
|
|
|
Modifies this request to remove headers that might produce a cached
|
|
|
|
response. That is, we remove ETags and If-Modified-Since headers.
|
|
|
|
"""
|
|
|
|
delheaders = [
|
|
|
|
"if-modified-since",
|
|
|
|
"if-none-match",
|
|
|
|
]
|
|
|
|
for i in delheaders:
|
|
|
|
self.headers.pop(i, None)
|
|
|
|
|
|
|
|
def anticomp(self):
|
|
|
|
"""
|
|
|
|
Modifies this request to remove headers that will compress the
|
|
|
|
resource's data.
|
|
|
|
"""
|
|
|
|
self.headers["accept-encoding"] = "identity"
|
|
|
|
|
|
|
|
def constrain_encoding(self):
|
|
|
|
"""
|
|
|
|
Limits the permissible Accept-Encoding values, based on what we can
|
|
|
|
decode appropriately.
|
|
|
|
"""
|
|
|
|
accept_encoding = self.headers.get("accept-encoding")
|
|
|
|
if accept_encoding:
|
|
|
|
self.headers["accept-encoding"] = (
|
|
|
|
', '.join(
|
|
|
|
e
|
|
|
|
for e in encoding.ENCODINGS
|
|
|
|
if e in accept_encoding
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def urlencoded_form(self):
|
|
|
|
"""
|
2015-09-26 15:39:50 +00:00
|
|
|
The URL-encoded form data as an :py:class:`ODict` object.
|
2015-09-25 22:39:04 +00:00
|
|
|
None if there is no data or the content-type indicates non-form data.
|
|
|
|
"""
|
|
|
|
is_valid_content_type = "application/x-www-form-urlencoded" in self.headers.get("content-type", "").lower()
|
|
|
|
if self.content and is_valid_content_type:
|
|
|
|
return ODict(utils.urldecode(self.content))
|
|
|
|
return None
|
|
|
|
|
|
|
|
@urlencoded_form.setter
|
|
|
|
def urlencoded_form(self, odict):
|
|
|
|
"""
|
|
|
|
Sets the body to the URL-encoded form data, and adds the appropriate content-type header.
|
|
|
|
This will overwrite the existing content if there is one.
|
|
|
|
"""
|
|
|
|
self.headers["content-type"] = "application/x-www-form-urlencoded"
|
|
|
|
self.content = utils.urlencode(odict.lst)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def multipart_form(self):
|
|
|
|
"""
|
2015-09-26 15:39:50 +00:00
|
|
|
The multipart form data as an :py:class:`ODict` object.
|
2015-09-25 22:39:04 +00:00
|
|
|
None if there is no data or the content-type indicates non-form data.
|
|
|
|
"""
|
|
|
|
is_valid_content_type = "multipart/form-data" in self.headers.get("content-type", "").lower()
|
|
|
|
if self.content and is_valid_content_type:
|
|
|
|
return ODict(utils.multipartdecode(self.headers,self.content))
|
|
|
|
return None
|
|
|
|
|
|
|
|
@multipart_form.setter
|
2016-02-08 01:10:48 +00:00
|
|
|
def multipart_form(self, value):
|
2015-09-25 22:39:04 +00:00
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
# Legacy
|
|
|
|
|
2015-09-26 18:07:11 +00:00
|
|
|
def get_cookies(self): # pragma: nocover
|
2015-09-25 22:39:04 +00:00
|
|
|
warnings.warn(".get_cookies is deprecated, use .cookies instead.", DeprecationWarning)
|
|
|
|
return self.cookies
|
|
|
|
|
2015-09-26 18:07:11 +00:00
|
|
|
def set_cookies(self, odict): # pragma: nocover
|
2015-09-25 22:39:04 +00:00
|
|
|
warnings.warn(".set_cookies is deprecated, use .cookies instead.", DeprecationWarning)
|
|
|
|
self.cookies = odict
|
|
|
|
|
2015-09-26 18:07:11 +00:00
|
|
|
def get_query(self): # pragma: nocover
|
2015-09-25 22:39:04 +00:00
|
|
|
warnings.warn(".get_query is deprecated, use .query instead.", DeprecationWarning)
|
|
|
|
return self.query or ODict([])
|
|
|
|
|
2015-09-26 18:07:11 +00:00
|
|
|
def set_query(self, odict): # pragma: nocover
|
2015-09-25 22:39:04 +00:00
|
|
|
warnings.warn(".set_query is deprecated, use .query instead.", DeprecationWarning)
|
|
|
|
self.query = odict
|
|
|
|
|
2015-09-26 18:07:11 +00:00
|
|
|
def get_path_components(self): # pragma: nocover
|
2015-09-25 22:39:04 +00:00
|
|
|
warnings.warn(".get_path_components is deprecated, use .path_components instead.", DeprecationWarning)
|
|
|
|
return self.path_components
|
|
|
|
|
2015-09-26 18:07:11 +00:00
|
|
|
def set_path_components(self, lst): # pragma: nocover
|
2015-09-25 22:39:04 +00:00
|
|
|
warnings.warn(".set_path_components is deprecated, use .path_components instead.", DeprecationWarning)
|
|
|
|
self.path_components = lst
|
|
|
|
|
2015-09-26 18:07:11 +00:00
|
|
|
def get_form_urlencoded(self): # pragma: nocover
|
2015-09-25 22:39:04 +00:00
|
|
|
warnings.warn(".get_form_urlencoded is deprecated, use .urlencoded_form instead.", DeprecationWarning)
|
|
|
|
return self.urlencoded_form or ODict([])
|
|
|
|
|
2015-09-26 18:07:11 +00:00
|
|
|
def set_form_urlencoded(self, odict): # pragma: nocover
|
2015-09-25 22:39:04 +00:00
|
|
|
warnings.warn(".set_form_urlencoded is deprecated, use .urlencoded_form instead.", DeprecationWarning)
|
|
|
|
self.urlencoded_form = odict
|
|
|
|
|
2015-09-26 18:07:11 +00:00
|
|
|
def get_form_multipart(self): # pragma: nocover
|
2015-09-25 22:39:04 +00:00
|
|
|
warnings.warn(".get_form_multipart is deprecated, use .multipart_form instead.", DeprecationWarning)
|
|
|
|
return self.multipart_form or ODict([])
|
|
|
|
|
|
|
|
@property
|
2015-09-26 18:07:11 +00:00
|
|
|
def form_in(self): # pragma: nocover
|
2015-09-25 22:39:04 +00:00
|
|
|
warnings.warn(".form_in is deprecated, use .first_line_format instead.", DeprecationWarning)
|
|
|
|
return self.first_line_format
|
|
|
|
|
|
|
|
@form_in.setter
|
2015-09-26 18:07:11 +00:00
|
|
|
def form_in(self, form_in): # pragma: nocover
|
2015-09-25 22:39:04 +00:00
|
|
|
warnings.warn(".form_in is deprecated, use .first_line_format instead.", DeprecationWarning)
|
|
|
|
self.first_line_format = form_in
|
|
|
|
|
|
|
|
@property
|
2015-09-26 18:07:11 +00:00
|
|
|
def form_out(self): # pragma: nocover
|
2015-09-25 22:39:04 +00:00
|
|
|
warnings.warn(".form_out is deprecated, use .first_line_format instead.", DeprecationWarning)
|
|
|
|
return self.first_line_format
|
|
|
|
|
|
|
|
@form_out.setter
|
2015-09-26 18:07:11 +00:00
|
|
|
def form_out(self, form_out): # pragma: nocover
|
2015-09-25 22:39:04 +00:00
|
|
|
warnings.warn(".form_out is deprecated, use .first_line_format instead.", DeprecationWarning)
|
2016-02-16 00:21:20 +00:00
|
|
|
self.first_line_format = form_out
|
|
|
|
|