aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--mitmproxy/addons/export.py15
-rw-r--r--test/mitmproxy/addons/test_export.py24
2 files changed, 24 insertions, 15 deletions
diff --git a/mitmproxy/addons/export.py b/mitmproxy/addons/export.py
index 90e95d3e..0c10f352 100644
--- a/mitmproxy/addons/export.py
+++ b/mitmproxy/addons/export.py
@@ -11,6 +11,15 @@ import mitmproxy.types
import pyperclip
+def cleanup_for_commands(request):
+ if ('content-length' in request.headers.keys() and
+ request.headers['content-length'] == '0' and
+ request.method == 'GET'):
+ request.headers.pop('content-length')
+ request.headers.pop(':authority', None)
+ return request
+
+
def raise_if_missing_request(f: flow.Flow) -> None:
if not hasattr(f, "request"):
raise exceptions.CommandError("Can't export flow with no request.")
@@ -21,10 +30,15 @@ def curl_command(f: flow.Flow) -> str:
data = "curl "
request = f.request.copy() # type: ignore
request.decode(strict=False)
+ request = cleanup_for_commands(request)
for k, v in request.headers.items(multi=True):
data += "-H '%s:%s' " % (k, v)
if request.method != "GET":
data += "-X %s " % request.method
+ for header in request.headers.keys():
+ if ('accept-encoding' == header.lower() and
+ any(comp in request.headers[header] for comp in ['gzip', 'deflate'])):
+ data += "--compressed "
data += "'%s'" % request.url
if request.content:
data += " --data-binary '%s'" % strutils.bytes_to_escaped_str(
@@ -39,6 +53,7 @@ def httpie_command(f: flow.Flow) -> str:
request = f.request.copy() # type: ignore
data = "http %s " % request.method
request.decode(strict=False)
+ request = cleanup_for_commands(request)
data += "%s" % request.url
for k, v in request.headers.items(multi=True):
data += " '%s:%s'" % (k, v)
diff --git a/test/mitmproxy/addons/test_export.py b/test/mitmproxy/addons/test_export.py
index f4bb0f64..c86e0c7d 100644
--- a/test/mitmproxy/addons/test_export.py
+++ b/test/mitmproxy/addons/test_export.py
@@ -14,29 +14,23 @@ from unittest import mock
@pytest.fixture
def get_request():
return tflow.tflow(
- req=tutils.treq(
- method=b'GET',
- content=b'',
- path=b"/path?a=foo&a=bar&b=baz"
- )
- )
+ req=tutils.treq(method=b'GET', content=b'', path=b"/path?a=foo&a=bar&b=baz"))
@pytest.fixture
def post_request():
return tflow.tflow(
- req=tutils.treq(
- method=b'POST',
- headers=(),
- content=bytes(range(256))
- )
- )
+ req=tutils.treq(method=b'POST', headers=(), content=bytes(range(256))))
@pytest.fixture
def patch_request():
return tflow.tflow(
- req=tutils.treq(method=b'PATCH', path=b"/path?query=param")
+ req=tutils.treq(
+ method=b'PATCH',
+ content=b'content',
+ path=b"/path?query=param"
+ )
)
@@ -47,7 +41,7 @@ def tcp_flow():
class TestExportCurlCommand:
def test_get(self, get_request):
- result = """curl -H 'header:qvalue' -H 'content-length:0' '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
def test_post(self, post_request):
@@ -67,7 +61,7 @@ class TestExportCurlCommand:
class TestExportHttpieCommand:
def test_get(self, get_request):
- result = """http GET http://address:22/path?a=foo&a=bar&b=baz 'header:qvalue' 'content-length:0'"""
+ result = """http GET http://address:22/path?a=foo&a=bar&b=baz 'header:qvalue'"""
assert export.httpie_command(get_request) == result
def test_post(self, post_request):