Send Date http header

It's not strict required but often expected.
change class to namespace
cleanup header
This commit is contained in:
Chocobo1 2017-04-12 23:57:21 +08:00
parent 9496b2a159
commit 4b2266a8e2
3 changed files with 18 additions and 4 deletions

View file

@ -29,9 +29,12 @@
* Contact : chris@qbittorrent.org * Contact : chris@qbittorrent.org
*/ */
#include "base/utils/gzip.h"
#include "responsegenerator.h" #include "responsegenerator.h"
#include <QDateTime>
#include "base/utils/gzip.h"
using namespace Http; using namespace Http;
QByteArray ResponseGenerator::generate(Response response) QByteArray ResponseGenerator::generate(Response response)
@ -49,6 +52,7 @@ QByteArray ResponseGenerator::generate(Response response)
} }
response.headers[HEADER_CONTENT_LENGTH] = QString::number(response.content.length()); response.headers[HEADER_CONTENT_LENGTH] = QString::number(response.content.length());
response.headers[HEADER_DATE] = httpDate();
QString ret(QLatin1String("HTTP/1.1 %1 %2\r\n%3\r\n")); QString ret(QLatin1String("HTTP/1.1 %1 %2\r\n%3\r\n"));
@ -64,3 +68,12 @@ QByteArray ResponseGenerator::generate(Response response)
return ret.toUtf8() + response.content; return ret.toUtf8() + response.content;
} }
QString ResponseGenerator::httpDate()
{
// [RFC 7231] 7.1.1.1. Date/Time Formats
// example: "Sun, 06 Nov 1994 08:49:37 GMT"
return QLocale::c().toString(QDateTime::currentDateTimeUtc(), QLatin1String("ddd, dd MMM yyyy HH:mm:ss"))
.append(QLatin1String(" GMT"));
}

View file

@ -37,10 +37,10 @@
namespace Http namespace Http
{ {
class ResponseGenerator namespace ResponseGenerator
{ {
public: QByteArray generate(Response response);
static QByteArray generate(Response response); QString httpDate();
}; };
} }

View file

@ -47,6 +47,7 @@ namespace Http
const QString HEADER_X_XSS_PROTECTION = "X-XSS-Protection"; const QString HEADER_X_XSS_PROTECTION = "X-XSS-Protection";
const QString HEADER_X_CONTENT_TYPE_OPTIONS = "X-Content-Type-Options"; const QString HEADER_X_CONTENT_TYPE_OPTIONS = "X-Content-Type-Options";
const QString HEADER_CONTENT_SECURITY_POLICY = "Content-Security-Policy"; const QString HEADER_CONTENT_SECURITY_POLICY = "Content-Security-Policy";
const QString HEADER_DATE = "Date";
const QString CONTENT_TYPE_CSS = "text/css; charset=UTF-8"; const QString CONTENT_TYPE_CSS = "text/css; charset=UTF-8";
const QString CONTENT_TYPE_GIF = "image/gif"; const QString CONTENT_TYPE_GIF = "image/gif";