18 maxAge=settings->value(
"maxAge",
"60000").toInt();
19 encoding=settings->value(
"encoding",
"UTF-8").toString();
20 docroot=settings->value(
"path",
".").toString();
25 if (QDir::isRelativePath(
docroot) && settings->format()!=QSettings::NativeFormat)
27 if (QDir::isRelativePath(
docroot))
30 QFileInfo configFile(settings->fileName());
31 docroot=QFileInfo(configFile.absolutePath(),
docroot).absoluteFilePath();
34 qDebug(
"StaticFileController: docroot=%s, encoding=%s, maxAge=%i",qPrintable(
docroot),qPrintable(
encoding),
maxAge);
36 cache.setMaxCost(settings->value(
"cacheSize",
"1000000").toInt());
37 cacheTimeout=settings->value(
"cacheTime",
"60000").toInt();
38 qDebug(
"StaticFileController: cache timeout=%i, size=%i",
cacheTimeout,
cache.maxCost());
50 if (QDir::isRelativePath(
docroot))
52 docroot = QFileInfo(QDir::currentPath(),
docroot).absoluteFilePath();
55 qDebug(
"StaticFileController: docroot=%s, encoding=%s, maxAge=%i",qPrintable(
docroot),qPrintable(
encoding),
maxAge);
59 qDebug(
"StaticFileController: cache timeout=%i, size=%i",
cacheTimeout,
cache.maxCost());
64 QByteArray path = request.
getPath();
72 qint64 now=QDateTime::currentMSecsSinceEpoch();
80 qDebug(
"StaticFileController: Cache hit for %s",path.data());
82 response.
setHeader(
"Cache-Control",
"max-age="+QByteArray::number(
maxAge/1000));
83 response.
write(document);
89 qDebug(
"StaticFileController: Cache miss for %s",path.data());
91 if (path.contains(
"/.."))
93 qWarning(
"StaticFileController::service: detected forbidden characters in path %s",path.data());
95 response.
write(
"403 forbidden",
true);
99 if (QFileInfo(
docroot+path).isDir())
105 qDebug(
"StaticFileController: Open file %s",qPrintable(file.fileName()));
106 if (file.open(QIODevice::ReadOnly))
109 response.
setHeader(
"Cache-Control",
"max-age="+QByteArray::number(
maxAge/1000));
114 while (!file.atEnd() && !file.error())
116 QByteArray buffer=file.read(65536);
117 response.
write(buffer);
130 while (!file.atEnd() && !file.error())
132 response.
write(file.read(65536));
140 qWarning(
"StaticFileController::service: Cannot open existing file %s for reading",qPrintable(file.fileName()));
142 response.
write(
"403 forbidden",
true);
146 qWarning(
"StaticFileController::service: File %s not found",qPrintable(file.fileName()));
148 response.
write(
"404 not found",
true);
156 if (fileName.endsWith(
".png"))
158 response.
setHeader(
"Content-Type",
"image/png");
160 else if (fileName.endsWith(
".jpg"))
162 response.
setHeader(
"Content-Type",
"image/jpeg");
164 else if (fileName.endsWith(
".gif"))
166 response.
setHeader(
"Content-Type",
"image/gif");
168 else if (fileName.endsWith(
".pdf"))
170 response.
setHeader(
"Content-Type",
"application/pdf");
172 else if (fileName.endsWith(
".txt"))
176 else if (fileName.endsWith(
".html") || fileName.endsWith(
".htm"))
180 else if (fileName.endsWith(
".css"))
182 response.
setHeader(
"Content-Type",
"text/css");
184 else if (fileName.endsWith(
".js"))
186 response.
setHeader(
"Content-Type",
"text/javascript");
188 else if (fileName.endsWith(
".svg"))
190 response.
setHeader(
"Content-Type",
"image/svg+xml");
192 else if (fileName.endsWith(
".woff"))
194 response.
setHeader(
"Content-Type",
"font/woff");
196 else if (fileName.endsWith(
".woff2"))
198 response.
setHeader(
"Content-Type",
"font/woff2");
200 else if (fileName.endsWith(
".ttf"))
202 response.
setHeader(
"Content-Type",
"application/x-font-ttf");
204 else if (fileName.endsWith(
".eot"))
206 response.
setHeader(
"Content-Type",
"application/vnd.ms-fontobject");
208 else if (fileName.endsWith(
".otf"))
210 response.
setHeader(
"Content-Type",
"application/font-otf");
212 else if (fileName.endsWith(
".yaml"))
214 response.
setHeader(
"Content-Type",
"text/plain");
219 qDebug(
"StaticFileController: unknown MIME type for filename '%s'", qPrintable(fileName));
QCache< QString, CacheEntry > cache
QByteArray getPath() const
StaticFileController(QSettings *settings, QObject *parent=NULL)
void setHeader(QByteArray name, QByteArray value)
void setContentType(QString file, HttpResponse &response) const
void write(QByteArray data, bool lastPart=false)
void setStatus(int statusCode, QByteArray description=QByteArray())
void service(HttpRequest &request, HttpResponse &response)