aboutsummaryrefslogtreecommitdiffstats
path: root/netlib/http/request.py
diff options
context:
space:
mode:
authorMaximilian Hils <git@maximilianhils.com>2016-02-18 23:17:02 +0100
committerMaximilian Hils <git@maximilianhils.com>2016-02-18 23:17:02 +0100
commitecb26c3c822ff4f4c61233c8265faed666625677 (patch)
tree7dc3fb338e456fd2ef1dcf4e508b85577e10b408 /netlib/http/request.py
parent7c6bf7abb3c0e94f9c4dfa77fe0690fe11c6d4d3 (diff)
parentd6ab9901d1f4d330a624b1a41d86d8d03c910b7a (diff)
downloadmitmproxy-ecb26c3c822ff4f4c61233c8265faed666625677.tar.gz
mitmproxy-ecb26c3c822ff4f4c61233c8265faed666625677.tar.bz2
mitmproxy-ecb26c3c822ff4f4c61233c8265faed666625677.zip
Merge remote-tracking branch 'duffer/pretty-host'
Diffstat (limited to 'netlib/http/request.py')
-rw-r--r--netlib/http/request.py25
1 files changed, 24 insertions, 1 deletions
diff --git a/netlib/http/request.py b/netlib/http/request.py
index b9076c0f..99662732 100644
--- a/netlib/http/request.py
+++ b/netlib/http/request.py
@@ -1,5 +1,6 @@
from __future__ import absolute_import, print_function, division
+import re
import warnings
import six
@@ -12,6 +13,10 @@ from .. import encoding
from .headers import Headers
from .message import Message, _native, _always_bytes, MessageData
+# 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+))?$")
class RequestData(MessageData):
def __init__(self, first_line_format, method, scheme, host, port, path, http_version, headers=None, content=None,
@@ -159,6 +164,18 @@ class Request(Message):
def url(self, url):
self.scheme, self.host, self.port, self.path = utils.parse_url(url)
+ 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
+
@property
def pretty_host(self):
"""
@@ -166,7 +183,13 @@ class Request(Message):
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.
"""
- return self.headers.get("host", self.host)
+ host, port = self._parse_host_header()
+ if not host:
+ return self.host
+ if not port:
+ port = 443 if self.scheme == 'https' else 80
+ # Prefer the original address if host header has an unexpected form
+ return host if port == self.port else self.host
@property
def pretty_url(self):