mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-11-26 18:18:25 +00:00
add h2 trailers example and fix sending
This commit is contained in:
parent
ebb061796c
commit
288ce65d73
26
examples/addons/http-trailers.py
Normal file
26
examples/addons/http-trailers.py
Normal 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"])
|
@ -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:
|
||||||
|
Loading…
Reference in New Issue
Block a user