示例#1
0
    def update_request_for_redirect(self, request: Request,
                                    response: Response):
        context = request.context  # type: ClientRequestContext
        status = response.status

        if status == 301 or status == 302:
            if self.non_standard_handling_of_301_302_redirect_method:
                # Change original request method to GET (Browser-like)
                request.method = b'GET'

        if status == 303:
            # 303 See Other
            # Change original request method to GET
            request.method = b'GET'

        location = self.extract_redirect_location(response)
        redirect_url = self.get_redirect_url(request, location)

        if redirect_url.schema.lower() not in {b'http', b'https'}:
            raise UnsupportedRedirect()

        self.validate_redirect(redirect_url, response, context)

        if status == 301 or status == 308:
            self._permanent_redirects_urls[request.url.value] = redirect_url

        request.url = redirect_url
示例#2
0
    def update_request_for_redirect(self, request: Request,
                                    response: Response) -> None:
        context: ClientRequestContext = getattr(
            request, "context", ClientRequestContext(request, self.cookie_jar))
        status = response.status

        if status == 301 or status == 302:
            if self.non_standard_handling_of_301_302_redirect_method:
                # Change original request method to GET (Browser-like)
                request.method = "GET"

        if status == 303:
            # 303 See Other
            # Change original request method to GET
            request.method = "GET"

        location = self.extract_redirect_location(response)
        redirect_url = self.get_redirect_url(request, location)

        self.validate_redirect(redirect_url, response, context)

        if status == 301 or status == 308:
            self._permanent_redirects_urls[request.url.value] = redirect_url

        request.url = redirect_url
示例#3
0
 def _validate_request_url(self, request: Request):
     if not request.url.is_absolute:
         if self.base_url:
             request.url = URL(self._get_url_without_params(request.url))
         else:
             raise ValueError(
                 'request.url must be a complete, absolute URL. Either provide a base_url '
                 'for the client, or specify a full URL for the request.')
示例#4
0
def test_request_can_update_url(initial_url, new_url):
    request = Request("GET", initial_url, None)

    assert request.url.value == initial_url

    request.url = URL(new_url)

    assert request.url.value == new_url
示例#5
0
 def _validate_request_url(self, request: Request):
     if not request.url.is_absolute:
         if self.base_url:
             request.url = URL(self.get_url_value(request.url))
         else:
             raise ValueError(
                 "request.url must be a complete, absolute URL. "
                 "Either provide a base_url "
                 "for the client, or specify a full URL for the request.")
示例#6
0
 def check_permanent_redirects(self, request: Request) -> None:
     if (self._permanent_redirects_urls is not None
             and request.url.value in self._permanent_redirects_urls):
         redirect_url = self._permanent_redirects_urls[request.url.value]
         if redirect_url is not None:
             request.url = redirect_url