foreseen

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

commit f106f75aa53c7ed41fed7e595fdfc7196b823209
parent fd701678aed478e2034f29fd890f6591b35a4af1
Author: Andrew Laack <andrew@laack.co>
Date:   Tue, 22 Sep 2026 19:21:24 -0500

Added ssl timeouts, default home page, and a bunch of edge case checks

Diffstat:
Minclude/utils.hpp | 1+
Msrc/browser.cpp | 67+++++++++++++++++++++++++++++++++++++++++++++++++++----------------
Msrc/gemini-client.cpp | 38+++++++++++++++++++++++++++++++++-----
Msrc/link.cpp | 3+--
Msrc/main.cpp | 118+++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------
Msrc/site.cpp | 3++-
Msrc/utils.cpp | 14+++++++++++++-
Mtests/fetch-test.cpp | 2+-
8 files changed, 179 insertions(+), 67 deletions(-)

diff --git a/include/utils.hpp b/include/utils.hpp @@ -12,3 +12,4 @@ 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); +std::string getNewTab(); diff --git a/src/browser.cpp b/src/browser.cpp @@ -25,12 +25,13 @@ void Browser::goToSite(std::string url, bool addToHistory) { destination = new Link{url,prior->getLinkDestination()}; } - Site* site = client.fetchSite(*destination); + if(site == nullptr || site->getUnreachable()) { if(site != nullptr) { delete site; } + goBack(); return; } @@ -108,30 +109,64 @@ std::optional<uri> Browser::getPriorUri() { } void Browser::followLinkNumber(int linkToFollow) { - Line* ptr = lines[links[linkToFollow-1]]; - Link* ptrLnk = dynamic_cast<Link*>(ptr); - goToSite(ptrLnk->getLinkDestination().to_string(), true); + + if(links.size() > linkToFollow-1 && linkToFollow-1 > 0) { + std::size_t pos = links[linkToFollow-1]; + if(lines.size() > pos && pos > 0) { + Line* ptr = lines[pos]; + Link* ptrLnk = dynamic_cast<Link*>(ptr); + goToSite(ptrLnk->getLinkDestination().to_string(), true); + } + } + } void Browser::goBack() { + + int original = previousIdx; + previousIdx -= 1; - int prSC = previousStatusCodes[siteHistory[previousIdx]->getLinkDestination().to_string()]; - while(!(prSC >= 20 && prSC <= 29)) { - previousIdx -= 1; - prSC = previousStatusCodes[siteHistory[previousIdx]->getLinkDestination().to_string()]; - } - goToSite(this->siteHistory[previousIdx]->getLinkDestination().to_string(), false); + if(siteHistory.size() > previousIdx && previousIdx >= 0) { + + int prSC = previousStatusCodes[siteHistory[previousIdx]->getLinkDestination().to_string()]; + while(!(prSC >= 20 && prSC <= 29)) { + previousIdx -= 1; + if(siteHistory.size() > previousIdx && previousIdx >= 0) { + prSC = previousStatusCodes[siteHistory[previousIdx]->getLinkDestination().to_string()]; + } else { + previousIdx = original; + return; + } + } + + goToSite(this->siteHistory[previousIdx]->getLinkDestination().to_string(), false); + + } else { + previousIdx = original; + } } void Browser::goForward() { + + int original = previousIdx; + previousIdx += 1; - int prSC = previousStatusCodes[siteHistory[previousIdx]->getLinkDestination().to_string()]; - while(!(prSC >= 20 && prSC <= 29)) { - previousIdx += 1; - prSC = previousStatusCodes[siteHistory[previousIdx]->getLinkDestination().to_string()]; - } + if(siteHistory.size() > previousIdx && previousIdx >= 0) { + int prSC = previousStatusCodes[siteHistory[previousIdx]->getLinkDestination().to_string()]; + while(!(prSC >= 20 && prSC <= 29)) { + previousIdx += 1; + if(siteHistory.size() > previousIdx && previousIdx >= 0) { + prSC = previousStatusCodes[siteHistory[previousIdx]->getLinkDestination().to_string()]; + } else { + previousIdx = original; + return; + } + } - goToSite(this->siteHistory[previousIdx]->getLinkDestination().to_string(), false); + goToSite(this->siteHistory[previousIdx]->getLinkDestination().to_string(), false); + } else { + previousIdx = original; + } } diff --git a/src/gemini-client.cpp b/src/gemini-client.cpp @@ -1,8 +1,11 @@ +#include <sys/socket.h> +#include <sys/time.h> #include "../include/gemini-client.hpp" #include "../include/site.hpp" #include "../include/utils.hpp" #include "../include/errors.hpp" #include <openssl/ssl.h> +#include <string> Site* GeminiClient::getNetworkedSite(Link link) { @@ -10,6 +13,10 @@ Site* GeminiClient::getNetworkedSite(Link link) { std::string req = link.getLinkDestination().to_string() + "\r\n"; std::string conn = host + ":1965"; + if(link.getLinkDestination().get_port()) { + conn = host + ":" + std::to_string(link.getLinkDestination().get_port()); + } + SSL_CTX* ctx = SSL_CTX_new(TLS_client_method()); BIO* bio = BIO_new_ssl_connect(ctx); @@ -24,24 +31,42 @@ Site* GeminiClient::getNetworkedSite(Link link) { return nullptr; } + // timeout, 10 seconds + int fd = -1; + if (BIO_get_fd(bio, &fd) >= 0 && fd >= 0) { + timeval tv{10, 0}; + setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof tv); + setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof tv); + } + BIO_write(bio, req.data(), (int)req.size()); std::string response; char buf[4096]; int n; - while ((n = BIO_read(bio, buf, sizeof buf)) > 0) + while ((n = BIO_read(bio, buf, sizeof buf)) > 0) { response.append(buf, n); + } BIO_free_all(bio); SSL_CTX_free(ctx); int nl = response.find("\n"); + if (nl == -1) { + return nullptr; + } + + std::size_t end = nl; + + if (end > 0 && response[end - 1] == '\r') { + end -= 1; + } - std::string status = response.substr(0,nl+1); - response = response.substr(nl+1); + std::string status = response.substr(0, end); + std::string body = response.substr(nl + 1); - return new Site(status, response); + return new Site(status, body); } @@ -58,7 +83,7 @@ Site* GeminiClient::fetchSite(Link link) { unreach->setUnreachable(); return unreach; } - } else if (isPrefixed(destination, "file://")){ + } else if (isPrefixed(destination, "file://")){ // TODO: This seems wrong; it should probably be fullpath with that prefix. destination = destination.substr(7, destination.size() - 7); std::string fileStr = ""; try { @@ -68,8 +93,11 @@ Site* GeminiClient::fetchSite(Link link) { } // TODO: how should I discern file types? return new Site {"20 text/gemini\r\n", fileStr}; + } else if(isPrefixed(destination, "about:")){ + return new Site {"20 text/gemini\r\n", getNewTab()}; } else { throw NotImplemented(); } + throw NotImplemented(); } diff --git a/src/link.cpp b/src/link.cpp @@ -1,5 +1,4 @@ #include "../include/link.hpp" -#include <iostream> #include <optional> #include <string> #include "../include/utils.hpp" @@ -71,7 +70,7 @@ uri Link::parseDestination(std::string destination, std::optional<uri> prior) { } } else { - destination = "file://" + destination; + destination = "file://" + destination; // TODO: this should only be done in the context of starting... } } try { diff --git a/src/main.cpp b/src/main.cpp @@ -37,10 +37,10 @@ struct DrawState { int y; std::vector<std::pair<std::string, int>> strLs; bool handleInput; - bool handleLinkFollow; + bool handleOpenOther; bool handleRedirect; std::string redirInput; - std::string linkInput; + std::string openOtherInput; std::string userInput; }; @@ -50,68 +50,95 @@ void draw(DrawState ds) { move(0,0); clear(); - if(ds.handleInput) { - addstr("input: "); - addstr(ds.userInput.c_str()); - } else if (ds.handleRedirect) { + if (ds.handleRedirect) { addstr("Follow redirect (y/n): "); addstr(ds.redirInput.c_str()); - } else if (ds.handleLinkFollow) { + } else { for(int i = ds.y;i-ds.y+1 < LINES && i < ds.strLs.size(); ++i) { move(i - ds.y, 0); attron(COLOR_PAIR(ds.strLs[i].second + 1)); addstr(ds.strLs[i].first.c_str()); attroff(COLOR_PAIR(ds.strLs[i].second + 1)); } + } - move(LINES-1, 0); - addstr("Following: "); - addstr(ds.linkInput.c_str()); + if(ds.handleInput) { + move(LINES/2-1, COLS/4); + for(int i = 0; i < COLS/2; ++i) { + addstr("-"); + } + move(LINES/2, COLS/4); + addstr("input: "); + addstr(ds.userInput.c_str()); + move(LINES/2 + 1, COLS/4); + for(int i = 0; i < COLS/2; ++i) { + addstr("-"); + } + } - } else { - for(int i = ds.y;i-ds.y+1 < LINES && i < ds.strLs.size(); ++i) { - move(i - ds.y, 0); - attron(COLOR_PAIR(ds.strLs[i].second + 1)); - addstr(ds.strLs[i].first.c_str()); - attroff(COLOR_PAIR(ds.strLs[i].second + 1)); + if (ds.handleOpenOther) { + move(LINES/2-1, COLS/4); + for(int i = 0; i < COLS/2; ++i) { + addstr("-"); + } + move(LINES/2, COLS/4); + addstr("Destination / Link Number: "); + addstr(ds.openOtherInput.c_str()); + + move(LINES/2 + 1, COLS/4); + for(int i = 0; i < COLS/2; ++i) { + addstr("-"); } } + + refresh(); + } int lowestPos(std::vector<std::pair<std::string, int>>& strLs) { return strLs.size() - LINES; } -int linkHandler(DrawState ds) { +std::string openPageHandler(DrawState ds) { - ds.handleLinkFollow = true; + ds.handleOpenOther = true; draw(ds); + std::string acc = ""; - int acc = 0; - int num = 0; - int itr = 0; - - while(num!=-1) { + while(true) { int sel = getch(); - num = (sel - 0x30); - if(sel == '\n' || sel == KEY_ENTER) { - num = -1; + if(sel == KEY_BACKSPACE) { + if(acc.size() > 0) { + acc = acc.substr(0,acc.size() - 1); + } + ds.openOtherInput = acc; + draw(ds); continue; } - if(num > 9 || num < 0) { - continue; + if(sel == 27) { + acc = ""; + break; + } + + if(sel == '\n' || sel == KEY_ENTER) { + break; } - acc = (acc * 10) + num; - itr += 1; - ds.linkInput = std::to_string(acc); + acc += std::string {(char)sel}; + + ds.openOtherInput = acc; draw(ds); } - ds.handleLinkFollow = false; + + ds.openOtherInput = acc; + ds.handleOpenOther = false; + draw(ds); + + return acc; } @@ -125,6 +152,7 @@ Direction handleRedir(DrawState ds) { ds.handleRedirect = true; ds.redirInput = ""; + draw(ds); while(true) { int input = getch(); @@ -144,6 +172,7 @@ Direction handleRedir(DrawState ds) { } std::string handleUserInput(DrawState ds) { + ds.handleInput = true; draw(ds); std::string acc = ""; @@ -182,6 +211,7 @@ int main(int argc, char** argv) { DrawState ds {}; initscr(); + set_escdelay(25); noecho(); // don't echo user inputs cbreak(); // make C-c and C-z work curs_set(0); // hide cursor @@ -192,10 +222,12 @@ int main(int argc, char** argv) { use_default_colors(); initColors(); + b.goToSite("about:newtab",true); + if(argc > 1) { b.goToSite(argv[1],true); } else { - b.goToSite("gemini://tlgs.one/search/2?test",true); + b.goToSite("gemini://tlgs.one",true); } auto current = b.renderSite(); @@ -231,15 +263,22 @@ int main(int argc, char** argv) { current = b.renderSite(); removeNonAscii(current); - } else if(input == ' ') { - int linkToFollow = linkHandler(ds); - if(linkToFollow != -1) { - b.followLinkNumber(linkToFollow); + } else if(input == 'o') { + std::string locationToGo = openPageHandler(ds); + + // TODO: Check if this is an int and if it is try to use that link. + // if it's not, then try to go to domain (should consider having a default search engine too.) + + try { + b.followLinkNumber(std::stoi(locationToGo)); + } catch (...) { + if(locationToGo != "") { + b.goToSite(locationToGo, true); + } } } - if(b.getCurrentSite()->getStatusCode() >= 10 && b.getCurrentSite()->getStatusCode() <= 19) { std::string inputQuery = handleUserInput(ds); if(inputQuery != "?") { // TODO: Better handling @@ -265,10 +304,7 @@ int main(int argc, char** argv) { y = std::max(0,std::min(y,lowestPos(current))); ds.y = y; ds.strLs = current; - draw(ds); - - refresh(); input = getch(); } diff --git a/src/site.cpp b/src/site.cpp @@ -1,5 +1,5 @@ #include "../include/site.hpp" -#include <iostream> +#include <algorithm> #include <string> Site::Site(std::string header, std::string body) { @@ -21,6 +21,7 @@ uint32_t Site::getStatusCode() { std::string Site::getMeta() { std::string meta = header.substr(3); + std::replace(meta.begin(), meta.end(), '\n', ' '); return meta; } diff --git a/src/utils.cpp b/src/utils.cpp @@ -7,7 +7,6 @@ #include <fstream> #include <iomanip> #include <ios> -#include <iostream> #include <sstream> #include <string> #include <vector> @@ -91,3 +90,16 @@ std::string urlEncode(const std::string& value) { return escaped.str(); } + +std::string getNewTab() { + std::string st = + "# New Tab\n" + "\n" + "This is a new tab. We have a few keybindings around here:\n" + "\n" + "* b -> back a page\n" + "* f -> forward a page\n" + "* o -> show url entry / link selection\n" + ; + return st; +} diff --git a/tests/fetch-test.cpp b/tests/fetch-test.cpp @@ -70,7 +70,7 @@ TEST_CASE("Send basic gemini requests") { auto ln = Link{"=> gemini://laack.co"}; Site* s = client.fetchSite(ln); REQUIRE(s->getStatusCode() == 20); - REQUIRE(s->getHeader() == "20 text/gemini;lang=en-US\r\n"); + REQUIRE(s->getHeader() == "20 text/gemini;lang=en-US"); delete s; }