Merge pull request #393 from onlywade/master

Fixing issue #392, adding tests.
This commit is contained in:
Maximilian Hils 2014-11-01 18:07:32 +01:00
commit 8522c6ebd6
2 changed files with 31 additions and 9 deletions

View File

@ -251,7 +251,7 @@ class HTTPRequest(HTTPMessage):
- authority-form (CONNECT example.com:443) - authority-form (CONNECT example.com:443)
Details: http://tools.ietf.org/html/draft-ietf-httpbis-p1-messaging-25#section-5.3 Details: http://tools.ietf.org/html/draft-ietf-httpbis-p1-messaging-25#section-5.3
form_out: The request form which mitmproxy has send out to the form_out: The request form which mitmproxy will send out to the
destination destination
timestamp_start: Timestamp indicating when request transmission started timestamp_start: Timestamp indicating when request transmission started
@ -401,9 +401,8 @@ class HTTPRequest(HTTPMessage):
form = form or self.form_out form = form or self.form_out
if form == "relative": if form == "relative":
path = self.path if self.method != "OPTIONS" else "*"
request_line = '%s %s HTTP/%s.%s' % ( request_line = '%s %s HTTP/%s.%s' % (
self.method, path, self.httpversion[0], self.httpversion[1] self.method, self.path, self.httpversion[0], self.httpversion[1]
) )
elif form == "authority": elif form == "authority":
request_line = '%s %s:%s HTTP/%s.%s' % ( request_line = '%s %s:%s HTTP/%s.%s' % (

View File

@ -23,7 +23,7 @@ def test_stripped_chunked_encoding_no_content():
class TestHTTPRequest: class TestHTTPRequest:
def test_asterisk_form(self): def test_asterisk_form_in(self):
s = StringIO("OPTIONS * HTTP/1.1") s = StringIO("OPTIONS * HTTP/1.1")
f = tutils.tflow(req=None) f = tutils.tflow(req=None)
f.request = HTTPRequest.from_stream(s) f.request = HTTPRequest.from_stream(s)
@ -33,7 +33,7 @@ class TestHTTPRequest:
f.request.scheme = "http" f.request.scheme = "http"
assert f.request.assemble() == "OPTIONS * HTTP/1.1\r\nHost: address:22\r\n\r\n" assert f.request.assemble() == "OPTIONS * HTTP/1.1\r\nHost: address:22\r\n\r\n"
def test_origin_form(self): def test_relative_form_in(self):
s = StringIO("GET /foo\xff HTTP/1.1") s = StringIO("GET /foo\xff HTTP/1.1")
tutils.raises("Bad HTTP request line", HTTPRequest.from_stream, s) tutils.raises("Bad HTTP request line", HTTPRequest.from_stream, s)
s = StringIO("GET /foo HTTP/1.1\r\nConnection: Upgrade\r\nUpgrade: h2c") s = StringIO("GET /foo HTTP/1.1\r\nConnection: Upgrade\r\nUpgrade: h2c")
@ -52,8 +52,7 @@ class TestHTTPRequest:
r.update_host_header() r.update_host_header()
assert "Host" in r.headers assert "Host" in r.headers
def test_authority_form_in(self):
def test_authority_form(self):
s = StringIO("CONNECT oops-no-port.com HTTP/1.1") s = StringIO("CONNECT oops-no-port.com HTTP/1.1")
tutils.raises("Bad HTTP request line", HTTPRequest.from_stream, s) tutils.raises("Bad HTTP request line", HTTPRequest.from_stream, s)
s = StringIO("CONNECT address:22 HTTP/1.1") s = StringIO("CONNECT address:22 HTTP/1.1")
@ -62,13 +61,37 @@ class TestHTTPRequest:
assert r.assemble() == "CONNECT address:22 HTTP/1.1\r\nHost: address:22\r\n\r\n" assert r.assemble() == "CONNECT address:22 HTTP/1.1\r\nHost: address:22\r\n\r\n"
assert r.pretty_url(False) == "address:22" assert r.pretty_url(False) == "address:22"
def test_absolute_form(self): def test_absolute_form_in(self):
s = StringIO("GET oops-no-protocol.com HTTP/1.1") s = StringIO("GET oops-no-protocol.com HTTP/1.1")
tutils.raises("Bad HTTP request line", HTTPRequest.from_stream, s) tutils.raises("Bad HTTP request line", HTTPRequest.from_stream, s)
s = StringIO("GET http://address:22/ HTTP/1.1") s = StringIO("GET http://address:22/ HTTP/1.1")
r = HTTPRequest.from_stream(s) r = HTTPRequest.from_stream(s)
assert r.assemble() == "GET http://address:22/ HTTP/1.1\r\nHost: address:22\r\n\r\n" assert r.assemble() == "GET http://address:22/ HTTP/1.1\r\nHost: address:22\r\n\r\n"
def test_http_options_relative_form_in(self):
"""
Exercises fix for Issue #392.
"""
s = StringIO("OPTIONS /secret/resource HTTP/1.1")
r = HTTPRequest.from_stream(s)
r.host = 'address'
r.port = 80
r.scheme = "http"
assert r.assemble() == ("OPTIONS "
"/secret/resource "
"HTTP/1.1\r\nHost: address\r\n\r\n")
def test_http_options_absolute_form_in(self):
s = StringIO("OPTIONS http://address/secret/resource HTTP/1.1")
r = HTTPRequest.from_stream(s)
r.host = 'address'
r.port = 80
r.scheme = "http"
assert r.assemble() == ("OPTIONS "
"http://address:80/secret/resource "
"HTTP/1.1\r\nHost: address\r\n\r\n")
def test_assemble_unknown_form(self): def test_assemble_unknown_form(self):
r = tutils.treq() r = tutils.treq()
tutils.raises("Invalid request form", r.assemble, "antiauthority") tutils.raises("Invalid request form", r.assemble, "antiauthority")
@ -133,4 +156,4 @@ class TestInvalidRequests(tservers.HTTPProxTest):
p.connect() p.connect()
r = p.request("get:/p/200") r = p.request("get:/p/200")
assert r.status_code == 400 assert r.status_code == 400
assert "Invalid HTTP request form" in r.content assert "Invalid HTTP request form" in r.content