MCPcopy
hub / github.com/pallets/werkzeug / resolve_redirect

Method resolve_redirect

src/werkzeug/test.py:995–1056  ·  view source on GitHub ↗

Perform a new request to the location given by the redirect response to the previous request. :meta private:

(
        self, response: TestResponse, buffered: bool = False
    )

Source from the content-addressed store, hash-verified

993 return rv
994
995 def resolve_redirect(
996 self, response: TestResponse, buffered: bool = False
997 ) -> TestResponse:
998 """Perform a new request to the location given by the redirect
999 response to the previous request.
1000
1001 :meta private:
1002 """
1003 scheme, netloc, path, qs, anchor = urlsplit(response.location)
1004 builder = EnvironBuilder.from_environ(
1005 response.request.environ, path=path, query_string=qs
1006 )
1007
1008 to_name_parts = netloc.split(":", 1)[0].split(".")
1009 from_name_parts = builder.server_name.split(".")
1010
1011 if to_name_parts != [""]:
1012 # The new location has a host, use it for the base URL.
1013 builder.url_scheme = scheme
1014 builder.host = netloc
1015 else:
1016 # A local redirect with autocorrect_location_header=False
1017 # doesn't have a host, so use the request's host.
1018 to_name_parts = from_name_parts
1019
1020 # Explain why a redirect to a different server name won't be followed.
1021 if to_name_parts != from_name_parts:
1022 if to_name_parts[-len(from_name_parts) :] == from_name_parts:
1023 if not self.allow_subdomain_redirects:
1024 raise RuntimeError("Following subdomain redirects is not enabled.")
1025 else:
1026 raise RuntimeError("Following external redirects is not supported.")
1027
1028 path_parts = path.split("/")
1029 root_parts = builder.script_root.split("/")
1030
1031 if path_parts[: len(root_parts)] == root_parts:
1032 # Strip the script root from the path.
1033 builder.path = path[len(builder.script_root) :]
1034 else:
1035 # The new location is not under the script root, so use the
1036 # whole path and clear the previous root.
1037 builder.path = path
1038 builder.script_root = ""
1039
1040 # Only 307 and 308 preserve all of the original request.
1041 if response.status_code not in {307, 308}:
1042 # HEAD is preserved, everything else becomes GET.
1043 if builder.method != "HEAD":
1044 builder.method = "GET"
1045
1046 # Clear the body and the headers that describe it.
1047
1048 if builder.input_stream is not None:
1049 builder.input_stream.close()
1050 builder.input_stream = None
1051
1052 builder.content_type = None

Callers 1

openMethod · 0.95

Calls 4

openMethod · 0.95
from_environMethod · 0.80
closeMethod · 0.45
popMethod · 0.45

Tested by

no test coverage detected