correctly handle headers

This commit is contained in:
andreas 2023-08-24 14:07:42 +02:00
parent 56321d2bf7
commit ec4bf842ec
1 changed files with 48 additions and 10 deletions

View File

@ -116,10 +116,34 @@
); );
header('Access-Control-Allow-Origin:*'); header('Access-Control-Allow-Origin:*');
return curl_exec($ch); return curl_exec($ch);
}
function setFw($curl){
$headers=getallheaders();
$FWHDR = ['User-Agent'];
$outHeaders = array();
foreach ($FWHDR as $k) {
if (isset($headers[$k])) {
array_push($outHeaders, "$k: $headers[$k]");
}
}
curl_setopt($curl, CURLOPT_HTTPHEADER, $outHeaders);
}
function getJson($url){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$url);
curl_setopt($curl,CURLOPT_RETURNTRANSFER, true);
setFw($curl);
$response = curl_exec($curl);
if($e = curl_error($curl)) {
curl_close($curl);
return array('error'=>$e);
} else {
curl_close($curl);
return json_decode($response, true);
}
} }
function proxy($url) function proxy($url)
{ {
$headers=getallheaders();
$ch = curl_init($url); $ch = curl_init($url);
curl_setopt_array( curl_setopt_array(
$ch, $ch,
@ -128,14 +152,7 @@
CURLOPT_CONNECTTIMEOUT => 30, CURLOPT_CONNECTTIMEOUT => 30,
] ]
); );
$FWHDR = ['User-Agent']; setFw($ch);
$outHeaders = array();
foreach ($FWHDR as $k) {
if (isset($headers[$k])) {
array_push($outHeaders, "$k: $headers[$k]");
}
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $outHeaders);
$response = curl_exec_follow($ch); $response = curl_exec_follow($ch);
curl_close($ch); curl_close($ch);
} }
@ -151,5 +168,26 @@
proxy(replaceVars($download,$vars)); proxy(replaceVars($download,$vars));
exit(0); exit(0);
} }
if (isset($_REQUEST['flash'])){
$vars=fillUserAndRepo();
$json=getJson(replaceVars($api,$vars));
$assets=$json['assets'];
$targetUrl=null;
$targetBase=$_REQUEST['flash'];
$mode='all';
if (isset($_REQUEST['update'])) $mode='update';
$lb=strlen($targetBase);
foreach ($assets as &$asset){
if (substr($asset['name'],0,$lb) == $targetBase){
if (! preg_match("/-$mode.bin/",$asset['name'])) continue;
$targetUrl=$asset['browser_download_url'];
break;
}
}
if (! $targetUrl) die("unable to find $targetBase $mode\n");
#echo("download for $targetBase=$targetUrl\n");
proxy($targetUrl);
exit(0);
}
die("invalid request"); die("invalid request");
?> ?>