0, up to 5 hops. * Browsers never follow the redirects, so 301 responses are never cached in the browser. */ class RedirectTester { const MAX_HOPS = 5; /** * Turn a path or URL into an absolute same-site URL for testing. * * @param string $url Path or URL. * @return string */ public static function absolute_url( $url ) { $url = trim( (string) $url ); if ( preg_match( '#^https?://#i', $url ) ) { return $url; } return home_url( '/' . ltrim( Normalizer::strip_home_path( $url ), '/' ) ); } /** * Trace a URL through HTTP HEAD requests. * * @param string $initial_url Path or URL to test. * @return array{hops: array, final_url: string, final_status: int, simulated: bool, notice?: string} */ public static function test_url( $initial_url ) { $hops = array(); $current_url = self::absolute_url( $initial_url ); if ( ! Normalizer::is_same_site( $current_url ) ) { return array( 'hops' => array(), 'final_url' => $current_url, 'final_status' => 0, 'simulated' => false, 'notice' => __( 'Only URLs on this site can be tested.', 'slugh' ), ); } for ( $i = 0; $i < self::MAX_HOPS; $i++ ) { $response = wp_remote_head( $current_url, array( 'timeout' => 10, 'redirection' => 0, // Same-site request; follow core's loopback SSL setting. 'sslverify' => apply_filters( 'https_local_ssl_verify', false, $current_url ), // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Core hook. 'user-agent' => 'SlugH Redirect Tester/' . SLUGH_VERSION, ) ); if ( is_wp_error( $response ) ) { $sim = self::simulate_internal( $current_url ); return array( 'hops' => array_merge( $hops, $sim['hops'] ), 'final_url' => $sim['final_url'], 'final_status' => $sim['final_status'], 'simulated' => true, 'notice' => sprintf( /* translators: %s: HTTP error message */ __( 'This host refused the loopback request (%s). The result below is a simulation that resolves slugH rules internally; it does not prove what the server sends.', 'slugh' ), $response->get_error_message() ), ); } $status_code = (int) wp_remote_retrieve_response_code( $response ); $location = (string) wp_remote_retrieve_header( $response, 'location' ); $hops[] = array( 'url' => $current_url, 'status' => $status_code, 'location' => '' !== $location ? $location : null, 'by' => (string) wp_remote_retrieve_header( $response, 'x-redirect-by' ), ); if ( ! in_array( $status_code, array( 301, 302, 303, 307, 308 ), true ) || '' === $location ) { return array( 'hops' => $hops, 'final_url' => $current_url, 'final_status' => $status_code, 'simulated' => false, ); } $current_url = 0 === strpos( $location, '/' ) ? home_url( Normalizer::strip_home_path( $location ) ) : $location; if ( ! Normalizer::is_same_site( $current_url ) ) { break; } } return array( 'hops' => $hops, 'final_url' => $current_url, 'final_status' => 0, 'simulated' => false, 'notice' => count( $hops ) >= self::MAX_HOPS ? __( 'Stopped after 5 hops.', 'slugh' ) : __( 'The chain leaves this site; it was not followed further.', 'slugh' ), ); } /** * Resolve SlugH rules internally when loopback requests fail. Labeled as a simulation. * * @param string $url URL to simulate. * @return array{hops: array, final_url: string, final_status: int} */ private static function simulate_internal( $url ) { $hops = array(); $current_url = $url; $handler = new RedirectHandler(); for ( $i = 0; $i < self::MAX_HOPS; $i++ ) { $canonical = Normalizer::normalize_path( $current_url ); $redirect = RedirectRepository::find_by_hash( Normalizer::compute_hash( $canonical ), $canonical ); $live_id = url_to_postid( $current_url ); if ( ! $redirect || ( '404_only' === $redirect->match_mode && $live_id ) ) { $hops[] = array( 'url' => $current_url, 'status' => $live_id || '/' === $canonical ? 200 : 404, 'location' => null, 'by' => 'simulation', ); return array( 'hops' => $hops, 'final_url' => $current_url, 'final_status' => $live_id || '/' === $canonical ? 200 : 404, ); } $status = (int) $redirect->status_code; if ( 410 === $status ) { $hops[] = array( 'url' => $current_url, 'status' => 410, 'location' => null, 'by' => 'simulation', ); return array( 'hops' => $hops, 'final_url' => $current_url, 'final_status' => 410, ); } $dest_url = $handler->build_destination_url( $redirect->destination, $redirect->destination_type, $current_url ); $hops[] = array( 'url' => $current_url, 'status' => $status, 'location' => $dest_url, 'by' => 'simulation', ); $current_url = $dest_url; } return array( 'hops' => $hops, 'final_url' => $current_url, 'final_status' => 0, ); } /** * Whether a trace reached the expected URL in exactly one redirect hop. * * @param array $trace Result of test_url(). * @param string $expected_url Expected destination URL. * @return bool */ public static function reaches_in_one_hop( array $trace, $expected_url ) { if ( count( $trace['hops'] ) < 1 || empty( $trace['hops'][0]['location'] ) ) { return false; } $first_hop_target = 0 === strpos( $trace['hops'][0]['location'], '/' ) ? home_url( $trace['hops'][0]['location'] ) : $trace['hops'][0]['location']; return Normalizer::normalize_path( $first_hop_target ) === Normalizer::normalize_path( $expected_url ) && in_array( (int) $trace['hops'][0]['status'], array( 301, 302, 307, 308 ), true ); } }