mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-11-26 10:16:27 +00:00
improve curl/httpie export
This commit is contained in:
parent
0873566ff0
commit
01ddda75e8
@ -1,47 +1,60 @@
|
|||||||
import typing
|
|
||||||
import shlex
|
import shlex
|
||||||
|
import typing
|
||||||
from mitmproxy import ctx
|
|
||||||
from mitmproxy import command
|
|
||||||
from mitmproxy import flow
|
|
||||||
from mitmproxy import exceptions
|
|
||||||
from mitmproxy.utils import strutils
|
|
||||||
from mitmproxy.net.http.http1 import assemble
|
|
||||||
import mitmproxy.types
|
|
||||||
|
|
||||||
import pyperclip
|
import pyperclip
|
||||||
|
|
||||||
|
import mitmproxy.types
|
||||||
|
from mitmproxy import command
|
||||||
|
from mitmproxy import ctx, http
|
||||||
|
from mitmproxy import exceptions
|
||||||
|
from mitmproxy import flow
|
||||||
|
from mitmproxy.net.http.http1 import assemble
|
||||||
|
from mitmproxy.utils import strutils
|
||||||
|
|
||||||
def cleanup_request(f: flow.Flow):
|
|
||||||
|
def cleanup_request(f: flow.Flow) -> http.HTTPRequest:
|
||||||
if not hasattr(f, "request"):
|
if not hasattr(f, "request"):
|
||||||
raise exceptions.CommandError("Can't export flow with no request.")
|
raise exceptions.CommandError("Can't export flow with no request.")
|
||||||
request = f.request.copy() # type: ignore
|
assert isinstance(f, http.HTTPFlow)
|
||||||
|
request = f.request.copy()
|
||||||
request.decode(strict=False)
|
request.decode(strict=False)
|
||||||
# a bit of clean-up
|
# a bit of clean-up - these headers should be automatically set by curl/httpie
|
||||||
if request.method == 'GET' and request.headers.get("content-length", None) == "0":
|
request.headers.pop('content-length')
|
||||||
request.headers.pop('content-length')
|
if request.headers.get("host", "") == request.host:
|
||||||
request.headers.pop(':authority', None)
|
request.headers.pop("host")
|
||||||
|
if request.headers.get(":authority", "") == request.host:
|
||||||
|
request.headers.pop(":authority")
|
||||||
return request
|
return request
|
||||||
|
|
||||||
|
|
||||||
|
def request_content_for_console(request: http.HTTPRequest) -> str:
|
||||||
|
try:
|
||||||
|
text = request.get_text(strict=True)
|
||||||
|
except ValueError:
|
||||||
|
# shlex.quote doesn't support a bytes object
|
||||||
|
# see https://github.com/python/cpython/pull/10871
|
||||||
|
raise exceptions.CommandError("Request content must be valid unicode")
|
||||||
|
escape_control_chars = {chr(i): f"\\x{i:02x}" for i in range(32)}
|
||||||
|
return "".join(
|
||||||
|
escape_control_chars.get(x, x)
|
||||||
|
for x in text
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def curl_command(f: flow.Flow) -> str:
|
def curl_command(f: flow.Flow) -> str:
|
||||||
request = cleanup_request(f)
|
request = cleanup_request(f)
|
||||||
args = ["curl"]
|
args = ["curl"]
|
||||||
for k, v in request.headers.items(multi=True):
|
for k, v in request.headers.items(multi=True):
|
||||||
args += ["--compressed "] if k == 'accept-encoding' else []
|
if k.lower() == "accept-encoding":
|
||||||
args += ["-H", "%s:%s" % (k, v)]
|
args.append("--compressed")
|
||||||
|
else:
|
||||||
|
args += ["-H", f"{k}: {v}"]
|
||||||
|
|
||||||
if request.method != "GET":
|
if request.method != "GET":
|
||||||
args += ["-X", request.method]
|
args += ["-X", request.method]
|
||||||
args.append(request.url)
|
args.append(request.url)
|
||||||
if request.content:
|
if request.content:
|
||||||
try:
|
args += ["-d", request_content_for_console(request)]
|
||||||
content = strutils.always_str(request.content)
|
|
||||||
except UnicodeDecodeError:
|
|
||||||
# shlex.quote doesn't support a bytes object
|
|
||||||
# see https://github.com/python/cpython/pull/10871
|
|
||||||
raise exceptions.CommandError("Request content must be valid unicode")
|
|
||||||
args += ["--data-binary", content]
|
|
||||||
return ' '.join(shlex.quote(arg) for arg in args)
|
return ' '.join(shlex.quote(arg) for arg in args)
|
||||||
|
|
||||||
|
|
||||||
@ -49,16 +62,10 @@ def httpie_command(f: flow.Flow) -> str:
|
|||||||
request = cleanup_request(f)
|
request = cleanup_request(f)
|
||||||
args = ["http", request.method, request.url]
|
args = ["http", request.method, request.url]
|
||||||
for k, v in request.headers.items(multi=True):
|
for k, v in request.headers.items(multi=True):
|
||||||
args.append("%s:%s" % (k, v))
|
args.append(f"{k}: {v}")
|
||||||
cmd = ' '.join(shlex.quote(arg) for arg in args)
|
cmd = ' '.join(shlex.quote(arg) for arg in args)
|
||||||
if request.content:
|
if request.content:
|
||||||
try:
|
cmd += " <<< " + shlex.quote(request_content_for_console(request))
|
||||||
content = strutils.always_str(request.content)
|
|
||||||
except UnicodeDecodeError:
|
|
||||||
# shlex.quote doesn't support a bytes object
|
|
||||||
# see https://github.com/python/cpython/pull/10871
|
|
||||||
raise exceptions.CommandError("Request content must be valid unicode")
|
|
||||||
cmd += " <<< " + shlex.quote(content)
|
|
||||||
return cmd
|
return cmd
|
||||||
|
|
||||||
|
|
||||||
@ -67,9 +74,9 @@ def raw(f: flow.Flow) -> bytes:
|
|||||||
|
|
||||||
|
|
||||||
formats = dict(
|
formats = dict(
|
||||||
curl = curl_command,
|
curl=curl_command,
|
||||||
httpie = httpie_command,
|
httpie=httpie_command,
|
||||||
raw = raw,
|
raw=raw,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -42,22 +42,23 @@ def tcp_flow():
|
|||||||
|
|
||||||
class TestExportCurlCommand:
|
class TestExportCurlCommand:
|
||||||
def test_get(self, get_request):
|
def test_get(self, get_request):
|
||||||
result = """curl -H header:qvalue 'http://address:22/path?a=foo&a=bar&b=baz'"""
|
result = """curl -H 'header: qvalue' 'http://address:22/path?a=foo&a=bar&b=baz'"""
|
||||||
assert export.curl_command(get_request) == result
|
assert export.curl_command(get_request) == result
|
||||||
|
|
||||||
def test_post(self, post_request):
|
def test_post(self, post_request):
|
||||||
post_request.request.content = b'nobinarysupport'
|
post_request.request.content = b'nobinarysupport'
|
||||||
result = "curl -H content-length:15 -X POST http://address:22/path --data-binary nobinarysupport"
|
result = "curl -X POST http://address:22/path -d nobinarysupport"
|
||||||
assert export.curl_command(post_request) == result
|
assert export.curl_command(post_request) == result
|
||||||
|
|
||||||
def test_fails_with_binary_data(self, post_request):
|
def test_fails_with_binary_data(self, post_request):
|
||||||
# shlex.quote doesn't support a bytes object
|
# shlex.quote doesn't support a bytes object
|
||||||
# see https://github.com/python/cpython/pull/10871
|
# see https://github.com/python/cpython/pull/10871
|
||||||
|
post_request.request.headers["Content-Type"] = "application/json; charset=utf-8"
|
||||||
with pytest.raises(exceptions.CommandError):
|
with pytest.raises(exceptions.CommandError):
|
||||||
export.curl_command(post_request)
|
export.curl_command(post_request)
|
||||||
|
|
||||||
def test_patch(self, patch_request):
|
def test_patch(self, patch_request):
|
||||||
result = """curl -H header:qvalue -H content-length:7 -X PATCH 'http://address:22/path?query=param' --data-binary content"""
|
result = """curl -H 'header: qvalue' -X PATCH 'http://address:22/path?query=param' -d content"""
|
||||||
assert export.curl_command(patch_request) == result
|
assert export.curl_command(patch_request) == result
|
||||||
|
|
||||||
def test_tcp(self, tcp_flow):
|
def test_tcp(self, tcp_flow):
|
||||||
@ -73,28 +74,29 @@ class TestExportCurlCommand:
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
command = export.curl_command(request)
|
command = export.curl_command(request)
|
||||||
assert shlex.split(command)[-2] == '--data-binary'
|
assert shlex.split(command)[-2] == '-d'
|
||||||
assert shlex.split(command)[-1] == "'&#"
|
assert shlex.split(command)[-1] == "'&#"
|
||||||
|
|
||||||
|
|
||||||
class TestExportHttpieCommand:
|
class TestExportHttpieCommand:
|
||||||
def test_get(self, get_request):
|
def test_get(self, get_request):
|
||||||
result = """http GET 'http://address:22/path?a=foo&a=bar&b=baz' header:qvalue"""
|
result = """http GET 'http://address:22/path?a=foo&a=bar&b=baz' 'header: qvalue'"""
|
||||||
assert export.httpie_command(get_request) == result
|
assert export.httpie_command(get_request) == result
|
||||||
|
|
||||||
def test_post(self, post_request):
|
def test_post(self, post_request):
|
||||||
post_request.request.content = b'nobinarysupport'
|
post_request.request.content = b'nobinarysupport'
|
||||||
result = "http POST http://address:22/path content-length:15 <<< nobinarysupport"
|
result = "http POST http://address:22/path <<< nobinarysupport"
|
||||||
assert export.httpie_command(post_request) == result
|
assert export.httpie_command(post_request) == result
|
||||||
|
|
||||||
def test_fails_with_binary_data(self, post_request):
|
def test_fails_with_binary_data(self, post_request):
|
||||||
# shlex.quote doesn't support a bytes object
|
# shlex.quote doesn't support a bytes object
|
||||||
# see https://github.com/python/cpython/pull/10871
|
# see https://github.com/python/cpython/pull/10871
|
||||||
|
post_request.request.headers["Content-Type"] = "application/json; charset=utf-8"
|
||||||
with pytest.raises(exceptions.CommandError):
|
with pytest.raises(exceptions.CommandError):
|
||||||
export.httpie_command(post_request)
|
export.httpie_command(post_request)
|
||||||
|
|
||||||
def test_patch(self, patch_request):
|
def test_patch(self, patch_request):
|
||||||
result = """http PATCH 'http://address:22/path?query=param' header:qvalue content-length:7 <<< content"""
|
result = """http PATCH 'http://address:22/path?query=param' 'header: qvalue' <<< content"""
|
||||||
assert export.httpie_command(patch_request) == result
|
assert export.httpie_command(patch_request) == result
|
||||||
|
|
||||||
def test_tcp(self, tcp_flow):
|
def test_tcp(self, tcp_flow):
|
||||||
|
Loading…
Reference in New Issue
Block a user