Beispiel #1
0
    def test_parse_mimetype(self):
        self.assertEqual(helpers.parse_mimetype(''), ('', '', '', {}))

        self.assertEqual(helpers.parse_mimetype('*'), ('*', '*', '', {}))

        self.assertEqual(helpers.parse_mimetype('application/json'),
                         ('application', 'json', '', {}))

        self.assertEqual(
            helpers.parse_mimetype('application/json;  charset=utf-8'),
            ('application', 'json', '', {
                'charset': 'utf-8'
            }))

        self.assertEqual(
            helpers.parse_mimetype('''application/json; charset=utf-8;'''),
            ('application', 'json', '', {
                'charset': 'utf-8'
            }))

        self.assertEqual(
            helpers.parse_mimetype('ApPlIcAtIoN/JSON;ChaRseT="UTF-8"'),
            ('application', 'json', '', {
                'charset': 'UTF-8'
            }))

        self.assertEqual(helpers.parse_mimetype('application/rss+xml'),
                         ('application', 'rss', 'xml', {}))

        self.assertEqual(helpers.parse_mimetype('text/plain;base64'),
                         ('text', 'plain', '', {
                             'base64': ''
                         }))
Beispiel #2
0
    def test_parse_mimetype(self):
        self.assertEqual(
            helpers.parse_mimetype(''), ('', '', '', {}))

        self.assertEqual(
            helpers.parse_mimetype('*'), ('*', '*', '', {}))

        self.assertEqual(
            helpers.parse_mimetype('application/json'),
            ('application', 'json', '', {}))

        self.assertEqual(
            helpers.parse_mimetype('application/json;  charset=utf-8'),
            ('application', 'json', '', {'charset': 'utf-8'}))

        self.assertEqual(
            helpers.parse_mimetype('''application/json; charset=utf-8;'''),
            ('application', 'json', '', {'charset': 'utf-8'}))

        self.assertEqual(
            helpers.parse_mimetype('ApPlIcAtIoN/JSON;ChaRseT="UTF-8"'),
            ('application', 'json', '', {'charset': 'UTF-8'}))

        self.assertEqual(
            helpers.parse_mimetype('application/rss+xml'),
            ('application', 'rss', 'xml', {}))

        self.assertEqual(
            helpers.parse_mimetype('text/plain;base64'),
            ('text', 'plain', '', {'base64': ''}))
Beispiel #3
0
    def test_parse_mimetype(self):
        self.assertEqual(helpers.parse_mimetype(""), ("", "", "", {}))

        self.assertEqual(helpers.parse_mimetype("*"), ("*", "*", "", {}))

        self.assertEqual(helpers.parse_mimetype("application/json"), ("application", "json", "", {}))

        self.assertEqual(
            helpers.parse_mimetype("application/json;  charset=utf-8"),
            ("application", "json", "", {"charset": "utf-8"}),
        )

        self.assertEqual(
            helpers.parse_mimetype("""application/json; charset=utf-8;"""),
            ("application", "json", "", {"charset": "utf-8"}),
        )

        self.assertEqual(
            helpers.parse_mimetype('ApPlIcAtIoN/JSON;ChaRseT="UTF-8"'),
            ("application", "json", "", {"charset": "UTF-8"}),
        )

        self.assertEqual(helpers.parse_mimetype("application/rss+xml"), ("application", "rss", "xml", {}))

        self.assertEqual(helpers.parse_mimetype("text/plain;base64"), ("text", "plain", "", {"base64": ""}))
Beispiel #4
0
def test_parse_mimetype_6():
    assert helpers.parse_mimetype('ApPlIcAtIoN/JSON;ChaRseT="UTF-8"') == (
        "application",
        "json",
        "",
        {"charset": "UTF-8"},
    )
Beispiel #5
0
    def _try_client_response(self, headers, parsed):
        """
        Sends a response back to the client based on Accept header
        Defaults to JSON
        """
        media_msg = 'The requested media type is unsupported'
        mime_type = None
        sub_type = None

        try:
            accept_types = headers['Accept']
            mime_type, sub_type, _, _ = parse_mimetype(accept_types)
        except KeyError:
            pass

        if mime_type == 'application' and sub_type == 'octet-stream':
            return web.Response(
                content_type='application/octet-stream',
                body=parsed.SerializeToString()
            )

        if ((mime_type in ['application', '*'] or mime_type is None)
                and (sub_type in ['json', '*'] or sub_type is None)):
            return web.Response(
                content_type='application/json',
                text=MessageToJson(parsed)
            )

        raise web.HTTPUnsupportedMediaType(reason=media_msg)
Beispiel #6
0
async def post(request: web.Request) -> web.Response:
    # language=rst
    """POST handler for ``/files``"""
    mimetype = helpers.parse_mimetype(request.headers[aiohttp.hdrs.CONTENT_TYPE])
    if mimetype.type != 'multipart':
        raise web.HTTPBadRequest(
            text='multipart/* content type expected'
        )
    reader = await request.multipart()
    field = await reader.next()
    if field.name != 'distribution':
        raise web.HTTPBadRequest(
            text='file field must be named “distribution”'
        )
    uuid = uuid4().hex
    content_type = field.headers.get(
        aiohttp.hdrs.CONTENT_TYPE,
        'application/octet-stream'
    )
    await _put_file_to_object_store(
        uuid, content_type,
        field, filename=field.filename
    )
    return web.Response(
        status=201,
        headers={
            'Location': _BASE_URL + uuid
        }
    )
Beispiel #7
0
    def _try_client_response(headers, parsed):
        """
        Used by pre-spec /state and /batches routes
        Should be removed when routes are updated to spec
        """
        media_msg = 'The requested media type is unsupported'
        mime_type = None
        sub_type = None

        try:
            accept_types = headers['Accept']
            mime_type, sub_type, _, _ = parse_mimetype(accept_types)
        except KeyError:
            pass

        if mime_type == 'application' and sub_type == 'octet-stream':
            return web.Response(
                content_type='application/octet-stream',
                body=parsed.SerializeToString()
            )

        if ((mime_type in ['application', '*'] or mime_type is None)
                and (sub_type in ['json', '*'] or sub_type is None)):
            return web.Response(
                content_type='application/json',
                text=MessageToJson(parsed)
            )

        raise web.HTTPUnsupportedMediaType(reason=media_msg)
Beispiel #8
0
def test_parse_mimetype_5():
    assert helpers.parse_mimetype("""application/json; charset=utf-8;""") == (
        "application",
        "json",
        "",
        {"charset": "utf-8"},
    )
Beispiel #9
0
 def _check_valid_mimetype(self, request):
     typ, subtype, suffix, params = parse_mimetype(
         request.headers.get('Content-Type'))
     valid_type = (typ, subtype, suffix) == ('application', 'json', '')
     valid_profile = (params.get('profile') == self.profile
                      or self.profile is None)
     valid_version = (params.get('version') == self.version
                      or self.version is None)
     if not all((valid_type, valid_profile, valid_version)):
         raise APIError('BadRequest', message='Invalid request MIME type')
Beispiel #10
0
    def __init__(self, resp, *, loop=None, buffer_size=0):
        self._active = True
        self._exc = None
        self._queue = asyncio.Queue(maxsize=buffer_size or self.buffer_size, loop=loop)
        self._resp = resp

        ctype = resp.headers.get(CONTENT_TYPE, "").lower()
        *_, params = parse_mimetype(ctype)
        self._encoding = params.get("charset", "utf-8")  # pylint: disable=E1101

        asyncio.Task(self._loop(), loop=loop)
Beispiel #11
0
    def __init__(self, resp, *, loop=None, buffer_size=buffer_size):
        self._active = True
        self._exc = None
        self._queue = asyncio.Queue(maxsize=buffer_size or self.buffer_size,
                                    loop=loop)
        self._resp = resp

        ctype = resp.headers.get('CONTENT-TYPE', '').lower()
        *_, params = parse_mimetype(ctype)
        self._encoding = params.get('charset', 'utf-8')

        asyncio.Task(self._loop(), loop=loop)
Beispiel #12
0
    def __init__(self, resp, *, loop=None, buffer_size=0):
        self._active = True
        self._exc = None
        self._queue = asyncio.Queue(maxsize=buffer_size or self.buffer_size,
                                    loop=loop)
        self._resp = resp

        ctype = resp.headers.get(CONTENT_TYPE, '').lower()
        mimetype = parse_mimetype(ctype)
        self._encoding = mimetype.parameters.get('charset', 'utf-8')  # pylint: disable=E1101

        asyncio.Task(self._loop(), loop=loop)
Beispiel #13
0
    def dispatch(self, headers):
        """Dispatches the response by the `Content-Type` header, returning
        suitable reader instance.

        :param dict headers: Response headers
        """
        ctype = headers.get('CONTENT-TYPE', '')
        mtype, stype, *_ = parse_mimetype(ctype)
        for key in ((mtype, stype), (mtype, None), None):
            handler = self.dispatch_map.get(key)
            if handler is not None:
                return handler(self, headers)
        raise AttributeError('no handler available for content type %r', ctype)
Beispiel #14
0
def parse_accept(accept):
    parts = accept.split(',')
    types = []
    for item in parts:
        if not item:
            continue
        mimetype = parse_mimetype(item)
        mimestring = '+'.join(filter(None,
                                     ['/'.join(filter(None,
                                                      [mimetype.type,
                                                       mimetype.subtype])),
                                      mimetype.suffix]))
        types.append(mimestring)
    return types
Beispiel #15
0
async def _iter_content_generator(response, decode_unicode):
    encoding = None

    if decode_unicode:
        ctype = response.headers.get('content-type', '').lower()
        mimetype = parse_mimetype(ctype)

        # Fallback to UTF-8
        encoding = mimetype.parameters.get('charset', 'UTF-8')

    async for chunk in response.content.iter_chunked(100 * 1024):
        if decode_unicode:
            chunk = chunk.decode(encoding)
            # Replace CRLF newlines with LF, Python will handle
            # platform specific newlines if written to file.
            chunk = chunk.replace('\r\n', '\n')
            # Chunk could be ['...\r', '\n...'], strip trailing \r
            chunk = chunk.rstrip('\r')
        yield chunk
Beispiel #16
0
    def get_encoding(self) -> str:
        c_type = self.headers.get("Content-Type", "").lower()
        mimetype = helpers.parse_mimetype(c_type)

        encoding = mimetype.parameters.get("charset")
        if encoding:
            try:
                codecs.lookup(encoding)
            except LookupError:
                encoding = None
        if not encoding:
            if mimetype.type == "application" and (
                    mimetype.subtype == "json" or mimetype.subtype == "rdap"):
                encoding = "utf-8"
            elif self._body is None:
                raise RuntimeError("Cannot guess the encoding of "
                                   "a not yet read body")
            else:
                encoding = cchardet.detect(self._body)["encoding"]
        if not encoding:
            encoding = "utf-8"

        return encoding
Beispiel #17
0
def function354():
    assert (helpers.parse_mimetype('ApPlIcAtIoN/JSON;ChaRseT="UTF-8"') == (
        'application', 'json', '', {
            'charset': 'UTF-8',
        }))
Beispiel #18
0
def test_parse_mimetype_2():
    assert helpers.parse_mimetype("*") == ("*", "*", "", {})
Beispiel #19
0
 def get_mimetype(self):
     ctype = self.headers.get(hdrs.CONTENT_TYPE, '').lower()
     return helpers.parse_mimetype(ctype)
Beispiel #20
0
def test_parse_mimetype_7():
    assert (helpers.parse_mimetype('application/rss+xml') == ('application',
                                                              'rss', 'xml',
                                                              {}))
Beispiel #21
0
def test_parse_mimetype_1():
    assert helpers.parse_mimetype('') == ('', '', '', {})
Beispiel #22
0
def test_parse_mimetype_2():
    assert helpers.parse_mimetype('*') == ('*', '*', '', {})
Beispiel #23
0
def test_parse_mimetype_5():
    assert (helpers.parse_mimetype('''application/json; charset=utf-8;''') == (
        'application', 'json', '', {
            'charset': 'utf-8'
        }))
Beispiel #24
0
def charset(r):
    ctype = r.headers.get('content-type', '').lower()
    _, _, _, params = parse_mimetype(ctype)
    return params.get('charset')
Beispiel #25
0
    def test_default_subtype(self, writer):
        mimetype = parse_mimetype(writer.headers.get(CONTENT_TYPE))

        assert 'multipart' == mimetype.type
        assert 'mixed' == mimetype.subtype
Beispiel #26
0
def function85():
    assert (helpers.parse_mimetype('application/json; charset=utf-8;') == (
        'application', 'json', '', {
            'charset': 'utf-8',
        }))
Beispiel #27
0
def test_parse_mimetype_7():
    assert helpers.parse_mimetype("application/rss+xml") == ("application", "rss", "xml", {})
Beispiel #28
0
def function632():
    assert (helpers.parse_mimetype('application/json') == ('application',
                                                           'json', '', {}))
Beispiel #29
0
def function1803():
    assert (helpers.parse_mimetype('*') == ('*', '*', '', {}))
Beispiel #30
0
def function423():
    assert (helpers.parse_mimetype('') == ('', '', '', {}))
Beispiel #31
0
def test_parse_mimetype_1():
    assert helpers.parse_mimetype("") == ("", "", "", {})
Beispiel #32
0
def test_parse_mimetype_8():
    assert helpers.parse_mimetype("text/plain;base64") == ("text", "plain", "", {"base64": ""})
Beispiel #33
0
def get_encoding(response):
    ctype = response.headers.get('content-type', '').lower()
    mimetype = parse_mimetype(ctype)

    # Fallback to UTF-8
    return mimetype.parameters.get('charset', 'UTF-8')
Beispiel #34
0
def test_parse_mimetype_1():
    assert helpers.parse_mimetype('') == ('', '', '', {})
Beispiel #35
0
    def get_encoding(self):
        ctype = self.response.headers.get('content-type', '').lower()
        mtype, stype, _, params = parse_mimetype(ctype)

        # Fallback to UTF-8
        return params.get('charset', 'UTF-8')
Beispiel #36
0
def test_parse_mimetype_3():
    assert (helpers.parse_mimetype('application/json') ==
            ('application', 'json', '', {}))
Beispiel #37
0
    def test_default_subtype(self, writer) -> None:
        mimetype = parse_mimetype(writer.headers.get(CONTENT_TYPE))

        assert 'multipart' == mimetype.type
        assert 'mixed' == mimetype.subtype
Beispiel #38
0
def test_parse_mimetype_6():
    assert(
        helpers.parse_mimetype('ApPlIcAtIoN/JSON;ChaRseT="UTF-8"') ==
        ('application', 'json', '', {'charset': 'UTF-8'}))
Beispiel #39
0
def test_parse_mimetype_3():
    assert (helpers.parse_mimetype('application/json') == ('application',
                                                           'json', '', {}))
Beispiel #40
0
def test_parse_mimetype_8():
    assert (
        helpers.parse_mimetype('text/plain;base64') ==
        ('text', 'plain', '', {'base64': ''}))
Beispiel #41
0
def test_parse_mimetype_6():
    assert (helpers.parse_mimetype('ApPlIcAtIoN/JSON;ChaRseT="UTF-8"') == (
        'application', 'json', '', {
            'charset': 'UTF-8'
        }))
Beispiel #42
0
def test_parse_mimetype(mimetype, expected):
    result = helpers.parse_mimetype(mimetype)

    assert isinstance(result, helpers.MimeType)
    assert result == expected
Beispiel #43
0
def test_parse_mimetype_8():
    assert (helpers.parse_mimetype('text/plain;base64') == ('text', 'plain',
                                                            '', {
                                                                'base64': ''
                                                            }))
Beispiel #44
0
def charset(r):
    ctype = r.headers.get('content-type', '').lower()
    _, _, _, params = parse_mimetype(ctype)
    return params.get('charset')
Beispiel #45
0
def function1524():
    assert (helpers.parse_mimetype('application/rss+xml') == ('application',
                                                              'rss', 'xml',
                                                              {}))
Beispiel #46
0
def test_parse_mimetype_3():
    assert helpers.parse_mimetype("application/json") == ("application", "json", "", {})
Beispiel #47
0
def test_parse_mimetype_2():
    assert helpers.parse_mimetype('*') == ('*', '*', '', {})
Beispiel #48
0
def function356():
    assert (helpers.parse_mimetype('text/plain;base64') == ('text', 'plain',
                                                            '', {
                                                                'base64': '',
                                                            }))
Beispiel #49
0
def test_parse_mimetype_5():
    assert (
        helpers.parse_mimetype('''application/json; charset=utf-8;''') ==
        ('application', 'json', '', {'charset': 'utf-8'}))
Beispiel #50
0
def get_boundary(headers):
    mtype, *_, params = parse_mimetype(headers['CONTENT-TYPE'])
    assert mtype == 'multipart'
    return ('--%s' % params['boundary']).encode()
Beispiel #51
0
def test_parse_mimetype_7():
    assert (
        helpers.parse_mimetype('application/rss+xml') ==
        ('application', 'rss', 'xml', {}))
Beispiel #52
0
def get_charset(headers, default=None):
    ctype = headers.get('CONTENT-TYPE', '')
    *_, params = parse_mimetype(ctype)
    return params.get('charset', default)
Beispiel #53
0
def test_parse_mimetype(mimetype, expected) -> None:
    result = helpers.parse_mimetype(mimetype)

    assert isinstance(result, helpers.MimeType)
    assert result == expected
Beispiel #54
0
 def get_mimetype(self):
     ctype = self.headers.get(hdrs.CONTENT_TYPE, "").lower()
     return helpers.parse_mimetype(ctype)