-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpserver.cpp
More file actions
160 lines (141 loc) · 4.7 KB
/
httpserver.cpp
File metadata and controls
160 lines (141 loc) · 4.7 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
#include "httpserver.h"
#include <QDebug>
#include <QRegularExpression>
#include <QFile>
#include <QFileInfo>
#include <QMimeDatabase>
#include <QMimeType>
HttpServer::HttpServer(QObject *parent) : QObject(parent)
{
server = new QTcpServer(this);
connect(server, &QTcpServer::newConnection, this, &HttpServer::onNewConnection);
}
void HttpServer::startServer(quint16 port)
{
if (!server->listen(QHostAddress::Any, port))
{
qDebug() << "Server could not start!";
}
else
{
qDebug() << "Server started!";
}
}
void HttpServer::onNewConnection()
{
QTcpSocket *socket = server->nextPendingConnection();
connect(socket, &QTcpSocket::readyRead, this, &HttpServer::onReadyRead);
}
void HttpServer::onReadyRead()
{
QTcpSocket *socket = qobject_cast<QTcpSocket *>(sender());
if (!socket) {
return;
}
while (socket->canReadLine())
{
QString requestLine = socket->readLine().trimmed();
if (requestLine.isEmpty()) {
// Fin des en-têtes, traitement de la requête
handleRequest(socket);
return;
}
// Stocker les lignes de la requête pour analyse ultérieure
if (!socket->property("request").isValid()) {
socket->setProperty("request", QVariant::fromValue(QStringList()));
}
QStringList requestLines = socket->property("request").toStringList();
requestLines.append(requestLine);
socket->setProperty("request", QVariant::fromValue(requestLines));
}
}
void HttpServer::handleRequest(QTcpSocket *socket)
{
QStringList requestLines = socket->property("request").toStringList();
if (requestLines.isEmpty()) {
socket->close();
return;
}
// Parse the request line
QRegularExpression requestLineRegex("^(\\S+)\\s(\\S+)\\sHTTP/1\\.1$");
QRegularExpressionMatch match = requestLineRegex.match(requestLines.first());
if (!match.hasMatch())
{
qDebug() << "Invalid request line";
socket->close();
return;
}
QString method = match.captured(1);
QString path = match.captured(2);
QMap<QString, QString> headers;
QByteArray body;
// Read the headers
for (int i = 1; i < requestLines.size(); ++i) {
const QString& line = requestLines[i];
if (line.isEmpty()) {
// End of headers
break;
}
QStringList headerParts = line.split(": ");
if (headerParts.size() == 2) {
headers[headerParts[0]] = headerParts[1].trimmed();
}
}
// Read the body (if any)
if (headers.contains("Content-Length"))
{
int contentLength = headers["Content-Length"].toInt();
body = socket->read(contentLength);
}
// Generate and send the response
QByteArray response = generateResponse(method, path, headers, body);
socket->write(response);
socket->flush();
socket->close();
}
QByteArray HttpServer::generateResponse(const QString &method, const QString &path, const QMap<QString, QString> &headers, const QByteArray &body)
{
Q_UNUSED(headers);
Q_UNUSED(body);
QByteArray response;
if (method == "GET" && path == "/")
{
response.append("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
response.append("Hello, World!");
}
else if (method == "GET" && path == "/test")
{
response.append("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
response.append("This is a test page!");
}
else if (method == "GET" && path.startsWith("/getfile/cc/"))
{
QString filePath = path.mid(QString("/getfile/cc/").length());
QFile file(filePath);
if (file.exists() && file.open(QIODevice::ReadOnly))
{
QFileInfo fileInfo(file);
QMimeDatabase mimeDb;
QMimeType mimeType = mimeDb.mimeTypeForFile(fileInfo);
QByteArray fileContent = file.readAll();
file.close();
response.append("HTTP/1.1 200 OK\r\n");
response.append("Content-Type: " + mimeType.name().toUtf8() + "\r\n");
response.append("Content-Disposition: attachment; filename=\"" + fileInfo.fileName().toUtf8() + "\"\r\n");
response.append("Content-Length: " + QByteArray::number(fileContent.size()) + "\r\n");
response.append("\r\n");
response.append(fileContent);
}
else
{
response.append("HTTP/1.1 404 Not Found\r\nContent-Type: text/html\r\n\r\n");
response.append("File not found.");
}
}
else
{
response.append("HTTP/1.1 404 Not Found\r\nContent-Type: text/html\r\n\r\n");
response.append("Page not found.");
}
return response;
}