-
Notifications
You must be signed in to change notification settings - Fork 1
/
HandlerServer.cpp
180 lines (124 loc) · 4.73 KB
/
HandlerServer.cpp
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
169
170
171
172
173
174
175
176
177
178
179
180
#include <httq/HandlerServer.h>
#include <httq/HttpRequest.h>
#include <httq/AbstractHandler.h>
#include <httq/HttpRequest.h>
#include <httq/Logger.h>
#include <QJsonDocument>
#include <QJsonObject>
#include <QWebSocketServer>
#include <QWebSocket>
#include <QRegularExpression>
namespace httq
{
HandlerServer::HandlerServer(QObject *parent)
: AbstractServer(parent)
, mWsSvr(new QWebSocketServer("httq", QWebSocketServer::SslMode::NonSecureMode, this))
{
connect(mWsSvr, &QWebSocketServer::newConnection,
this, [this]()
{
QWebSocket *ws = mWsSvr->nextPendingConnection();
//ws->setParent(this);
connect(ws, &QWebSocket::disconnected,
ws, &QObject::deleteLater);
connect(ws, QOverload<QAbstractSocket::SocketError>::of(&QWebSocket::error),
ws, &QObject::deleteLater);
qWarning() << "ws client url" << ws->requestUrl().toString();
QUrl url(ws->requestUrl());
qWarning() << "path" << url.path();
if (!handleWs(ws, url.path(), mBase))
{
//qWarning() << "no handler found for" << request->toString();
missingWsHandlerHandler(ws);
ws->deleteLater();
return;
}
//LOG << "handler found for" << request->toString();
});
}
bool HandlerServer::newHttpConnection(HttpRequest *request)
{
// qWarning() << "newHttpConnection";
if (!handle(request, mBase))
{
//qWarning() << "no handler found for" << request->toString();
missingHttpHandlerHandler(request);
request->deleteLater();
return false;
}
//LOG << "handler found for" << request->toString();
return true;
}
void HandlerServer::missingHttpHandlerHandler(HttpRequest *request)
{
request->write(400, QJsonObject({{ "error", "no handler found" }}));
}
bool HandlerServer::newWebSocketConnection(QWebSocket *ws)
{
qWarning() << "newWebSocketConnection";
if (!handleWs(ws, ws->requestUrl().path(), mBase))
{
qWarning() << "no handler found for" << ws->requestUrl().path();
missingWsHandlerHandler(ws);
ws->deleteLater();
return false;
}
qWarning() << "ws handler found for" << ws->requestUrl().path();
return true;
}
void HandlerServer::missingWsHandlerHandler(QWebSocket *ws)
{
ws->sendTextMessage("{ error: \"no websocket handler found\" }");
}
bool HandlerServer::handleWs(QWebSocket *ws, const QString &path, const QString &base)
{
auto it = std::find_if(mWebSocketHandlers.begin(), mWebSocketHandlers.end(), [ws, &path](const WebSocketHandlerDefinition &handler)
{
bool pathMatch = QRegularExpression(QRegularExpression::wildcardToRegularExpression(handler.mPath)).match(path).hasMatch();
bool ret = pathMatch;
qWarning()/*LOG*/ << "handleWs: handler path:" << handler.mPath << "- request url:" << path << "- path match?" << pathMatch;
return ret;
});
bool ret = (it != mWebSocketHandlers.end());
if (!ret)
return false;
AbstractWebSocketHandler *handler = (*it).mHandlerFactory(ws);
handler->setLogger(getLoggerFactory()->createLogger(handler));
#warning ?????
// ws->setParent(handler); ?????
connect(handler, &QObject::destroyed,
ws, &QObject::deleteLater); // delete request if handler was deleted (end-developer controlled environment)
return true;
/* QUrl url(ws->requestUrl());
QString _path(url.path().remove(base));
connect(handler, &QObject::destroyed,
ws, &QObject::deleteLater); // delete request if handler was deleted (end-developer controlled environment)
connect(ws, &QWebSocket::textMessageReceived,
handler, &AbstractWebSocketHandler::_handleMessage);
return true;
*/
}
bool HandlerServer::handle(HttpRequest *request, const QString &base)
{
const QString path(request->url().path().remove(base));
auto it = std::find_if(mHandlers.begin(), mHandlers.end(), [request, &path](const HandlerDefinition &handler)
{
bool pathMatch = QRegularExpression(QRegularExpression::wildcardToRegularExpression(handler.mPath)).match(path).hasMatch();
bool methodMatch = (handler.mMethod.toUpper() == request->methodString());
bool ret = (methodMatch && pathMatch);
//qWarning()/*LOG*/ << "handle: handler path:" << handler.mPath << "- request url:" << path << "- path match?" << pathMatch << "- method match?" << methodMatch << "- request" << request->toString();
return ret;
});
bool ret = (it != mHandlers.end());
if (!ret)
return false;
AbstractHandler *handler = (*it).mHandlerFactory();
handler->setLogger(getLoggerFactory()->createLogger(handler));
connect(handler, &QObject::destroyed,
request, &QObject::deleteLater); // delete request if handler was deleted (end-developer controlled environment)
handler->setRequest(request);
request->setParent(handler);
/*return*/ handler->handle();
return true;
}
}