From aaec817480cb3731c4d44a25e693ac7ec9c9791c Mon Sep 17 00:00:00 2001 From: pezcurrel Date: Sat, 26 Oct 2024 23:09:44 +0200 Subject: [PATCH] =?UTF-8?q?Now=20it=20always=20returns=20=E2=80=9Creal=20h?= =?UTF-8?q?eader=E2=80=9D,=20that=20is=20the=20last=20one,=20after=20the?= =?UTF-8?q?=20possible=20=E2=80=9C100=E2=80=9D=20or=20=E2=80=9C302?= =?UTF-8?q?=E2=80=9D=20http=20headers,=20or=20the=20likes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/curl.php | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/curl.php b/lib/curl.php index c561c73..1835481 100644 --- a/lib/curl.php +++ b/lib/curl.php @@ -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)) {