Fixes pages not being read from cache due to differing trailing slash

Also fixes favourites with the same problem
This commit is contained in:
Mike Skec 2021-01-07 08:35:25 +11:00 committed by Felix Queißner
parent b7f07140cb
commit 50729c19eb
5 changed files with 42 additions and 13 deletions

View File

@ -1,11 +1,13 @@
#include "cachehandler.hpp"
#include "kristall.hpp"
#include "ioutil.hpp"
#include <QDebug>
void CacheHandler::push(const QUrl &url, const QByteArray &body, const MimeType &mime)
void CacheHandler::push(const QUrl &u, const QByteArray &body, const MimeType &mime)
{
QString urlstr = url.toString(QUrl::FullyEncoded | QUrl::RemoveFragment);
QUrl url = IoUtil::uniformUrl(u);
QString urlstr = url.toString(QUrl::FullyEncoded);;
if (this->page_cache.find(urlstr) != this->page_cache.end())
{
@ -34,7 +36,7 @@ std::shared_ptr<CachedPage> CacheHandler::find(const QString &url)
std::shared_ptr<CachedPage> CacheHandler::find(const QUrl &url)
{
return this->find(url.toString(QUrl::FullyEncoded | QUrl::RemoveFragment));
return this->find(IoUtil::uniformUrlString(url));
}
bool CacheHandler::contains(const QString &url) const
@ -44,7 +46,7 @@ bool CacheHandler::contains(const QString &url) const
bool CacheHandler::contains(const QUrl &url) const
{
return this->contains(url.toString(QUrl::FullyEncoded | QUrl::RemoveFragment));
return this->contains(IoUtil::uniformUrlString(url));
}
CacheMap const& CacheHandler::getPages() const

View File

@ -51,14 +51,17 @@ class CacheHandler
public:
void push(QUrl const & url, QByteArray const & body, MimeType const & mime);
std::shared_ptr<CachedPage> find(QString const &url);
std::shared_ptr<CachedPage> find(QUrl const &url);
bool contains(QString const & url) const;
bool contains(QUrl const & url) const;
CacheMap const& getPages() const;
private:
std::shared_ptr<CachedPage> find(QString const &url);
bool contains(QString const & url) const;
private:
// In-memory cache storage.
CacheMap page_cache;

View File

@ -1,4 +1,5 @@
#include "favouritecollection.hpp"
#include "ioutil.hpp"
#include <cassert>
#include <QDebug>
@ -98,7 +99,7 @@ void FavouriteCollection::load(QSettings &settings)
auto fav = std::make_unique<FavouriteNode>();
fav->favourite.title = settings.value("title").toString();
fav->favourite.destination = settings.value("url").toUrl();
fav->favourite.destination = IoUtil::uniformUrl(settings.value("url").toUrl());
group->children.emplace_back(std::move(fav));
}
@ -178,8 +179,9 @@ void FavouriteCollection::editFavouriteTitle(const QModelIndex &index, const QSt
this->getMutableFavourite(index)->title = title;
}
bool FavouriteCollection::editFavouriteTitle(const QUrl &url, const QString &new_title)
bool FavouriteCollection::editFavouriteTitle(const QUrl &u, const QString &new_title)
{
QUrl url = IoUtil::uniformUrl(u);
for(auto const & group : this->root.children)
{
for(auto const & ident : group->children)
@ -197,11 +199,12 @@ bool FavouriteCollection::editFavouriteTitle(const QUrl &url, const QString &new
void FavouriteCollection::editFavouriteDest(const QModelIndex &index, const QUrl &url)
{
this->getMutableFavourite(index)->destination = url;
this->getMutableFavourite(index)->destination = IoUtil::uniformUrl(url);
}
Favourite FavouriteCollection::getFavourite(const QUrl &url) const
Favourite FavouriteCollection::getFavourite(const QUrl &u) const
{
QUrl url = IoUtil::uniformUrl(u);
for(auto const & group : this->root.children)
{
for(auto const & ident : group->children)
@ -345,8 +348,9 @@ QVector<QPair<QString, Favourite const *>> FavouriteCollection::allFavourites()
return identities;
}
bool FavouriteCollection::containsUrl(const QUrl &url) const
bool FavouriteCollection::containsUrl(const QUrl &u) const
{
QUrl url = IoUtil::uniformUrl(u);
for(auto const & group : this->root.children)
{
for(auto const & ident : group->children)
@ -364,12 +368,13 @@ bool FavouriteCollection::addUnsorted(const QUrl &url, const QString &t)
return false;
return addFavourite(tr("Unsorted"), Favourite {
t,
url,
IoUtil::uniformUrl(url),
});
}
bool FavouriteCollection::removeUrl(const QUrl &url)
bool FavouriteCollection::removeUrl(const QUrl &u)
{
QUrl url = IoUtil::uniformUrl(u);
for(auto const & group : this->root.children)
{
size_t index = 0;

View File

@ -34,3 +34,18 @@ QString IoUtil::size_human(qint64 size)
}
return QString().setNum(num,'f',2)+" "+unit;
}
QUrl IoUtil::uniformUrl(QUrl url)
{
// We have to manually strip the root path slash
// since StripTrailingSlash doesn't strip it for some reason.
if (url.path() == "/") url.setPath(QString { });
// This will strip slashes from non-root paths
return url.adjusted(QUrl::RemoveFragment | QUrl::StripTrailingSlash);
}
QString IoUtil::uniformUrlString(QUrl url)
{
return IoUtil::uniformUrl(url).toString(QUrl::FullyEncoded);
}

View File

@ -2,12 +2,16 @@
#define IOUTIL_HPP
#include <QIODevice>
#include <QUrl>
struct IoUtil
{
static bool writeAll(QIODevice & dst, QByteArray const & src);
static QString size_human(qint64 size);
static QUrl uniformUrl(QUrl url);
static QString uniformUrlString(QUrl url);
};
#endif // IOUTIL_HPP