Пример #1
0
    def prepare_response(self, req, resp):
        """Builds a :class:`Response <trip.Response>` object from a tornado
        response. This should not be called from user code, and is only exposed
        for use when subclassing the
        :class:`HTTPAdapter <trip.adapters.HTTPAdapter>`

        :param req: The :class:`PreparedRequest <PreparedRequest>` used to
        generate the response.
        :param resp: The :class:`MessageDelegate <MessageDelegate>` response
        object.
        :rtype: requests.Response
        """

        response = Response()

        # Fallback to None if there's no status_code, for whatever reason.
        response.status_code = getattr(resp, 'code', None)

        # Make headers case-insensitive.
        response.headers = CaseInsensitiveDict(getattr(resp, 'headers', {}))

        # Set encoding.
        response.encoding = get_encoding_from_headers(response.headers)
        response.raw = resp
        response.reason = getattr(resp, 'reason', '')

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

        # Add new cookies from the server
        headerDict = HTTPHeaderDict(response.headers)
        response.cookies.extract_cookies(MockResponse(headerDict),
                                         MockRequest(req))
        self.cookies.extract_cookies(MockResponse(headerDict),
                                     MockRequest(req))

        response.request = req
        # response.connection = self

        return response
Пример #2
0
    def _bake_cookies(self):
        resp = self._get_resp(self.path)
        cookie_match = self.COOKIE_RE.search(resp.text)
        if not cookie_match:
            print('No apparent initialization cookie')
            return

        attrs = parse_ns_headers([cookie_match[1]])

        jar: RequestsCookieJar = self._rsession.cookies
        req = MockRequest(resp.request)
        cookies = jar._cookies_from_attrs_set(attrs, req)
        for cookie in cookies:
            jar.set_cookie(cookie)

        print('Cookies set: ' + ', '.join(jar.keys()))
Пример #3
0
def _extract_cookies(request, response, cookies):
    """Add cookies to the response.

    Cookies in requests are extracted from the headers in the original_response
    httplib.HTTPMessage which we don't create so we have to do this step
    manually.
    """
    # This will add cookies set manually via the Set-Cookie or Set-Cookie2
    # header but this only allows 1 cookie to be set.
    http_message = compat._FakeHTTPMessage(response.headers)
    response.cookies.extract_cookies(MockResponse(http_message),
                                     MockRequest(request))

    # This allows you to pass either a CookieJar or a dictionary to request_uri
    # or directly to create_response. To allow more than one cookie to be set.
    if cookies:
        merge_cookies(response.cookies, cookies)
Пример #4
0
    def extract_cookies_to_jar(self, request, response):
        """Extract the cookies from the response into a CookieJar.

        :param jar: cookielib.CookieJar (not necessarily a RequestsCookieJar)
        :param request: our own requests.Request object
        :param response: urllib3.HTTPResponse object
        """
        # if not (hasattr(response, '_original_response') and
        #         response._original_response):
        #     return
        # the _original_response field is the wrapped httplib.HTTPResponse object,
        req = MockRequest(request)
        # pull out the HTTPMessage with the headers and put it in the mock:
        # res = MockResponse(response._original_response.msg)
        headers = response
        if not hasattr(headers, "keys"):
            headers = headers.headers
        headers.getheaders = headers.get_list
        res = MockResponse(headers)
        self.extract_cookies(res, req)
Пример #5
0
def extract_cookies_to_jar(jar, request, response):
    req = MockRequest(request)
    res = MockResponse(response)
    jar.extract_cookies(res, req)
Пример #6
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)
Пример #7
0
    def resolve_redirects(self,
                          resp,
                          req,
                          stream=False,
                          timeout=None,
                          verify=True,
                          cert=None,
                          proxies=None,
                          yield_requests=False,
                          **adapter_kwargs):
        """Receives a Response. Returns a generator of Responses or Requests."""

        hist = []  # keep track of history

        url = self.get_redirect_target(resp)
        while url:
            prepared_request = req.copy()

            # Update history and keep track of redirects.
            # resp.history must ignore the original request in this loop
            hist.append(resp)
            resp.history = hist[1:]

            # Consume socket so it can be released
            resp.content

            if self.max_redirects <= len(resp.history):
                raise TooManyRedirects('Exceeded %s redirects.' %
                                       self.max_redirects,
                                       response=resp)

            # Release the connection
            resp.close()

            # Handle redirection without scheme (see: RFC 1808 Section 4)
            if url.startswith('//'):
                parsed_rurl = urlparse(resp.url)
                url = '%s:%s' % (to_native_string(parsed_rurl.scheme), url)

            # The scheme should be lower case...
            parsed = urlparse(url)
            url = parsed.geturl()

            # Facilitate relative 'location' headers, as allowed by RFC 7231.
            # (e.g. '/path/to/resource' instead of 'http://domain.tld/path/to/resource')
            # Compliant with RFC3986, we percent encode the url.
            if not parsed.netloc:
                url = urljoin(resp.url, requote_uri(url))
            else:
                url = requote_uri(url)

            prepared_request.url = to_native_string(url)

            self.rebuild_method(prepared_request, resp)

            # https://github.com/requests/requests/issues/1084
            if resp.status_code not in (codes.temporary_redirect,
                                        codes.permanent_redirect):
                # https://github.com/requests/requests/issues/3490
                purged_headers = ('Content-Length', 'Content-Type',
                                  'Transfer-Encoding')
                for header in purged_headers:
                    prepared_request.headers.pop(header, None)
                prepared_request.body = None

            headers = prepared_request.headers
            try:
                del headers['Cookie']
            except KeyError:
                pass

            # Extract any cookies sent on the response to the cookiejar
            # in the new request. Because we've mutated our copied prepared
            # request, use the old one that we haven't yet touched.
            prepared_request._cookies.extract_cookies(
                MockResponse(HTTPHeaderDict(resp.headers)), MockRequest(req))
            merge_cookies(prepared_request._cookies, self.cookies)
            prepared_request.prepare_cookies(prepared_request._cookies)

            # Rebuild auth and proxy information.
            proxies = self.rebuild_proxies(prepared_request, proxies)
            self.rebuild_auth(prepared_request, resp)

            # Override the original request.
            req = prepared_request
            req.adapt_prepare()

            if yield_requests:
                yield req
            else:
                resp = self.send(req,
                                 stream=stream,
                                 timeout=timeout,
                                 verify=verify,
                                 cert=cert,
                                 proxies=proxies,
                                 allow_redirects=False,
                                 **adapter_kwargs)

                yield resp

                while not resp.done():
                    yield resp
                resp = resp.result()

                self.cookies.extract_cookies(
                    MockResponse(HTTPHeaderDict(resp.headers)),
                    MockRequest(prepared_request))

                # extract redirect url, if any, for the next loop
                url = self.get_redirect_target(resp)
Пример #8
0
 def _unsecure_cookie(self, url_str, response):
     url = urlparse(url_str)
     if url.scheme == 'http':
         for cookie in response.cookies:
             cookie.secure = False
             self.cookies.set_cookie_if_ok(cookie, MockRequest(response.request))
Пример #9
0
 def _extract_cookies(self, session, request, raw_response, response):
     mocked_response = _MockResponse(raw_response)
     mocked_request = MockRequest(request)
     for obj in (response, session):
         obj.cookies.extract_cookies(mocked_response, mocked_request)
Пример #10
0
 def get_cookie_header(self, request):
     """Produce an appropriate Cookie header string to be sent with `request`, or None."""
     r = MockRequest(request)
     self.add_cookie_header(r)
     return r.get_new_headers().get('Cookie')
Пример #11
0
def extract_cookies_to_jar(jar, request, response):
    req = MockRequest(request)
    res = MockResponse(response)
    reveal_type(jar)