示例#1
0
 def _get_best_content_type_match_score(self, request, subhandler):
     request_accept = request.getHeader('Accept') or '*/*'
     logging.debug("subhandler accept {0} /// requestaccept {1} ".format(
         subhandler["accept"], request_accept))
     return max(
         map(
             lambda handler_mimetype: quality(
                 handler_mimetype, request_accept), subhandler["accept"]))
示例#2
0
def fetch_http_node(state, task_meta, **options):
    url = task_meta['url']

    if options.get('cache_backend'):
        session = requests_cache.CachedSession(
            backend=options['cache_backend'], expire_after=options.get('cache_expire_after', 300))
    else:
        session = requests.Session()

    result = session.get(
        url, headers={'Accept': 'application/ld+json, application/json, image/png, image/svg+xml'}
    )

    try:
        json.loads(result.text)
    except ValueError:
        content_type = result.headers.get('Content-Type', 'UNKNOWN')

        if mimeparse.quality(content_type, 'image/svg+xml') > 0.9:
            parsed_type = 'image/svg+xml'
        elif mimeparse.quality(content_type, 'image/png') > 0.9:
            parsed_type = 'image/png'
        else:
            return task_result(
                success=False,
                message="Unknown Content-Type (Not image/png or image/svg+xml). Response could not be interpreted from url {}".format(
                    url)
            )

        b64content = b''.join([b'data:', parsed_type.encode(), b';base64,', base64.b64encode(result.content)])
        actions = [store_original_resource(node_id=url, data=b64content)]
        if task_meta.get('is_potential_baked_input', False):
            actions += [add_task(PROCESS_BAKED_RESOURCE, node_id=url)]

        return task_result(
            True, 'Successfully fetched image from {}'.format(url), actions)

    actions = [
        store_original_resource(node_id=url, data=result.text),
        add_task(INTAKE_JSON, data=result.text, node_id=url,
                 expected_class=task_meta.get('expected_class'),
                 source_node_path=task_meta.get('source_node_path'))]
    return task_result(message="Successfully fetched JSON data from {}".format(url), actions=actions)
示例#3
0
def client_accepts_json(accept):
    # Taken from https://github.com/falconry/falcon/blob/master/falcon/request.py#L967
    """Determine whether or not the client accepts json media type.

    Args:
        media_type (str): the ``Accept`` request header

    Returns:
        bool: ``True`` if the client  has  indicated in the accept header
        that accepts application/json media type. Otherwise, returns
        ``False``
    """
    if (accept == 'application/json') or (accept == '*/*'):
        return True

    try:
        return mimeparse.quality('application/json', accept) != 0.0
    except ValueError:
        return False
示例#4
0
    def accept_type_supported(self, request, response):
        if 'HTTP_ACCEPT' in request.META:
            accept_type = request.META['HTTP_ACCEPT']
            mimetypes = list(self.supported_accept_types)
            mimetypes.reverse()
            match = mimeparse.best_match(mimetypes, accept_type)

            if match:
                request._accept_type = match
                return True

            # Only if `Accept` explicitly contains a `*/*;q=0.0`
            # does it preclude from returning a non-matching mimetype.
            # This may be desirable behavior (or not), so add this as an
            # option, e.g. `force_accept_type`
            if mimeparse.quality('*/*', accept_type) == 0:
                return False

        # If `supported_accept_types` is empty, it is assumed that the resource
        # will return whatever it wants.
        if self.supported_accept_types:
            response._accept_type = self.supported_accept_types[0]
        return True
示例#5
0
    def client_accepts(self, media_type):
        """Returns the client's preferred media type.

        Args:
            media_type: Media type to check

        Returns:
            True IFF the client has indicated in the Accept header that
            they accept at least one of the specified media types.
        """

        accept = self.accept

        # PERF(kgriffs): Usually the following will be true, so
        # try it first.
        if (accept == media_type) or (accept == '*/*'):
            return True

        # Fall back to full-blown parsing
        try:
            return mimeparse.quality(media_type, accept) != 0.0
        except ValueError:
            return False
示例#6
0
    def client_accepts(self, media_type):
        """Returns the client's preferred media type.

        Args:
            media_type: Media type to check

        Returns:
            True IFF the client has indicated in the Accept header that
            they accept at least one of the specified media types.
        """

        accept = self.accept

        # PERF(kgriffs): Usually the following will be true, so
        # try it first.
        if (accept == media_type) or (accept == '*/*'):
            return True

        # Fall back to full-blown parsing
        try:
            return mimeparse.quality(media_type, accept) != 0.0
        except ValueError:
            return False
示例#7
0
    def client_accepts(self, media_type):
        """Determines whether or not the client accepts a given media type.

        Args:
            media_type (str): An Internet media type to check.

        Returns:
            bool: ``True`` if the client has indicated in the Accept header
            that it accepts the specified media type. Otherwise, returns
            ``False``.
        """

        accept = self.accept

        # PERF(kgriffs): Usually the following will be true, so
        # try it first.
        if (accept == media_type) or (accept == '*/*'):
            return True

        # Fall back to full-blown parsing
        try:
            return mimeparse.quality(media_type, accept) != 0.0
        except ValueError:
            return False
示例#8
0
    def client_accepts(self, media_type):
        """Determines whether or not the client accepts a given media type.

        Args:
            media_type (str): An Internet media type to check.

        Returns:
            bool: ``True`` if the client has indicated in the Accept header
                that it accepts the specified media type. Otherwise, returns
                ``False``.
        """

        accept = self.accept

        # PERF(kgriffs): Usually the following will be true, so
        # try it first.
        if (accept == media_type) or (accept == "*/*"):
            return True

        # Fall back to full-blown parsing
        try:
            return mimeparse.quality(media_type, accept) != 0.0
        except ValueError:
            return False
 def _test_quality(self, args, expected):
     result = mimeparse.quality(args[0], args[1])
     message = "Expected: '%s' but got %s" % (expected, result)
     self.assertEqual(expected, result, message)
def test_quality(args, expected):
    result = mimeparse.quality(args[0], args[1])
    message = "Expected: '%s' but got %s" % (expected, result)
    assert expected == result, message
示例#11
0
文件: base.py 项目: raminetinati/indx
 def _get_best_content_type_match_score(self,request,subhandler):
     request_accept = request.getHeader('Accept') or '*/*'
     logging.debug("subhandler accept {0} /// requestaccept {1} ".format(subhandler["accept"], request_accept))
     return max(map(lambda handler_mimetype:quality(handler_mimetype,request_accept), subhandler["accept"]))