#2 better header analysis

This commit is contained in:
n 2021-02-28 01:31:44 +01:00
parent b0da900d3f
commit c47822f70e
Signed by: n
GPG key ID: E96086FC951DAE30

View file

@ -122,11 +122,13 @@ function shaargem_dummy_translation()
t('Provides Gemini protocol support.'); t('Provides Gemini protocol support.');
} }
/**
* Find capsule's title
*/
function fetch_iri_title($iri) function fetch_iri_title($iri)
{ {
$ret = preg_match(",gemini://([\d\w\.\+\-@\:%._\~#=]+)/?,", $iri ,$domain); $ret = preg_match(",gemini://([\d\w\.\+\-@\:%._\~#=]+)/?,", $iri ,$domain);
static $redirect_counter = 0; static $redirect_counter = 0;
error_log("ret:".$ret);
if ($ret) if ($ret)
{ {
$stream_context = stream_context_create([ 'ssl' => [ $stream_context = stream_context_create([ 'ssl' => [
@ -142,33 +144,53 @@ function fetch_iri_title($iri)
} else { } else {
fwrite($fp, $iri."\r\n"); fwrite($fp, $iri."\r\n");
$header = fgets($fp); $header = fgets($fp);
error_log("headers:".$headers);
$ret = preg_match(",^(\d\d) (.*)\r\n$,", $header, $headers); $ret = preg_match(",^(\d\d) (.*)\r\n$,", $header, $headers);
$status = $headers[1]; $status = $headers[1];
$meta = $headers[2]; $meta = $headers[2];
error_log("status:".$status.", meta:".$meta);
$max_redirects = 10; $max_redirects = 10;
if ($status[0] == 3 and $redirect_counter < $max_redirects)
error_log("status:".$status.", meta:".$meta);
// status 1x cannot be handled by shaarli, but input field can be used as title
if ($status[0] == 1)
{ {
$redirect_counter++; return $meta;
error_log("[shaargem] found status 30. redirecting to ".$meta." (".$redirect_counter."/".$max_redirects.")");
$title[1] = fetch_iri_title($meta);
} }
// status 2x ok
elseif ($status[0] == 2) elseif ($status[0] == 2)
{ {
$nblines = 1; $nblines = 1;
while($line = fgets($fp)) { while($line = fgets($fp)) {
$ret = preg_match(",# (.*),",$line,$title); $ret = preg_match(",# (.*),",$line,$title);
if ($title[0] or $nblines > 100 ) { if ($title[0] or $nblines > 100 ) {
error_log($nblines);
break; break;
} }
$nblines++; $nblines++;
} }
} }
// status 3x redirect
elseif ($status[0] == 3 and $redirect_counter < $max_redirects)
{
$redirect_counter++;
error_log("[shaargem] found status 30. redirecting to ".$meta." (".$redirect_counter."/".$max_redirects.")");
$title[1] = fetch_iri_title($meta);
}
// 4x 5x 6x are error statuses, should find a better way to display that
elseif ($status[0] == 4)
{
return 'Status '.$status.' (TEMPORARY FAILURE): '.$meta;
}
elseif ($status[0] == 5)
{
return 'Status '.$status.' (PERMANENT FAILURE): '.$meta;
}
elseif ($status[0] == 6)
{
return 'Status '.$status.' (CLIENT CERTIFICATE REQUIRED): '.$meta;
}
else else
{ {
error_log("[shaargem] invalid response:".$header); return "Invalid response:".$header;
} }
fclose($fp); fclose($fp);
} }