From 5e650ba413d2fc730ae923802bf4ebd2e694f0e3 Mon Sep 17 00:00:00 2001 From: Morten Sorvig Date: Mon, 7 Sep 2009 06:25:35 +0200 Subject: Pre-load the static files at server startup --- src/webclientserver.cpp | 39 +++++++++++++++++---------------------- src/webclientserver.h | 2 ++ 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/src/webclientserver.cpp b/src/webclientserver.cpp index 0e32717..bb80223 100644 --- a/src/webclientserver.cpp +++ b/src/webclientserver.cpp @@ -418,6 +418,21 @@ FileServer::FileServer() << ":qwebclient.css" << ":dojo.js" << ":json2.js" << ":draghandler.js" << ":sessionhandler.js" << ":eventhandler.js"; + + foreach (const QString &fileName, allowedFileNames) { + QFile file(fileName); + if (file.exists() == false) { +// DEBUG << "no file" << filePath; + continue; + } + + file.open(QIODevice::ReadOnly); + QByteArray fileContents = file.readAll(); + //fileContents.replace("INSERT_HOSTNAME", request->hostName()); + fileData[fileName] = fileContents; + fileDataDeflated[fileName] = qCompress(fileContents); + eTags[fileName] = QByteArray::number(qHash(fileContents)); + } } void FileServer::handleRequest(HttpRequest *request, HttpResponse *response) @@ -436,11 +451,6 @@ void FileServer::handleRequest(HttpRequest *request, HttpResponse *response) if (allowedFileNames.contains(filePath) == false) return; // ### drop connection? - QFile file(filePath); - if (file.exists() == false) { -// DEBUG << "no file" << filePath; - return; - } // Check if the client sends an If-None-Match, return // 304 Not Modified if it matches the server's eTag @@ -453,21 +463,6 @@ void FileServer::handleRequest(HttpRequest *request, HttpResponse *response) } } - file.open(QIODevice::ReadOnly); - QByteArray fileContents = file.readAll(); - fileContents.replace("INSERT_HOSTNAME", request->hostName()); - static int pageId = 0; - -/* - if (fileContents.contains("INSERT_PAGE_ID")) - fileContents.replace("INSERT_PAGE_ID", QByteArray::number(++pageId)); -*/ - QByteArray eTag = eTags[filePath]; - if (eTag.isEmpty()) { - eTag = QByteArray::number(qHash(fileContents)); - eTags[filePath] = eTag; - } - - response->seteTag(eTag); - response->setBody(fileContents); + response->seteTag(eTags[filePath]); + response->setBody(fileData[filePath]); } diff --git a/src/webclientserver.h b/src/webclientserver.h index fa4818b..032b101 100644 --- a/src/webclientserver.h +++ b/src/webclientserver.h @@ -86,6 +86,8 @@ public slots: virtual void handleRequest(HttpRequest *request, HttpResponse *response); private: QSet allowedFileNames; + QHash fileData; + QHash fileDataDeflated; QHash eTags; }; -- cgit v1.2.3