MCPcopy Index your code
hub / github.com/mitmproxy/mitmproxy / _hash

Method _hash

mitmproxy/addons/serverplayback.py:177–223  ·  view source on GitHub ↗

Calculates a loose hash of the flow request.

(self, flow: http.HTTPFlow)

Source from the content-addressed store, hash-verified

175 return sum(len(i) for i in self.flowmap.values())
176
177 def _hash(self, flow: http.HTTPFlow) -> Hashable:
178 """
179 Calculates a loose hash of the flow request.
180 """
181 r = flow.request
182 _, _, path, _, query, _ = urllib.parse.urlparse(r.url)
183 queriesArray = urllib.parse.parse_qsl(query, keep_blank_values=True)
184
185 key: list[Any] = [str(r.scheme), str(r.method), str(path)]
186 if not ctx.options.server_replay_ignore_content:
187 if ctx.options.server_replay_ignore_payload_params and r.multipart_form:
188 key.extend(
189 (k, v)
190 for k, v in r.multipart_form.items(multi=True)
191 if k.decode(errors="replace")
192 not in ctx.options.server_replay_ignore_payload_params
193 )
194 elif ctx.options.server_replay_ignore_payload_params and r.urlencoded_form:
195 key.extend(
196 (k, v)
197 for k, v in r.urlencoded_form.items(multi=True)
198 if k not in ctx.options.server_replay_ignore_payload_params
199 )
200 else:
201 key.append(str(r.raw_content))
202
203 if not ctx.options.server_replay_ignore_host:
204 key.append(r.pretty_host)
205 if not ctx.options.server_replay_ignore_port:
206 key.append(r.port)
207
208 filtered = []
209 ignore_params = ctx.options.server_replay_ignore_params or []
210 for p in queriesArray:
211 if p[0] not in ignore_params:
212 filtered.append(p)
213 for p in filtered:
214 key.append(p[0])
215 key.append(p[1])
216
217 if ctx.options.server_replay_use_headers:
218 headers = []
219 for i in ctx.options.server_replay_use_headers:
220 v = r.headers.get(i)
221 headers.append((i, v))
222 key.append(headers)
223 return hashlib.sha256(repr(key).encode("utf8", "surrogateescape")).digest()
224
225 def next_flow(self, flow: http.HTTPFlow) -> http.HTTPFlow | None:
226 """

Callers 11

test_ignore_hostFunction · 0.95
test_ignore_contentFunction · 0.95
test_hashFunction · 0.95
test_headersFunction · 0.95
test_ignore_paramsFunction · 0.95
thashFunction · 0.95
add_flowsMethod · 0.95
next_flowMethod · 0.95

Calls 6

extendMethod · 0.45
itemsMethod · 0.45
decodeMethod · 0.45
appendMethod · 0.45
getMethod · 0.45
encodeMethod · 0.45

Tested by 9

test_ignore_hostFunction · 0.76
test_ignore_contentFunction · 0.76
test_hashFunction · 0.76
test_headersFunction · 0.76
test_ignore_paramsFunction · 0.76
thashFunction · 0.76