Now it always returns “real header”, that is the last one, after the possible “100” or “302” http headers, or the likes

This commit is contained in:
pezcurrel 2024-10-26 23:09:44 +02:00
parent 6751e11e69
commit aaec817480

View file

@ -55,6 +55,8 @@ function curl($url,$napid=null,$headers=null,$postdata=null,$conntimeout=null,$f
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_FAILONERROR,false);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
curl_setopt($ch,CURLOPT_MAXREDIRS,10);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$conntimeout);
curl_setopt($ch,CURLOPT_TIMEOUT,$functimeout);
curl_setopt($ch,CURLOPT_HEADER,true);
@ -78,8 +80,17 @@ function curl($url,$napid=null,$headers=null,$postdata=null,$conntimeout=null,$f
$gheaders=substr($res,0,$gheaders_sz-4);// "-4" accounts for the 2 "\r\n" that separates headers from body
// echo "{$gheaders}\n";
$gheaders=explode("\r\n",$gheaders);
// the line below is needed to discard from the top of the headers possible "HTTP/(version) 100 Continue" and the empty lines that may follow
while (trim($gheaders[0])=='' || preg_match('#^HTTP/\d+\.\d+\s+100\s+#',$gheaders[0])===1) array_shift($gheaders);
// code until "---" is to find the beginning of "real headers", that is: after all the possible "100" or "302" http headers, or the likes
$count=count($gheaders)-1;
$rhi=null;
for ($i=$count; $i>-1; $i--) {
if (trim($gheaders[$i])=='') {
$rhi=$i+1;
break;
}
}
if (!is_null($rhi)) $gheaders=array_slice($gheaders,$rhi);
// ---
$res=substr($res,$gheaders_sz);
$httpcode=preg_replace('#^\S+\s+(\d+).*$#','$1',$gheaders[0]);
if (!is_null($napid)) {