1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- /***************************************************************************
- * Copyright (C) 2009, 2010 by Axel Jaeger <axeljaeger@googlemail.com> *
- * *
- * This file is part of Glowworm. *
- * *
- * Glowworm is free software: you can redistribute it and/or modify *
- * it under the terms of the GNU General Public License as published by *
- * the Free Software Foundation, version 3 of the License. *
- * *
- * Glowworm is distributed in the hope that it will be useful, *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
- * GNU General Public License for more details. *
- * *
- * You should have received a copy of the GNU General Public License *
- * along with Glowworm. If not, see <http://www.gnu.org/licenses/>. *
- ***************************************************************************/
- #include "FileServlet.h"
- #include "HttpResponse.h"
- #include "HttpRequest.h"
- #include <QFileInfo>
- #include <QDir>
- FileServlet::FileServlet(HttpServer* parent) : AbstractServlet(parent)
- {
- }
- FileServlet::~FileServlet()
- {
- }
- QDir FileServlet::wwwRoot() const
- {
- return m_wwwRoot;
- }
- void FileServlet::setWwwRoot(const QDir & dir)
- {
- m_wwwRoot = dir;
- }
- void FileServlet::request(HttpRequest* request, HttpResponse* response)
- {
- QFileInfo file_info(wwwRoot().path() + QDir::separator() +request->url().path());
- if (!file_info.exists()) {
- response->setStatusCode(HttpResponse::HttpFileNotFound);
- return;
- }
- if (file_info.isDir()) {
- QDir dir(file_info.filePath());
- QStringList filters;
- filters << "index.*";
- QStringList index_files(dir.entryList(filters));
- if (!index_files.isEmpty()) {
- QFileInfo index_info(file_info.absoluteFilePath() + QDir::separator() + index_files.first());
- file_info = index_info;
- } else {
- response->setStatusCode(HttpResponse::HttpFileNotFound);
- return;
- }
- }
- QFile file(file_info.filePath());
- if (! file.open(QIODevice::ReadOnly)) {
- response->setStatusCode(HttpResponse::HttpForbidden);
- } else {
- response->setStatusCode(HttpResponse::HttpOK);
- response->setMimeType(mimeTypeForFile(file_info));
- response->sendHeader();
- response->outputDevice()->write(file.readAll());
- }
- }
- QString FileServlet::mimeTypeForFile(const QFileInfo & file)
- {
- QString extension(file.suffix());
- QString mimeType("application/octet-stream");
- if (extension == "html" || extension == "htm") {
- mimeType = "text/html";
- } else if (extension == "png") {
- mimeType = "image/png";
- } else if (extension == "css") {
- mimeType = "text/css";
- } else if (extension == "js") {
- mimeType = "text/javascript";
- }
- return mimeType;
- }
|