mirror of
https://github.com/overte-org/overte.git
synced 2025-08-08 21:36:47 +02:00
add basic SSI include support to HttpManager
This commit is contained in:
parent
e68dc1b142
commit
bce40a9963
7 changed files with 49 additions and 8 deletions
|
@ -5,7 +5,7 @@
|
||||||
<link href='css/style.css' rel='stylesheet'>
|
<link href='css/style.css' rel='stylesheet'>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<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/jquery-2.0.3.min.js'></script>
|
||||||
<script src='js/ace/ace.js' type='text/javascript'></script>
|
<script src='js/ace/ace.js' type='text/javascript'></script>
|
||||||
<script src='js/assignment.js' type='text/javascript'></script>
|
<script src='js/assignment.js' type='text/javascript'></script>
|
||||||
|
|
|
@ -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>
|
<div id="nodes-lead" class="table-lead"><h3>Nodes</h3><div class="lead-line"></div></div>
|
||||||
<table id="nodes-table" class="table table-striped">
|
<table id="nodes-table" class="table table-striped">
|
||||||
<thead>
|
<thead>
|
||||||
|
@ -27,6 +27,6 @@
|
||||||
<tbody>
|
<tbody>
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
<!--#include file="footer.shtml"-->
|
<!--#include file="footer.html"-->
|
||||||
<script src='js/tables.js'></script>
|
<script src='js/tables.js'></script>
|
||||||
<!--#include file="page-end.shtml"-->
|
<!--#include file="page-end.html"-->
|
|
@ -123,7 +123,6 @@ void HttpConnection::respond(const char* code, const QByteArray& content, const
|
||||||
_socket->write("Connection: close\r\n\r\n");
|
_socket->write("Connection: close\r\n\r\n");
|
||||||
|
|
||||||
if (csize > 0) {
|
if (csize > 0) {
|
||||||
qDebug() << "Writing" << QString(content) << "\n";
|
|
||||||
_socket->write(content);
|
_socket->write(content);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -43,15 +43,57 @@ bool HttpManager::handleRequest(HttpConnection* connection, const QString& path)
|
||||||
filePath = _documentRoot + subPath;
|
filePath = _documentRoot + subPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (!filePath.isEmpty()) {
|
if (!filePath.isEmpty()) {
|
||||||
static QMimeDatabase mimeDatabase;
|
static QMimeDatabase mimeDatabase;
|
||||||
|
|
||||||
qDebug() << "Serving file at" << filePath;
|
|
||||||
|
|
||||||
QFile localFile(filePath);
|
QFile localFile(filePath);
|
||||||
localFile.open(QIODevice::ReadOnly);
|
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 {
|
} else {
|
||||||
// respond with a 404
|
// respond with a 404
|
||||||
connection->respond("404 Not Found", "Resource not found.");
|
connection->respond("404 Not Found", "Resource not found.");
|
||||||
|
|
Loading…
Reference in a new issue