From 58043aa9dc3120291d59407ea33c0dd6b63c7cdf Mon Sep 17 00:00:00 2001 From: Rinat Khaziev Date: Thu, 1 Aug 2024 18:37:47 -0500 Subject: [PATCH 1/2] Prototype 50x response interceptor --- 5xx-interceptor.php | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 5xx-interceptor.php diff --git a/5xx-interceptor.php b/5xx-interceptor.php new file mode 100644 index 0000000000..a0d4bd9354 --- /dev/null +++ b/5xx-interceptor.php @@ -0,0 +1,40 @@ +handle_404() has already been called, potentially overriding any user 503 to 200. +add_action( 'parse_request', '\Automattic\VIP\rewrite_50x_headers', PHP_INT_MIN ); +function rewrite_50x_headers() { + $current_response_code = http_response_code(); + $override_codes = [ + 503 => [ + 'code' => 583, + 'description' => 'Service Unavailable', + ], + 502 => [ + 'code' => 582, + 'description' => 'Bad Gateway', + ], + ]; + + // Technically headers_sent() should never be true at this moment. + if ( ! headers_sent() && isset( $override_codes[ $current_response_code ] ) ) { + // 404 handler may override a 404 with a 200, so we need to ensure that the 404 handler is not triggered. + add_filter( 'pre_handle_404', '__return_true' ); + // The description is important for the core logic, otherwise the non-standard codes get discarded and replaced by 200. + // @see wp-includes/functions.php:status_header() + status_header( $override_codes[ $current_response_code ]['code'], $override_codes[ $current_response_code ]['description'] ); + } +} From 8144f28f3369183671c9e8d2996f7fd64af61b25 Mon Sep 17 00:00:00 2001 From: Rinat K Date: Fri, 11 Apr 2025 11:07:06 -0500 Subject: [PATCH 2/2] Update 5xx-interceptor.php, remove 502 to limit the scope --- 5xx-interceptor.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/5xx-interceptor.php b/5xx-interceptor.php index a0d4bd9354..107690aacf 100644 --- a/5xx-interceptor.php +++ b/5xx-interceptor.php @@ -23,10 +23,6 @@ function rewrite_50x_headers() { 'code' => 583, 'description' => 'Service Unavailable', ], - 502 => [ - 'code' => 582, - 'description' => 'Bad Gateway', - ], ]; // Technically headers_sent() should never be true at this moment.