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: