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

@ -73,6 +73,21 @@ function getArtifacts($job,$slug){
return getJson($url,getTokenHeaders(),true); return getJson($url,getTokenHeaders(),true);
} }
function getArtifactsForPipeline($pipeline,$wf=workflowName,$job=jobName){
$jstat=getJobStatus($pipeline,$wf,$job);
if (! isset($jstat['job_number'])){
throw new Exception("no job number");
}
if (! isset($jstat['status'])){
throw new Exception("no job status");
}
if ($jstat['status'] != 'success'){
throw new Exception("invalid job status ".$jstat['status']);
}
$astat=getArtifacts($jstat['job_number'],$jstat['project_slug']);
return $astat;
}
try {
if (isset($_REQUEST['api'])) { if (isset($_REQUEST['api'])) {
$action = $_REQUEST['api']; $action = $_REQUEST['api'];
header("Content-Type: application/json"); header("Content-Type: application/json");
@ -99,17 +114,7 @@ if (isset($_REQUEST['api'])){
array('workflow' => workflowName, 'job' => jobName) array('workflow' => workflowName, 'job' => jobName)
); );
try { try {
$jstat=getJobStatus($par['pipeline'], $par['workflow'], $par['job']); $astat = getArtifactsForPipeline($par['pipeline'], $par['workflow'], $par['job']);
if (! isset($jstat['project_slug'])){
throw new Exception("no project_slug in job");
}
if (! isset($jstat['status'])){
throw new Exception("no job status");
}
if ($jstat['status'] != 'success'){
throw new Exception("invalid job status ".$jstat['status']);
}
$astat=getArtifacts($jstat['job_number'],$jstat['project_slug']);
echo (json_encode($astat)); echo (json_encode($astat));
} catch (Exception $e) { } catch (Exception $e) {
echo (json_encode(array('status' => 'error', 'error' => $e->getMessage()))); echo (json_encode(array('status' => 'error', 'error' => $e->getMessage())));
@ -118,5 +123,29 @@ if (isset($_REQUEST['api'])){
} }
die("invalid api $action"); die("invalid api $action");
} }
if (isset($_REQUEST['download'])) {
$pipeline = $_REQUEST['download'];
$par = array('pipeline' => $pipeline);
addVars(
$par,
['workflow', 'job'],
array('workflow' => workflowName, 'job' => jobName)
);
$astat = getArtifactsForPipeline($par['pipeline'], $par['workflow'], $par['job']);
if (!isset($astat['items']) || count($astat['items']) < 1) {
die("no artifacts for job");
}
$dlurl = $astat['items'][0]['url'];
#echo("DL: $dlurl\n");
proxy($dlurl);
exit(0);
}
die("no action"); die("no action");
} catch (HTTPErrorException $h) {
header($_SERVER['SERVER_PROTOCOL'] . " " . $h->code . " " . $h->getMessage());
die($h->getMessage());
} catch (Exception $e) {
header($_SERVER['SERVER_PROTOCOL'] . ' 500 ' . $e->getMessage());
die($e->getMessage());
}
?> ?>

View File

@ -39,6 +39,7 @@ function addVars(&$vars,$names,$defaults=null){
function curl_exec_follow(/*resource*/ $ch, /*int*/ &$maxredirect = null) { function curl_exec_follow(/*resource*/ $ch, /*int*/ &$maxredirect = null) {
$mr = $maxredirect === null ? 5 : intval($maxredirect); $mr = $maxredirect === null ? 5 : intval($maxredirect);
#echo("###handling redirects $mr\n");
if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off') && false) { if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off') && false) {
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $mr > 0); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $mr > 0);
curl_setopt($ch, CURLOPT_MAXREDIRS, $mr); 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); $newurl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
$rch = curl_copy_handle($ch); $rch = curl_copy_handle($ch);
curl_setopt($rch, CURLOPT_HEADER, true); 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_FORBID_REUSE, false);
curl_setopt($rch, CURLOPT_RETURNTRANSFER, true); curl_setopt($rch, CURLOPT_RETURNTRANSFER, true);
do { do {
#echo("###trying $newurl\n");
curl_setopt($rch, CURLOPT_URL, $newurl); curl_setopt($rch, CURLOPT_URL, $newurl);
curl_setopt($ch, CURLOPT_URL, $newurl);
$header = curl_exec($rch); $header = curl_exec($rch);
if (curl_errno($rch)) { if (curl_errno($rch)) {
$code = 0; $code = 0;
} else { } else {
$code = curl_getinfo($rch, CURLINFO_HTTP_CODE); $code = curl_getinfo($rch, CURLINFO_HTTP_CODE);
#echo("###code=$code\n");
if ($code == 301 || $code == 302) { if ($code == 301 || $code == 302) {
preg_match('/Location:(.*?)\n/', $header, $matches); preg_match('/Location:(.*?)\n/', $header, $matches);
$newurl = trim(array_pop($matches)); $newurl = trim(array_pop($matches));
} else { } else {
if ($code >= 300){
trigger_error("HTTP error $code");
}
$code = 0; $code = 0;
} }
} }
@ -98,7 +105,7 @@ function curl_exec_follow(/*resource*/ $ch, /*int*/ &$maxredirect = null) {
return curl_exec($ch); return curl_exec($ch);
} }
function setFw($curl,$aheaders=null){ function getFwHeaders($aheaders=null){
$headers=getallheaders(); $headers=getallheaders();
$FWHDR = ['User-Agent']; $FWHDR = ['User-Agent'];
$outHeaders = array(); $outHeaders = array();
@ -112,41 +119,98 @@ function setFw($curl,$aheaders=null){
array_push($outHeaders,"$hk: $hv"); 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 = curl_init();
curl_setopt($curl, CURLOPT_URL,$url); curl_setopt($curl, CURLOPT_URL,$url);
curl_setopt($curl,CURLOPT_RETURNTRANSFER, true); curl_setopt($curl,CURLOPT_RETURNTRANSFER, true);
setFw($curl,$headers); curl_setopt($curl, CURLOPT_HTTPHEADER, getFwHeaders($headers));
$response = curl_exec($curl); $response = curl_exec($curl);
$httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE); $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
#echo("curl exec for $url:$response:$httpcode\n"); #echo("curl exec for $url:$response:$httpcode\n");
if($e = curl_error($curl)) { if($e = curl_error($curl)) {
curl_close($curl); curl_close($curl);
if ($doThrow) throw new Exception($e);
return array('error'=>$e); return array('error'=>$e);
} else { } else {
if ($httpcode >= 300){ if ($httpcode >= 300){
curl_close($curl); curl_close($curl);
if ($doThrow) throw new Exception("HTTP error $httpcode");
return array('error'=>"HTTP code ".$httpcode); return array('error'=>"HTTP code ".$httpcode);
} }
curl_close($curl); curl_close($curl);
return json_decode($response, true); 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) function proxy($url)
{ {
$ch = curl_init($url); header('Access-Control-Allow-Origin:*');
curl_setopt_array( return proxy_impl($url,30,getFwHeaders());
$ch,
[
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => 30,
]
);
setFw($ch);
$response = curl_exec_follow($ch);
curl_close($ch);
} }
?> ?>

View File

@ -4,7 +4,7 @@
$api = "https://api.github.com/repos/#user#/#repo#/releases/latest"; $api = "https://api.github.com/repos/#user#/#repo#/releases/latest";
$download = "https://github.com/#user#/#repo#/releases/download/#dlVersion#/#dlName#"; $download = "https://github.com/#user#/#repo#/releases/download/#dlVersion#/#dlName#";
$manifest = "?dlName=#mName#&dlVersion=#mVersion#&user=#user#&repo=#repo#"; $manifest = "?dlName=#mName#&dlVersion=#mVersion#&user=#user#&repo=#repo#";
try {
if (isset($_REQUEST['api'])) { if (isset($_REQUEST['api'])) {
$vars = fillUserAndRepo(); $vars = fillUserAndRepo();
proxy(replaceVars($api, $vars)); proxy(replaceVars($api, $vars));
@ -23,19 +23,29 @@
$targetUrl = null; $targetUrl = null;
$targetBase = $_REQUEST['flash']; $targetBase = $_REQUEST['flash'];
$mode = 'all'; $mode = 'all';
if (isset($_REQUEST['update'])) $mode='update'; if (isset($_REQUEST['update']))
$mode = 'update';
$lb = strlen($targetBase); $lb = strlen($targetBase);
foreach ($assets as &$asset) { foreach ($assets as &$asset) {
if (substr($asset['name'], 0, $lb) == $targetBase) { if (substr($asset['name'], 0, $lb) == $targetBase) {
if (! preg_match("/-$mode.bin/",$asset['name'])) continue; if (!preg_match("/-$mode.bin/", $asset['name']))
continue;
$targetUrl = $asset['browser_download_url']; $targetUrl = $asset['browser_download_url'];
break; break;
} }
} }
if (! $targetUrl) die("unable to find $targetBase $mode\n"); if (!$targetUrl)
die("unable to find $targetBase $mode\n");
#echo("download for $targetBase=$targetUrl\n"); #echo("download for $targetBase=$targetUrl\n");
proxy($targetUrl); proxy($targetUrl);
exit(0); exit(0);
} }
} catch (HTTPErrorException $h) {
header($_SERVER['SERVER_PROTOCOL'] . " " . $h->code . " " . $h->getMessage());
die($h->getMessage());
} catch (Exception $e) {
header($_SERVER['SERVER_PROTOCOL'] . ' 500 ' . $e->getMessage());
die($e->getMessage());
}
die("invalid request"); die("invalid request");
?> ?>