diff --git a/netlib/http/http2/framereader.py b/netlib/http/http2/framereader.py index d45be6461..eb9b069a8 100644 --- a/netlib/http/http2/framereader.py +++ b/netlib/http/http2/framereader.py @@ -1,6 +1,7 @@ import codecs import hyperframe +from ...exceptions import HttpException def http2_read_raw_frame(rfile): @@ -8,7 +9,7 @@ def http2_read_raw_frame(rfile): length = int(codecs.encode(header[:3], 'hex_codec'), 16) if length == 4740180: - raise ValueError("Length field looks more like HTTP/1.1: %s" % rfile.peek(20)) + raise HttpException("Length field looks more like HTTP/1.1:\n{}".format(rfile.read(-1))) body = rfile.safe_read(length) return [header, body] diff --git a/test/mitmproxy/test_protocol_http2.py b/test/mitmproxy/test_protocol_http2.py index 23072260c..932c8df2e 100644 --- a/test/mitmproxy/test_protocol_http2.py +++ b/test/mitmproxy/test_protocol_http2.py @@ -13,6 +13,7 @@ from mitmproxy.cmdline import APP_HOST, APP_PORT import netlib from ..netlib import tservers as netlib_tservers +from netlib.exceptions import HttpException from netlib.http.http2 import framereader from . import tservers @@ -50,6 +51,9 @@ class _Http2ServerBase(netlib_tservers.ServerTestBase): try: raw = b''.join(framereader.http2_read_raw_frame(self.rfile)) events = h2_conn.receive_data(raw) + except HttpException: + print(traceback.format_exc()) + assert False except: break self.wfile.write(h2_conn.data_to_send()) @@ -60,9 +64,7 @@ class _Http2ServerBase(netlib_tservers.ServerTestBase): if not self.server.handle_server_event(event, h2_conn, self.rfile, self.wfile): done = True break - except Exception as e: - print(repr(e)) - print(traceback.format_exc()) + except: done = True break @@ -200,9 +202,12 @@ class TestSimple(_Http2TestBase, _Http2ServerBase): done = False while not done: try: - events = h2_conn.receive_data(b''.join(framereader.http2_read_raw_frame(client.rfile))) - except: - break + raw = b''.join(framereader.http2_read_raw_frame(client.rfile)) + events = h2_conn.receive_data(raw) + except HttpException: + print(traceback.format_exc()) + assert False + client.wfile.write(h2_conn.data_to_send()) client.wfile.flush() @@ -270,9 +275,12 @@ class TestWithBodies(_Http2TestBase, _Http2ServerBase): done = False while not done: try: - events = h2_conn.receive_data(b''.join(framereader.http2_read_raw_frame(client.rfile))) - except: - break + raw = b''.join(framereader.http2_read_raw_frame(client.rfile)) + events = h2_conn.receive_data(raw) + except HttpException: + print(traceback.format_exc()) + assert False + client.wfile.write(h2_conn.data_to_send()) client.wfile.flush() @@ -364,6 +372,9 @@ class TestPushPromise(_Http2TestBase, _Http2ServerBase): try: raw = b''.join(framereader.http2_read_raw_frame(client.rfile)) events = h2_conn.receive_data(raw) + except HttpException: + print(traceback.format_exc()) + assert False except: break client.wfile.write(h2_conn.data_to_send()) @@ -412,9 +423,12 @@ class TestPushPromise(_Http2TestBase, _Http2ServerBase): responses = 0 while not done: try: - events = h2_conn.receive_data(b''.join(framereader.http2_read_raw_frame(client.rfile))) - except: - break + raw = b''.join(framereader.http2_read_raw_frame(client.rfile)) + events = h2_conn.receive_data(raw) + except HttpException: + print(traceback.format_exc()) + assert False + client.wfile.write(h2_conn.data_to_send()) client.wfile.flush() @@ -481,6 +495,9 @@ class TestConnectionLost(_Http2TestBase, _Http2ServerBase): try: raw = b''.join(framereader.http2_read_raw_frame(client.rfile)) h2_conn.receive_data(raw) + except HttpException: + print(traceback.format_exc()) + assert False except: break try: