'Too many redirects.', ); } /** * Normalized redirect identity: scheme/host/port/path/query, no fragment. * * @param string $url * * @return string */ private function redirect_key( $url ) { $parsed = wp_parse_url( $url ); if ( ! is_array( $parsed ) || empty( $parsed['host'] ) ) { return (string) $url; } $scheme = isset( $parsed['scheme'] ) ? strtolower( $parsed['scheme'] ) : 'http'; $host = Http::normalize_host( $parsed['host'] ); $port = isset( $parsed['port'] ) ? (int) $parsed['port'] : ( 'https' === $scheme ? 443 : 80 ); $path = isset( $parsed['path'] ) ? $parsed['path'] : ''; $query = isset( $parsed['query'] ) ? $parsed['query'] : ''; return $scheme . '://' . $host . ':' . $port . $path . ( '' !== $query ? '?' . $query : '' ); } /** * @param string $url * * @return string */ private function url_host( $url ) { $host = wp_parse_url( $url, PHP_URL_HOST ); return is_string( $host ) ? Http::normalize_host( $host ) : ''; } /** * Drop credential-bearing headers before a cross-host redirect hop. * * @param array $headers * * @return array */ private function strip_sensitive_headers( $headers ) { $sensitive = array( 'authorization', 'cookie' ); return array_values( array_filter( $headers, function ( $header ) use ( $sensitive ) { $name = strtolower( trim( strtok( $header, ':' ) ) ); return ! in_array( $name, $sensitive, true ); } ) ); } /** * Case-insensitive lookup of a response header value (fopen raw_headers). * * @param array $raw_headers * @param string $name * * @return string|null */ private function find_header( $raw_headers, $name ) { $name = strtolower( trim( $name ) ); foreach ( (array) $raw_headers as $header ) { $pos = strpos( $header, ':' ); if ( false !== $pos && strtolower( trim( substr( $header, 0, $pos ) ) ) === $name ) { return trim( substr( $header, $pos + 1 ) ); } } return null; } }