Skip to content

Commit 777fc9a

Browse files
ruff format
1 parent 482399f commit 777fc9a

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

src/python/mitmproxy_addon.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44
import websockets
55
import json
66

7+
78
async def send_ready_notification() -> None:
89
uri = "ws://localhost:8001"
9-
logging.info(f'sending mitmproxyReady JSON-RPC notification to {uri}')
10+
logging.info(f"sending mitmproxyReady JSON-RPC notification to {uri}")
1011
async with websockets.connect(uri) as websocket:
11-
notification_dto = { "jsonrpc": "2.0", "method": "mitmproxyReady" }
12+
notification_dto = {"jsonrpc": "2.0", "method": "mitmproxyReady"}
1213
data = json.dumps(notification_dto)
1314
await websocket.send(data)
1415

16+
1517
class MitmproxyAddon:
1618
def running(self) -> None:
1719
# tell the control API that we’re ready to receive traffic
@@ -26,7 +28,9 @@ def request(self, flow: http.HTTPFlow) -> None:
2628

2729
# (b'Connection', b'Upgrade'), (b'Upgrade', b'websocket')
2830
intercept = MitmproxyAddon.is_websocket_upgrade_request(flow.request)
29-
logging.info(f'MitmproxyAddon {"intercepting" if intercept else "not intercepting"} `request` {flow.request.url}, headers {flow.request.headers}')
31+
logging.info(
32+
f'MitmproxyAddon {"intercepting" if intercept else "not intercepting"} `request` {flow.request.url}, headers {flow.request.headers}'
33+
)
3034
# pretty_host takes the "Host" header of the request into account,
3135
# which is useful in transparent mode where we usually only have the IP
3236
# otherwise.
@@ -38,18 +42,26 @@ def request(self, flow: http.HTTPFlow) -> None:
3842

3943
flow.request.host = "localhost"
4044
flow.request.port = 8002
41-
flow.request.scheme = 'http'
45+
flow.request.scheme = "http"
4246
# TODO understand how port fits into this
43-
flow.request.headers['Ably-Test-Host'] = original_host
47+
flow.request.headers["Ably-Test-Host"] = original_host
4448
match original_scheme:
45-
case 'http':
46-
flow.request.headers['Ably-Test-Proto'] = 'ws'
47-
case 'https':
48-
flow.request.headers['Ably-Test-Proto'] = 'wss'
49+
case "http":
50+
flow.request.headers["Ably-Test-Proto"] = "ws"
51+
case "https":
52+
flow.request.headers["Ably-Test-Proto"] = "wss"
4953

5054
@staticmethod
5155
def is_websocket_upgrade_request(request: http.Request) -> bool:
5256
# TODO this request handling is a bit fragile, the special case for `split` is just to handle the fact that Firefox sends 'Connection: keep-alive, Upgrade'
53-
return True if 'Connection' in request.headers and ('Upgrade' in request.headers['Connection'].split(", ")) and 'Upgrade' in request.headers and request.headers['Upgrade'] == 'websocket' else False
57+
return (
58+
True
59+
if "Connection" in request.headers
60+
and ("Upgrade" in request.headers["Connection"].split(", "))
61+
and "Upgrade" in request.headers
62+
and request.headers["Upgrade"] == "websocket"
63+
else False
64+
)
65+
5466

5567
addons = [MitmproxyAddon()]

0 commit comments

Comments
 (0)