Hobrasoft httpd server
Embedded HTTP server for Qt and C++
staticfilecontroller.cpp
Go to the documentation of this file.
1 
8 #include "staticfilecontroller.h"
9 #include "httpconnection.h"
10 #include "httpsettings.h"
11 #include "httpresponse.h"
12 #include "httprequest.h"
13 #include <QFileInfo>
14 #include <QDir>
15 #include <QDateTime>
16 #include <QFile>
17 #include <QDir>
18 #include <QDateTime>
19 #include <QDebug>
20 #include <QRegExp>
21 
22 using namespace HobrasoftHttpd;
23 
24 QHash<QString, QString> StaticFileController::m_mimetypes;
25 
27 
28  if (m_mimetypes.isEmpty()) {
29  addMimeType("png", "image/png");
30  addMimeType("jpeg", "image/jpeg");
31  addMimeType("jpg", "image/jpeg");
32  addMimeType("gif", "image/gif");
33  addMimeType("txt", "text/plain");
34  addMimeType("html", "text/html");
35  addMimeType("xhtml", "text/html");
36  addMimeType("shtml", "text/html");
37  addMimeType("htm", "text/html");
38  addMimeType("css", "text/css");
39  addMimeType("json", "application/json");
40  addMimeType("js", "application/javascript");
41  }
42 
43  m_parent = parent;
44 }
45 
46 
48  return m_parent->settings();
49 }
50 
51 
52 void StaticFileController::addMimeType(const QString& fileSuffix, const QString& mimetype) {
53  m_mimetypes[fileSuffix] = mimetype;
54 }
55 
56 
58  QString path = request->path();
59  QString rootpath = QDir::fromNativeSeparators(settings()->docroot());
60 
61  if (path.startsWith("/..")) {
62  response->setStatus(403,"Forbidden");
63  response->write("403 Forbidden");
64  response->flush();
65  return;
66  }
67 
68  QString filename = QDir::fromNativeSeparators(rootpath + path);
69  QFileInfo fileinfo(filename);
70  if (fileinfo.isDir()) {
71  filename += "/" + QDir::fromNativeSeparators(settings()->indexFile());
72  fileinfo.setFile(filename);
73  }
74 
75  QFile file(fileinfo.canonicalFilePath());
76  if (!file.exists()) {
77  response->setStatus(404, "Not found");
78  response->write("404 Not found");
79  response->flush();
80  return;
81  }
82 
83  if (!file.open(QIODevice::ReadOnly)) {
84  response->setStatus(403, "Forbidden");
85  response->write("403 Forbidden");
86  response->flush();
87  return;
88  }
89 
90  QString suffix = fileinfo.suffix();
91  if (!suffix.isEmpty() && m_mimetypes.contains(suffix)) {
92  response->setHeader("Content-Type", m_mimetypes[suffix]);
93  }
94 
95  response->setHeader("Cache-Control", QString("Public,max-age=") + QString("%1").arg(settings()->maxAge()) );
96  response->setHeader("Expires", toGMTString(QDateTime::currentDateTime().addSecs(settings()->maxAge()).toUTC()) );
97  response->write( file.readAll() );
98  response->flush();
99 }
100 
101 
102 QString StaticFileController::toGMTString(const QDateTime& x) {
103  QString dayname;
104  switch (x.date().dayOfWeek()) {
105  case 1: dayname = "Mon"; break;
106  case 2: dayname = "Tue"; break;
107  case 3: dayname = "Wed"; break;
108  case 4: dayname = "Thu"; break;
109  case 5: dayname = "Fri"; break;
110  case 6: dayname = "Sat"; break;
111  case 7: dayname = "Sun"; break;
112  }
113 
114  QString monthname;
115  switch (x.date().month()) {
116  case 1: monthname = "Jan"; break;
117  case 2: monthname = "Feb"; break;
118  case 3: monthname = "Mar"; break;
119  case 4: monthname = "Apr"; break;
120  case 5: monthname = "May"; break;
121  case 6: monthname = "Jun"; break;
122  case 7: monthname = "Jul"; break;
123  case 8: monthname = "Aug"; break;
124  case 9: monthname = "Sep"; break;
125  case 10: monthname = "Oct"; break;
126  case 11: monthname = "Nov"; break;
127  case 12: monthname = "Dec"; break;
128  }
129 
130  QString string = QString("%1, %2 %3 %4 %5:%6:%7 GMT")
131  .arg(dayname)
132  .arg(x.date().day())
133  .arg(monthname)
134  .arg(x.date().year())
135  .arg(x.time().hour())
136  .arg(x.time().minute())
137  .arg(x.time().second())
138  ;
139  return string;
140 }
141 
142 
HttpResponse * response()
Returns new instance of HttpResponse class.
QString path() const
Returns path of the request (/files/index.html)
Definition: httprequest.h:78
StaticFileController(HttpConnection *parent)
Constructor set default parameters from configuration.
void addMimeType(const QString &fileSuffix, const QString &mimetype)
Adds mime type to static table of mime types (common for all class instances)
void flush()
Writes last part of the response and closes the socket when possible.
const HttpSettings * settings() const
Returs pointer to HttpSettings used in the HttpServer.
Response to HTTP request - headers, cookies and body.
Definition: httpresponse.h:30
void write(const QByteArray &data)
Writes data to response body.
void service(HttpRequest *request, HttpResponse *response)
Processes the request.
Configuration of the http server instance.
Definition: httpsettings.h:39
Processes incoming requests.
Processes HTTP request, parses headers, body and files sent by HTTP protocol.
Definition: httprequest.h:23
void setStatus(int code, const QString &description=QString())
Set the status code and the description of the response.
void setHeader(const QString &name, const QString &value)
Sets or rewrite one header.
Namespace of HTTP server.
One single connection to http server.