Пример #1
0
def accept_type(available):
    """Returns the best match for the supplied list of media types in the
    Accept header.

    >>> accept_type([MediaType("text", "plain"), MediaType("text", "html")])
    MediaType('text', 'html')

    >>> os.environ["HTTP_ACCEPT"] = "text/html, text/*, */*"
    >>> accept_type([MediaType("text", "html"), MediaType("image", "gif")])
    MediaType('text', 'html')
    >>> accept_type([MediaType("text", "plain"), MediaType("image", "gif")])
    MediaType('text', 'plain')
    >>> accept_type([MediaType("image", "gif")])
    MediaType('image', 'gif')
    """
    accept = request.accept()
    if not accept:
        accept = [MediaRange("text", "html"), MediaRange("*", "*")]

    for pattern in accept:
        for content_type in available:
            if isinstance(content_type, MediaType):
                parsed = content_type
            else:
                try:
                    parsed = parse_media_type(content_type)
                except Exception as e:
                    raise Exception("Couldn't parse media type %s" % content_type)
            if pattern.matches(parsed):
                return content_type

    raise NotAcceptable(available)
Пример #2
0
def content_type():
    """Returns the content type of the request (if any).

    >>> print content_type()
    None
    >>> os.environ["CONTENT_TYPE"] = "text/plain; charset=UTF-8"
    >>> content_type()
    MediaType('text', 'plain', charset='UTF-8')
    """
    if not os.environ.has_key("CONTENT_TYPE"):
        return None

    return parse_media_type(os.environ["CONTENT_TYPE"])