Skip to content

Commit

Permalink
rewrite absolute-form HTTP requests to relative form, fix #1759 (#1765)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhils authored and Kriechi committed Nov 20, 2016
1 parent 79c753d commit f74e561
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
5 changes: 5 additions & 0 deletions mitmproxy/proxy/protocol/http.py
Expand Up @@ -268,6 +268,11 @@ def _process_flow(self, f):

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
if self.config.options.mode == "reverse":
f.request.headers["Host"] = self.config.upstream_server.address.host
Expand Down
2 changes: 2 additions & 0 deletions pathod/pathod.py
Expand Up @@ -144,6 +144,7 @@ def handle_http_request(self, logger):
path = req.path
http_version = req.http_version
headers = req.headers
first_line_format = req.first_line_format

clientcert = None
if self.clientcert:
Expand All @@ -167,6 +168,7 @@ def handle_http_request(self, logger):
sni=self.sni,
remote_address=self.address(),
clientcert=clientcert,
first_line_format=first_line_format
),
cipher=None,
)
Expand Down
15 changes: 15 additions & 0 deletions test/mitmproxy/test_server.py
Expand Up @@ -282,6 +282,21 @@ def test_stream_modify(self):
assert d.content == b"bar"
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):
def test_auth(self):
Expand Down

0 comments on commit f74e561

Please sign in to comment.