2020-07-03 10:49:31 +00:00
|
|
|
"""
|
|
|
|
This script simply prints all received HTTP Trailers.
|
|
|
|
|
2022-05-30 07:53:44 +00:00
|
|
|
HTTP requests and responses can contain trailing headers which are sent after
|
2020-07-03 10:49:31 +00:00
|
|
|
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
|
2021-02-04 01:14:27 +00:00
|
|
|
from mitmproxy.http import Headers
|
2020-07-03 10:49:31 +00:00
|
|
|
|
2020-07-05 23:01:48 +00:00
|
|
|
|
2020-07-03 10:49:31 +00:00
|
|
|
def request(flow: http.HTTPFlow):
|
|
|
|
if flow.request.trailers:
|
|
|
|
print("HTTP Trailers detected! Request contains:", flow.request.trailers)
|
|
|
|
|
2020-10-09 00:27:36 +00:00
|
|
|
if flow.request.path == "/inject_trailers":
|
2020-10-13 14:06:06 +00:00
|
|
|
if flow.request.is_http10:
|
|
|
|
# HTTP/1.0 doesn't support trailers
|
|
|
|
return
|
|
|
|
elif flow.request.is_http11:
|
|
|
|
if not flow.request.content:
|
|
|
|
# Avoid sending a body on GET requests or a 0 byte chunked body with trailers.
|
|
|
|
# Otherwise some servers return 400 Bad Request.
|
|
|
|
return
|
|
|
|
# HTTP 1.1 requires transfer-encoding: chunked to send trailers
|
2020-10-09 00:27:36 +00:00
|
|
|
flow.request.headers["transfer-encoding"] = "chunked"
|
2020-10-13 14:06:06 +00:00
|
|
|
# HTTP 2+ supports trailers on all requests/responses
|
2020-10-09 00:27:36 +00:00
|
|
|
|
|
|
|
flow.request.headers["trailer"] = "x-my-injected-trailer-header"
|
2022-04-26 11:53:35 +00:00
|
|
|
flow.request.trailers = Headers([(b"x-my-injected-trailer-header", b"foobar")])
|
2020-10-09 00:27:36 +00:00
|
|
|
print("Injected a new request trailer...", flow.request.headers["trailer"])
|
|
|
|
|
2020-07-05 23:01:48 +00:00
|
|
|
|
2020-07-03 10:49:31 +00:00
|
|
|
def response(flow: http.HTTPFlow):
|
|
|
|
if flow.response.trailers:
|
|
|
|
print("HTTP Trailers detected! Response contains:", flow.response.trailers)
|
|
|
|
|
|
|
|
if flow.request.path == "/inject_trailers":
|
2020-10-13 14:06:06 +00:00
|
|
|
if flow.request.is_http10:
|
|
|
|
return
|
|
|
|
elif flow.request.is_http11:
|
|
|
|
if not flow.response.content:
|
|
|
|
return
|
2020-10-09 00:27:36 +00:00
|
|
|
flow.response.headers["transfer-encoding"] = "chunked"
|
|
|
|
|
2020-07-03 10:49:31 +00:00
|
|
|
flow.response.headers["trailer"] = "x-my-injected-trailer-header"
|
2022-04-26 11:53:35 +00:00
|
|
|
flow.response.trailers = Headers([(b"x-my-injected-trailer-header", b"foobar")])
|
2020-10-09 00:27:36 +00:00
|
|
|
print("Injected a new response trailer...", flow.response.headers["trailer"])
|