-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathqfilesystemhandler.cpp
More file actions
168 lines (145 loc) · 5.56 KB
/
qfilesystemhandler.cpp
File metadata and controls
168 lines (145 loc) · 5.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* Copyright (c) 2015 Nathan Osman
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include <QFile>
#include <QFileInfo>
#include <QFileInfoList>
#include <QUrl>
#include "QHttpEngine/qfilesystemhandler.h"
#include "QHttpEngine/qiodevicecopier.h"
#include "qfilesystemhandler_p.h"
// Template for listing directory contents
const QString ListTemplate =
"<!DOCTYPE html>"
"<html>"
"<head>"
"<meta charset=\"utf-8\">"
"<title>%1</title>"
"</head>"
"<body>"
"<h1>%1</h1>"
"<p>Directory listing:</p>"
"<ul>%2</ul>"
"<hr>"
"<p><em>QHttpEngine %3</em></p>"
"</body>"
"</html>";
QFilesystemHandlerPrivate::QFilesystemHandlerPrivate(QFilesystemHandler *handler)
: QObject(handler)
{
}
bool QFilesystemHandlerPrivate::absolutePath(const QString &path, QString &absolutePath)
{
// Resolve the path according to the document root
absolutePath = documentRoot.absoluteFilePath(path);
// Perhaps not the most efficient way of doing things, but one way to
// determine if path is within the document root is to convert it to a
// relative path and check to see if it begins with "../" (it shouldn't)
return documentRoot.exists(absolutePath) && !documentRoot.relativeFilePath(path).startsWith("../");
}
QByteArray QFilesystemHandlerPrivate::mimeType(const QString &absolutePath)
{
// Query the MIME database based on the filename and its contents
return database.mimeTypeForFile(absolutePath).name().toUtf8();
}
void QFilesystemHandlerPrivate::processFile(QHttpSocket *socket, const QString &absolutePath)
{
// Attempt to open the file for reading
QFile *file = new QFile(absolutePath);
if(!file->open(QIODevice::ReadOnly)) {
delete file;
socket->writeError(QHttpSocket::Forbidden);
return;
}
// Create a QIODeviceCopier to copy the file contents to the socket
QIODeviceCopier *copier = new QIODeviceCopier(file, socket);
connect(copier, SIGNAL(finished()), copier, SLOT(deleteLater()));
connect(copier, SIGNAL(finished()), file, SLOT(deleteLater()));
// Set the mimetype and content length
socket->setHeader("Content-Type", mimeType(absolutePath));
socket->setHeader("Content-Length", QByteArray::number(file->size()));
socket->writeHeaders();
// Start the copy
copier->start();
}
#if QT_VERSION >= 0x050000
# define HTMLESCAPE(x) ((x).toHtmlEscaped())
#else
# include <QtGui/qtextdocument.h>
# define HTMLESCAPE(x) (Qt::escape(x))
#endif
void QFilesystemHandlerPrivate::processDirectory(QHttpSocket *socket, const QString &path, const QString &absolutePath)
{
// Add entries for each of the files
QString listing;
foreach(QFileInfo info, QDir(absolutePath).entryInfoList()) {
listing.append(QString("<li><a href=\"%1%2\">%1%2</a></li>")
.arg(HTMLESCAPE(info.fileName()))
.arg(info.isDir() ? "/" : ""));
}
// Build the response and convert the string to UTF-8
QByteArray data = ListTemplate
.arg("/" + HTMLESCAPE(path))
.arg(listing)
.arg(QHTTPENGINE_VERSION)
.toUtf8();
// Set the headers and write the content
socket->setHeader("Content-Type", "text/html");
socket->setHeader("Content-Length", QByteArray::number(data.length()));
socket->write(data);
socket->close();
}
QFilesystemHandler::QFilesystemHandler(QObject *parent)
: QHttpHandler(parent),
d(new QFilesystemHandlerPrivate(this))
{
}
QFilesystemHandler::QFilesystemHandler(const QString &documentRoot, QObject *parent)
: QHttpHandler(parent),
d(new QFilesystemHandlerPrivate(this))
{
setDocumentRoot(documentRoot);
}
void QFilesystemHandler::setDocumentRoot(const QString &documentRoot)
{
d->documentRoot.setPath(documentRoot);
}
void QFilesystemHandler::process(QHttpSocket *socket, const QString &path)
{
// If a document root is not set, an error has occurred
if(d->documentRoot.path().isNull()) {
socket->writeError(QHttpSocket::InternalServerError);
return;
}
// URL-decode the path
QString decodedPath = QUrl::fromPercentEncoding(path.toUtf8());
// Attempt to retrieve the absolute path
QString absolutePath;
if(!d->absolutePath(decodedPath, absolutePath)) {
socket->writeError(QHttpSocket::NotFound);
return;
}
if(QFileInfo(absolutePath).isDir()) {
d->processDirectory(socket, decodedPath, absolutePath);
} else {
d->processFile(socket, absolutePath);
}
}