-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpserver.h
More file actions
94 lines (81 loc) · 3.46 KB
/
Copy pathhttpserver.h
File metadata and controls
94 lines (81 loc) · 3.46 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
#pragma once
#include <QTcpServer>
#include <QTcpSocket>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QMimeDatabase>
#include <QCoreApplication>
#include <QDebug>
#include <QUrl>
class HttpServer : public QTcpServer {
Q_OBJECT
public:
HttpServer(QObject *parent = nullptr) : QTcpServer(parent) {
// Prefer the source directory for hot-reloading web code without rebuilding
const QString appDir = QCoreApplication::applicationDirPath();
m_docRoot = QDir(appDir + "/../web_editor").absolutePath();
if (!QDir(m_docRoot).exists()) {
m_docRoot = QDir(appDir).filePath("web_editor");
}
// Listen on any available port on localhost
listen(QHostAddress::LocalHost, 0);
}
QString getBaseUrl() const {
return QString("http://127.0.0.1:%1").arg(serverPort());
}
QString getUrlFor(const QString &file, const QString &queryParams = "") const {
QString urlStr = getBaseUrl() + file;
if (!queryParams.isEmpty()) {
urlStr += "?" + queryParams;
}
return urlStr;
}
protected:
void incomingConnection(qintptr socketDescriptor) override {
QTcpSocket *socket = new QTcpSocket(this);
socket->setSocketDescriptor(socketDescriptor);
connect(socket, &QTcpSocket::readyRead, this, [this, socket]() {
if (socket->canReadLine()) {
QString requestLine = socket->readLine();
if (requestLine.startsWith("GET")) {
QString path = requestLine.split(' ').value(1, "/");
if (path.contains('?')) {
path = path.section('?', 0, 0);
}
// Consume the rest of the HTTP headers
while (socket->canReadLine()) {
if (socket->readLine() == "\r\n") break;
}
// Sanitize path to avoid double slashes making QDir::filePath absolute
while(path.startsWith('/')) {
path = path.mid(1);
}
if (path.isEmpty()) {
path = "workspace.html";
}
QString fullPath = QDir(m_docRoot).filePath(path);
QFile file(fullPath);
if (file.exists() && file.open(QIODevice::ReadOnly)) {
QMimeDatabase db;
QString mime = db.mimeTypeForFile(fullPath).name();
QByteArray data = file.readAll();
socket->write("HTTP/1.1 200 OK\r\n");
socket->write("Content-Type: " + mime.toUtf8() + "\r\n");
// Enable CORS in case WebEngine needs it
socket->write("Access-Control-Allow-Origin: *\r\n");
socket->write("Content-Length: " + QByteArray::number(data.size()) + "\r\n");
socket->write("Connection: close\r\n\r\n");
socket->write(data);
} else {
socket->write("HTTP/1.1 404 Not Found\r\nConnection: close\r\n\r\n");
}
}
}
socket->disconnectFromHost();
});
connect(socket, &QTcpSocket::disconnected, socket, &QTcpSocket::deleteLater);
}
private:
QString m_docRoot;
};