From c4bb96adb02b83200ce709bd6d785a28d333b92b Mon Sep 17 00:00:00 2001 From: acetone Date: Tue, 22 Nov 2022 12:24:41 +0300 Subject: [PATCH] display traffic volume since last visit --- src/g.cpp | 1 + src/g.h | 1 + src/html/index.html | 2 +- src/httpdocument.cpp | 10 +++++++--- src/httpdocument.h | 3 ++- src/socketrunnable.cpp | 16 ++++++++++++++-- src/webpage.cpp | 27 ++++++++++++++++++++++++--- src/webpage.h | 4 ++-- 8 files changed, 52 insertions(+), 12 deletions(-) diff --git a/src/g.cpp b/src/g.cpp index 227cd98..c0d0934 100644 --- a/src/g.cpp +++ b/src/g.cpp @@ -36,6 +36,7 @@ namespace c { const qint64 DB_DAILY_TOP_TABLE_ACTUALIZE_MINIMAL_INTERVAL_MS = 300000; // 5 min const qint64 CACHE_ACTUALIZE_TOP_LISTS_FROM_DB_MINIMAL_INTERVAL_MS = 5000; // 5 sec const QString HA_PAGE_TITLE = "{{PAGE_TITLE}}"; + const QString HA_TRAFFIC_SECTION_TITLE = "{{TRAFFIC_SECTION_TITLE}}"; const QString HA_CUSTOM_CSS = "{{CUSTOM_CSS}}"; const QString HA_DAILY_UPLOAD = "{{DAILY_UPLOAD}}"; const QString HA_DAILY_UPLOAD_DECIMAL = "{{DAILY_UPLOAD_DECIMAL}}"; diff --git a/src/g.h b/src/g.h index 1ae4c59..5682481 100644 --- a/src/g.h +++ b/src/g.h @@ -38,6 +38,7 @@ namespace c /* for Constants */ { extern const qint64 DB_DAILY_TOP_TABLE_ACTUALIZE_MINIMAL_INTERVAL_MS; extern const qint64 CACHE_ACTUALIZE_TOP_LISTS_FROM_DB_MINIMAL_INTERVAL_MS; extern const QString HA_PAGE_TITLE; + extern const QString HA_TRAFFIC_SECTION_TITLE; extern const QString HA_CUSTOM_CSS; extern const QString HA_DAILY_UPLOAD; extern const QString HA_DAILY_UPLOAD_DECIMAL; diff --git a/src/html/index.html b/src/html/index.html index bf2e59e..a86d36e 100644 --- a/src/html/index.html +++ b/src/html/index.html @@ -14,7 +14,7 @@
- Traffic + {{TRAFFIC_SECTION_TITLE}}
diff --git a/src/httpdocument.cpp b/src/httpdocument.cpp index 2963e3c..2f1ebef 100644 --- a/src/httpdocument.cpp +++ b/src/httpdocument.cpp @@ -53,6 +53,7 @@ const QString HttpDocument::CommonHeader::ContentType = "Content-Type"; const QString HttpDocument::CommonHeader::ContentLength = "Content-Length"; const QString HttpDocument::CommonHeader::ETag = "ETag"; const QString HttpDocument::CommonHeader::ProcessingDuration = "Processing-duration"; +const QString HttpDocument::CommonHeader::SetCookie = "Set-Cookie"; HttpDocument::HttpDocument() : m_processingDurationStartMarker(QDateTime::currentMSecsSinceEpoch()) @@ -67,7 +68,11 @@ void HttpDocument::setCode(const QString &code) void HttpDocument::setHeader(const QString &name, const QString &value) { - if (name.isEmpty() or value.isEmpty()) return; + if (name.isEmpty() or value.isEmpty()) + { + qWarning() << "HTTP header setting rejected (empty value):" << name << ";" << value; + return; + } m_headers.insert(name, value); } @@ -159,8 +164,7 @@ QByteArray HttpDocument::document() const QByteArray result = "HTTP/1.0 " + code().toUtf8() + HTML_NEWLINE; - auto h = headers(); - QMapIterator iter(h); + QMapIterator iter( headers() ); while (iter.hasNext()) { iter.next(); diff --git a/src/httpdocument.h b/src/httpdocument.h index c14e128..27e39a0 100644 --- a/src/httpdocument.h +++ b/src/httpdocument.h @@ -33,6 +33,7 @@ public: static const QString ContentLength; static const QString ETag; static const QString ProcessingDuration; + static const QString SetCookie; }; struct ContentType @@ -68,7 +69,7 @@ public: QString code() const { return m_code; } void setHeader(const QString& name, const QString& value); - QMap headers() const { return m_headers; } + const QMap& headers() const { return m_headers; } void setBody(const QByteArray& body); void setBody(const QJsonObject& body); diff --git a/src/socketrunnable.cpp b/src/socketrunnable.cpp index 8527f51..1221d8e 100644 --- a/src/socketrunnable.cpp +++ b/src/socketrunnable.cpp @@ -26,6 +26,8 @@ along with this program. If not, see . constexpr const int getValueBeforeSpaceLIMIT = 200; constexpr const int getValueBeforeBodyLIMIT = 1000; +constexpr const char* lastTrafficVolumeCookie = "trafficvolume"; + SocketRunnable::SocketRunnable(qintptr socketDescriptor, QObject *parent) : SocketRunnableBase(socketDescriptor, parent) { @@ -35,7 +37,17 @@ void SocketRunnable::get() { if (urlPath() == "/index.html") { - httpDocument().setBody(WebPage::document()); + quint64 lastTrafficVolume = 0; + const QString cookieString = g::getValue(m_headers, "Cookie", g::GetValueType::HttpHeader); + if (not cookieString.isEmpty()) + { + lastTrafficVolume = g::getValue(m_headers, lastTrafficVolumeCookie).toULongLong(); + } + + quint64 currentTrafficVolume = 0; + httpDocument().setBody( WebPage::document(lastTrafficVolume, ¤tTrafficVolume) ); + httpDocument().setHeader(HttpDocument::CommonHeader::SetCookie, + QString(lastTrafficVolumeCookie)+"="+QString::number(currentTrafficVolume)+"; max-age=31536000;"); return; } @@ -79,7 +91,7 @@ void SocketRunnable::reader() } else if (reqType != "HEAD") { - httpDocument().setCode(HttpDocument::Code::_400); // default code is 200 + httpDocument().setCode(HttpDocument::Code::_400); } setDataToWrite(httpDocument().document()); diff --git a/src/webpage.cpp b/src/webpage.cpp index 5a3062e..4007c9d 100644 --- a/src/webpage.cpp +++ b/src/webpage.cpp @@ -89,7 +89,7 @@ struct HumanReadableValue QString measure = "anon"; }; -QByteArray WebPage::document() +QByteArray WebPage::document(quint64 lastTrafficVolume, quint64* currentTrafficVolume) { QFile src(":/html/index.html"); if (not src.open(QIODevice::ReadOnly)) @@ -104,12 +104,18 @@ QByteArray WebPage::document() setTitle(page); customStyles(page); lastDestinations(page); - trafficBlock(page); topDestinations(page); blockedDestinations(page); informationBlock(page); footer(page); + quint64 traffic = trafficBlock(page, lastTrafficVolume); + + if (currentTrafficVolume != nullptr) + { + *currentTrafficVolume = traffic; + } + return page.toUtf8(); } @@ -164,8 +170,21 @@ void WebPage::lastDestinations(QString &document) document.replace(g::c::HA_LAST_DESTINATIONS_LIST, destinationsBlock); } -void WebPage::trafficBlock(QString &document) +quint64 WebPage::trafficBlock(QString &document, quint64 lastTrafficVolume) { + quint64 currentVolume = Statistics::totalUpload() + Statistics::totalDownload(); + if (lastTrafficVolume != 0 and currentVolume > lastTrafficVolume) + { + quint64 difference = currentVolume - lastTrafficVolume; + auto differenceHr = bytesToHumanReadableString(difference); + document.replace(g::c::HA_TRAFFIC_SECTION_TITLE, "Since your last visit: " + + differenceHr.integer+"."+differenceHr.decimal+" "+differenceHr.measure); + } + else + { + document.replace(g::c::HA_TRAFFIC_SECTION_TITLE, "Traffic"); + } + auto dailyUpload = bytesToHumanReadableString( Statistics::dailyUpload() ); document.replace(g::c::HA_DAILY_UPLOAD, dailyUpload.integer); document.replace(g::c::HA_DAILY_UPLOAD_DECIMAL, dailyUpload.decimal); @@ -181,6 +200,8 @@ void WebPage::trafficBlock(QString &document) auto totalDownload = bytesToHumanReadableString( Statistics::totalDownload() ); document.replace(g::c::HA_TOTAL_DOWNLOAD, totalDownload.integer + " " + totalDownload.measure); + + return currentVolume; } void WebPage::topDestinations(QString &document) diff --git a/src/webpage.h b/src/webpage.h index af59d45..f7e9006 100644 --- a/src/webpage.h +++ b/src/webpage.h @@ -28,13 +28,13 @@ class WebPage { public: WebPage() = delete; - static QByteArray document(); + static QByteArray document(quint64 lastTrafficVolume, quint64* currentTrafficVolume = nullptr); private: static void setTitle(QString& document); static void customStyles(QString& document); static void lastDestinations(QString& document); - static void trafficBlock(QString& document); + static quint64 trafficBlock(QString& document, quint64 lastTrafficVolume); static void topDestinations(QString& document); static void blockedDestinations(QString& document); static void informationBlock(QString& document);