Browse Source

Define and use a proper HTTP header structure

adaptive-webui-19844
Chocobo1 5 years ago
parent
commit
74a2168bc1
No known key found for this signature in database
GPG Key ID: 210D9C873253A68C
  1. 4
      src/base/http/responsebuilder.cpp
  2. 2
      src/base/http/responsebuilder.h
  3. 6
      src/base/http/types.h
  4. 12
      src/webui/webapplication.cpp
  5. 9
      src/webui/webapplication.h

4
src/base/http/responsebuilder.cpp

@ -35,9 +35,9 @@ void ResponseBuilder::status(const uint code, const QString &text)
m_response.status = {code, 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) void ResponseBuilder::print(const QString &text, const QString &type)

2
src/base/http/responsebuilder.h

@ -37,7 +37,7 @@ namespace Http
{ {
public: public:
void status(uint code = 200, const QString &text = QLatin1String("OK")); 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 QString &text, const QString &type = CONTENT_TYPE_HTML);
void print(const QByteArray &data, const QString &type = CONTENT_TYPE_HTML); void print(const QByteArray &data, const QString &type = CONTENT_TYPE_HTML);
void clear(); void clear();

6
src/base/http/types.h

@ -92,6 +92,12 @@ namespace Http
QByteArray data; QByteArray data;
}; };
struct Header
{
QString name;
QString value;
};
struct Request struct Request
{ {
QString version; QString version;

12
src/webui/webapplication.cpp

@ -400,7 +400,7 @@ void WebApplication::sendFile(const QString &path)
const auto it = m_translatedFiles.constFind(path); const auto it = m_translatedFiles.constFind(path);
if ((it != m_translatedFiles.constEnd()) && (lastModified <= it->lastModified)) { if ((it != m_translatedFiles.constEnd()) && (lastModified <= it->lastModified)) {
print(it->data, it->mimeType); print(it->data, it->mimeType);
header(Http::HEADER_CACHE_CONTROL, getCachingInterval(it->mimeType)); header({Http::HEADER_CACHE_CONTROL, getCachingInterval(it->mimeType)});
return; return;
} }
@ -432,7 +432,7 @@ void WebApplication::sendFile(const QString &path)
} }
print(data, mimeType.name()); 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) 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); print(error.message(), Http::CONTENT_TYPE_TXT);
} }
for (const CustomHTTPHeader &prebuiltHeader : asConst(m_prebuiltHeaders)) for (const Http::Header &prebuiltHeader : asConst(m_prebuiltHeaders))
header(prebuiltHeader.name, prebuiltHeader.value); header(prebuiltHeader);
return response(); return response();
} }
@ -562,7 +562,7 @@ void WebApplication::sessionStart()
QByteArray cookieRawForm = cookie.toRawForm(); QByteArray cookieRawForm = cookie.toRawForm();
if (m_isCSRFProtectionEnabled) if (m_isCSRFProtectionEnabled)
cookieRawForm.append("; SameSite=Strict"); cookieRawForm.append("; SameSite=Strict");
header(Http::HEADER_SET_COOKIE, cookieRawForm); header({Http::HEADER_SET_COOKIE, cookieRawForm});
} }
void WebApplication::sessionEnd() void WebApplication::sessionEnd()
@ -576,7 +576,7 @@ void WebApplication::sessionEnd()
delete m_sessions.take(m_currentSession->id()); delete m_sessions.take(m_currentSession->id());
m_currentSession = nullptr; 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 bool WebApplication::isCrossSiteRequest(const Http::Request &request) const

9
src/webui/webapplication.h

@ -156,12 +156,5 @@ private:
bool m_isHostHeaderValidationEnabled; bool m_isHostHeaderValidationEnabled;
bool m_isHttpsEnabled; bool m_isHttpsEnabled;
// Custom HTTP headers QVector<Http::Header> m_prebuiltHeaders;
struct CustomHTTPHeader
{
QString name;
QString value;
};
QVector<CustomHTTPHeader> m_prebuiltHeaders;
}; };

Loading…
Cancel
Save