Home / Class/ SessionRedirectMixin Class — requests Architecture

SessionRedirectMixin Class — requests Architecture

Architecture documentation for the SessionRedirectMixin class in sessions.py from the requests codebase.

Entity Profile

Dependency Diagram

graph TD
  29cd946c_8c0d_e37c_acb9_dfb2b83bf42b["SessionRedirectMixin"]
  ea1101aa_233b_1206_7b38_a38f0fe92a52["sessions.py"]
  29cd946c_8c0d_e37c_acb9_dfb2b83bf42b -->|defined in| ea1101aa_233b_1206_7b38_a38f0fe92a52
  45e5de53_d23e_6c69_97ea_d5a29d1fcd7b["get_redirect_target()"]
  29cd946c_8c0d_e37c_acb9_dfb2b83bf42b -->|method| 45e5de53_d23e_6c69_97ea_d5a29d1fcd7b
  878c62b1_e60f_77fd_3028_6c51dffaeedf["should_strip_auth()"]
  29cd946c_8c0d_e37c_acb9_dfb2b83bf42b -->|method| 878c62b1_e60f_77fd_3028_6c51dffaeedf
  cbf6862f_d124_817f_3342_f1142c276f25["resolve_redirects()"]
  29cd946c_8c0d_e37c_acb9_dfb2b83bf42b -->|method| cbf6862f_d124_817f_3342_f1142c276f25
  18349e5e_3ab4_19be_23d5_20c1111b761b["rebuild_auth()"]
  29cd946c_8c0d_e37c_acb9_dfb2b83bf42b -->|method| 18349e5e_3ab4_19be_23d5_20c1111b761b
  37e21bd7_763f_f217_e84e_39590f4b9478["rebuild_proxies()"]
  29cd946c_8c0d_e37c_acb9_dfb2b83bf42b -->|method| 37e21bd7_763f_f217_e84e_39590f4b9478
  16c8903e_3ca3_7db8_eb77_e4f913e304f4["rebuild_method()"]
  29cd946c_8c0d_e37c_acb9_dfb2b83bf42b -->|method| 16c8903e_3ca3_7db8_eb77_e4f913e304f4

Relationship Graph

Source Code

src/requests/sessions.py lines 107–354

class SessionRedirectMixin:
    def get_redirect_target(self, resp):
        """Receives a Response. Returns a redirect URI or ``None``"""
        # Due to the nature of how requests processes redirects this method will
        # be called at least once upon the original response and at least twice
        # on each subsequent redirect response (if any).
        # If a custom mixin is used to handle this logic, it may be advantageous
        # to cache the redirect location onto the response object as a private
        # attribute.
        if resp.is_redirect:
            location = resp.headers["location"]
            # Currently the underlying http module on py3 decode headers
            # in latin1, but empirical evidence suggests that latin1 is very
            # rarely used with non-ASCII characters in HTTP headers.
            # It is more likely to get UTF8 header rather than latin1.
            # This causes incorrect handling of UTF8 encoded location headers.
            # To solve this, we re-encode the location in latin1.
            location = location.encode("latin1")
            return to_native_string(location, "utf8")
        return None

    def should_strip_auth(self, old_url, new_url):
        """Decide whether Authorization header should be removed when redirecting"""
        old_parsed = urlparse(old_url)
        new_parsed = urlparse(new_url)
        if old_parsed.hostname != new_parsed.hostname:
            return True
        # Special case: allow http -> https redirect when using the standard
        # ports. This isn't specified by RFC 7235, but is kept to avoid
        # breaking backwards compatibility with older versions of requests
        # that allowed any redirects on the same host.
        if (
            old_parsed.scheme == "http"
            and old_parsed.port in (80, None)
            and new_parsed.scheme == "https"
            and new_parsed.port in (443, None)
        ):
            return False

        # Handle default port usage corresponding to scheme.
        changed_port = old_parsed.port != new_parsed.port
        changed_scheme = old_parsed.scheme != new_parsed.scheme
        default_port = (DEFAULT_PORTS.get(old_parsed.scheme, None), None)
        if (
            not changed_scheme
            and old_parsed.port in default_port
            and new_parsed.port in default_port
        ):
            return False

        # Standard case: root URI must match
        return changed_port or changed_scheme

    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)
        previous_fragment = urlparse(req.url).fragment
        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:]

            try:
                resp.content  # Consume socket so it can be released

Domain

Frequently Asked Questions

What is the SessionRedirectMixin class?
SessionRedirectMixin is a class in the requests codebase, defined in src/requests/sessions.py.
Where is SessionRedirectMixin defined?
SessionRedirectMixin is defined in src/requests/sessions.py at line 107.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free