foreseen

A text-based gemini browser with prefetching
git clone git://git.laack.co/foreseen.git
Log | Files | Refs | README | LICENSE

commit b1983d79fe1b1599ed3445c819f2aa0c22f76990
parent 07ef03cea845f76bc1505c5f1e4cc5f003ff0ada
Author: Andrew Laack <andrew@laack.co>
Date:   Sun,  4 Oct 2026 11:37:10 -0500

Show users status codes, meta lines, and download status information on interactions.

Diffstat:
APOSSIBLE_CHANGES.txt | 11+++++++++++
MTODO.txt | 22++--------------------
Minclude/browser.hpp | 23++++++++++++++++++++---
Minclude/shared.hpp | 2+-
Msrc/browser.cpp | 53+++++++++++++++++++++++++++++++++++++++++------------
Msrc/gemini-client.cpp | 3---
Msrc/main.cpp | 1+
Msrc/render.cpp | 118+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------
Msrc/shared.cpp | 4++--
Mtests/browser-test.cpp | 16++++++++--------
Atests/cgi-bin/long-error-message.py | 3+++
Mtests/pure-test.cpp | 1+
12 files changed, 182 insertions(+), 75 deletions(-)

diff --git a/POSSIBLE_CHANGES.txt b/POSSIBLE_CHANGES.txt @@ -0,0 +1,11 @@ +- way to show where links go +- y-value memory in f/b history +- horizontal scroll when lines are too long +- ability to go left / right in input box. +- auto-follow redirects based on domain? + - A/y/n -- always, no, yes +- detect if a file is not a normal text file + - source code, txt, md, gmi, stuff like that + - right now we just truncate, which is good enough for general usage: if you want more, use gemget. +- url manipulation + - sometimes it's nice to go from {example}.com/blog/whatever -> {example}.com/blog without having to find links. diff --git a/TODO.txt b/TODO.txt @@ -1,22 +1,7 @@ -- way to show where links go -- consideration for y-value memory in f/b history -- horizontal scroll when lines are too long? -- ability to go left / right in input box. -- auto-follow redirects based on domain? - - A/y/n -- always, no, yes -- downloading and interactions broadly should show more information - - reloading should have progress / indication - - perhaps I should just re-use the entry box for download (specify a file) / success type stuff? - - I'm wary to make it too interactive as that might draw focus away from the text being read. -- gotosite should return status code on failure to be shown to users - - this will aid in understanding: - - server unreachable - - request rejected - - cert issues - - ... - man pages - if I enter some large fucking number like 120349871023948712304987 it searches because stoi probably fails, or our check does. - localhost access + - localhost:1965 broken - cleanups - function signatures (const, noexcept) - types @@ -24,8 +9,5 @@ - imports - tests - remove useless ones + - formatter - put all colors into shared.hpp -- detect if a file is not a normal text file - - source code, txt, md, gmi, stuff like that - - if not, give option to download site - - this seems involved, but useful. diff --git a/include/browser.hpp b/include/browser.hpp @@ -11,7 +11,24 @@ #include "cache.hpp" #include "link.hpp" #include "utils.hpp" -#include "shared.hpp" + +const std::string OPENED_EXT_TXT = "Externally opened"; + +enum SiteLoadResult { + SITE_LOADED, // These are valid status codes so 1X, 2X, and 3e + SITE_TEMPORARY_FAILURE, + SITE_PERMANENT_FAILURE, + SITE_REQUIRES_CERTIFICATE, + SITE_REJECTED_CERTIFICATE, + SITE_INVALID_CERTIFICATE, + SITE_UNEXPECTED_STATUS_CODE, + LINK_DOES_NOT_EXIST // only for link following +}; + +struct SiteLoadPair { // TODO: Set this up. + SiteLoadResult result; + std::string metaLine; +}; class Browser { private: @@ -33,7 +50,7 @@ class Browser { Browser(); void setLinksOfCurrentLines(); ~Browser(); - bool goToSite(std::string url, bool addToHistory = true, bool refresh = false); + SiteLoadPair goToSite(std::string url, bool addToHistory = true, bool refresh = false); void setDone(int threadIdx); void refresh(); Site* getCurrentSite(); @@ -42,7 +59,7 @@ class Browser { std::optional<uri> getPriorUri(); std::vector<std::pair<std::string, TextRender>> renderSite(); std::vector<Line*> toLines(Site* site); - bool followLinkNumber(int linkToFollow); + SiteLoadPair followLinkNumber(int linkToFollow); std::vector<Link>* getLinkLines(); Identity getIdentity(uri uriInput); void justCacheSite(Link link); diff --git a/include/shared.hpp b/include/shared.hpp @@ -6,7 +6,7 @@ extern std::string DEFAULT_SEARCH_ENGINE; // default search option when search is inferred extern int SITE_CACHE_LIMIT; // number of links to prefetch per page extern int THREAD_NUM; // thread count in thread pool for pre-fetching -extern int maxWidth; // max text width +extern int MAX_WIDTH; // max text width extern int CACHE_SIZE; // max elements in history cache and max in prefetch cache extern uint8_t COLOR_LINK; // color used for links extern uint8_t COLOR_PREFORMATTED; // color used for preformatted text regions diff --git a/src/browser.cpp b/src/browser.cpp @@ -6,6 +6,7 @@ #include <thread> #include <unistd.h> #include "../include/site.hpp" +#include "../include/shared.hpp" #include "../include/errors.hpp" #include "../include/gemini-client.hpp" #include "../include/utils.hpp" @@ -94,7 +95,7 @@ void Browser::refresh() { goToSite(getPriorUri().value().to_string(), false, true); } -bool Browser::goToSite(std::string url, bool addToHistory, bool refresh) { +SiteLoadPair Browser::goToSite(std::string url, bool addToHistory, bool refresh) { Link* prior = nullptr; @@ -124,7 +125,7 @@ bool Browser::goToSite(std::string url, bool addToHistory, bool refresh) { openThread = std::thread(openUrl, urlString); delete destination; - return true; + return SiteLoadPair{SITE_LOADED, OPENED_EXT_TXT}; } @@ -151,14 +152,42 @@ bool Browser::goToSite(std::string url, bool addToHistory, bool refresh) { delete site; } delete destination; - return false; + if(site == nullptr) { + return SiteLoadPair{SITE_PERMANENT_FAILURE, "Connection error"}; + } + return SiteLoadPair{SITE_PERMANENT_FAILURE, site->getMeta()}; } int sc = site->getStatusCode(); + std::string meta = site->getMeta(); if (sc < 10 || sc >= 40) { delete site; delete destination; - return false; + + if(sc < 10 || sc > 69) { + return SiteLoadPair{SITE_UNEXPECTED_STATUS_CODE, meta}; + } + + if(sc < 50) { // 40 -> 49 + return SiteLoadPair{SITE_TEMPORARY_FAILURE, meta}; + } + if(sc < 60) { // 50 -> 59 + return SiteLoadPair{SITE_PERMANENT_FAILURE, meta}; + } + + if(sc == 60) { + return SiteLoadPair{SITE_REQUIRES_CERTIFICATE, meta}; + } + + if(sc == 61) { + return SiteLoadPair{SITE_REJECTED_CERTIFICATE, meta}; + } + if(sc == 62) { + return SiteLoadPair{SITE_INVALID_CERTIFICATE, meta}; + } + + return SiteLoadPair{SITE_REQUIRES_CERTIFICATE, meta}; + } if(addToHistory) { @@ -195,7 +224,7 @@ bool Browser::goToSite(std::string url, bool addToHistory, bool refresh) { } tryCacheTargets(); - return true; + return SiteLoadPair{SITE_LOADED, meta}; } Identity Browser::getIdentity(uri uriInput) { @@ -272,8 +301,8 @@ Browser::Browser() : threads(THREAD_NUM), done(THREAD_NUM){ } // this ensures some nice invariants about the browser, like always having at least one valid page. - bool start = goToSite("about://newtab"); - if(!start) { + SiteLoadPair start = goToSite("about://newtab"); + if(start.result != SITE_LOADED) { throw std::runtime_error("Browser unexpectedly failed to start."); } } @@ -345,17 +374,17 @@ std::vector<Link>* Browser::getLinkLines() { } -bool Browser::followLinkNumber(int linkToFollow) { +SiteLoadPair Browser::followLinkNumber(int linkToFollow) { if((int)links.size() > linkToFollow-1 && linkToFollow-1 >= 0) { std::size_t pos = links[linkToFollow-1]; if(lines.size() > pos) { Line* ptr = lines[pos]; Link* ptrLnk = dynamic_cast<Link*>(ptr); - bool res = goToSite(ptrLnk->getLinkDestination().to_string(), true); + SiteLoadPair res = goToSite(ptrLnk->getLinkDestination().to_string(), true); return res; } } - return false; + return SiteLoadPair{LINK_DOES_NOT_EXIST, "Link not found"}; } void Browser::goBack() { @@ -388,7 +417,7 @@ void Browser::goBack() { return; // safely fail with rollback } } - if(!goToSite(this->siteHistory[previousIdx]->getLinkDestination().to_string(), false)) { + if(!(goToSite(this->siteHistory[previousIdx]->getLinkDestination().to_string(), false).result == SITE_LOADED)) { previousIdx = original; } @@ -421,7 +450,7 @@ void Browser::goForward() { } } - if(!goToSite(this->siteHistory[previousIdx]->getLinkDestination().to_string(), false)) { + if(!(goToSite(this->siteHistory[previousIdx]->getLinkDestination().to_string(), false).result == SITE_LOADED)) { previousIdx = original; } } else { diff --git a/src/gemini-client.cpp b/src/gemini-client.cpp @@ -104,9 +104,6 @@ Site* GeminiClient::getNetworkedSite(Link link, std::string crtPath, std::string end -= 1; } - // TODO: should we track when these are truncated? We have a variable for that - // but nothign about it in our site. - std::string status = response.substr(0, end); std::string body = response.substr(nl + 1); diff --git a/src/main.cpp b/src/main.cpp @@ -1,6 +1,7 @@ #include "../include/browser.hpp" #include "../include/utils.hpp" #include "../include/render.hpp" +#include "../include/shared.hpp" #include <cassert> #include <csignal> #include <ncurses.h> diff --git a/src/render.cpp b/src/render.cpp @@ -1,8 +1,10 @@ #include "../include/browser.hpp" #include "../include/utils.hpp" #include "../include/render.hpp" +#include "../include/shared.hpp" #include <algorithm> #include <cassert> +#include <cctype> #include <chrono> #include <cstdint> #include <cstdlib> @@ -17,7 +19,7 @@ #endif #ifdef DEBUG_MODE - const char* OUT_LOCATION = "/tmp/foreseen"; // TODO: This is bad. Pass these into fn + const char* OUT_LOCATION = "/tmp/foreseen"; #else const char* OUT_LOCATION = ""; #endif @@ -119,7 +121,7 @@ void draw(DrawState& ds) { } if(ds.reBreak) { - ds.broken = breakLines(ds.prior, std::min(ds.columns, maxWidth), ds.columns); + ds.broken = breakLines(ds.prior, std::min(ds.columns, MAX_WIDTH), ds.columns); ds.reBreak = false; } if(ds.toLowest) { @@ -185,7 +187,19 @@ void draw(DrawState& ds) { move(0,0); attron(A_BOLD); attron(COLOR_PAIR(COLOR_RED+1)); - addstr(ds.issueText.c_str()); + + std::string tr = ds.issueText; + std::string truncated; + + for(auto& ch : tr) { + truncated += (char)toascii(ch); + } + + if(ds.issueText.size() > ds.columns) { + truncated = truncated.substr(0,ds.columns - 3) + "..."; + } + + addstr(truncated.c_str()); attroff(COLOR_PAIR(COLOR_RED+1)); attroff(A_BOLD); } @@ -269,18 +283,69 @@ void handleUserInput(DrawState& ds, Browser& b, int sel) { ds.userInput += std::string {(char)sel}; } } +void handleResult(DrawState& ds, SiteLoadPair result) { + switch (result.result) { + case SITE_LOADED: + if(result.metaLine != OPENED_EXT_TXT) { + ds.y = 0; + ds.mustReRender = true; + } + break; + case SITE_TEMPORARY_FAILURE: + if(result.metaLine != "") { + ds.issueText = "Temporary site failure: " + result.metaLine; + } else { + ds.issueText = "Temporary site failure"; + } + break; + case SITE_PERMANENT_FAILURE: + if(result.metaLine != "") { + ds.issueText = "Permanent site failure: " + result.metaLine; + } else { + ds.issueText = "Permanent site failure"; + } -void tryVisitSite(DrawState& ds , Browser& b, std::string site) { - bool visitSuccess = b.goToSite(site); - if(visitSuccess) { - ds.mustReRender = true; - } - if(!visitSuccess) { - ds.issueText = "Unable to access the requested site."; - ds.timeToClearIssueText = getCurrentTime() + 1000; - } else { - ds.y = 0; + break; + case SITE_REQUIRES_CERTIFICATE: + if(result.metaLine != "") { + ds.issueText = "Site requires a certificate: " + result.metaLine; + } else { + ds.issueText = "Site requires a certificate"; + } + + break; + case SITE_REJECTED_CERTIFICATE: + if(result.metaLine != "") { + ds.issueText = "Your certificate is not authorized: " + result.metaLine; + } + else { + ds.issueText = "Your certificate is not authorized"; + } + + break; + case SITE_INVALID_CERTIFICATE: + + if(result.metaLine != "") { + ds.issueText = "Your certificate has been rejected as invalid: " + result.metaLine; + } else { + ds.issueText = "Your certificate has been rejected as invalid"; + } + + break; + case SITE_UNEXPECTED_STATUS_CODE: + ds.issueText = "The server replied with an invalid status code"; + break; + case LINK_DOES_NOT_EXIST: + ds.issueText = "Link not found"; + break; } + ds.timeToClearIssueText = getCurrentTime() + 1000; +} + + +void tryVisitSite(DrawState& ds , Browser& b, std::string site) { + SiteLoadPair result = b.goToSite(site); + handleResult(ds,result); } bool mainLoop(DrawState& ds, Browser& b, int input, int cols, int lines) { @@ -298,14 +363,11 @@ bool mainLoop(DrawState& ds, Browser& b, int input, int cols, int lines) { draw(ds); if(ds.openOtherInput != "") { Destination destination = handleDestinationResolution(ds.openOtherInput , false); - bool res; + SiteLoadPair res; switch(destination.t) { case NUMBER_DESTINATION: res = b.followLinkNumber(destination.linkNumber); - if(res) { - ds.y = 0; - } - ds.mustReRender = true; + handleResult(ds,res); break; case STRING_DESTINATION: tryVisitSite(ds, b, destination.destination); @@ -364,14 +426,20 @@ bool mainLoop(DrawState& ds, Browser& b, int input, int cols, int lines) { } else if(input == 'f') { b.goForward(); ds.mustReRender = true; - ds.y = 0; // todo: make this part of state somewhere. + ds.y = 0; } else if(input == 'd') { - // TODO: Handle outLocation == "" meaning failed std::string outLocation = b.tryDownloadPage(OUT_LOCATION); + if(outLocation == "") { + ds.issueText = "Failed to download file"; + ds.timeToClearIssueText = getCurrentTime() + 1000; + } else { + ds.issueText = "File downloaded to " + outLocation; + ds.timeToClearIssueText = getCurrentTime() + 1000; + } } else if(input == 'b') { b.goBack(); ds.mustReRender = true; - ds.y = 0; // todo: make this part of state somewhere. + ds.y = 0; } else if(input == 'o') { ds.handleOpenOther = true; } else if (input == 'e'){ @@ -391,14 +459,12 @@ bool mainLoop(DrawState& ds, Browser& b, int input, int cols, int lines) { quoted += "'"; system((editor + " " + quoted).c_str()); refresh(); + } else { + ds.issueText = "Failed to download file"; + ds.timeToClearIssueText = getCurrentTime() + 1000; } } - // this is the loop where we deal with redirects and stuff like that. - // TODO: I don't think this has to still be a loop? - // Broadly, we are moving away from a loop based approach, sequestering them to either browser with forward / backward - // or main.cpp / tests. - if( (b.getCurrentSite()->getStatusCode() < 20 || b.getCurrentSite()->getStatusCode() > 29) && !ds.handleRedirect && !ds.handleInput) { if(b.getCurrentSite()->getStatusCode() >= 10 && b.getCurrentSite()->getStatusCode() <= 19) { diff --git a/src/shared.cpp b/src/shared.cpp @@ -3,8 +3,8 @@ std::string DEFAULT_SEARCH_ENGINE = "gemini://tlgs.one/search"; int SITE_CACHE_LIMIT = 10; int THREAD_NUM = 4; -int maxWidth = 80; +int MAX_WIDTH = 80; int CACHE_SIZE = 500; uint8_t COLOR_LINK = 159; uint8_t COLOR_PREFORMATTED = 201; -int RESPONSE_SIZE_LIMIT_MB = 5; +int RESPONSE_SIZE_LIMIT_MB = 5; // This will truncate at the specified size. diff --git a/tests/browser-test.cpp b/tests/browser-test.cpp @@ -87,8 +87,8 @@ TEST_CASE("Test browser doesn't crash on invalid sites.") { TEST_CASE("Test browser doesn't crash when accessing sites with specified port number that is not responsive.") { Browser b{}; - bool res = b.goToSite("gemini://localhost:3847"); - REQUIRE(!res); + SiteLoadPair res = b.goToSite("gemini://localhost:3847"); + REQUIRE(res.result == SITE_PERMANENT_FAILURE); b.goForward(); b.goBack(); REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() == "about://newtab"); @@ -159,15 +159,15 @@ TEST_CASE("Test downloading to specific directory") { TEST_CASE("Test going forwards doesn't break when accessing an invalid site prior.") { Browser b{}; - bool res = b.goToSite("gemini://aroisetnatsr.aoirseaorstie"); - REQUIRE(!res); + SiteLoadPair res = b.goToSite("gemini://aroisetnatsr.aoirseaorstie"); + REQUIRE(res.result == SITE_PERMANENT_FAILURE); b.goForward(); } TEST_CASE("Test going backwards doesn't break when accessing an invalid site prior.") { Browser b{}; - bool res = b.goToSite("gemini://aroisetnatsr.aoirseaorstie"); - REQUIRE(!res); + SiteLoadPair res = b.goToSite("gemini://aroisetnatsr.aoirseaorstie"); + REQUIRE(res.result == SITE_PERMANENT_FAILURE); b.goBack(); } @@ -183,9 +183,9 @@ TEST_CASE("Sanitize characters to draw tests") { Browser b {}; std::string cwd = std::filesystem::current_path(); std::string st = "file://" + cwd + "/tests/sites/line-return.gmi"; - bool res = b.goToSite(st); + SiteLoadPair res = b.goToSite(st); - REQUIRE(res); + REQUIRE(res.result == SITE_LOADED); REQUIRE(b.getCurrentSite()->getStatusCode() >= 20); REQUIRE(b.getCurrentSite()->getStatusCode() <= 29); diff --git a/tests/cgi-bin/long-error-message.py b/tests/cgi-bin/long-error-message.py @@ -0,0 +1,3 @@ +#!/usr/bin/env python3 + +print(f"50 this is a really long message that you shouldn't even be able to see, but it also shouldn't crash so yeah yes anyways, what's been up man, how's your", end="\r\n") diff --git a/tests/pure-test.cpp b/tests/pure-test.cpp @@ -10,6 +10,7 @@ #include <rapidcheck/Check.h> #include <utility> #include "../include/browser.hpp" +#include "../include/shared.hpp" #include "../include/utils.hpp" TEST_CASE("Test cli input handling") {