aboutsummaryrefslogtreecommitdiffstats
path: root/libmproxy/utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'libmproxy/utils.py')
-rw-r--r--libmproxy/utils.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/libmproxy/utils.py b/libmproxy/utils.py
index 51f2dc26..b84c589a 100644
--- a/libmproxy/utils.py
+++ b/libmproxy/utils.py
@@ -69,6 +69,33 @@ def urlencode(s):
return urllib.urlencode(s, False)
+def multipartdecode(hdrs, content):
+ """
+ Takes a multipart boundary encoded string and returns list of (key, value) tuples.
+ """
+ v = hdrs.get_first("content-type")
+ if v:
+ v = parse_content_type(v)
+ if not v:
+ return []
+ boundary = v[2].get("boundary")
+ if not boundary:
+ return []
+
+ rx = re.compile(r'\bname="([^"]+)"')
+ r = []
+
+ for i in content.split("--" + boundary):
+ parts = i.splitlines()
+ if len(parts) > 1 and parts[0][0:2] != "--":
+ match = rx.search(parts[1])
+ if match:
+ key = match.group(1)
+ value = "".join(parts[3+parts[2:].index(""):])
+ r.append((key, value))
+ return r
+ return []
+
def pretty_size(size):
suffixes = [
("B", 2**10),