Merge pull request #192 from mitmproxy/refactor_read_http_body

move CONTINUE checks into mitmproxy
This commit is contained in:
Aldo Cortesi 2014-01-04 14:37:47 -08:00
commit d5f9b02615
2 changed files with 18 additions and 8 deletions

View File

@ -424,8 +424,9 @@ class ProxyHandler(tcp.BaseHandler):
raise ProxyError(400, "Bad HTTP request line: %s"%repr(line)) raise ProxyError(400, "Bad HTTP request line: %s"%repr(line))
method, scheme, host, port, path, httpversion = r method, scheme, host, port, path, httpversion = r
headers = self.read_headers(authenticate=True) headers = self.read_headers(authenticate=True)
content = http.read_http_body_request( self.handle_expect_header(headers, httpversion)
self.rfile, self.wfile, headers, httpversion, self.config.body_size_limit content = http.read_http_body(
self.rfile, headers, self.config.body_size_limit, True
) )
r = flow.Request( r = flow.Request(
client_conn, httpversion, host, port, scheme, method, path, headers, content, client_conn, httpversion, host, port, scheme, method, path, headers, content,
@ -457,8 +458,9 @@ class ProxyHandler(tcp.BaseHandler):
raise ProxyError(400, "Bad HTTP request line: %s"%repr(line)) raise ProxyError(400, "Bad HTTP request line: %s"%repr(line))
method, path, httpversion = r method, path, httpversion = r
headers = self.read_headers(authenticate=False) headers = self.read_headers(authenticate=False)
content = http.read_http_body_request( self.handle_expect_header(headers, httpversion)
self.rfile, self.wfile, headers, httpversion, self.config.body_size_limit content = http.read_http_body(
self.rfile, headers, self.config.body_size_limit, True
) )
r = flow.Request( r = flow.Request(
client_conn, httpversion, host, port, scheme, method, path, headers, content, client_conn, httpversion, host, port, scheme, method, path, headers, content,
@ -467,6 +469,14 @@ class ProxyHandler(tcp.BaseHandler):
r.set_live(self.rfile, self.wfile) r.set_live(self.rfile, self.wfile)
return r return r
def handle_expect_header(self, headers, httpversion):
if "expect" in headers:
if "100-continue" in headers['expect'] and httpversion >= (1, 1):
#FIXME: Check if content-length is over limit
self.wfile.write('HTTP/1.1 100 Continue\r\n'
'\r\n')
del headers['expect']
def read_headers(self, authenticate=False): def read_headers(self, authenticate=False):
headers = http.read_headers(self.rfile) headers = http.read_headers(self.rfile)
if headers is None: if headers is None:

View File

@ -176,10 +176,10 @@ class TestHTTPAuth(tservers.HTTPProxTest):
class TestHTTPConnectSSLError(tservers.HTTPProxTest): class TestHTTPConnectSSLError(tservers.HTTPProxTest):
certfile = True certfile = True
def test_go(self): def test_go(self):
p = self.pathoc() p = self.pathoc_raw()
req = "connect:'localhost:%s'"%self.proxy.port dst = ("localhost", self.proxy.port)
assert p.request(req).status_code == 200 p.connect(connect_to=dst)
assert p.request(req).status_code == 400 tutils.raises("400 - Bad Request", p.http_connect, dst)
class TestHTTPS(tservers.HTTPProxTest, CommonMixin): class TestHTTPS(tservers.HTTPProxTest, CommonMixin):