MCPcopy Index your code
hub / github.com/RustPython/RustPython / redirect_request

Method redirect_request

Lib/urllib/request.py:621–654  ·  view source on GitHub ↗

Return a Request or None in response to a redirect. This is called by the http_error_30x methods when a redirection response is received. If a redirection should take place, return a new Request to allow http_error_30x to perform the redirect. Otherwise, raise HTTP

(self, req, fp, code, msg, headers, newurl)

Source from the content-addressed store, hash-verified

619 max_redirections = 10
620
621 def redirect_request(self, req, fp, code, msg, headers, newurl):
622 """Return a Request or None in response to a redirect.
623
624 This is called by the http_error_30x methods when a
625 redirection response is received. If a redirection should
626 take place, return a new Request to allow http_error_30x to
627 perform the redirect. Otherwise, raise HTTPError if no-one
628 else should try to handle this url. Return None if you can't
629 but another Handler might.
630 """
631 m = req.get_method()
632 if (not (code in (301, 302, 303, 307, 308) and m in ("GET", "HEAD")
633 or code in (301, 302, 303) and m == "POST")):
634 raise HTTPError(req.full_url, code, msg, headers, fp)
635
636 # Strictly (according to RFC 2616), 301 or 302 in response to
637 # a POST MUST NOT cause a redirection without confirmation
638 # from the user (of urllib.request, in this case). In practice,
639 # essentially all clients do redirect in this case, so we do
640 # the same.
641
642 # Be conciliant with URIs containing a space. This is mainly
643 # redundant with the more complete encoding done in http_error_302(),
644 # but it is kept for compatibility with other callers.
645 newurl = newurl.replace(' ', '%20')
646
647 CONTENT_HEADERS = ("content-length", "content-type")
648 newheaders = {k: v for k, v in req.headers.items()
649 if k.lower() not in CONTENT_HEADERS}
650 return Request(newurl,
651 method="HEAD" if m == "HEAD" else "GET",
652 headers=newheaders,
653 origin_req_host=req.origin_req_host,
654 unverifiable=True)
655
656 # Implementation note: To avoid the server sending us into an
657 # infinite loop, the request object needs to track what URLs we

Callers 2

http_error_302Method · 0.95

Calls 6

HTTPErrorClass · 0.90
RequestClass · 0.70
get_methodMethod · 0.45
replaceMethod · 0.45
itemsMethod · 0.45
lowerMethod · 0.45

Tested by 1