add websocket messages in HAR file (#5663)

* add websocket messages in HAR file

* use websocket_end instead of websocket_message

* remove requestID_to_entry

* remove request

* add base64-encoded data if it is binary

* har dumper: nits

Co-authored-by: Maximilian Hils <git@maximilianhils.com>
This commit is contained in:
inceabdullah 2022-10-21 03:44:01 +03:00 committed by GitHub
parent c0f25e0470
commit be2b9a962e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -58,10 +58,7 @@ def configure(updated):
# An empty value works fine.
def response(flow: mitmproxy.http.HTTPFlow):
"""
Called when a server response has been received.
"""
def flow_entry(flow: mitmproxy.http.HTTPFlow) -> dict:
# -1 indicates that these values do not apply to current request
ssl_time = -1
@ -163,6 +160,38 @@ def response(flow: mitmproxy.http.HTTPFlow):
HAR["log"]["entries"].append(entry)
return entry
def response(flow: mitmproxy.http.HTTPFlow):
"""
Called when a server response has been received.
"""
if flow.websocket is None:
flow_entry(flow)
def websocket_end(flow: mitmproxy.http.HTTPFlow):
entry = flow_entry(flow)
websocket_messages = []
for message in flow.websocket.messages:
if message.is_text:
data = message.text
else:
data = base64.b64encode(message.content).decode()
websocket_message = {
'type': 'send' if message.from_client else 'receive',
'time': message.timestamp,
'opcode': message.type.value,
'data': data
}
websocket_messages.append(websocket_message)
entry['_resourceType'] = 'websocket'
entry['_webSocketMessages'] = websocket_messages
def done():
"""