From 721966b08e812a014838af5d5b6b3cc07463ca08 Mon Sep 17 00:00:00 2001 From: Abe Tomoaki Date: Wed, 18 Sep 2024 08:45:28 +0900 Subject: [PATCH 1/2] Fix `Location` replacement Fix `$backend_url` as it is not replaced as expected depending on the value of it. For example, the result of the replacement may be `//path/to/`. Starting with two slashes does not redirect as expected. --- no.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/no.php b/no.php index 4417084..06af87f 100644 --- a/no.php +++ b/no.php @@ -143,7 +143,7 @@ function build_multipart_data_files($delimiter, $fields, $files) { if ( !preg_match( '/^Transfer-Encoding:/i', $header ) ) { if ( preg_match( '/^Location:/i', $header ) ) { # rewrite absolute local redirects to relative ones - $header = str_replace($backend_url, "/", $header); + $header = str_replace(rtrim($backend_url, '/'), '', $header); } else if ( preg_match( '/^set-cookie:/i', $header ) ) { # replace original domain name in Set-Cookie headers with our server's domain From 6017c724957fbc6372a189c1ce184f45ecd9b222 Mon Sep 17 00:00:00 2001 From: Abe Tomoaki Date: Mon, 22 Sep 2025 14:23:15 +0900 Subject: [PATCH 2/2] Improve Location header replacement --- no.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/no.php b/no.php index 06af87f..f0ae424 100644 --- a/no.php +++ b/no.php @@ -110,6 +110,11 @@ function build_multipart_data_files($delimiter, $fields, $files) { return $data; } +function get_base_url($parsed_url) { + $port = isset($parsed_url['port']) ? ":$parsed_url[port]" : ''; + return "$parsed_url[scheme]://$parsed_url[host]$port"; +} + $curl = curl_init( $url ); curl_setopt( $curl, CURLOPT_HTTPHEADER, getRequestHeaders() ); curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true ); # follow redirects @@ -143,7 +148,7 @@ function build_multipart_data_files($delimiter, $fields, $files) { if ( !preg_match( '/^Transfer-Encoding:/i', $header ) ) { if ( preg_match( '/^Location:/i', $header ) ) { # rewrite absolute local redirects to relative ones - $header = str_replace(rtrim($backend_url, '/'), '', $header); + $header = str_replace(get_base_url($backend_info), '', $header); } else if ( preg_match( '/^set-cookie:/i', $header ) ) { # replace original domain name in Set-Cookie headers with our server's domain