Skip to content

Commit da89536

Browse files
committed
Return 504 Gateway Timeout on proxy timeout
1 parent 9a05e60 commit da89536

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

proxy.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,15 @@
3737
$maxredirs = $opts[CURLOPT_MAXREDIRS] ?? 20;
3838
do
3939
{
40-
// Set generic options
41-
curl_setopt_array($curl, [
40+
// Set options
41+
curl_setopt_array($curl,
42+
[
4243
CURLOPT_URL => $url,
4344
CURLOPT_HTTPHEADER => $headers,
4445
CURLOPT_HEADER => true,
45-
] + ($opts??[]) + [
46+
]
47+
+ ($curl_opts??[]) +
48+
[
4649
CURLOPT_FOLLOWLOCATION => true,
4750
CURLOPT_MAXREDIRS => $maxredirs,
4851
]);
@@ -68,9 +71,22 @@
6871

6972
// Perform request
7073
ob_start();
71-
curl_exec($curl) or http_response_code(500) and exit(curl_error($curl));
74+
curl_exec($curl);
7275
$out = ob_get_clean();
7376

77+
// Light error handling
78+
// http://php.net/manual/en/curl.constants.php#117723
79+
if(curl_errno($curl))
80+
switch(curl_errno($curl))
81+
{
82+
case 7:
83+
case 28:
84+
http_response_code(504);
85+
86+
default:
87+
exit(curl_error($curl));
88+
}
89+
7490
// HACK: If for any reason redirection doesn't work, do it manually...
7591
$url = curl_getinfo($curl, CURLINFO_REDIRECT_URL);
7692
}

test/proxy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@
2626
}
2727

2828
// Call/Use the proxy
29-
Geekality\CrossOriginProxy::proxy($whitelist);
29+
Geekality\CrossOriginProxy::proxy($whitelist, [CURLOPT_TIMEOUT => 5]);

0 commit comments

Comments
 (0)