bool HTTPManager::handleHTTPRequest(HTTPConnection* connection, const QUrl& url, bool skipSubHandler) { if (!skipSubHandler && requestHandledByRequestHandler(connection, url)) { // this request was handled by our request handler object // so we don't need to attempt to do so in the document root return true; } if (!_documentRoot.isEmpty()) { // check to see if there is a file to serve from the document root for this path QString subPath = url.path(); // remove any slash at the beginning of the path if (subPath.startsWith('/')) { subPath.remove(0, 1); } QString filePath; if (QFileInfo(_documentRoot + subPath).isFile()) { filePath = _documentRoot + subPath; } else if (subPath.size() > 0 && !subPath.endsWith('/')) { // this could be a directory with a trailing slash // send a redirect to the path with a slash so we can QString redirectLocation = '/' + subPath + '/'; if (!url.query().isEmpty()) { redirectLocation += "?" + url.query(); } QHash<QByteArray, QByteArray> redirectHeader; redirectHeader.insert(QByteArray("Location"), redirectLocation.toUtf8()); connection->respond(HTTPConnection::StatusCode301, "", HTTPConnection::DefaultContentType, redirectHeader); } // if the last thing is a trailing slash then we want to look for index file if (subPath.endsWith('/') || subPath.size() == 0) { QStringList possibleIndexFiles = QStringList() << "index.html" << "index.shtml"; foreach (const QString& possibleIndexFilename, possibleIndexFiles) { if (QFileInfo(_documentRoot + subPath + possibleIndexFilename).exists()) { filePath = _documentRoot + subPath + possibleIndexFilename; break; } } }
bool HTTPManager::handleHTTPRequest(HTTPConnection* connection, const QUrl& url, bool skipSubHandler) { // Reject paths with embedded NULs if (url.path().contains(QChar(0x00))) { connection->respond(HTTPConnection::StatusCode400, "Embedded NULs not allowed in requests"); qCWarning(embeddedwebserver) << "Received a request with embedded NULs"; return true; } if (!skipSubHandler && requestHandledByRequestHandler(connection, url)) { // this request was handled by our request handler object // so we don't need to attempt to do so in the document root return true; } if (!_documentRoot.isEmpty()) { // check to see if there is a file to serve from the document root for this path QString subPath = url.path(); // remove any slash at the beginning of the path if (subPath.startsWith('/')) { subPath.remove(0, 1); } QString absoluteDocumentRoot { QFileInfo(_documentRoot).absolutePath() }; QString filePath; QFileInfo pathFileInfo { _documentRoot + subPath }; QString absoluteFilePath { pathFileInfo.absoluteFilePath() }; // The absolute path for this file isn't under the document root if (absoluteFilePath.indexOf(absoluteDocumentRoot) != 0) { qCWarning(embeddedwebserver) << absoluteFilePath << "is outside the document root"; connection->respond(HTTPConnection::StatusCode400, "Requested path outside document root"); return true; } if (pathFileInfo.isFile()) { filePath = absoluteFilePath; } else if (subPath.size() > 0 && !subPath.endsWith('/') && pathFileInfo.isDir()) { // this could be a directory with a trailing slash // send a redirect to the path with a slash so we can QString redirectLocation = '/' + subPath + '/'; if (!url.query().isEmpty()) { redirectLocation += "?" + url.query(); } QHash<QByteArray, QByteArray> redirectHeader; redirectHeader.insert(QByteArray("Location"), redirectLocation.toUtf8()); connection->respond(HTTPConnection::StatusCode302, "", HTTPConnection::DefaultContentType, redirectHeader); return true; } // if the last thing is a trailing slash then we want to look for index file if (subPath.endsWith('/') || subPath.size() == 0) { QStringList possibleIndexFiles = QStringList() << "index.html" << "index.shtml"; foreach (const QString& possibleIndexFilename, possibleIndexFiles) { if (QFileInfo(absoluteFilePath + possibleIndexFilename).exists()) { filePath = absoluteFilePath + possibleIndexFilename; break; } } }