diff options
-rw-r--r-- | mitmproxy/exceptions.py | 4 | ||||
-rw-r--r-- | mitmproxy/protocol/http.py | 11 | ||||
-rw-r--r-- | mitmproxy/proxy/server.py | 2 | ||||
-rw-r--r-- | netlib/http/response.py | 19 | ||||
-rw-r--r-- | test/netlib/http/test_response.py | 5 |
5 files changed, 26 insertions, 15 deletions
diff --git a/mitmproxy/exceptions.py b/mitmproxy/exceptions.py index c4797e21..6873215c 100644 --- a/mitmproxy/exceptions.py +++ b/mitmproxy/exceptions.py @@ -27,6 +27,10 @@ class Kill(ProxyException): class ProtocolException(ProxyException): + """ + ProtocolExceptions are caused by invalid user input, unavailable network resources, + or other events that are outside of our influence. + """ pass diff --git a/mitmproxy/protocol/http.py b/mitmproxy/protocol/http.py index 1418d6e9..1632e66f 100644 --- a/mitmproxy/protocol/http.py +++ b/mitmproxy/protocol/http.py @@ -153,12 +153,13 @@ class HttpLayer(base.Layer): # We optimistically guess there might be an HTTP client on the # other end self.send_error_response(400, repr(e)) - self.log( - "request", - "warn", - "HTTP protocol error in client request: %s" % e + six.reraise( + exceptions.ProtocolException, + exceptions.ProtocolException( + "HTTP protocol error in client request: {}".format(e) + ), + sys.exc_info()[2] ) - return self.log("request", "debug", [repr(request)]) diff --git a/mitmproxy/proxy/server.py b/mitmproxy/proxy/server.py index 4fd5755a..c5fd5f9e 100644 --- a/mitmproxy/proxy/server.py +++ b/mitmproxy/proxy/server.py @@ -132,7 +132,7 @@ class ConnectionHandler(object): self.log(str(e), "warn") self.log("Invalid certificate, closing connection. Pass --insecure to disable validation.", "warn") else: - self.log(repr(e), "warn") + self.log(str(e), "warn") self.log(traceback.format_exc(), "debug") # If an error propagates to the topmost level, diff --git a/netlib/http/response.py b/netlib/http/response.py index ae29298f..385e233a 100644 --- a/netlib/http/response.py +++ b/netlib/http/response.py @@ -84,15 +84,6 @@ class Response(message.Message): (), None ) - # Assign this manually to update the content-length header. - if isinstance(content, bytes): - resp.content = content - elif isinstance(content, str): - resp.text = content - else: - raise TypeError("Expected content to be str or bytes, but is {}.".format( - type(content).__name__ - )) # Headers can be list or dict, we differentiate here. if isinstance(headers, dict): @@ -104,6 +95,16 @@ class Response(message.Message): type(headers).__name__ )) + # Assign this manually to update the content-length header. + if isinstance(content, bytes): + resp.content = content + elif isinstance(content, str): + resp.text = content + else: + raise TypeError("Expected content to be str or bytes, but is {}.".format( + type(content).__name__ + )) + return resp @property diff --git a/test/netlib/http/test_response.py b/test/netlib/http/test_response.py index c7b1b646..e97cc419 100644 --- a/test/netlib/http/test_response.py +++ b/test/netlib/http/test_response.py @@ -34,6 +34,11 @@ class TestResponseCore(object): assert r.status_code == 200 assert r.content == b"" + r = Response.make(418, "teatime") + assert r.status_code == 418 + assert r.content == b"teatime" + assert r.headers["content-length"] == "7" + Response.make(content=b"foo") Response.make(content="foo") with raises(TypeError): |