codekingpro/portable-devtools
115k
1import html2import textwrap3from dataclasses import dataclass4 5from mitmproxy import http6from mitmproxy.connection import Connection7from mitmproxy.proxy import commands8from mitmproxy.proxy import events9from mitmproxy.proxy import layer10from mitmproxy.proxy.context import Context11 12StreamId = int13 14 15@dataclass16class HttpEvent(events.Event):17 # we need stream ids on every event to avoid race conditions18 stream_id: StreamId19 20 21class HttpConnection(layer.Layer):22 conn: Connection23 24 def __init__(self, context: Context, conn: Connection):25 super().__init__(context)26 self.conn = conn27 28 29class HttpCommand(commands.Command):30 pass31 32 33class ReceiveHttp(HttpCommand):34 event: HttpEvent35 36 def __init__(self, event: HttpEvent):37 self.event = event38 39 def __repr__(self) -> str:40 return f"Receive({self.event})"41 42 43def format_error(status_code: int, message: str) -> bytes:44 reason = http.status_codes.RESPONSES.get(status_code, "Unknown")45 return (46 textwrap.dedent(47 f"""48 <html>49 <head>50 <title>{status_code} {reason}</title>51 </head>52 <body>53 <h1>{status_code} {reason}</h1>54 <p>{html.escape(message)}</p>55 </body>56 </html>57 """58 )59 .strip()60 .encode("utf8", "replace")61 )62 