FileServlet.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /***************************************************************************
  2. * Copyright (C) 2009, 2010 by Axel Jaeger <axeljaeger@googlemail.com> *
  3. * *
  4. * This file is part of Glowworm. *
  5. * *
  6. * Glowworm is free software: you can redistribute it and/or modify *
  7. * it under the terms of the GNU General Public License as published by *
  8. * the Free Software Foundation, version 3 of the License. *
  9. * *
  10. * Glowworm is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with Glowworm. If not, see <http://www.gnu.org/licenses/>. *
  17. ***************************************************************************/
  18. #include "FileServlet.h"
  19. #include "HttpResponse.h"
  20. #include "HttpRequest.h"
  21. #include <QFileInfo>
  22. #include <QDir>
  23. FileServlet::FileServlet(HttpServer* parent) : AbstractServlet(parent)
  24. {
  25. }
  26. FileServlet::~FileServlet()
  27. {
  28. }
  29. QDir FileServlet::wwwRoot() const
  30. {
  31. return m_wwwRoot;
  32. }
  33. void FileServlet::setWwwRoot(const QDir & dir)
  34. {
  35. m_wwwRoot = dir;
  36. }
  37. void FileServlet::request(HttpRequest* request, HttpResponse* response)
  38. {
  39. QFileInfo file_info(wwwRoot().path() + QDir::separator() +request->url().path());
  40. if (!file_info.exists()) {
  41. response->setStatusCode(HttpResponse::HttpFileNotFound);
  42. return;
  43. }
  44. if (file_info.isDir()) {
  45. QDir dir(file_info.filePath());
  46. QStringList filters;
  47. filters << "index.*";
  48. QStringList index_files(dir.entryList(filters));
  49. if (!index_files.isEmpty()) {
  50. QFileInfo index_info(file_info.absoluteFilePath() + QDir::separator() + index_files.first());
  51. file_info = index_info;
  52. } else {
  53. response->setStatusCode(HttpResponse::HttpFileNotFound);
  54. return;
  55. }
  56. }
  57. QFile file(file_info.filePath());
  58. if (! file.open(QIODevice::ReadOnly)) {
  59. response->setStatusCode(HttpResponse::HttpForbidden);
  60. } else {
  61. response->setStatusCode(HttpResponse::HttpOK);
  62. response->setMimeType(mimeTypeForFile(file_info));
  63. response->sendHeader();
  64. response->outputDevice()->write(file.readAll());
  65. }
  66. }
  67. QString FileServlet::mimeTypeForFile(const QFileInfo & file)
  68. {
  69. QString extension(file.suffix());
  70. QString mimeType("application/octet-stream");
  71. if (extension == "html" || extension == "htm") {
  72. mimeType = "text/html";
  73. } else if (extension == "png") {
  74. mimeType = "image/png";
  75. } else if (extension == "css") {
  76. mimeType = "text/css";
  77. } else if (extension == "js") {
  78. mimeType = "text/javascript";
  79. }
  80. return mimeType;
  81. }