def test_parse_bad_content_type(self):
     parser = FormDataParser()
     assert parser.parse("", "bad-mime-type", 0) == (
         "",
         MultiDict([]),
         MultiDict([]),
     )
Beispiel #2
0
def decode_multipart(stream, size, boundary):
    """Decode multipart data.

    :param stream:
        object that has `read()` to restore data from
    :param size:
        number of bytes to read from `stream`
    :param boundary:
        boundary string that separates multipart portions
    :returns:
        iterator object that returns `(filename, stream)` for each
        multipart portion

    """
    from werkzeug.formparser import FormDataParser

    if hasattr(stream, 'recv'):
        stream = SocketFile(stream)

    parser = FormDataParser(
        silent=False,
        stream_factory=lambda *args: tempfile.NamedTemporaryFile())
    __, __, files = parser.parse(stream, 'multipart/form-data', size,
                                 {'boundary': boundary})

    for __, chunk in files.iteritems(multi=True):
        yield chunk.filename, chunk.stream
        chunk.close()
Beispiel #3
0
def multi_part_parser(body: bytes, headers: dict = None) -> ImmutableMultiDict:
    mimetype, options = get_mimetype_and_options(headers)
    content_length = get_content_length(headers)
    parser = FormDataParser()
    _, form, files = parser.parse(BytesIO(body), mimetype, content_length,
                                  options)
    return ImmutableMultiDict(list(form.items()) + list(files.items()))
Beispiel #4
0
 def parse(self, headers: http.Headers,
           stream: http.RequestStream) -> ImmutableMultiDict:
     mimetype, options = self.get_mimetype_and_options(headers)
     content_length = self.get_content_length(headers)
     parser = FormDataParser()
     stream, form, files = parser.parse(stream, mimetype, content_length,
                                        options)
     return ImmutableMultiDict(list(form.items()) + list(files.items()))
Beispiel #5
0
    def decode(self, bytestring, headers, **options):
        try:
            content_length = max(0, int(headers['content-length']))
        except (KeyError, ValueError, TypeError):
            content_length = None

        try:
            mime_type, mime_options = parse_options_header(headers['content-type'])
        except KeyError:
            mime_type, mime_options = '', {}

        body_file = BytesIO(bytestring)
        parser = FormDataParser()
        stream, form, files = parser.parse(body_file, mime_type, content_length, mime_options)
        return ImmutableMultiDict(chain(form.items(multi=True), files.items(multi=True)))
Beispiel #6
0
def TestOneInput(data):
    parser = FormDataParser()
    parser.parse(io.BytesIO(data), "multipart/form-data", 0)
    parser.parse(io.BytesIO(data), "application/x-url-encoded", 0)
    parser.parse(io.BytesIO(data), "application/x-www-form-urlencoded", 0)

    multiparser = MultiPartParser(len(data))
    try:
        multiparser.parse(io.BytesIO(data), b"", len(data))
    except ValueError as e:
        if "Invalid form-data" in str(e):
            return
        raise e
Beispiel #7
0
async def get_request_data(headers: http.Headers, message: UMIMessage, channels: UMIChannels):
    content_type = headers.get('Content-Type')
    if content_type:
        mimetype, options = parse_options_header(content_type)
    else:
        mimetype, options = None, {}

    if mimetype is None:
        value = None
    elif mimetype == 'application/json':
        body = await get_body(message, channels)
        value = json.loads(body.decode('utf-8'))
    elif mimetype in ('multipart/form-data', 'application/x-www-form-urlencoded'):
        body = await get_body(message, channels)
        stream = io.BytesIO(body)
        content_length = _get_content_length(headers)
        parser = FormDataParser()
        stream, form, files = parser.parse(stream, mimetype, content_length, options)
        value = ImmutableMultiDict(list(form.items()) + list(files.items()))
    else:
        raise exceptions.UnsupportedMediaType()

    return value
Beispiel #8
0
 def test_parse_bad_content_type(self):
     parser = FormDataParser()
     assert parser.parse('', 'bad-mime-type', 0) == \
         ('', MultiDict([]), MultiDict([]))
Beispiel #9
0
 def test_parse_bad_content_type(self):
     parser = FormDataParser()
     assert parser.parse('', 'bad-mime-type', 0) == \
         ('', MultiDict([]), MultiDict([]))