Skip to content

Commit ed24e54

Browse files
committed
Corrected timeout exit status
1 parent 65703a5 commit ed24e54

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

proxy.php

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,20 @@
99

1010
// Check that we have a URL
1111
if( ! $url)
12-
http_response_code(400) and exit("X-Proxy-Url header missing");
12+
failure(400, "X-Proxy-Url header missing");
1313

1414
// Check that the URL looks like an absolute URL
1515
if( ! parse_url($url, PHP_URL_SCHEME))
16-
http_response_code(403) and exit("Not an absolute URL: $url");
16+
failure(403, "Not an absolute URL: $url");
1717

1818
// Check referer hostname
1919
if( ! parse_url($headers['Referer'] ?? null, PHP_URL_HOST) == $_SERVER['HTTP_HOST'])
20-
http_response_code(403) and exit("Invalid referer");
20+
failure(403, "Invalid referer");
2121

2222
// Check whitelist, if not empty
2323
if( ! array_reduce($whitelist ?? [], 'is_bad', [$url, false]))
24-
http_response_code(403) and exit("Not whitelisted: $url");
24+
failure(403, "Not whitelisted: $url");
25+
2526

2627

2728
// Remove ignored headers and prepare the rest for resending
@@ -32,6 +33,8 @@
3233
foreach($headers as $key => &$value)
3334
$value = ucwords($key, '-').": $value";
3435

36+
37+
3538
// Init curl
3639
$curl = curl_init();
3740
$maxredirs = $opts[CURLOPT_MAXREDIRS] ?? 20;
@@ -79,12 +82,17 @@
7982
if(curl_errno($curl))
8083
switch(curl_errno($curl))
8184
{
85+
// Connect timeout => Service Unavailable
8286
case 7:
87+
failure(503, $curl);
88+
89+
// Operation timeout => Gateway Timeout
8390
case 28:
84-
http_response_code(504);
91+
failure(504, $curl);
8592

93+
// Other errors => Service Unavailable
8694
default:
87-
exit(curl_error($curl));
95+
failure(503, $curl);
8896
}
8997

9098
// HACK: If for any reason redirection doesn't work, do it manually...
@@ -93,11 +101,13 @@
93101
while($url and --$maxredirs > 0);
94102

95103

104+
96105
// Get curl info and close handler
97106
$info = curl_getinfo($curl);
98107
curl_close($curl);
99108

100109

110+
101111
// Remove any existing headers
102112
header_remove();
103113

@@ -125,6 +135,14 @@
125135

126136

127137

138+
function failure(int $status, $text)
139+
{
140+
if(is_resource($text))
141+
$text = curl_error($text);
142+
http_response_code($status);
143+
exit($text);
144+
}
145+
128146
function is_bad($carry, array $rule): bool
129147
{
130148
static $url;

0 commit comments

Comments
 (0)