summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Weipert <code@drogueronin.de>2023-11-22 17:41:42 +0100
committerDaniel Weipert <code@drogueronin.de>2023-11-22 17:41:42 +0100
commitccfecf6bd1f6f8ff40d76269ce3be52d4367e866 (patch)
treea31e8d6dac9d9d9f19e93d1912781615c08f1213
parent95d1b9428f3fc97f92c463c50c2afd17a5b5444e (diff)
catch non-20 response code
-rw-r--r--index.php144
1 files changed, 74 insertions, 70 deletions
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 "<ul>";
- foreach ($currentListItems as $listItem) {
- echo "<li>$listItem[text]</li>";
+ $wasListItem = false;
+ $currentListItems = [];
+ foreach ($lines as $line) {
+ if ($wasListItem && $line['type'] !== 'listitem') {
+ echo "<ul>";
+ foreach ($currentListItems as $listItem) {
+ echo "<li>$listItem[text]</li>";
+ }
+ echo "</ul>";
+ $wasListItem = false;
+ $currentListItems = [];
}
- echo "</ul>";
- $wasListItem = false;
- $currentListItems = [];
- }
- if ($line['type'] === 'preformatted_on') {
- echo "<div>$line[alt]</div><pre>";
- }
- else if ($line['type'] === 'preformatted') {
- echo htmlentities($line['raw']);
- }
- else if ($line['type'] === 'preformatted_off') {
- echo "</pre>";
- }
- else if ($line['type'] === 'heading') {
- echo "<h$line[size]>$line[text]</h$line[size]>";
- }
- else if ($line['type'] === 'listitem') {
- $currentListItems[] = $line;
- $wasListItem = true;
- }
- else if ($line['type'] === 'quote') {
- echo "<blockquote>$line[text]</blockquote>";
- }
- else if ($line['type'] === 'link') {
- $line['link'] = str_replace(["gemini://$geminiHost", $geminiHost], "https://$geminiHost", $line['link']);
- $line['text'] = $line['text'] ?: $line['link'];
- echo "<a href=\"$line[link]\">$line[text]</a>";
- }
- else if ($line['type'] === 'text') {
- echo "<p>$line[text]</p>";
+ if ($line['type'] === 'preformatted_on') {
+ echo "<div>$line[alt]</div><pre>";
+ }
+ else if ($line['type'] === 'preformatted') {
+ echo htmlentities($line['raw']);
+ }
+ else if ($line['type'] === 'preformatted_off') {
+ echo "</pre>";
+ }
+ else if ($line['type'] === 'heading') {
+ echo "<h$line[size]>$line[text]</h$line[size]>";
+ }
+ else if ($line['type'] === 'listitem') {
+ $currentListItems[] = $line;
+ $wasListItem = true;
+ }
+ else if ($line['type'] === 'quote') {
+ echo "<blockquote>$line[text]</blockquote>";
+ }
+ else if ($line['type'] === 'link') {
+ $line['link'] = str_replace(["gemini://$geminiHost", $geminiHost], "https://$geminiHost", $line['link']);
+ $line['text'] = $line['text'] ?: $line['link'];
+ echo "<a href=\"$line[link]\" style=\"display: block;\">$line[text]</a>";
+ }
+ else if ($line['type'] === 'text') {
+ echo "<p>$line[text]</p>";
+ }
}
}
-}
-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());
- ?>
- <img src="<?php echo $src; ?>">
- <?php
-}
+ else if (strpos($mime, 'image') === 0) {
+ $src = "data:{$mime};base64," . base64_encode($response->getBody());
+ ?>
+ <img src="<?php echo $src; ?>">
+ <?php
+ }
-else if (strpos($mime, 'video') === 0) {
- $src = "data:{$mime};base64," . base64_encode($response->getBody());
- ?>
- <video controls>
- <source src="<?php echo $src; ?>" type="<?php echo $mime; ?>">
- </video>
- <?php
-}
+ else if (strpos($mime, 'video') === 0) {
+ $src = "data:{$mime};base64," . base64_encode($response->getBody());
+ ?>
+ <video controls>
+ <source src="<?php echo $src; ?>" type="<?php echo $mime; ?>">
+ </video>
+ <?php
+ }
-else if (strpos($mime, 'audio') === 0) {
- $src = "data:{$mime};base64," . base64_encode($response->getBody());
- ?>
- <audio controls>
- <source src="<?php echo $src; ?>" type="<?php echo $mime; ?>">
- </audio>
- <?php
-}
+ else if (strpos($mime, 'audio') === 0) {
+ $src = "data:{$mime};base64," . base64_encode($response->getBody());
+ ?>
+ <audio controls>
+ <source src="<?php echo $src; ?>" type="<?php echo $mime; ?>">
+ </audio>
+ <?php
+ }
-else {
- echo "Unhandled MIME type: $mime";
+ else {
+ echo "Unhandled MIME type: $mime";
+ }
+} else {
+ echo $response->getHeader();
}
echo "<hr>";