From 288ce65d73551a13c818733c78689206110e9ef1 Mon Sep 17 00:00:00 2001 From: Thomas Kriechbaumer Date: Fri, 3 Jul 2020 12:49:31 +0200 Subject: [PATCH] add h2 trailers example and fix sending --- examples/addons/http-trailers.py | 26 ++++++++++++++++++++++++++ mitmproxy/proxy/protocol/http2.py | 6 +++--- 2 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 examples/addons/http-trailers.py diff --git a/examples/addons/http-trailers.py b/examples/addons/http-trailers.py new file mode 100644 index 000000000..77b9d1a40 --- /dev/null +++ b/examples/addons/http-trailers.py @@ -0,0 +1,26 @@ +""" +This script simply prints all received HTTP Trailers. + +HTTP requests and responses can container trailing headers which are sent after +the body is fully transmitted. Such trailers need to be announced in the initial +headers by name, so the receiving endpoint can wait and read them after the +body. +""" + +from mitmproxy import http +from mitmproxy.net.http import Headers + +def request(flow: http.HTTPFlow): + if flow.request.trailers: + print("HTTP Trailers detected! Request contains:", flow.request.trailers) + +def response(flow: http.HTTPFlow): + if flow.response.trailers: + print("HTTP Trailers detected! Response contains:", flow.response.trailers) + + if flow.request.path == "/inject_trailers": + flow.response.headers["trailer"] = "x-my-injected-trailer-header" + flow.response.trailers = Headers([ + (b"x-my-injected-trailer-header", b"foobar") + ]) + print("Injected a new trailer...", flow.response.headers["trailer"]) diff --git a/mitmproxy/proxy/protocol/http2.py b/mitmproxy/proxy/protocol/http2.py index 9275e6bdb..602946f65 100644 --- a/mitmproxy/proxy/protocol/http2.py +++ b/mitmproxy/proxy/protocol/http2.py @@ -615,7 +615,7 @@ class Http2SingleStreamLayer(httpbase._HttpTransmissionLayer, basethread.BaseThr @detect_zombie_stream def send_request_trailers(self, request): - self._send_trailers(self.server_conn, self.request_trailers) + self._send_trailers(self.server_conn, request.trailers) @detect_zombie_stream def send_request(self, request): @@ -686,8 +686,8 @@ class Http2SingleStreamLayer(httpbase._HttpTransmissionLayer, basethread.BaseThr ) @detect_zombie_stream - def send_response_trailers(self, _response): - self._send_trailers(self.client_conn, self.response_trailers) + def send_response_trailers(self, response): + self._send_trailers(self.client_conn, response.trailers) def _send_trailers(self, conn, trailers): if not trailers: