foreseen

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

commit a5edbd389086aeac78854d068ebff46b0925edf0
parent 00f98f9fe526f050e4ccb152c8a971c8ada1da97
Author: Andrew Laack <andrew@laack.co>
Date:   Mon, 21 Sep 2026 20:25:42 -0500

Basic tls stuff

Diffstat:
MMakefile | 6+++---
Minclude/gemini-client.hpp | 1+
Minclude/site.hpp | 1+
Msrc/gemini-client.cpp | 45+++++++++++++++++++++++++++++++++++++++++++--
Msrc/main.cpp | 3+--
Msrc/site.cpp | 4++++
Mtests/fetch-test.cpp | 12+++++++++++-
7 files changed, 64 insertions(+), 8 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,5 +1,5 @@ build: - g++ src/main.cpp src/link.cpp src/site.cpp src/utils.cpp src/gemini-client.cpp src/browser.cpp -lCatch2Main -lCatch2 -o browser.out + g++ src/main.cpp src/link.cpp src/site.cpp src/utils.cpp src/gemini-client.cpp src/browser.cpp -lssl -lcrypto -o browser.out install: echo "Not implemented" @@ -8,12 +8,12 @@ clean: echo "Not implemented" browser-test: - g++ tests/browser-test.cpp src/link.cpp src/browser.cpp src/site.cpp src/utils.cpp src/gemini-client.cpp -lCatch2Main -lCatch2 -o test.out + g++ tests/browser-test.cpp src/link.cpp src/browser.cpp src/site.cpp src/utils.cpp src/gemini-client.cpp -lCatch2Main -lCatch2 -lssl -lcrypto -o test.out ./test.out rm test.out fetch-test: - g++ tests/fetch-test.cpp src/link.cpp src/site.cpp src/utils.cpp src/gemini-client.cpp -lCatch2Main -lCatch2 -o test.out + g++ tests/fetch-test.cpp src/link.cpp src/site.cpp src/utils.cpp src/gemini-client.cpp -lCatch2Main -lCatch2 -lssl -lcrypto -o test.out ./test.out rm test.out diff --git a/include/gemini-client.hpp b/include/gemini-client.hpp @@ -4,6 +4,7 @@ #include "link.hpp" class GeminiClient { + Site* getNetworkedSite(Link link); public: Site* fetchSite(Link link); }; diff --git a/include/site.hpp b/include/site.hpp @@ -11,4 +11,5 @@ class Site{ Site(std::string header, std::string body); std::string getBody(); uint32_t getStatusCode(); + std::string getHeader(); }; diff --git a/src/gemini-client.cpp b/src/gemini-client.cpp @@ -2,7 +2,48 @@ #include "../include/site.hpp" #include "../include/utils.hpp" #include "../include/errors.hpp" -#include <iostream> +#include <openssl/ssl.h> + +Site* GeminiClient::getNetworkedSite(Link link) { + + std::string host = link.getLinkDestination().get_host(); + std::string req = link.getLinkDestination().to_string() + "\r\n"; + std::string conn = host + ":1965"; + + SSL_CTX* ctx = SSL_CTX_new(TLS_client_method()); + BIO* bio = BIO_new_ssl_connect(ctx); + + SSL* ssl; + BIO_get_ssl(bio, &ssl); + SSL_set_tlsext_host_name(ssl, host.c_str()); + BIO_set_conn_hostname(bio, conn.c_str()); + + if (BIO_do_connect(bio) <= 0) { + BIO_free_all(bio); + SSL_CTX_free(ctx); + return nullptr; + } + + 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) + response.append(buf, n); + + BIO_free_all(bio); + SSL_CTX_free(ctx); + + + int nl = response.find("\n"); + + std::string status = response.substr(0,nl+1); + response = response.substr(nl+1); + + return new Site(status, response); +} + Site* GeminiClient::fetchSite(Link link) { @@ -10,7 +51,7 @@ Site* GeminiClient::fetchSite(Link link) { // TODO: Actually handle uris if(isPrefixed(destination, "gemini://")) { - throw NotImplemented(); + return getNetworkedSite(link); } else if (isPrefixed(destination, "file://")){ destination = destination.substr(7, destination.size() - 7); diff --git a/src/main.cpp b/src/main.cpp @@ -5,6 +5,5 @@ int main() { Browser b{}; - b.goToSite("tests/sites/basic.gmi"); - b.goToSite("basic_2.gmi"); + b.goToSite("gemini://laack.co"); } diff --git a/src/site.cpp b/src/site.cpp @@ -7,6 +7,10 @@ Site::Site(std::string header, std::string body) { this->header = header; } +std::string Site::getHeader() { + return header; +} + std::string Site::getBody() { return body; } diff --git a/tests/fetch-test.cpp b/tests/fetch-test.cpp @@ -5,7 +5,7 @@ #include "../include/gemini-client.hpp" #include "../include/link.hpp" -TEST_CASE( "Link constructors" ) { +TEST_CASE("Link constructors") { auto l2 = Link{"=> basic_2.gmi Other Page!", uri{"file://tests/sites/basic.gmi"}}; @@ -64,3 +64,13 @@ TEST_CASE("Match status code on missing .gmi files") { delete s; } + +TEST_CASE("Send basic gemini requests") { + auto client = GeminiClient {}; + 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"); + delete s; +} +