1
0
mirror of https://github.com/thooge/esp32-nmea2000-obp60.git synced 2025-12-13 05:53:06 +01:00

handle artifact download correctly

This commit is contained in:
andreas
2023-09-04 19:45:12 +02:00
parent abf87f1955
commit 0709e92b6f
3 changed files with 192 additions and 89 deletions

View File

@@ -39,6 +39,7 @@ function addVars(&$vars,$names,$defaults=null){
function curl_exec_follow(/*resource*/ $ch, /*int*/ &$maxredirect = null) {
$mr = $maxredirect === null ? 5 : intval($maxredirect);
#echo("###handling redirects $mr\n");
if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off') && false) {
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $mr > 0);
curl_setopt($ch, CURLOPT_MAXREDIRS, $mr);
@@ -48,20 +49,26 @@ function curl_exec_follow(/*resource*/ $ch, /*int*/ &$maxredirect = null) {
$newurl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
$rch = curl_copy_handle($ch);
curl_setopt($rch, CURLOPT_HEADER, true);
curl_setopt($rch, CURLOPT_NOBODY, true);
#curl_setopt($rch, CURLOPT_NOBODY, true);
curl_setopt($rch, CURLOPT_FORBID_REUSE, false);
curl_setopt($rch, CURLOPT_RETURNTRANSFER, true);
do {
#echo("###trying $newurl\n");
curl_setopt($rch, CURLOPT_URL, $newurl);
curl_setopt($ch, CURLOPT_URL, $newurl);
$header = curl_exec($rch);
if (curl_errno($rch)) {
$code = 0;
} else {
$code = curl_getinfo($rch, CURLINFO_HTTP_CODE);
#echo("###code=$code\n");
if ($code == 301 || $code == 302) {
preg_match('/Location:(.*?)\n/', $header, $matches);
$newurl = trim(array_pop($matches));
} else {
if ($code >= 300){
trigger_error("HTTP error $code");
}
$code = 0;
}
}
@@ -98,7 +105,7 @@ function curl_exec_follow(/*resource*/ $ch, /*int*/ &$maxredirect = null) {
return curl_exec($ch);
}
function setFw($curl,$aheaders=null){
function getFwHeaders($aheaders=null){
$headers=getallheaders();
$FWHDR = ['User-Agent'];
$outHeaders = array();
@@ -112,41 +119,98 @@ function setFw($curl,$aheaders=null){
array_push($outHeaders,"$hk: $hv");
}
}
curl_setopt($curl, CURLOPT_HTTPHEADER, $outHeaders);
return $outHeaders;
}
function getJson($url,$headers=null){
function getJson($url,$headers=null,$doThrow=false){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$url);
curl_setopt($curl,CURLOPT_RETURNTRANSFER, true);
setFw($curl,$headers);
curl_setopt($curl, CURLOPT_HTTPHEADER, getFwHeaders($headers));
$response = curl_exec($curl);
$httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
#echo("curl exec for $url:$response:$httpcode\n");
if($e = curl_error($curl)) {
curl_close($curl);
if ($doThrow) throw new Exception($e);
return array('error'=>$e);
} else {
if ($httpcode >= 300){
curl_close($curl);
if ($doThrow) throw new Exception("HTTP error $httpcode");
return array('error'=>"HTTP code ".$httpcode);
}
curl_close($curl);
return json_decode($response, true);
}
}
class HTTPErrorException extends Exception{
public $code=0;
public function __construct($c,$text){
parent::__construct($text);
$this->code=$c;
}
};
function proxy_impl($url, $timeout=30,$headers=null,$num = 5)
{
$nexturl=$url;
while ($num > 0 && $nexturl != null) {
$num--;
$code=0;
$ch = curl_init($nexturl);
$nexturl=null;
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_TIMEOUT,$timeout);
if ($headers != null){
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
}
curl_setopt(
$ch,
CURLOPT_HEADERFUNCTION,
function ($curl, $header) use(&$nexturl,&$code){
#echo ("###header:$header\n");
if ($code == 0){
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
}
#echo ("???code=$code\n");
if ($code == 301 || $code == 302) {
if(preg_match('/Location:(.*?)\n/', $header, $matches)){
$nexturl = trim(array_pop($matches));
#echo("???nexturl=$nexturl\n");
}
}
if ($code != 0 && $code < 300){
header($header);
}
return strlen($header);
}
);
curl_setopt(
$ch,
CURLOPT_WRITEFUNCTION,
function ($curl, $body) use(&$code) {
if ($code != 0 && $code < 300){
#echo ("### body part " . strlen($body)."\n");
echo $body;
return strlen($body);
}
return false;
}
);
$rs = curl_exec($ch);
#echo ("###code=$code\n");
curl_close($ch);
if ($nexturl == null){
if ($code != 200) throw new HTTPErrorException($code,"HTTP status $code");
return true;
}
}
throw new HTTPErrorException(500,"too many redirects");
}
function proxy($url)
{
$ch = curl_init($url);
curl_setopt_array(
$ch,
[
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => 30,
]
);
setFw($ch);
$response = curl_exec_follow($ch);
curl_close($ch);
header('Access-Control-Allow-Origin:*');
return proxy_impl($url,30,getFwHeaders());
}
?>