aboutsummaryrefslogtreecommitdiffstats
path: root/mitmproxy/net/http/url.py
diff options
context:
space:
mode:
Diffstat (limited to 'mitmproxy/net/http/url.py')
-rw-r--r--mitmproxy/net/http/url.py17
1 files changed, 15 insertions, 2 deletions
diff --git a/mitmproxy/net/http/url.py b/mitmproxy/net/http/url.py
index ff3d5264..86ce9764 100644
--- a/mitmproxy/net/http/url.py
+++ b/mitmproxy/net/http/url.py
@@ -82,11 +82,24 @@ def unparse(scheme, host, port, path=""):
return "%s://%s%s" % (scheme, hostport(scheme, host, port), path)
-def encode(s: Sequence[Tuple[str, str]]) -> str:
+def encode(s: Sequence[Tuple[str, str]], similar_to: str=None) -> str:
"""
Takes a list of (key, value) tuples and returns a urlencoded string.
+ If similar_to is passed, the output is formatted similar to the provided urlencoded string.
"""
- return urllib.parse.urlencode(s, False, errors="surrogateescape")
+
+ remove_trailing_equal = False
+ if similar_to:
+ remove_trailing_equal = any("=" not in param for param in similar_to.split("&"))
+
+ encoded = urllib.parse.urlencode(s, False, errors="surrogateescape")
+
+ if remove_trailing_equal:
+ encoded = encoded.replace("=&", "&")
+ if encoded[-1] == '=':
+ encoded = encoded[:-1]
+
+ return encoded
def decode(s):