示例#1
0
    def parse(self, stream, content_type, content_length, context=None):
        """Parse the `stream` as a multipart encoded form.

        :param stream: the stream to be parsed.
        :param content_type: the content type of the request payload.
        :param content_length: the content length of the request payload.
        :param context: a dictionary containing extra context data
                        that can be useful for parsing.
        """
        if content_length is None:
            raise BadRequest('MultiPartParser.parse() requires '
                             '`content_length` argument')

        _, options = parse_options_header(content_type)
        boundary = options.get('boundary')
        if boundary is None:
            raise BadRequest('Multipart data missing boundary '
                             'in Content-Type header')
        boundary = boundary.encode('ascii')

        parser = WerkzeugMultiPartParser(default_stream_factory)
        try:
            form, files = parser.parse(stream, boundary, content_length)
            return form.to_dict(), files.to_dict()
        except ValueError:
            raise BadRequest('Multipart data is invalid')
示例#2
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
示例#3
0
    def parse(self, stream, media_type, **options):
        multipart_parser = WerkzeugMultiPartParser(default_stream_factory)

        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'

        try:
            return multipart_parser.parse(stream, boundary, content_length)
        except ValueError as exc:
            msg = 'Multipart parse error - %s' % text_type(exc)
            raise exceptions.ParseError(msg)
示例#4
0
    def parse(self, stream, media_type, **options):
        multipart_parser = WerkzeugMultiPartParser(default_stream_factory)

        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'

        try:
            return multipart_parser.parse(stream, boundary, content_length)
        except ValueError as exc:
            msg = 'Multipart parse error - %s' % text_type(exc)
            raise exceptions.ParseError(msg)
示例#5
0
    def parser(stream, **context):
        content_type = context['content_type']
        content_length = context['content_length']
        multipart_parser = WerkzeugMultiPartParser(default_stream_factory)

        params = parse_header_params(content_type)
        boundary = params.get('boundary')
        if boundary is None:
            raise BadRequest(errors['missing-boundary-param'])
        boundary = boundary.encode('ascii')

        try:
            data, files = multipart_parser.parse(stream, boundary, content_length)
        except ValueError:
            raise BadRequest(errors['malformed'])

        data.update(files)
        return data
示例#6
0
    def parser(stream, **context):
        content_type = context['content_type']
        content_length = context['content_length']
        multipart_parser = WerkzeugMultiPartParser(default_stream_factory)

        params = parse_header_params(content_type)
        boundary = params.get('boundary')
        if boundary is None:
            raise BadRequest(errors['missing-boundary-param'])
        boundary = boundary.encode('ascii')

        try:
            data, files = multipart_parser.parse(stream, boundary,
                                                 content_length)
        except ValueError:
            raise BadRequest(errors['malformed'])

        data.update(files)
        return data
示例#7
0
    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)
示例#8
0
    def process_request(self, req, resp, **kwargs):

        if 'multipart/form-data' not in (req.content_type or ''):
            return

        mimetype, options = parse_options_header(req.content_type)
        size = req.content_length
        #
        boundary = options.get('boundary').encode()
        if not boundary:
            raise ValueError('No boundary')
        #
        parser = MultiPartParser(stream_factory=default_stream_factory)
        form, files = parser.parse(req.stream, boundary, size)
        if 'form' in req._params.keys():
            raise ValueError('form exists')

        if 'files' in req._params.keys():
            raise ValueError('form exists')

        req._params['form'] = form
        req._params['files'] = files