mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-11-27 02:24:18 +00:00
maplocal addon: add mimetype guessing
This commit is contained in:
parent
1b6b3cd96c
commit
7022a54737
@ -1,3 +1,4 @@
|
|||||||
|
import mimetypes
|
||||||
import re
|
import re
|
||||||
import typing
|
import typing
|
||||||
import urllib
|
import urllib
|
||||||
@ -77,6 +78,14 @@ class MapLocal:
|
|||||||
|
|
||||||
return candidates
|
return candidates
|
||||||
|
|
||||||
|
def get_mime_type(self, file_path):
|
||||||
|
mimetype = (
|
||||||
|
mimetypes.guess_type(file_path)[0]
|
||||||
|
or "text/plain"
|
||||||
|
)
|
||||||
|
return mimetype
|
||||||
|
|
||||||
|
|
||||||
def request(self, flow: http.HTTPFlow) -> None:
|
def request(self, flow: http.HTTPFlow) -> None:
|
||||||
if flow.reply and flow.reply.has_message:
|
if flow.reply and flow.reply.has_message:
|
||||||
return
|
return
|
||||||
@ -84,8 +93,10 @@ class MapLocal:
|
|||||||
req = flow.request
|
req = flow.request
|
||||||
url = req.pretty_url
|
url = req.pretty_url
|
||||||
base_path = Path(spec.replacement_str)
|
base_path = Path(spec.replacement_str)
|
||||||
|
|
||||||
if spec.matches(flow) and re.search(spec.subject, url.encode("utf8", "surrogateescape")):
|
if spec.matches(flow) and re.search(spec.subject, url.encode("utf8", "surrogateescape")):
|
||||||
file_candidates = self.file_candidates(url, spec)
|
file_candidates = self.file_candidates(url, spec)
|
||||||
|
|
||||||
for file_candidate in file_candidates:
|
for file_candidate in file_candidates:
|
||||||
file_candidate = Path(file_candidate)
|
file_candidate = Path(file_candidate)
|
||||||
if self.sanitize_candidate_path(file_candidate, base_path):
|
if self.sanitize_candidate_path(file_candidate, base_path):
|
||||||
@ -97,8 +108,7 @@ class MapLocal:
|
|||||||
return
|
return
|
||||||
|
|
||||||
flow.response = http.HTTPResponse.make(
|
flow.response = http.HTTPResponse.make(
|
||||||
200, # (optional) status code
|
200,
|
||||||
replacement, # (optional) content
|
replacement,
|
||||||
# todo: guess mime type
|
{"Content-Type": self.get_mime_type(str(file_candidate))}
|
||||||
{"Content-Type": "image/jpeg"} # (optional) headers
|
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user