add h2 trailers example and fix sending

This commit is contained in:
Thomas Kriechbaumer 2020-07-03 12:49:31 +02:00
parent ebb061796c
commit 288ce65d73
2 changed files with 29 additions and 3 deletions

View File

@ -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"])

View File

@ -615,7 +615,7 @@ class Http2SingleStreamLayer(httpbase._HttpTransmissionLayer, basethread.BaseThr
@detect_zombie_stream @detect_zombie_stream
def send_request_trailers(self, request): 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 @detect_zombie_stream
def send_request(self, request): def send_request(self, request):
@ -686,8 +686,8 @@ class Http2SingleStreamLayer(httpbase._HttpTransmissionLayer, basethread.BaseThr
) )
@detect_zombie_stream @detect_zombie_stream
def send_response_trailers(self, _response): def send_response_trailers(self, response):
self._send_trailers(self.client_conn, self.response_trailers) self._send_trailers(self.client_conn, response.trailers)
def _send_trailers(self, conn, trailers): def _send_trailers(self, conn, trailers):
if not trailers: if not trailers: