foreseen

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

browser.cpp (13974B)


      1 #include "../include/browser.hpp"
      2 
      3 #include <unistd.h>
      4 
      5 #include <cassert>
      6 #include <cstddef>
      7 #include <cstdlib>
      8 #include <filesystem>
      9 #include <optional>
     10 #include <stdexcept>
     11 #include <string>
     12 #include <thread>
     13 #include <utility>
     14 #include <vector>
     15 
     16 #include "../include/gemini-client.hpp"
     17 #include "../include/identity-manager.hpp"
     18 #include "../include/shared.hpp"
     19 #include "../include/site.hpp"
     20 #include "../include/utils.hpp"
     21 
     22 std::string Browser::tryDownloadPage(std::string downloadDir) noexcept {
     23     std::string body = currentSite->getBody();
     24     Link* current = getCurrentLink();
     25     assert(
     26         current !=
     27         nullptr);  // calling download page should always happen from a page...
     28 
     29     std::string destination = encodeAsFilename(current->getLinkDestination());
     30 
     31     try {
     32         if (downloadDir != "") {
     33             std::filesystem::path pth = std::filesystem::path(downloadDir);
     34             std::filesystem::create_directories(pth);
     35             destination = pth / destination;
     36         }
     37         writeStringToFile(body, destination);
     38     } catch (...) {
     39         return "";
     40     }
     41 
     42     return destination;
     43 }
     44 
     45 void dispatch(std::vector<Link>* targets, Browser& b, int threadIdx) {
     46     std::vector<Link>& refT = *targets;
     47     for (int i = 0; i < (int)refT.size() && i < SITE_CACHE_LIMIT; ++i) {
     48         auto& target = refT[i];
     49 
     50         auto id = b.getIdentity(target.getLinkDestination());
     51         if (id.crtPath != "" || id.keyPath != "") {
     52             continue;  // don't try to prefetch for domains we normally pass a
     53                        // cert to.
     54         }
     55         b.justCacheSite(target);
     56     }
     57     b.setDone(threadIdx);
     58     delete targets;
     59 }
     60 
     61 void Browser::setDone(int threadIdx) { done[threadIdx] = true; }
     62 
     63 void Browser::tryCacheTargets() {
     64     bool dispatched = false;
     65     for (int i = 0; i < THREAD_NUM && dispatched == false; ++i) {
     66         if (done[i]) {
     67             if (threads[i].joinable()) {
     68                 threads[i].join();
     69             }
     70             auto* lls = getLinkLines();
     71             done[i] = false;
     72             threads[i] = std::thread(dispatch, lls, std::ref(*this), i);
     73             dispatched = true;
     74         }
     75     }
     76 }
     77 
     78 Site* Browser::findInCacheAndPromoteIfRelevant(std::string& urlString) {
     79     Site* site = nullptr;
     80     std::optional<Site> cachedSite = visitedCache->getSite(urlString);
     81     if (cachedSite != std::nullopt) {
     82         site = new Site(*cachedSite);
     83     }
     84     if (site == nullptr) {
     85         std::optional<Site> cached = preFetchCache->getSite(urlString);
     86         if (cached != std::nullopt) {
     87             site = new Site(*cached);
     88             visitedCache->addSite(urlString, *cached);
     89         }
     90     }
     91 
     92     return site;
     93 }
     94 
     95 void Browser::refresh() {
     96     goToSite(getPriorUri().value().to_string(), false, true);
     97 }
     98 
     99 SiteLoadPair Browser::goToSite(std::string url, bool addToHistory,
    100                                bool refresh) {
    101     Link* prior = nullptr;
    102 
    103     if ((int)siteHistory.size() > previousIdx && previousIdx >= 0) {
    104         prior = siteHistory[previousIdx];
    105     }
    106 
    107     auto client = GeminiClient{};
    108 
    109     Link* destination = nullptr;
    110 
    111     if (prior == nullptr) {
    112         destination = new Link{url};
    113     } else {
    114         destination = new Link{url, prior->getLinkDestination()};
    115     }
    116 
    117     std::string urlString = destination->getLinkDestination().to_string();
    118     std::string scheme = destination->getLinkDestination().get_scheme();
    119     if (scheme != "gemini" && scheme != "file" &&
    120         scheme != "about") {  //  TODO: Should  I use about or just a fs file?
    121         if (openThread.joinable()) {
    122             openThread.join();
    123         }
    124 
    125         // this doesn't have to be blocking...
    126         // my browser hangs very often so yea.
    127         openThread = std::thread(openUrl, urlString);
    128 
    129         delete destination;
    130         return SiteLoadPair{SITE_LOADED, OPENED_EXT_TXT};
    131     }
    132 
    133     Site* site = nullptr;
    134 
    135     Identity id =
    136         identityManager.getIdentityForURI(destination->getLinkDestination());
    137 
    138     if (urlString.find("gemini://") != std::string::npos && !refresh) {
    139         if (id.crtPath != "" && id.keyPath != "") {
    140             if (!addToHistory) {
    141                 site = findInCacheAndPromoteIfRelevant(urlString);
    142             }
    143         } else {
    144             site = findInCacheAndPromoteIfRelevant(urlString);
    145         }
    146     }
    147 
    148     if (site == nullptr) {
    149         site = client.fetchSite(*destination, id.crtPath, id.keyPath);
    150     }
    151 
    152     if (site == nullptr || site->getUnreachable()) {
    153         if (site != nullptr) {
    154             delete site;
    155         }
    156         delete destination;
    157         if (site == nullptr) {
    158             return SiteLoadPair{SITE_PERMANENT_FAILURE, "Connection error"};
    159         }
    160         return SiteLoadPair{SITE_PERMANENT_FAILURE, site->getMeta()};
    161     }
    162 
    163     int sc = site->getStatusCode();
    164     std::string meta = site->getMeta();
    165     if (sc < 10 || sc >= 40) {
    166         delete site;
    167         delete destination;
    168 
    169         if (sc < 10 || sc > 69) {
    170             return SiteLoadPair{SITE_UNEXPECTED_STATUS_CODE, meta};
    171         }
    172 
    173         if (sc < 50) {  // 40 -> 49
    174             return SiteLoadPair{SITE_TEMPORARY_FAILURE, meta};
    175         }
    176         if (sc < 60) {  // 50 -> 59
    177             return SiteLoadPair{SITE_PERMANENT_FAILURE, meta};
    178         }
    179 
    180         if (sc == 60) {
    181             return SiteLoadPair{SITE_REQUIRES_CERTIFICATE, meta};
    182         }
    183 
    184         if (sc == 61) {
    185             return SiteLoadPair{SITE_REJECTED_CERTIFICATE, meta};
    186         }
    187         if (sc == 62) {
    188             return SiteLoadPair{SITE_INVALID_CERTIFICATE, meta};
    189         }
    190 
    191         return SiteLoadPair{SITE_REQUIRES_CERTIFICATE, meta};
    192     }
    193 
    194     if (addToHistory) {
    195         while ((int)siteHistory.size() > previousIdx + 1) {
    196             delete siteHistory[siteHistory.size() - 1];
    197             siteHistory.pop_back();
    198         }
    199         siteHistory.push_back(destination);
    200         previousIdx = siteHistory.size() - 1;
    201     }
    202 
    203     if (currentSite != nullptr) {
    204         delete currentSite;
    205     }
    206     currentSite = site;
    207 
    208     if (urlString.find("gemini://") != std::string::npos) {
    209         int sc = site->getStatusCode();
    210         if (sc >= 20 && sc <= 29) {
    211             visitedCache->addSite(urlString, *site);
    212         }
    213     }
    214 
    215     for (auto* line : lines) {
    216         delete line;
    217     }
    218 
    219     lines = toLines(site);
    220 
    221     setLinksOfCurrentLines();
    222     previousStatusCodes[destination->getLinkDestination().to_string()] =
    223         site->getStatusCode();
    224     if (!addToHistory) {
    225         delete destination;
    226     }
    227 
    228     tryCacheTargets();
    229     return SiteLoadPair{SITE_LOADED, meta};
    230 }
    231 
    232 Identity Browser::getIdentity(uri uriInput) {
    233     return this->identityManager.getIdentityForURI(uriInput);
    234 }
    235 
    236 void Browser::justCacheSite(Link link) {
    237     auto client = GeminiClient{};
    238 
    239     std::string urlString = link.getLinkDestination().to_string();
    240 
    241     if (visitedCache->getSite(urlString) || preFetchCache->getSite(urlString)) {
    242         return;
    243     }
    244 
    245     Site* site = nullptr;
    246 
    247     if (urlString.find("gemini://") != std::string::npos) {
    248         site = client.fetchSite(link);
    249     }
    250 
    251     if (site != nullptr) {
    252         int sc = site->getStatusCode();
    253         if (sc >= 20 && sc <= 29) {
    254             preFetchCache->addSite(urlString, *site);
    255         }
    256         delete site;
    257     }
    258 
    259     return;
    260 }
    261 
    262 void Browser::setLinksOfCurrentLines() {
    263     links = std::vector<std::size_t>{};
    264     for (std::size_t i = 0; i < lines.size(); ++i) {
    265         if (lines[i]->type() == LINK) {
    266             links.push_back(i);
    267         }
    268     }
    269 }
    270 
    271 // TODO: This is a pure function.
    272 std::vector<Line*> Browser::toLines(Site* site) {
    273     auto lines = stringToList(site->getBody());
    274 
    275     std::vector<Line*> res{};
    276 
    277     int lc = 1;
    278 
    279     bool isPreformatted = false;
    280     for (auto line : lines) {
    281         res.push_back(lineToLine(line, getPriorUri(), lc, isPreformatted));
    282         if (res[res.size() - 1]->type() == FORMAT_SWITCH) {
    283             isPreformatted = !isPreformatted;
    284         }
    285         if (res[res.size() - 1]->type() == LINK) {
    286             lc += 1;
    287         }
    288     }
    289     return res;
    290 }
    291 
    292 Browser::Browser() : threads(THREAD_NUM), done(THREAD_NUM) {
    293     currentSite = nullptr;
    294     visitedCache = new Cache{};
    295     preFetchCache = new Cache{};
    296     for (auto& d : done) {
    297         d = true;
    298     }
    299 
    300     // this ensures some nice invariants about the browser, like always having
    301     // at least one valid page.
    302     SiteLoadPair start = goToSite("about://newtab");
    303     if (start.result != SITE_LOADED) {
    304         throw std::runtime_error("Browser unexpectedly failed to start.");
    305     }
    306 }
    307 
    308 // TODO: SHould add more stuff here too, like the links stuff.
    309 Browser::~Browser() {
    310     for (auto& t : threads) {
    311         if (t.joinable()) {
    312             t.join();
    313         }
    314     }
    315 
    316     if (openThread.joinable()) {
    317         openThread.join();
    318     }
    319 
    320     delete visitedCache;
    321     delete preFetchCache;
    322     delete currentSite;
    323 
    324     for (auto* line : lines) {
    325         delete line;
    326     }
    327 
    328     for (auto* link : siteHistory) {
    329         delete link;
    330     }
    331 }
    332 
    333 Site* Browser::getCurrentSite() { return currentSite; }
    334 
    335 std::vector<std::pair<std::string, TextRender>> Browser::renderSite() {
    336     std::vector<std::pair<std::string, TextRender>> res{};
    337     res.reserve(lines.size());
    338 
    339     for (auto* line : lines) {
    340         // TODO: Don't special case this; define an interface.
    341         if (line->type() == PREFORMATTED) {
    342             std::pair<std::string, TextRender> cp{
    343                 line->textToDraw(),
    344                 TextRender{line->getColor(), line->isBold(), false}};
    345             res.push_back(cp);
    346         } else {
    347             std::pair<std::string, TextRender> cp{
    348                 line->textToDraw(),
    349                 TextRender{line->getColor(), line->isBold()}};
    350             res.push_back(cp);
    351         }
    352     }
    353     return res;
    354 }
    355 
    356 std::optional<uri> Browser::getPriorUri() {
    357     if (previousIdx < (int)siteHistory.size() && previousIdx >= 0) {
    358         return siteHistory[previousIdx]->getLinkDestination();
    359     }
    360     return std::nullopt;
    361 }
    362 
    363 std::vector<Link>* Browser::getLinkLines() {
    364     std::vector<Link>* res = new std::vector<Link>{};
    365     for (auto& ln : links) {
    366         res->push_back(*dynamic_cast<Link*>(lines[ln]));
    367     }
    368     return res;
    369 }
    370 
    371 SiteLoadPair Browser::followLinkNumber(int linkToFollow) {
    372     if ((int)links.size() > linkToFollow - 1 && linkToFollow - 1 >= 0) {
    373         std::size_t pos = links[linkToFollow - 1];
    374         if (lines.size() > pos) {
    375             Line* ptr = lines[pos];
    376             Link* ptrLnk = dynamic_cast<Link*>(ptr);
    377             SiteLoadPair res =
    378                 goToSite(ptrLnk->getLinkDestination().to_string(), true);
    379             return res;
    380         }
    381     }
    382     return SiteLoadPair{LINK_DOES_NOT_EXIST, "Link not found"};
    383 }
    384 
    385 void Browser::goBack() {
    386     int original = previousIdx;
    387 
    388     // we track this because sometimes sites do this:
    389     // start site
    390     // input something
    391     // redirect back to start site
    392     // and in such cases, I'd expect back and forward to treat the same site as
    393     // one site, but only in cases where they are right next to each other
    394     // without any other 2X status code sites between them.
    395 
    396     std::string starting = getCurrentLink()->getLinkDestination().to_string();
    397 
    398     previousIdx -= 1;
    399 
    400     if ((int)siteHistory.size() > previousIdx && previousIdx >= 0) {
    401         int prSC = previousStatusCodes
    402             [siteHistory[previousIdx]->getLinkDestination().to_string()];
    403         std::string current =
    404             siteHistory[previousIdx]->getLinkDestination().to_string();
    405         while (!(prSC >= 20 && prSC <= 29) || starting == current) {
    406             previousIdx -= 1;
    407             if ((int)siteHistory.size() > previousIdx && previousIdx >= 0) {
    408                 prSC = previousStatusCodes[siteHistory[previousIdx]
    409                                                ->getLinkDestination()
    410                                                .to_string()];
    411                 current =
    412                     siteHistory[previousIdx]->getLinkDestination().to_string();
    413             } else {
    414                 previousIdx = original;
    415                 return;  // safely fail with rollback
    416             }
    417         }
    418         if (!(goToSite(this->siteHistory[previousIdx]
    419                            ->getLinkDestination()
    420                            .to_string(),
    421                        false)
    422                   .result == SITE_LOADED)) {
    423             previousIdx = original;
    424         }
    425 
    426     } else {
    427         previousIdx = original;
    428     }
    429 }
    430 
    431 void Browser::goForward() {
    432     int original = previousIdx;
    433 
    434     std::string starting = getCurrentLink()->getLinkDestination().to_string();
    435 
    436     previousIdx += 1;
    437 
    438     if ((int)siteHistory.size() > previousIdx && previousIdx >= 0) {
    439         int prSC = previousStatusCodes
    440             [siteHistory[previousIdx]->getLinkDestination().to_string()];
    441         std::string current =
    442             siteHistory[previousIdx]->getLinkDestination().to_string();
    443 
    444         while (!(prSC >= 20 && prSC <= 29) || starting == current) {
    445             previousIdx += 1;
    446             if ((int)siteHistory.size() > previousIdx && previousIdx >= 0) {
    447                 prSC = previousStatusCodes[siteHistory[previousIdx]
    448                                                ->getLinkDestination()
    449                                                .to_string()];
    450                 current =
    451                     siteHistory[previousIdx]->getLinkDestination().to_string();
    452             } else {
    453                 previousIdx = original;  // fail safely
    454                 return;
    455             }
    456         }
    457 
    458         if (!(goToSite(this->siteHistory[previousIdx]
    459                            ->getLinkDestination()
    460                            .to_string(),
    461                        false)
    462                   .result == SITE_LOADED)) {
    463             previousIdx = original;
    464         }
    465     } else {
    466         previousIdx = original;
    467     }
    468 }
    469 
    470 Link* Browser::getCurrentLink() {
    471     if (previousIdx >= 0 && previousIdx < (int)siteHistory.size()) {
    472         return this->siteHistory[previousIdx];
    473     }
    474     return nullptr;
    475 }