kio: insert Last-Modified header when serving files from KHTTP

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2022-12-26 21:49:11 +02:00
parent b1522a35dd
commit f6ef1e42f3

View file

@ -26,6 +26,7 @@
#include <QCoreApplication>
#include <QThread>
#include <QFile>
#include <QFileInfo>
#include <limits.h>
@ -241,6 +242,14 @@ static QByteArray HTTPStatusToBytes(const ushort httpstatus)
return QByteArray("OK");
}
static const QByteArray HTTPDate(const QDateTime &datetime)
{
Q_ASSERT(datetime.timeSpec() == Qt::UTC);
QByteArray httpdate = datetime.toString("ddd, dd MMM yyyy hh:mm:ss").toAscii();
httpdate.append(" GMT");
return httpdate;
}
static QByteArray HTTPStatusToContent(const ushort httpstatus)
{
QByteArray httpdata("<html>\n");
@ -256,8 +265,8 @@ static KHTTPHeaders HTTPHeaders(const QString &serverid, const bool authenticate
const QByteArray httpserver = serverid.toAscii();
KHTTPHeaders khttpheaders;
khttpheaders.insert("Server", httpserver);
const QString httpdate = QDateTime::currentDateTimeUtc().toString("ddd, dd MMM yyyy hh:mm:ss") + QLatin1String(" GMT");
khttpheaders.insert("Date", httpdate.toAscii());
const QByteArray httpdate = HTTPDate(QDateTime::currentDateTimeUtc());
khttpheaders.insert("Date", httpdate);
// optional for anything but 405, see:
// https://www.rfc-editor.org/rfc/rfc9110.html#section-10.2.1
khttpheaders.insert("Allow", "GET");
@ -463,6 +472,20 @@ void KHTTPPrivate::slotNewConnection()
return;
}
bool haslastmodified = false;
foreach (const QByteArray &httpkey, khttpheaders.keys()) {
if (qstricmp(httpkey.constData(), "Last-Modified") == 0) {
haslastmodified = true;
break;
}
}
if (!haslastmodified) {
kDebug(s_khttpdebugarea) << "adding Last-Modified";
const QDateTime responsefilelastmodified = QFileInfo(responsefilepath).lastModified();
const QByteArray httpfilelastmodified = HTTPDate(responsefilelastmodified.toUTC());
khttpheaders.insert("Last-Modified", httpfilelastmodified);
}
kDebug(s_khttpdebugarea) << "sending file to client" << responsefilepath << khttpheaders;
const QByteArray httpdata = HTTPData(responsestatus, khttpheaders, httpfile.size());
client->write(httpdata);