From ccfecf6bd1f6f8ff40d76269ce3be52d4367e866 Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Wed, 22 Nov 2023 17:41:42 +0100 Subject: catch non-20 response code --- index.php | 144 ++++++++++++++++++++++++++++++++------------------------------ 1 file changed, 74 insertions(+), 70 deletions(-) (limited to 'index.php') diff --git a/index.php b/index.php index c2bb48e..069e1bc 100644 --- a/index.php +++ b/index.php @@ -15,86 +15,90 @@ $url['query'] ??= ''; $client = new Client($geminiHost); $response = $client->request("$url[path]$url[query]"); -ob_start(); -$mime = explode(';', $response->getMeta())[0]; -if (strpos($mime, 'text/gemini') === 0) { - $parser = new Gemtext($response->getBody()); - $lines = $parser->parse(); +if ($response->getStatusCode()->value == 20) { + ob_start(); + $mime = explode(';', $response->getMeta())[0]; + if (strpos($mime, 'text/gemini') === 0) { + $parser = new Gemtext($response->getBody()); + $lines = $parser->parse(); - $wasListItem = false; - $currentListItems = []; - foreach ($lines as $line) { - if ($wasListItem && $line['type'] !== 'listitem') { - echo ""; - $wasListItem = false; - $currentListItems = []; - } - if ($line['type'] === 'preformatted_on') { - echo "
$line[alt]
";
-    }
-    else if ($line['type'] === 'preformatted') {
-      echo htmlentities($line['raw']);
-    }
-    else if ($line['type'] === 'preformatted_off') {
-      echo "
"; - } - else if ($line['type'] === 'heading') { - echo "$line[text]"; - } - else if ($line['type'] === 'listitem') { - $currentListItems[] = $line; - $wasListItem = true; - } - else if ($line['type'] === 'quote') { - echo "
$line[text]
"; - } - else if ($line['type'] === 'link') { - $line['link'] = str_replace(["gemini://$geminiHost", $geminiHost], "https://$geminiHost", $line['link']); - $line['text'] = $line['text'] ?: $line['link']; - echo "$line[text]"; - } - else if ($line['type'] === 'text') { - echo "

$line[text]

"; + if ($line['type'] === 'preformatted_on') { + echo "
$line[alt]
";
+      }
+      else if ($line['type'] === 'preformatted') {
+        echo htmlentities($line['raw']);
+      }
+      else if ($line['type'] === 'preformatted_off') {
+        echo "
"; + } + else if ($line['type'] === 'heading') { + echo "$line[text]"; + } + else if ($line['type'] === 'listitem') { + $currentListItems[] = $line; + $wasListItem = true; + } + else if ($line['type'] === 'quote') { + echo "
$line[text]
"; + } + else if ($line['type'] === 'link') { + $line['link'] = str_replace(["gemini://$geminiHost", $geminiHost], "https://$geminiHost", $line['link']); + $line['text'] = $line['text'] ?: $line['link']; + echo "$line[text]"; + } + else if ($line['type'] === 'text') { + echo "

$line[text]

"; + } } } -} -else if (strpos($mime, 'text') === 0) { - echo $response->getBody(); -} + else if (strpos($mime, 'text') === 0) { + echo $response->getBody(); + } -else if (strpos($mime, 'image') === 0) { - $src = "data:{$mime};base64," . base64_encode($response->getBody()); - ?> - - getBody()); + ?> + + getBody()); - ?> - - getBody()); + ?> + + getBody()); - ?> - - getBody()); + ?> + + getHeader(); } echo "
"; -- cgit v1.2.3