Skip to content

Commit

Permalink
fixing some mypy stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
lawrence-forooghian committed Jul 24, 2024
1 parent 2aa6438 commit 0740c36
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions mitmproxy_addon_2.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import mitmproxy
from mitmproxy import http
import logging
import asyncio
import websockets
import json

async def send_ready_notification():
async def send_ready_notification() -> None:
uri = "ws://localhost:8001"
logging.info(f'sending mitmproxyReady JSON-RPC notification to {uri}')
async with websockets.connect(uri) as websocket:
Expand All @@ -13,12 +13,12 @@ async def send_ready_notification():
await websocket.send(data)

class MitmproxyAddon2:
def running(self):
def running(self) -> None:
# tell the control API that we’re ready to receive traffic
asyncio.get_running_loop().create_task(send_ready_notification())

# Copied from https://docs.mitmproxy.org/stable/addons-examples/#http-redirect-requests
def request(self, flow: mitmproxy.http.HTTPFlow) -> None:
def request(self, flow: http.HTTPFlow) -> None:
# To make sure that when running in local redirect mode (and hence intercepting all traffic from the test process) we don’t mess with traffic from the test process to the control API
# TODO see extended comments re this in test-node.yml and why it hasn’t yet been an issue in practice on macOS
if not flow.request.port in [80, 443]:
Expand Down Expand Up @@ -47,7 +47,8 @@ def request(self, flow: mitmproxy.http.HTTPFlow) -> None:
case 'https':
flow.request.headers['Ably-Test-Proto'] = 'wss'

def is_websocket_upgrade_request(request: mitmproxy.http.Request):
@staticmethod
def is_websocket_upgrade_request(request: http.Request) -> bool:
# 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'
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

Expand Down

0 comments on commit 0740c36

Please sign in to comment.