rewrite absolute-form HTTP requests to relative form, fix #1759 (#1765)

This commit is contained in:
Maximilian Hils 2016-11-20 17:21:21 +01:00 committed by Thomas Kriechbaumer
parent 79c753d8f8
commit f74e561524
3 changed files with 22 additions and 0 deletions

View File

@ -268,6 +268,11 @@ class HttpLayer(base.Layer):
self.log("request", "debug", [repr(request)]) self.log("request", "debug", [repr(request)])
# set first line format to relative in regular mode,
# see https://github.com/mitmproxy/mitmproxy/issues/1759
if self.mode is HTTPMode.regular and request.first_line_format == "absolute":
request.first_line_format = "relative"
# update host header in reverse proxy mode # update host header in reverse proxy mode
if self.config.options.mode == "reverse": if self.config.options.mode == "reverse":
f.request.headers["Host"] = self.config.upstream_server.address.host f.request.headers["Host"] = self.config.upstream_server.address.host

View File

@ -144,6 +144,7 @@ class PathodHandler(tcp.BaseHandler):
path = req.path path = req.path
http_version = req.http_version http_version = req.http_version
headers = req.headers headers = req.headers
first_line_format = req.first_line_format
clientcert = None clientcert = None
if self.clientcert: if self.clientcert:
@ -167,6 +168,7 @@ class PathodHandler(tcp.BaseHandler):
sni=self.sni, sni=self.sni,
remote_address=self.address(), remote_address=self.address(),
clientcert=clientcert, clientcert=clientcert,
first_line_format=first_line_format
), ),
cipher=None, cipher=None,
) )

View File

@ -282,6 +282,21 @@ class TestHTTP(tservers.HTTPProxyTest, CommonMixin):
assert d.content == b"bar" assert d.content == b"bar"
self.master.addons.remove(s) self.master.addons.remove(s)
def test_first_line_rewrite(self):
"""
If mitmproxy is a regular HTTP proxy, it must rewrite an absolute-form request like
GET http://example.com/foo HTTP/1.0
to
GET /foo HTTP/1.0
when sending the request upstream. While any server should technically accept
the absolute form, this is not the case in practice.
"""
req = "get:'%s/p/200'" % self.server.urlbase
p = self.pathoc()
with p.connect():
assert p.request(req).status_code == 200
assert self.server.last_log()["request"]["first_line_format"] == "relative"
class TestHTTPAuth(tservers.HTTPProxyTest): class TestHTTPAuth(tservers.HTTPProxyTest):
def test_auth(self): def test_auth(self):