aboutsummaryrefslogtreecommitdiffstats
path: root/netlib/http/multipart.py
diff options
context:
space:
mode:
authorAldo Cortesi <aldo@nullcube.com>2016-06-01 09:58:01 +1200
committerAldo Cortesi <aldo@nullcube.com>2016-06-01 09:58:01 +1200
commita061e4587772f4a87eb43d84f2ed358f7cc98fbd (patch)
tree1aba05c9d0d6f654fe946897dcb8b5d9127a3de2 /netlib/http/multipart.py
parent06703542037d1c84b0dcb60c6d1c500a0d189e93 (diff)
parenta7abf8b731658b4e7ed8705f7a94a6a62f08d51d (diff)
downloadmitmproxy-a061e4587772f4a87eb43d84f2ed358f7cc98fbd.tar.gz
mitmproxy-a061e4587772f4a87eb43d84f2ed358f7cc98fbd.tar.bz2
mitmproxy-a061e4587772f4a87eb43d84f2ed358f7cc98fbd.zip
Merge branch 'master' of github.com:cortesi/mitmproxy
Diffstat (limited to 'netlib/http/multipart.py')
-rw-r--r--netlib/http/multipart.py32
1 files changed, 32 insertions, 0 deletions
diff --git a/netlib/http/multipart.py b/netlib/http/multipart.py
new file mode 100644
index 00000000..a135eb86
--- /dev/null
+++ b/netlib/http/multipart.py
@@ -0,0 +1,32 @@
+import re
+
+from . import headers
+
+
+def decode(hdrs, content):
+ """
+ Takes a multipart boundary encoded string and returns list of (key, value) tuples.
+ """
+ v = hdrs.get("content-type")
+ if v:
+ v = headers.parse_content_type(v)
+ if not v:
+ return []
+ try:
+ boundary = v[2]["boundary"].encode("ascii")
+ except (KeyError, UnicodeError):
+ return []
+
+ rx = re.compile(br'\bname="([^"]+)"')
+ r = []
+
+ for i in content.split(b"--" + boundary):
+ parts = i.splitlines()
+ if len(parts) > 1 and parts[0][0:2] != b"--":
+ match = rx.search(parts[1])
+ if match:
+ key = match.group(1)
+ value = b"".join(parts[3 + parts[2:].index(b""):])
+ r.append((key, value))
+ return r
+ return []