commit 573311708e8cb19e0d6b02b733fcd23e229d6be8
parent 8a6cbead7ed2307a8a83946dcdecec19daff36f1
Author: Andrew Laack <andrew@laack.co>
Date: Tue, 22 Sep 2026 22:25:57 -0500
Support for argv file specification, {name}.{tld} -> gemini://{name}.{tld}, and other proto (like http) handling.
Diffstat:
7 files changed, 82 insertions(+), 34 deletions(-)
diff --git a/src/browser.cpp b/src/browser.cpp
@@ -1,4 +1,7 @@
#include "../include/browser.hpp"
+#include <iostream>
+#include <stdexcept>
+#include <string>
#include <unistd.h>
#include "../include/site.hpp"
#include "../include/gemini-client.hpp"
@@ -31,10 +34,17 @@ void Browser::goToSite(std::string url, bool addToHistory, bool refresh) {
destination = new Link{url,prior->getLinkDestination()};
}
+ std::string urlString = destination->getLinkDestination().to_string();
+ std::string scheme = destination->getLinkDestination().get_scheme();
+ if(scheme != "gemini" && scheme != "file" && scheme != "about") { // TODO: Should I use about or just a fs file?
+ openUrl(urlString);
+ delete destination;
+ return;
+ }
+
Site* site = nullptr;
- std::string urlString = destination->getLinkDestination().to_string();
if(urlString.find("gemini://") != -1 && !refresh) {
std::optional<Site> cachedSite = cache->getSite(urlString);
if(cachedSite != std::nullopt) {
@@ -46,7 +56,6 @@ void Browser::goToSite(std::string url, bool addToHistory, bool refresh) {
site = client.fetchSite(*destination);
}
-
if(site == nullptr || site->getUnreachable()) {
if(site != nullptr) {
delete site;
@@ -143,16 +152,12 @@ std::optional<uri> Browser::getPriorUri() {
}
void Browser::followLinkNumber(int linkToFollow) {
- if(links.size() > linkToFollow-1 && linkToFollow-1 > 0) {
+ if(links.size() > linkToFollow-1 && linkToFollow-1 >= 0) {
std::size_t pos = links[linkToFollow-1];
- if(lines.size() > pos && pos > 0) {
+ if(lines.size() > pos && pos >= 0) {
Line* ptr = lines[pos];
Link* ptrLnk = dynamic_cast<Link*>(ptr);
- if(ptrLnk->getLinkDestination().to_string().find("gemini://") == -1) {
- openUrl(ptrLnk->getLinkDestination().to_string());
- } else {
- goToSite(ptrLnk->getLinkDestination().to_string(), true);
- }
+ goToSite(ptrLnk->getLinkDestination().to_string(), true);
}
}
diff --git a/src/gemini-client.cpp b/src/gemini-client.cpp
@@ -1,3 +1,5 @@
+#include <iostream>
+#include <stdexcept>
#include <sys/socket.h>
#include <sys/time.h>
#include "../include/gemini-client.hpp"
@@ -83,17 +85,45 @@ Site* GeminiClient::fetchSite(Link link) {
return unreach;
}
} else if (isPrefixed(destination, "file://")){ // TODO: This seems wrong; it should probably be fullpath with that prefix.
- destination = destination.substr(7, destination.size() - 7);
+
+ // TODO: this is messy and perhaps not necessary
+
+ std::string rest = destination.substr(7);
+ std::string host;
+ std::string path;
+
+ std::size_t slash = rest.find('/');
+ if (slash != std::string::npos) {
+ host = rest.substr(0, slash);
+ path = rest.substr(slash);
+ } else {
+ host = rest;
+ path = "";
+ }
+
+ if (host.empty()) {
+ host = "localhost";
+ }
+
std::string fileStr = "";
+
+ std::string fsPath;
+
+ if(host == "localhost") {
+ fsPath = path;
+ } else {
+ throw std::invalid_argument("The requested file appears to exist on another system.");
+ }
+
try {
- fileStr = readFileToString(destination);
+ fileStr = readFileToString(fsPath);
} catch (FileReadError e ) {
- return new Site {"51 \r\n", ""};
+ return new Site {"51 file not found", ""};
}
// TODO: how should I discern file types?
- return new Site {"20 text/gemini\r\n", fileStr};
+ return new Site {"20 text/gemini", fileStr};
} else if(isPrefixed(destination, "about:")){
- return new Site {"20 text/gemini\r\n", getNewTab()};
+ return new Site {"20 text/gemini", getNewTab()};
} else {
throw NotImplemented();
}
diff --git a/src/link.cpp b/src/link.cpp
@@ -1,4 +1,5 @@
#include "../include/link.hpp"
+#include <iostream>
#include <optional>
#include <string>
#include "../include/utils.hpp"
@@ -69,9 +70,6 @@ uri Link::parseDestination(std::string destination, std::optional<uri> prior) {
destination = base + path.substr(0, path.rfind('/') + 1) + destination;
}
}
- else {
- destination = "file://" + destination; // TODO: this should only be done in the context of starting...
- }
}
try {
return uri{destination};
diff --git a/src/main.cpp b/src/main.cpp
@@ -1,7 +1,9 @@
#include "../include/browser.hpp"
+#include "../include/utils.hpp"
#include <algorithm>
#include <cmath>
#include <cstddef>
+#include <filesystem>
#include <iostream>
#include <ncurses.h>
#include <string>
@@ -250,7 +252,9 @@ int main(int argc, char** argv) {
b.goToSite("about:newtab",true);
if(argc > 1) {
- b.goToSite(argv[1],true);
+ // TODO: This should have tests.
+ std::string path = "file:///" + std::filesystem::current_path().string() + "/" + argv[1];
+ b.goToSite(path,true);
} else {
b.goToSite("gemini://tlgs.one",true);
}
diff --git a/src/utils.cpp b/src/utils.cpp
@@ -4,9 +4,11 @@
#include "../include/plaintext.hpp"
#include <cstddef>
#include <cstdint>
+#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <ios>
+#include <linux/limits.h>
#include <sstream>
#include <string>
#include <vector>
diff --git a/tests/browser-test.cpp b/tests/browser-test.cpp
@@ -6,9 +6,20 @@
TEST_CASE( "Basic navigation" ) {
Browser b{};
- b.goToSite("tests/sites/basic.gmi", true);
+ b.goToSite("file:///home/andrew/gitRepos/gemini-browser/tests/sites/basic.gmi", true);
+ REQUIRE(b.getCurrentSite()->getBody() == readFileToString("tests/sites/basic.gmi"));
+ b.goToSite("file:///home/andrew/gitRepos/gemini-browser/tests/sites/basic_2.gmi", true);
+ REQUIRE(b.getCurrentSite()->getBody() == readFileToString("tests/sites/basic_2.gmi"));
+}
+
+
+TEST_CASE( "Local filesystem relative navigation" ) {
+
+ Browser b{};
+ b.goToSite("file:///home/andrew/gitRepos/gemini-browser/tests/sites/basic.gmi", true);
REQUIRE(b.getCurrentSite()->getBody() == readFileToString("tests/sites/basic.gmi"));
b.goToSite("basic_2.gmi", true);
REQUIRE(b.getCurrentSite()->getBody() == readFileToString("tests/sites/basic_2.gmi"));
}
+
diff --git a/tests/fetch-test.cpp b/tests/fetch-test.cpp
@@ -7,16 +7,16 @@
TEST_CASE("Link constructors") {
- auto l2 = Link{"=> basic_2.gmi Other Page!", uri{"file://tests/sites/basic.gmi"}};
+ auto l2 = Link{"=> basic_2.gmi Other Page!", uri{"file:///tests/sites/basic.gmi"}};
- REQUIRE("file://tests/sites/basic_2.gmi" == l2.getLinkDestination().to_string());
+ REQUIRE("file:///tests/sites/basic_2.gmi" == l2.getLinkDestination().to_string());
REQUIRE("Other Page!" == l2.getLinkText());
}
// this mostly exists in case utils.cpp is busted in some very obvious way.
TEST_CASE("Local Sanity Client") {
auto client = GeminiClient {};
- auto ln = Link{"=> tests/sites/basic_2.gmi"};
+ auto ln = Link{"=> file:///home/andrew/gitRepos/gemini-browser/tests/sites/basic_2.gmi"}; // TODO: Fix
Site* s = client.fetchSite(ln);
std::string expectedSite =
@@ -30,35 +30,33 @@ TEST_CASE("Local Sanity Client") {
delete s;
}
-
-TEST_CASE("Match on all .gmi files in tests/sites") {
-
+TEST_CASE("Match on all .gmi file headers in tests/sites") {
auto client = GeminiClient {};
-
for(const auto& current: std::filesystem::directory_iterator("tests/sites")) {
- auto ln = Link{"=> " + current.path().string()};
+ std::string full = "=> file:///home/andrew/gitRepos/gemini-browser/" + current.path().string();
+ auto ln = Link{full};
Site* s = client.fetchSite(ln);
- std::string expectedSite = readFileToString(current.path().string());
- REQUIRE(s->getBody() == expectedSite);
+ REQUIRE(s->getHeader() == "20 text/gemini");
delete s;
}
}
-TEST_CASE("Match status code on .gmi files in tests/sites") {
+TEST_CASE("Match on all .gmi files in tests/sites") {
auto client = GeminiClient {};
-
for(const auto& current: std::filesystem::directory_iterator("tests/sites")) {
- auto ln = Link{"=> " + current.path().string()};
+ std::string full = "=> file:///home/andrew/gitRepos/gemini-browser/" + current.path().string();
+ auto ln = Link{full};
Site* s = client.fetchSite(ln);
- REQUIRE(s->getStatusCode() == 20);
+ std::string expectedSite = readFileToString(current.path().string());
+ REQUIRE(s->getBody() == expectedSite);
delete s;
}
}
TEST_CASE("Match status code on missing .gmi files") {
auto client = GeminiClient {};
- auto ln = Link{"=> tests/sites/this_file_doesnt_exist.gmi"};
+ auto ln = Link{"=> file:///tests/sites/this_file_doesnt_exist.gmi"};
Site* s = client.fetchSite(ln);
REQUIRE(s->getStatusCode() == 51);
delete s;