diff options
author | Aldo Cortesi <aldo@nullcube.com> | 2016-06-01 09:58:01 +1200 |
---|---|---|
committer | Aldo Cortesi <aldo@nullcube.com> | 2016-06-01 09:58:01 +1200 |
commit | a061e4587772f4a87eb43d84f2ed358f7cc98fbd (patch) | |
tree | 1aba05c9d0d6f654fe946897dcb8b5d9127a3de2 /netlib/http/multipart.py | |
parent | 06703542037d1c84b0dcb60c6d1c500a0d189e93 (diff) | |
parent | a7abf8b731658b4e7ed8705f7a94a6a62f08d51d (diff) | |
download | mitmproxy-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.py | 32 |
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 [] |