示例#1
0
def _request_send_hook(self, *args, **kwargs):
    if self.method not in _config['allowable_methods']:
        return _original_request_send(self, *args, **kwargs)

    def send_request_and_cache_response():
        result = _original_request_send(self, *args, **kwargs)
        if result and self.response.status_code in _config['allowable_codes']:
            _cache.save_response(self.response.url, self.response)
        return result

    response, timestamp = _cache.get_response_and_time(self.url)
    if response is None:
        return send_request_and_cache_response()

    if _config['expire_after'] is not None:
        difference = datetime.now() - timestamp
        if difference > timedelta(minutes=_config['expire_after']):
            _cache.del_cached_url(self.url)
            return send_request_and_cache_response()

    self.sent = True
    self.response = response
    # TODO: is it stable api?
    if dispatch_hook is not None:
        dispatch_hook('response', self.hooks, self.response)
        r = dispatch_hook('post_request', self.hooks, self)
        self.__dict__.update(r.__dict__)
    return True
示例#2
0
文件: core.py 项目: 5x5x5x5/try_git
    def send(self, request, **kwargs):
        if (self._is_cache_disabled
            or request.method not in self._cache_allowable_methods):
            response = super(CachedSession, self).send(request, **kwargs)
            response.from_cache = False
            return response

        cache_key = self.cache.create_key(request)

        def send_request_and_cache_response():
            response = super(CachedSession, self).send(request, **kwargs)
            if response.status_code in self._cache_allowable_codes:
                self.cache.save_response(cache_key, response)
            response.from_cache = False
            return response

        response, timestamp = self.cache.get_response_and_time(cache_key)
        if response is None:
            return send_request_and_cache_response()

        if self._cache_expire_after is not None:
            difference = datetime.utcnow() - timestamp
            if difference > timedelta(seconds=self._cache_expire_after):
                self.cache.delete(cache_key)
                return send_request_and_cache_response()
        # dispatch hook here, because we've removed it before pickling
        response.from_cache = True
        response = dispatch_hook('response', request.hooks, response, **kwargs)
        return response
示例#3
0
文件: core.py 项目: myun/sql_lesson
    def send(self, request, **kwargs):
        if (self._is_cache_disabled
                or request.method not in self._cache_allowable_methods):
            response = super(CachedSession, self).send(request, **kwargs)
            response.from_cache = False
            return response

        cache_key = self.cache.create_key(request)

        def send_request_and_cache_response():
            response = super(CachedSession, self).send(request, **kwargs)
            if response.status_code in self._cache_allowable_codes:
                self.cache.save_response(cache_key, response)
            response.from_cache = False
            return response

        response, timestamp = self.cache.get_response_and_time(cache_key)
        if response is None:
            return send_request_and_cache_response()

        if self._cache_expire_after is not None:
            difference = datetime.utcnow() - timestamp
            if difference > timedelta(seconds=self._cache_expire_after):
                self.cache.delete(cache_key)
                return send_request_and_cache_response()
        # dispatch hook here, because we've removed it before pickling
        response.from_cache = True
        response = dispatch_hook('response', request.hooks, response, **kwargs)
        return response
示例#4
0
    def send(self, request, **kwargs) -> UpgradeResponse:
        kwargs.setdefault('stream', self.stream)
        kwargs.setdefault('verify', self.verify)
        kwargs.setdefault('cert', self.cert)
        kwargs.setdefault('proxies', self.proxies)

        if isinstance(request, requests.Request):
            raise ValueError('You can only send PreparedRequests.')
        if isinstance(request, HttpRequest):
            request.check()
        allow_redirects = kwargs.pop('allow_redirects', True)
        hooks = request.hooks

        adapter = self.get_adapter(url=request.url)

        start = preferred_clock()

        r = adapter.send(request, **kwargs)

        elapsed = preferred_clock() - start
        r.elapsed = timedelta(seconds=elapsed)

        # this function not change response and result params
        if len(self._dispatch_hooks) > 0:
            dispatch(self._dispatch_hooks, hooks, r, **kwargs)

        # dispatch hook will change response and request params
        r = dispatch_hook('response', hooks, r, **kwargs)

        if r.history:
            for resp in r.history:
                extract_cookies_to_jar(self.cookies, resp.request, resp.raw)

        extract_cookies_to_jar(self.cookies, request, r.raw)

        gen = self.resolve_redirects(r, request, **kwargs)

        history = [resp for resp in gen] if allow_redirects else []

        if history:
            history.insert(0, r)
            r = history.pop()
            r.history = history

        if not allow_redirects:
            try:
                r._next = next(
                    self.resolve_redirects(r,
                                           request,
                                           yield_requests=True,
                                           **kwargs))
            except StopIteration:
                pass

        return r
示例#5
0
    def send(self, request, **kwargs):
        if (self._is_cache_disabled
                or request.method not in self._cache_allowable_methods):
            response = super(CachedSession, self).send(request, **kwargs)
            response.from_cache = False
            return response

        cache_key = self.cache.create_key(request)

        def send_request_and_cache_response():
            if self._deny_outbound:
                print(request.url)
                raise Exception(("ERROR: OutBound communication was attempted,"
                                 " but deny_outbound was set to True"))

            cache_response = True
            response = super(CachedSession, self).send(request, **kwargs)
            if response.status_code in self._cache_allowable_codes:

                #
                # Special case for cblr:
                # if we get a status of pending then don't cache
                #
                try:
                    if request.url.find(
                            'cblr') != -1 and request.method == 'GET':
                        if isinstance(response.json(),
                                      dict) and response.json().get(
                                          'status', '') == 'pending':
                            cache_response = False
                except:
                    cache_response = True
                if cache_response:
                    self.cache.save_response(cache_key, response)

            response.from_cache = False
            return response

        response = self.cache.get_response(cache_key)
        if response is None:
            return send_request_and_cache_response()

        if 'Content-Encoding' in response.headers:
            del response.headers['Content-Encoding']

        adapter = HTTPAdapter()
        response = adapter.build_response(request, response)

        # dispatch hook here, because we've removed it before pickling
        response.from_cache = True
        response = dispatch_hook('response', request.hooks, response, **kwargs)
        return response
示例#6
0
    def send(self, request, **kwargs):
        do_not_cache = (self._is_cache_disabled
                        or request.method not in self._cache_allowable_methods
                        or self._request_expire_after is None)
        if do_not_cache:
            response = super().send(request, **kwargs)
            response.from_cache = False
            response.cache_date = None
            response.expiration_date = None
            response.expire_after = 'default'
            return response

        cache_key = self.cache.create_key(request)

        try:
            response, timestamp = self.cache.get_response_and_time(cache_key)
        except (ImportError, TypeError):
            response, timestamp = None, None

        if response is None:
            return self.send_request_and_cache_response(
                request, cache_key, **kwargs)

        if getattr(response, 'expiration_date', None) is not None:
            now = datetime.now(timezone.utc)
            is_expired = now > response.expiration_date
        else:
            is_expired = False

        cache_invalid = response.expire_after != self._request_expire_after and self._request_expire_after != 'default'
        if cache_invalid or is_expired:
            if not self._return_old_data_on_error:
                self.cache.delete(cache_key)
                return self.send_request_and_cache_response(
                    request, cache_key, **kwargs)
            try:
                new_response = self.send_request_and_cache_response(
                    request, cache_key, **kwargs)
            except Exception:
                return response
            else:
                if new_response.status_code not in self._cache_allowable_codes:
                    return response
                return new_response

        # dispatch hook here, because we've removed it before pickling
        response.from_cache = True
        response.cache_date = timestamp
        response = dispatch_hook('response', request.hooks, response, **kwargs)
        return response
示例#7
0
    def send(self, request, **kwargs):
        if (self._is_cache_disabled
            or request.method not in self._cache_allowable_methods):
            response = super(CachedSession, self).send(request, **kwargs)
            response.from_cache = False
            return response

        cache_key = self.cache.create_key(request)

        def send_request_and_cache_response():
            if self._deny_outbound:
                print(request.url)
                raise Exception(("ERROR: OutBound communication was attempted,"
                                 " but deny_outbound was set to True"))

            cache_response = True
            response = super(CachedSession, self).send(request, **kwargs)
            if response.status_code in self._cache_allowable_codes:

                #
                # Special case for cblr:
                # if we get a status of pending then don't cache
                #
                try:
                    if request.url.find('cblr') != -1 and request.method == 'GET':
                        if isinstance(response.json(), dict) and response.json().get('status', '') == 'pending':
                            cache_response = False
                except:
                    cache_response = True
                if cache_response:
                    self.cache.save_response(cache_key, response)

            response.from_cache = False
            return response

        response = self.cache.get_response(cache_key)
        if response is None:
            return send_request_and_cache_response()

        if 'Content-Encoding' in response.headers:
            del response.headers['Content-Encoding']

        adapter = HTTPAdapter()
        response = adapter.build_response(request, response)


        # dispatch hook here, because we've removed it before pickling
        response.from_cache = True
        response = dispatch_hook('response', request.hooks, response, **kwargs)
        return response
示例#8
0
def _request_send_hook(self, *args, **kwargs):
    if self.method not in _config["allowable_methods"]:
        return _original_request_send(self, *args, **kwargs)

    if self.method == "POST":
        data = self._encode_params(getattr(self, "data", {}))
        if isinstance(data, tuple):  # old requests versions
            data = data[1]
        cache_url = self.full_url + str(data)
    else:
        cache_url = self.full_url

    def send_request_and_cache_response():
        result = _original_request_send(self, *args, **kwargs)
        if result and self.response.status_code in _config["allowable_codes"]:
            _cache.save_response(cache_url, self.response)
        return result

    response, timestamp = _cache.get_response_and_time(cache_url)
    if response is None:
        return send_request_and_cache_response()

    if _config["expire_after"] is not None:
        difference = datetime.now() - timestamp
        if difference > timedelta(minutes=_config["expire_after"]):
            _cache.del_cached_url(cache_url)
            return send_request_and_cache_response()

    response.from_cache = True
    self.sent = True
    self.response = response
    # TODO: is it stable api?
    if dispatch_hook is not None:
        dispatch_hook("response", self.hooks, self.response)
        r = dispatch_hook("post_request", self.hooks, self)
        self.__dict__.update(r.__dict__)
    return True
示例#9
0
    def send(self, request, from_cache=True, update_cache=False, **kwargs):
        print("URL:{0}, FROM_CACHE:{1}, UPDATE_CACHE:{2}".format(
            request.url, from_cache, update_cache))
        if (self._is_cache_disabled
                or request.method not in self._cache_allowable_methods) or (
                    not from_cache and not update_cache):
            response = super(CachedSession, self).send(request, **kwargs)
            response.from_cache = False
            return response

        cache_key = self.cache.create_key(request)

        def send_request_and_cache_response():
            response = super(CachedSession, self).send(request, **kwargs)
            if response.status_code in self._cache_allowable_codes:
                self.cache.save_response(cache_key, response)
            response.from_cache = False
            return response

        if update_cache:
            return send_request_and_cache_response()

        response, timestamp = self.cache.get_response_and_time(cache_key)
        if response is None:
            return send_request_and_cache_response()

        if self._cache_expire_after is not None:
            is_expired = datetime.utcnow(
            ) - timestamp > self._cache_expire_after
            if is_expired:
                if not self._return_old_data_on_error:
                    self.cache.delete(cache_key)
                    return send_request_and_cache_response()
                try:
                    new_response = send_request_and_cache_response()
                except Exception:
                    return response
                else:
                    if new_response.status_code not in self._cache_allowable_codes:
                        return response
                    return new_response

        # dispatch hook here, because we've removed it before pickling
        response.from_cache = True
        response = dispatch_hook('response', request.hooks, response, **kwargs)
        return response
    def send(self, request, **kwargs):
        if (self._is_cache_disabled
                or request.method not in self._cache_allowable_methods):
            response = super(CachedSession, self).send(request, **kwargs)
            response.from_cache = False
            return response

        cache_key = self.cache.create_key(request)

        def send_request_and_cache_response():
            response = super(CachedSession, self).send(request, **kwargs)
            if response.status_code in self._cache_allowable_codes:
                self.cache.save_response(cache_key, response)
            response.from_cache = False
            return response

        try:
            response, timestamp = self.cache.get_response_and_time(cache_key)
        except (ImportError, TypeError):
            return send_request_and_cache_response()

        if response is None:
            return send_request_and_cache_response()

        if self._cache_expire_after is not None:
            is_expired = datetime.utcnow(
            ) - timestamp > self._cache_expire_after
            if is_expired:
                if not self._return_old_data_on_error:
                    self.cache.delete(cache_key)
                    return send_request_and_cache_response()
                try:
                    new_response = send_request_and_cache_response()
                except Exception:
                    return response
                else:
                    if new_response.status_code not in self._cache_allowable_codes:
                        return response
                    return new_response

        # dispatch hook here, because we've removed it before pickling
        response.from_cache = True
        response = dispatch_hook('response', request.hooks, response, **kwargs)
        return response
示例#11
0
def build_response(request, data, code, encoding):
    '''Builds a response object from the data returned by ftplib, using the
    specified encoding.'''
    response = Response()

    response.encoding = encoding

    # Fill in some useful fields.
    response.raw = data
    response.url = request.url
    response.request = request
    response.status_code = code.split()[0]

    # Make sure to seek the file-like raw object back to the start.
    response.raw.seek(0)

    # Run the response hook.
    response = dispatch_hook('response', request.hooks, response)
    return response
def build_response(request, data, code, encoding):
    response = Response()

    response.encoding = encoding

    # Fill in some useful fields.

    raw = StringIO()
    raw.write(data)
    raw.seek(0)

    response.raw = raw
    response.url = request.url
    response.request = request
    response.status_code = code

    # Run the response hook.
    response = dispatch_hook('response', request.hooks, response)
    return response
示例#13
0
def build_response(request, data, code, encoding):
    response = Response()

    response.encoding = encoding

    # Fill in some useful fields.

    raw = six.BytesIO()
    raw.write(data)
    raw.seek(0)

    response.raw = raw
    response.url = request.url
    response.request = request
    response.status_code = code

    # Run the response hook.
    response = dispatch_hook('response', request.hooks, response)
    return response
示例#14
0
def build_response(request, data, code, encoding):
    """Builds a response object from the data returned by ftplib, using the
    specified encoding."""
    response = Response()

    response.encoding = encoding

    # Fill in some useful fields.
    response.raw = data
    response.url = request.url
    response.request = request
    response.status_code = get_status_code_from_code_response(code)

    # Make sure to seek the file-like raw object back to the start.
    response.raw.seek(0)

    # Run the response hook.
    response = dispatch_hook('response', request.hooks, response)
    return response
示例#15
0
文件: core.py 项目: Mr19/cbapi-python
    def send(self, request, **kwargs):
        if (self._is_cache_disabled
            or request.method not in self._cache_allowable_methods):
            response = super(CachedSession, self).send(request, **kwargs)
            response.from_cache = False
            return response

        cache_key = self.cache.create_key(request)

        def send_request_and_cache_response():
            if self._deny_outbound:
                raise Exception(("ERROR: OutBound communication was attempted,"
                                 " but deny_outbound was set to True"))
            response = super(CachedSession, self).send(request, **kwargs)
            if response.status_code in self._cache_allowable_codes:
                self.cache.save_response(cache_key, response)
            response.from_cache = False
            return response

        response, timestamp = self.cache.get_response_and_time(cache_key)
        if response is None:
            return send_request_and_cache_response()

        if self._cache_expire_after is not None:
            is_expired = datetime.utcnow() - timestamp > self._cache_expire_after
            if is_expired:
                if not self._return_old_data_on_error:
                    self.cache.delete(cache_key)
                    return send_request_and_cache_response()
                try:
                    new_response = send_request_and_cache_response()
                except Exception:
                    return response
                else:
                    if new_response.status_code not in self._cache_allowable_codes:
                        return response
                    return new_response

        # dispatch hook here, because we've removed it before pickling
        response.from_cache = True
        response = dispatch_hook('response', request.hooks, response, **kwargs)
        return response
示例#16
0
    def send(self, request, *args, **kwargs):
        """Send a given PreparedRequest."""
        # Set defaults that the hooks can utilize to ensure they always have
        # the correct parameters to reproduce the previous request.
        kwargs.setdefault('proxies', self.proxies)
        kwargs.setdefault('verify', self.verify)
        kwargs.setdefault('cert', self.cert)

        # It's possible that users might accidentally send a Request object.
        # Guard against that specific failure case.
        if not isinstance(request, requests.PreparedRequest):
            raise ValueError('You can only send PreparedRequests.')

        # Set up variables needed for resolve_redirects and dispatching of hooks

        # Get the appropriate adapter to use
        adapter = self.get_adapter(url=request.url)

        # Start time (approximately) of the request
        start = datetime.utcnow()

        # Send the request
        r = adapter.send(request, *args, **kwargs)

        # Total elapsed time of the request (approximately)
        r.elapsed = datetime.utcnow() - start

        # Response manipulation hooks
        r = dispatch_hook('response', request.hooks, r, *args, **kwargs)

        # Resolve redirects if allowed.
        history = []

        # Shuffle things around if there's history.
        if history:
            # Insert the first (original) request at the start
            history.insert(0, r)
            # Get the last request made
            r = history.pop()
            r.history = tuple(history)

        return r
示例#17
0
    def send(self, request: PreparedRequest, **kwargs) -> AnyResponse:
        """Send a prepared request, with caching."""
        # If we shouldn't cache the response, just send the request
        if not self._is_cacheable(request):
            logger.debug(f'Request for URL {request.url} is not cacheable')
            response = super().send(request, **kwargs)
            return set_response_defaults(response)

        # Attempt to fetch the cached response
        cache_key = self.cache.create_key(request, **kwargs)
        response = self.cache.get_response(cache_key)

        # Attempt to fetch and cache a new response, if needed
        if response is None:
            return self._send_and_cache(request, cache_key, **kwargs)
        if response.is_expired:
            return self._handle_expired_response(request, response, cache_key, **kwargs)

        # Dispatch hook here, because we've removed it before pickling
        return dispatch_hook('response', request.hooks, response, **kwargs)
示例#18
0
    def send(self, request, **kwargs):
        if (self._is_cache_disabled
            or request.method not in self._cache_allowable_methods):
            response = super(CachedSession, self).send(request, **kwargs)
            response.from_cache = False
            return response

        cache_key = self.cache.create_key(request)

        def send_request_and_cache_response():
            response = super(CachedSession, self).send(request, **kwargs)
            if response.status_code in self._cache_allowable_codes \
                and len(response.content.strip()) > 5 :
                self.cache.save_response(cache_key, response)
            response.from_cache = False
            return response

        response, timestamp = self.cache.get_response_and_time(cache_key)
        if response is None:
            return send_request_and_cache_response()

        if self._cache_expire_after is not None:
            is_expired = datetime.utcnow() - timestamp > self._cache_expire_after
            if is_expired:
                if not self._return_old_data_on_error:
                    self.cache.delete(cache_key)
                    return send_request_and_cache_response()
                try:
                    new_response = send_request_and_cache_response()
                except Exception:
                    return response
                else:
                    if new_response.status_code not in self._cache_allowable_codes \
                        or len(new_response.content.strip()) < 5:
                        return response
                    return new_response

        # dispatch hook here, because we've removed it before pickling
        response.from_cache = True
        response = dispatch_hook('response', request.hooks, response, **kwargs)
        return response
示例#19
0
def build_response(request, data, code, encoding):
    '''Builds a response object from the data returned by ftplib, using the
    specified encoding.'''
    response = Response()

    response.encoding = encoding

    # Fill in some useful fields.
    response.raw = data
    response.url = request.url
    response.request = request
    response.status_code = int(code.split()[0])
    if hasattr(data, "content_len"):
        response.headers['Content-Length'] = str(data.content_len)

    # Make sure to seek the file-like raw object back to the start.
    response.raw.seek(0)

    # Run the response hook.
    response = dispatch_hook('response', request.hooks, response)
    return response
示例#20
0
def build_response(request, data, code, encoding):
    '''Builds a response object from the data returned by ftplib, using the
    specified encoding.'''
    response = Response()

    response.encoding = encoding

    # Fill in some useful fields.
    response.raw = data
    response.url = request.url
    response.request = request
    response.status_code = int(code.split()[0])
    if hasattr(data, "content_len"):
        response.headers['Content-Length'] = str(data.content_len)

    # Make sure to seek the file-like raw object back to the start.
    response.raw.seek(0)

    # Run the response hook.
    response = dispatch_hook('response', request.hooks, response)
    return response
示例#21
0
    def send(self, request, **kwargs):
        if self._is_cache_disabled or request.method not in self._cache_allowable_methods:
            response = super(CachedSession, self).send(request, **kwargs)
            response.from_cache = False
            return response

        cache_key = self.cache.create_key(request)

        def wait_for_throttle():
            requests_per_second = self._lookup_throttle(request.url)
            if requests_per_second is not None and requests_per_second > 0:
                # enventually we'll use the cache to distribute these
                # but for now just wait
                seconds_per_request = 1.0 / requests_per_second
                sleep(seconds_per_request)

        def send_request_and_cache_response():
            wait_for_throttle()
            response = super(CachedSession, self).send(request, **kwargs)
            if response.status_code in self._cache_allowable_codes:
                self.cache.save_response(cache_key, response)
            response.from_cache = False
            return response

        response, timestamp = self.cache.get_response_and_time(cache_key)
        if response is None:
            return send_request_and_cache_response()

        expire_after = self._lookup_expire_after(request.url)
        if expire_after is not None:
            difference = datetime.utcnow() - timestamp
            if difference > timedelta(seconds=expire_after):
                self.cache.delete(cache_key)
                return send_request_and_cache_response()

        # dispatch hook here, because we've removed it before pickling
        response.from_cache = True
        response = dispatch_hook("response", request.hooks, response, **kwargs)
        return response
def build_response(request, data, code, encoding):
    '''Builds a response object from the data returned by ftplib, using the
    specified encoding.'''
    response = Response()

    response.encoding = encoding

    # Fill in some useful fields.

    raw = StringIO()
    raw.write(data)
    raw.seek(0)

    response.raw = raw
    response.url = request.url
    response.request = request
    response.status_code = code


    # Run the response hook.
    response = dispatch_hook('response', request.hooks, response)
    return response
示例#23
0
    def build_response(self, req, resp):
        response = Response()

        response.status_code = resp.status_code
        response.headers = CaseInsensitiveDict((k, v) for k, v in resp.items())

        response.encoding = get_encoding_from_headers(response.headers)
        response.raw = StringIO(resp.content)
        response.reason = None

        if isinstance(req.url, bytes):
            response.url = req.url.decode('utf-8')
        else:
            response.url = req.url

        # Convert from django's SimpleCookie to request's CookieJar
        cookiejar_from_dict(resp.cookies, response.cookies)

        # context
        response.request = req
        response.connection = self

        response = dispatch_hook('response', req.hooks, response)
        return response
示例#24
0
    def send(self, request, stream=False, verify=None, cert=None, proxies=None,
            timeout=None):
        """issue request"""

        data = url_unquote(request.url[len('data:'):])

        if ',' not in data:
            raise InvalidURL('data URL missing comma')

        mime, content = data.split(',', 1)
        content = content.strip()

        base64 = False
        charset = None

        while ';' in mime:
            mime, encoding_spec = mime.rsplit(';', 1)
            encoding_spec = encoding_spec.strip()
            if encoding_spec == 'base64':
                base64 = True
            elif not encoding_spec.startswith('charset='):
                raise InvalidURL(
                    'unrecognized encoding parameter: %r' % encoding_spec
                )
            else:
                charset = encoding_spec[len('charset='):]

        try:
            if base64:
                content = a2b_base64(content)

            content_type = mime.strip()
            if charset:
                content_type += "; charset=" + charset

            response = Response()
            response.url = request.url
            response.headers['Date'] = formatdate(timeval=None, localtime=True)

            if request.method in ('GET', 'HEAD'):
                response.status_code = 200
                response.headers['Content-Length'] = len(content)
                response.headers['Last-Modified'] = formatdate()
                response.headers['Content-Type'] = content_type
                if charset:
                    response.encoding = charset
                response.raw = StringIO(str(content))
            else:
                response.status_code = 405
                response.headers['Status'] = '405 Method Not Allowed'
        except Exception:
            response.status_code = 500
            response.headers['Status'] = '500 Internal Server Error'
            response.raw = StringIO(format_exc())

        # context
        response.request = request
        response.connection = self

        # hooks
        response = dispatch_hook('response', request.hooks, response)

        # streaming
        if not stream:
            response.content

        return response
示例#25
0
def test_hooks(hooks_list, result):
    assert hooks.dispatch_hook('response', {'response': hooks_list}, 'Data') == result
示例#26
0
    def send(self, req, **kwargs):
        """Send a given PreparedRequest.

        :rtype: trip.gen.Future
        """

        if not isinstance(req, PreparedRequest):
            raise ValueError('You can only send PreparedRequests.')

        allow_redirects = kwargs.pop('allow_redirects', True)
        start_time = preferred_clock()

        r = yield self.adapter.send(req, **kwargs)

        if isinstance(r, Exception):
            raise gen.Return(r)
        else:
            r = self.prepare_response(req, r)

        r.elapsed = timedelta(seconds=(preferred_clock() - start_time))

        # Response manipulation hooks
        r = dispatch_hook('response', req.hooks, r, **kwargs)

        # Persist cookies
        if r.history:
            # If the hooks create history then we want those cookies too
            for resp in r.history:
                self.cookies.extract_cookies(
                    MockResponse(HTTPHeaderDict(resp.headers)),
                    MockRequest(resp.request))

        self.cookies.extract_cookies(MockResponse(HTTPHeaderDict(r.headers)),
                                     MockRequest(req))

        # Redirect resolving generator.
        redirect_gen = self.resolve_redirects(r, req, **kwargs)

        # Resolve redirects if allowed.
        history = []
        if allow_redirects:
            for resp in redirect_gen:
                resp = yield resp
                history.append(resp)

        # Shuffle things around if there's history.
        if history:
            # Insert the first (original) request at the start
            history.insert(0, r)
            # Get the last request made
            r = history.pop()
            r.history = history

        # If redirects aren't being followed, store the response on the Request for Response.next().
        if not allow_redirects:
            try:
                r._next = next(
                    self.resolve_redirects(r,
                                           req,
                                           yield_requests=True,
                                           **kwargs))
            except StopIteration:
                pass

        raise gen.Return(r)
示例#27
0
    def send(self,
             request,
             stream=False,
             verify=None,
             cert=None,
             proxies=None,
             timeout=None):
        """issue request"""

        data = url_unquote(request.url[len('data:'):])

        if ',' not in data:
            raise InvalidURL('data URL missing comma')

        mime, content = data.split(',', 1)
        content = content.strip()

        base64 = False
        charset = None

        while ';' in mime:
            mime, encoding_spec = mime.rsplit(';', 1)
            encoding_spec = encoding_spec.strip()
            if encoding_spec == 'base64':
                base64 = True
            elif not encoding_spec.startswith('charset='):
                raise InvalidURL('unrecognized encoding parameter: %r' %
                                 encoding_spec)
            else:
                charset = encoding_spec[len('charset='):]

        try:
            if base64:
                content = a2b_base64(content)

            content_type = mime.strip()
            if charset:
                content_type += "; charset=" + charset

            response = Response()
            response.url = request.url
            response.headers['Date'] = formatdate(timeval=None, localtime=True)

            if request.method in ('GET', 'HEAD'):
                response.status_code = 200
                response.headers['Content-Length'] = len(content)
                response.headers['Last-Modified'] = formatdate()
                response.headers['Content-Type'] = content_type
                if charset:
                    response.encoding = charset
                response.raw = StringIO(str(content))
            else:
                response.status_code = 405
                response.headers['Status'] = '405 Method Not Allowed'
        except Exception:
            response.status_code = 500
            response.headers['Status'] = '500 Internal Server Error'
            response.raw = StringIO(format_exc())

        # context
        response.request = request
        response.connection = self

        # hooks
        response = dispatch_hook('response', request.hooks, response)

        # streaming
        if not stream:
            response.content

        return response
示例#28
0
def test_hooks(hooks_list, result):
    assert hooks.dispatch_hook("response", {"response": hooks_list},
                               "Data") == result
示例#29
0
def test_hooks(hooks_list, result):
    assert hooks.dispatch_hook('response', {'response': hooks_list},
                               'Data') == result
示例#30
0
    def send(
            self, request, stream=False, verify=None, cert=None, proxies=None,
            timeout=None
        ):
        """issue request"""

        fname = url_unquote(request.url[len('file://'):])
        if not fname:
            raise InvalidURL('missing file name')
        if '/' not in fname:
            raise InvalidURL(
                'hostname without filename (perhaps missing a /?)'
            )
        host, fname = fname.split('/', 1)
        fname = self.resolve_host(host, fname)

        response = Response()
        response.url = request.url
        response.headers['Date'] = formatdate(timeval=None, localtime=True)

        try:
            if request.method in ('GET', 'HEAD'):
                statdata = stat(fname)
                etag = '"%s/%s/%s' \
                    % (statdata.st_dev, statdata.st_ino, statdata.st_mtime)
                if S_ISLNK(statdata.st_mode):
                    # handle relative symlinks!
                    target_file = abspath(readlink(fname))
                    response.status_code = 302
                    response.headers['Status'] = '302 Found'
                    response.headers['Location'] = \
                        url_quote('file://' + target_file)
                elif S_ISDIR(statdata.st_mode):
                    response.status_code = 200
                    response.headers['Status'] = '200 Ok'
                    body = \
                        """<html><head><title>%s</title></head><body><ul>""" \
                        % fname
                    for subfname in sorted(listdir(fname)):
                        body += '<li><a href="file://' + \
                                url_quote(subfname) + '">' + \
                                html_escape(fname) + '</a></li>'
                    body += '</body></html>'
                    response.headers['ETag'] = 'W/' + etag
                    response.raw = StringIO(body)
                elif S_ISREG(statdata.st_mode):
                    response.status_code = 200
                    response.headers['Content-Length'] = statdata.st_size
                    response.headers['Last-Modified'] = formatdate(
                        timeval=statdata.st_mtime,
                        localtime=True
                    )
                    mt, enc = guess_mime_type(request.url, strict=False)
                    if mt is None:
                        mt = 'application/octet-stream'
                    if enc is not None:
                        response.headers['Content-Encoding'] = enc
                    response.headers['Content-Type'] = mt
                    response.headers['ETag'] = etag
                    if request.method == 'GET':
                        response.raw = open(fname, 'r')
                else:
                    response.status_code = 500
                    response.headers['Status'] = '500 Internal Server Error'
            elif request.method == 'PUT':
                open(fname, 'w').write(request.body)  # FIXME: Is this right?
                response.status_code = 200
                response.headers['Status'] = '200 Ok'
            elif request.method == 'POST':
                if exists(fname):  # FIXME: Is this right?
                    response.status_code = 409
                    response.headers['Status'] = '409 Conflict'
                else:
                    open(fname, 'w').write(request.body)
            elif request.method == 'DELETE':
                unlink(fname)
                response.status_code = 200
                response.headers['Status'] = '200 Ok'
            else:
                response.status_code = 405
                response.headers['Status'] = '405 Method Not Allowed'
        except OSError as e:
            if e.errno == errno.ENOENT:
                if request.method == 'DELETE':
                    response.status_code = 410
                    response.headers['Status'] = '410 Gone'
                else:
                    response.status_code = 404
                    response.headers['Status'] = '404 Not Found'
            elif e.errno == errno.EISDIR:
                response.status_code = 405
                response.headers['Status'] = '405 Method Not Allowed'
                response.raw = StringIO('Cannot %r a directory...'
                    % request.method)
            elif e.errno == errno.EACCES:
                response.status_code = 403
                response.headers['Status'] = '403 Forbidden'
            else:
                response.status_code = 500
                response.headers['Status'] = '500 Internal Server Error'
                response.raw = StringIO('OSError: ' + strerror(e.errno))
        except Exception:
            response.status_code = 500
            response.headers['Status'] = '500 Internal Server Error'
            response.raw = StringIO(format_exc())

        # context
        response.request = request
        response.connection = self

        # hooks
        response = dispatch_hook('response', request.hooks, response)

        # streaming
        if not stream:
            response.content

        return response