From 74a2168bc145f7e94559cb49627dda2b5e6523f2 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Sun, 10 May 2020 02:48:21 +0800 Subject: [PATCH] Define and use a proper HTTP header structure --- src/base/http/responsebuilder.cpp | 4 ++-- src/base/http/responsebuilder.h | 2 +- src/base/http/types.h | 6 ++++++ src/webui/webapplication.cpp | 12 ++++++------ src/webui/webapplication.h | 9 +-------- 5 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/base/http/responsebuilder.cpp b/src/base/http/responsebuilder.cpp index fd136a224..6371cf6fc 100644 --- a/src/base/http/responsebuilder.cpp +++ b/src/base/http/responsebuilder.cpp @@ -35,9 +35,9 @@ void ResponseBuilder::status(const uint code, const QString &text) m_response.status = {code, text}; } -void ResponseBuilder::header(const QString &name, const QString &value) +void ResponseBuilder::header(const Header &header) { - m_response.headers[name] = value; + m_response.headers[header.name] = header.value; } void ResponseBuilder::print(const QString &text, const QString &type) diff --git a/src/base/http/responsebuilder.h b/src/base/http/responsebuilder.h index f1053cc81..6de4a0878 100644 --- a/src/base/http/responsebuilder.h +++ b/src/base/http/responsebuilder.h @@ -37,7 +37,7 @@ namespace Http { public: void status(uint code = 200, const QString &text = QLatin1String("OK")); - void header(const QString &name, const QString &value); + void header(const Header &header); void print(const QString &text, const QString &type = CONTENT_TYPE_HTML); void print(const QByteArray &data, const QString &type = CONTENT_TYPE_HTML); void clear(); diff --git a/src/base/http/types.h b/src/base/http/types.h index d63d1bbb1..e6d6d153c 100644 --- a/src/base/http/types.h +++ b/src/base/http/types.h @@ -92,6 +92,12 @@ namespace Http QByteArray data; }; + struct Header + { + QString name; + QString value; + }; + struct Request { QString version; diff --git a/src/webui/webapplication.cpp b/src/webui/webapplication.cpp index 76fbb54b6..2861e2cbb 100644 --- a/src/webui/webapplication.cpp +++ b/src/webui/webapplication.cpp @@ -400,7 +400,7 @@ void WebApplication::sendFile(const QString &path) const auto it = m_translatedFiles.constFind(path); if ((it != m_translatedFiles.constEnd()) && (lastModified <= it->lastModified)) { print(it->data, it->mimeType); - header(Http::HEADER_CACHE_CONTROL, getCachingInterval(it->mimeType)); + header({Http::HEADER_CACHE_CONTROL, getCachingInterval(it->mimeType)}); return; } @@ -432,7 +432,7 @@ void WebApplication::sendFile(const QString &path) } print(data, mimeType.name()); - header(Http::HEADER_CACHE_CONTROL, getCachingInterval(mimeType.name())); + header({Http::HEADER_CACHE_CONTROL, getCachingInterval(mimeType.name())}); } Http::Response WebApplication::processRequest(const Http::Request &request, const Http::Environment &env) @@ -469,8 +469,8 @@ Http::Response WebApplication::processRequest(const Http::Request &request, cons print(error.message(), Http::CONTENT_TYPE_TXT); } - for (const CustomHTTPHeader &prebuiltHeader : asConst(m_prebuiltHeaders)) - header(prebuiltHeader.name, prebuiltHeader.value); + for (const Http::Header &prebuiltHeader : asConst(m_prebuiltHeaders)) + header(prebuiltHeader); return response(); } @@ -562,7 +562,7 @@ void WebApplication::sessionStart() QByteArray cookieRawForm = cookie.toRawForm(); if (m_isCSRFProtectionEnabled) cookieRawForm.append("; SameSite=Strict"); - header(Http::HEADER_SET_COOKIE, cookieRawForm); + header({Http::HEADER_SET_COOKIE, cookieRawForm}); } void WebApplication::sessionEnd() @@ -576,7 +576,7 @@ void WebApplication::sessionEnd() delete m_sessions.take(m_currentSession->id()); m_currentSession = nullptr; - header(Http::HEADER_SET_COOKIE, cookie.toRawForm()); + header({Http::HEADER_SET_COOKIE, cookie.toRawForm()}); } bool WebApplication::isCrossSiteRequest(const Http::Request &request) const diff --git a/src/webui/webapplication.h b/src/webui/webapplication.h index d5f7ffcaa..0ca070f21 100644 --- a/src/webui/webapplication.h +++ b/src/webui/webapplication.h @@ -156,12 +156,5 @@ private: bool m_isHostHeaderValidationEnabled; bool m_isHttpsEnabled; - // Custom HTTP headers - struct CustomHTTPHeader - { - QString name; - QString value; - }; - - QVector m_prebuiltHeaders; + QVector m_prebuiltHeaders; };