#2 better header analysis
This commit is contained in:
parent
b0da900d3f
commit
c47822f70e
1 changed files with 32 additions and 10 deletions
42
shaargem.php
42
shaargem.php
|
@ -122,11 +122,13 @@ function shaargem_dummy_translation()
|
|||
t('Provides Gemini protocol support.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Find capsule's title
|
||||
*/
|
||||
function fetch_iri_title($iri)
|
||||
{
|
||||
$ret = preg_match(",gemini://([\d\w\.\+\-@\:%._\~#=]+)/?,", $iri ,$domain);
|
||||
static $redirect_counter = 0;
|
||||
error_log("ret:".$ret);
|
||||
if ($ret)
|
||||
{
|
||||
$stream_context = stream_context_create([ 'ssl' => [
|
||||
|
@ -142,33 +144,53 @@ function fetch_iri_title($iri)
|
|||
} else {
|
||||
fwrite($fp, $iri."\r\n");
|
||||
$header = fgets($fp);
|
||||
error_log("headers:".$headers);
|
||||
$ret = preg_match(",^(\d\d) (.*)\r\n$,", $header, $headers);
|
||||
$status = $headers[1];
|
||||
$meta = $headers[2];
|
||||
error_log("status:".$status.", meta:".$meta);
|
||||
$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++;
|
||||
error_log("[shaargem] found status 30. redirecting to ".$meta." (".$redirect_counter."/".$max_redirects.")");
|
||||
$title[1] = fetch_iri_title($meta);
|
||||
}
|
||||
return $meta;
|
||||
}
|
||||
// status 2x ok
|
||||
elseif ($status[0] == 2)
|
||||
{
|
||||
$nblines = 1;
|
||||
while($line = fgets($fp)) {
|
||||
$ret = preg_match(",# (.*),",$line,$title);
|
||||
if ($title[0] or $nblines > 100 ) {
|
||||
error_log($nblines);
|
||||
break;
|
||||
}
|
||||
$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
|
||||
{
|
||||
error_log("[shaargem] invalid response:".$header);
|
||||
return "Invalid response:".$header;
|
||||
}
|
||||
fclose($fp);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue