def _get_multipart_form(self): is_valid_content_type = "multipart/form-data" in self.headers.get("content-type", "").lower() if is_valid_content_type: try: return multipart.decode(self.headers, self.content) except ValueError: pass return ()
def __call__(self, data: bytes, content_type: Optional[str] = None, **metadata): if content_type is None: return v = multipart.decode(content_type, data) if v: return "Multipart form", self._format(v)
def test_decode(): boundary = 'somefancyboundary' content = ("--{0}\n" "Content-Disposition: form-data; name=\"field1\"\n\n" "value1\n" "--{0}\n" "Content-Disposition: form-data; name=\"field2\"\n\n" "value2\n" "--{0}--".format(boundary).encode()) form = multipart.decode(f'multipart/form-data; boundary={boundary}', content) assert len(form) == 2 assert form[0] == (b"field1", b"value1") assert form[1] == (b"field2", b"value2") boundary = 'boundary茅莽' result = multipart.decode(f'multipart/form-data; boundary={boundary}', content) assert result == [] assert multipart.decode("", content) == []
def test_decode(): boundary = 'somefancyboundary' headers = Headers(content_type='multipart/form-data; boundary=' + boundary) content = ("--{0}\n" "Content-Disposition: form-data; name=\"field1\"\n\n" "value1\n" "--{0}\n" "Content-Disposition: form-data; name=\"field2\"\n\n" "value2\n" "--{0}--".format(boundary).encode()) form = multipart.decode(headers, content) assert len(form) == 2 assert form[0] == (b"field1", b"value1") assert form[1] == (b"field2", b"value2") boundary = 'boundary茅莽' headers = Headers(content_type='multipart/form-data; boundary=' + boundary) result = multipart.decode(headers, content) assert result == [] headers = Headers(content_type='') assert multipart.decode(headers, content) == []
def test_decode(): boundary = 'somefancyboundary' headers = Headers( content_type='multipart/form-data; boundary=' + boundary ) content = ( "--{0}\n" "Content-Disposition: form-data; name=\"field1\"\n\n" "value1\n" "--{0}\n" "Content-Disposition: form-data; name=\"field2\"\n\n" "value2\n" "--{0}--".format(boundary).encode() ) form = multipart.decode(headers, content) assert len(form) == 2 assert form[0] == (b"field1", b"value1") assert form[1] == (b"field2", b"value2")
def __init__(self, stream): self.stream = stream self.group = 0 self.url_dict = url_to_dict(stream.request.url) self.content_type = '' self.resp_type = '' self.content_dict = {} self.resp_content = {} self.raw_content = stream.request.content.decode(encoding="utf-8", errors="replace") self.resp_raw = '' if ((str(stream.request.headers.get("content-type")).find('multipart')) > -1): self.content_type = 'multipart' form = multipart.decode(stream.request.headers, stream.request.content) form = [(tup[0].decode('utf-8', errors="replace"), [tup[1].decode('utf-8', errors="replace")]) for tup in form] self.content_dict = dict(form) elif jsonFun.is_json(self.raw_content): self.content_type = 'json' self.content_dict = jsonFun.json_dict(json.loads(self.raw_content)) else: self.content_type = 'urlencode' self.content_dict = urllib.parse.parse_qs(self.raw_content) #self.content_key = list(self.content.keys()) try: self.resp_raw = bytes.decode(stream.response.content) if ((str(stream.response.headers.get("content-type")).find( 'multipart')) > -1): form = multipart.decode(stream.response.headers, stream.response.content) form = [(tup[0].decode('utf-8', errors="replace"), [tup[1].decode('utf-8', errors="replace")]) for tup in form] self.resp_content = dict(form) self.resp_type = 'multipart' elif jsonFun.is_json(stream.response.content): self.resp_type = 'json' self.resp_content = jsonFun.json_dict( json.loads(self.stream.response.content)) elif str(stream.response.headers.get("content-type")).find( 'urlencode') > -1: self.resp_content = urllib.parse.parse_qs( self.stream.response.content) elif BeautifulSoup(self.resp_raw, "html.parser").find(): self.resp_type = 'html' except: pass #print(self.resp_content) raw_cookie = dict(stream.request.headers.fields).get(b'cookie') cookie = SimpleCookie() if (type(raw_cookie) == type(b'cookie')): cookie.load(bytes.decode(raw_cookie)) self.cookies = {} for key, value in cookie.items(): self.cookies[key] = value.value self.resp_cookies = {} if (hasattr(stream.response, 'headers')): raw_cookie = dict( stream.response.headers.fields).get(b'set-cookie') cookie = SimpleCookie() if (type(raw_cookie) == type(b'cookie')): cookie.load(bytes.decode(raw_cookie)) for key, value in cookie.items(): self.resp_cookies[key] = value.value