-
Notifications
You must be signed in to change notification settings - Fork 4
/
bkmultipartparser.py
38 lines (32 loc) · 1.42 KB
/
bkmultipartparser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from flask_api.parsers import BaseParser
from werkzeug.formparser import MultiPartParser as WerkzeugMultiPartParser
from werkzeug.formparser import default_stream_factory
from werkzeug._compat import BytesIO, text_type
from flask_api import exceptions
class BKMultipartParser(BaseParser):
"""
This multipart/form-data parser fix wrong iOS request
"""
media_type = 'multipart/form-data'
handles_file_uploads = True
handles_form_data = True
def parse(self, data, media_type, **options):
multipart_parser = WerkzeugMultiPartParser(default_stream_factory)
# TODO: dirty code
boundary = media_type.split('boundary=')[-1].strip()
#boundary = media_type.params.get('boundary')
if boundary is None:
msg = 'Multipart message missing boundary in Content-Type header'
raise exceptions.ParseError(msg)
boundary = boundary.encode('ascii')
content_length = options.get('content_length')
assert content_length is not None, 'MultiPartParser.parse() requires `content_length` argument'
if data.rstrip()[-2:] != '--':
data = data.rstrip() + '--\r\n'
try:
return multipart_parser.parse(BytesIO(data), boundary, len(data))
except ValueError as exc:
msg = 'Multipart parse error - %s' % text_type(exc)
raise exceptions.ParseError(msg)