mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-11-26 10:16:27 +00:00
828ba0c2e7
This allow's trailers without the initial Trailer header announcement. In HTTP/2 the stream ends with any frame containing END_SREAM. In the case of trailers, it is a final HEADERS frame after all the DATA frames. Therefore we do not need to explicitly check for the trailer announcement header, but can simply wait until the response message / stream has ended.
29 lines
984 B
Python
29 lines
984 B
Python
"""
|
|
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"])
|