commit 1e7f1766d6b2a6627eb5b92aa2d171623b2d2f00
parent 1ffce7aa5aa6063435ce408558a0ea431f8c22a9
Author: Andrew Laack <andrew@laack.co>
Date: Tue, 22 Sep 2026 12:37:16 -0500
Basic support for inputs
Diffstat:
9 files changed, 117 insertions(+), 20 deletions(-)
diff --git a/include/site.hpp b/include/site.hpp
@@ -10,9 +10,12 @@ class Site{
std::string body;
std::vector<Line*> lines;
std::string header;
+ bool unreachable = false;
public:
Site(std::string header, std::string body);
std::string getBody();
uint32_t getStatusCode();
std::string getHeader();
+ void setUnreachable();
+ bool getUnreachable();
};
diff --git a/include/utils.hpp b/include/utils.hpp
@@ -11,3 +11,4 @@ bool isWhiteSpace(std::string& line, int idx);
std::string truncateAfter(std::string input, char truncate);
std::vector<std::string> stringToList(std::string input);
Line* lineToLine(std::string input, std::optional<uri> prior, int linkCount);
+std::string urlEncode(const std::string& value);
diff --git a/src/browser.cpp b/src/browser.cpp
@@ -4,7 +4,6 @@
#include "../include/utils.hpp"
#include <cstddef>
#include <iostream>
-#include <iterator>
#include <optional>
#include <utility>
#include <vector>
@@ -27,11 +26,18 @@ void Browser::goToSite(std::string url) {
destination = new Link{url,prior->getLinkDestination()};
}
- // TODO: Will need to clean up
+
+ Site* site = client.fetchSite(*destination);
+ if(site == nullptr || site->getUnreachable()) {
+ if(site != nullptr) {
+ delete site;
+ }
+ return;
+ }
+
siteHistory.push_back(destination);
previousIdx += 1;
- Site* site = client.fetchSite(*destination);
if(currentSite != nullptr) {
delete currentSite;
diff --git a/src/gemini-client.cpp b/src/gemini-client.cpp
@@ -51,19 +51,21 @@ Site* GeminiClient::fetchSite(Link link) {
// TODO: Actually handle uris
if(isPrefixed(destination, "gemini://")) {
- return getNetworkedSite(link);
+ try {
+ return getNetworkedSite(link);
+ } catch (...) {
+ auto* unreach = new Site{"", ""};
+ unreach->setUnreachable();
+ return unreach;
+ }
} else if (isPrefixed(destination, "file://")){
-
destination = destination.substr(7, destination.size() - 7);
-
std::string fileStr = "";
-
try {
fileStr = readFileToString(destination);
} catch (FileReadError e ) {
return new Site {"51 \r\n", ""};
}
-
// TODO: how should I discern file types?
return new Site {"20 text/gemini\r\n", fileStr};
} else {
diff --git a/src/link.cpp b/src/link.cpp
@@ -10,11 +10,14 @@ Link::Link(std::string text, std::optional<uri> prior, int linkNumber) {
this->linkNumber = linkNumber;
+ if (text.substr(0, 2) != "=>") {
+ linkDestination = parseDestination(text, prior);
+ return;
+ }
+
std::string afterPrefix = text;
- if(text.substr(0,2) == "=>") {
- afterPrefix = text.substr(2,text.size());
- }
+ afterPrefix = text.substr(2,text.size());
int highest = -1;
@@ -45,12 +48,26 @@ Link::Link(std::string text, std::optional<uri> prior, int linkNumber) {
uri Link::parseDestination(std::string destination, std::optional<uri> prior) {
// TODO: Not sure if this is right w/ how file paths work.
- if(destination.find("://") == -1) {
+ if(destination.find(":") == -1) {
if(prior != std::nullopt) {
- // TODO: Handle port
- std::string truncated = truncateAfter(prior->get_scheme() + "://" + prior->get_host() + "/" + prior->get_path(), '/');
-
- destination = truncated + destination;
+ // query parameter special casing
+ if(destination.substr(0,1) == "?") {
+ std::string encoded = urlEncode(destination.substr(1));
+ return uri{prior.value().to_string() + "?" + encoded};
+ }
+
+ std::string base = prior->get_scheme() + "://" + prior->get_host();
+
+ if (destination[0] == '/') {
+ destination = base + destination;
+ } else {
+
+ std::string path = prior->get_path();
+ if (path[0] != '/') {
+ path = "/" + path;
+ }
+ destination = base + path.substr(0, path.rfind('/') + 1) + destination;
+ }
}
else {
destination = "file://" + destination;
diff --git a/src/main.cpp b/src/main.cpp
@@ -2,6 +2,7 @@
#include <algorithm>
#include <cmath>
#include <cstddef>
+#include <iostream>
#include <ncurses.h>
#include <utility>
#include <vector>
@@ -71,6 +72,22 @@ int linkHandler() {
return acc;
}
+std::string handleUserInput() {
+
+ std::string acc = "?";
+
+ while(true) {
+ int sel = getch();
+ if(sel == '\n' || sel == KEY_ENTER) {
+ break;
+ }
+
+ acc += std::string {(char)sel};
+ }
+
+ return acc;
+}
+
int main() {
@@ -88,7 +105,7 @@ int main() {
initColors();
endwin();
- b.goToSite("gemini://tlgs.one/known-hosts");
+ b.goToSite("gemini://tlgs.one");
auto current = b.renderSite();
removeNonAscii(current);
@@ -114,11 +131,20 @@ int main() {
int linkToFollow = linkHandler();
if(linkToFollow != -1) {
b.followLinkNumber(linkToFollow);
- current = b.renderSite();
- removeNonAscii(current);
+ if(b.getCurrentSite()->getStatusCode() >= 10 && b.getCurrentSite()->getStatusCode() <= 19) {
+ std::string inputQuery = handleUserInput();
+ if(inputQuery != "?") { // TODO: Better handling
+ b.goToSite(inputQuery);
+ current = b.renderSite();
+ removeNonAscii(current);
+ }
+
+ } else {
+ current = b.renderSite();
+ removeNonAscii(current);
+ }
}
-
}
y = std::max(0,y);
diff --git a/src/site.cpp b/src/site.cpp
@@ -18,3 +18,12 @@ uint32_t Site::getStatusCode() {
uint32_t statusCode = std::stoi(header.substr(0,2));
return statusCode;
}
+
+void Site::setUnreachable() {
+ unreachable = true;
+}
+
+bool Site::getUnreachable() {
+ return unreachable;
+}
+
diff --git a/src/utils.cpp b/src/utils.cpp
@@ -5,6 +5,8 @@
#include <cstddef>
#include <cstdint>
#include <fstream>
+#include <iomanip>
+#include <ios>
#include <iostream>
#include <sstream>
#include <string>
@@ -65,3 +67,27 @@ Line* lineToLine(std::string input, std::optional<uri> prior, int linkCount) {
}
return new Plaintext{input};
}
+
+
+std::string urlEncode(const std::string& value) {
+ std::ostringstream escaped;
+ escaped.fill('0');
+ escaped << std::hex;
+
+ for (std::string::const_iterator i = value.begin(), n = value.end(); i != n; ++i) {
+ std::string::value_type c = (*i);
+
+ // Keep alphanumeric and other accepted characters intact
+ if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') {
+ escaped << c;
+ continue;
+ }
+
+ // Any other characters are percent-encoded
+ escaped << std::uppercase;
+ escaped << '%' << std::setw(2) << int((unsigned char) c);
+ escaped << std::nouppercase;
+ }
+
+ return escaped.str();
+}
diff --git a/tests/fetch-test.cpp b/tests/fetch-test.cpp
@@ -74,3 +74,10 @@ TEST_CASE("Send basic gemini requests") {
delete s;
}
+
+TEST_CASE("Send request to non-existent gemini server") {
+ auto client = GeminiClient {};
+ auto ln = Link{"=> gemini://this.cant.be.a.site123"};
+ Site* s = client.fetchSite(ln);
+ REQUIRE(s == nullptr);
+}