add basic SSI include support to HttpManager

This commit is contained in:
Stephen Birarda 2014-01-17 10:40:49 -08:00
parent e68dc1b142
commit bce40a9963
7 changed files with 49 additions and 8 deletions

View file

@ -5,7 +5,7 @@
<link href='css/style.css' rel='stylesheet'>
</head>
<body>
<pre id='editor' style='font-size: 14px;'><!--#include "placeholder.js"--></pre>
<pre id='editor' style='font-size: 14px;'><!--#include file="placeholder.js"--></object></pre>
<script src='../js/jquery-2.0.3.min.js'></script>
<script src='js/ace/ace.js' type='text/javascript'></script>
<script src='js/assignment.js' type='text/javascript'></script>

View file

@ -1,4 +1,4 @@
<!--#include file="header.shtml"-->
<!--#include file="header.html"-->
<div id="nodes-lead" class="table-lead"><h3>Nodes</h3><div class="lead-line"></div></div>
<table id="nodes-table" class="table table-striped">
<thead>
@ -27,6 +27,6 @@
<tbody>
</tbody>
</table>
<!--#include file="footer.shtml"-->
<!--#include file="footer.html"-->
<script src='js/tables.js'></script>
<!--#include file="page-end.shtml"-->
<!--#include file="page-end.html"-->

View file

@ -123,7 +123,6 @@ void HttpConnection::respond(const char* code, const QByteArray& content, const
_socket->write("Connection: close\r\n\r\n");
if (csize > 0) {
qDebug() << "Writing" << QString(content) << "\n";
_socket->write(content);
}

View file

@ -42,16 +42,58 @@ bool HttpManager::handleRequest(HttpConnection* connection, const QString& path)
} else if (QFileInfo(_documentRoot + subPath).isFile()) {
filePath = _documentRoot + subPath;
}
if (!filePath.isEmpty()) {
static QMimeDatabase mimeDatabase;
qDebug() << "Serving file at" << filePath;
QFile localFile(filePath);
localFile.open(QIODevice::ReadOnly);
QString localFileString(localFile.readAll());
connection->respond("200 OK", localFile.readAll(), qPrintable(mimeDatabase.mimeTypeForFile(filePath).name()));
QFileInfo localFileInfo(filePath);
if (localFileInfo.completeSuffix() == "shtml") {
// this is a file that may have some SSI statements
// the only thing we support is the include directive, but check the contents for that
// setup our static QRegExp that will catch <!--#include virtual ... --> and <!--#include file .. --> directives
const QString includeRegExpString = "<!--\\s*#include\\s+(virtual|file)\\s?=\\s?\"(\\S+)\"\\s*-->";
QRegExp includeRegExp(includeRegExpString);
int matchPosition = 0;
while ((matchPosition = includeRegExp.indexIn(localFileString, matchPosition)) != -1) {
// check if this is a file or vitual include
bool isFileInclude = includeRegExp.cap(1) == "file";
// setup the correct file path for the included file
QString includeFilePath = isFileInclude
? localFileInfo.canonicalPath() + "/" + includeRegExp.cap(2)
: _documentRoot + includeRegExp.cap(2);
QString replacementString;
if (QFileInfo(includeFilePath).isFile()) {
QFile includedFile(includeFilePath);
includedFile.open(QIODevice::ReadOnly);
replacementString = QString(includedFile.readAll());
} else {
qDebug() << "SSI include directive referenced a missing file:" << includeFilePath;
}
// replace the match with the contents of the file, or an empty string if the file was not found
localFileString.replace(matchPosition, includeRegExp.matchedLength(), replacementString);
// push the match position forward so we can check the next match
matchPosition += includeRegExp.matchedLength();
}
}
connection->respond("200 OK", localFileString.toLocal8Bit(), qPrintable(mimeDatabase.mimeTypeForFile(filePath).name()));
} else {
// respond with a 404
connection->respond("404 Not Found", "Resource not found.");