-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttpserverpublisher.cpp
228 lines (174 loc) · 6.91 KB
/
httpserverpublisher.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include "httpserverpublisher.h"
HttpServerPublisher::HttpServerPublisher(quint16 newPortNumber,QString newServiceFolder)
{
qDebug() << Q_FUNC_INFO;
mPortNumber=newPortNumber;
mContentRoot=createHeaderOk();
mServiceFolder=newServiceFolder;
connect(this,&HttpServerPublisher::signalServerRuns,this,&HttpServerPublisher::slotTest);
mContentSubscribe=createSubscribeResponse("true");
mContentUnsubscribe=createUnsubscribeResponse("false");
// slotStartServer();
}
int HttpServerPublisher::slotStartServer()
{
qDebug() << Q_FUNC_INFO;
this->route(mServiceFolder,mBodyContentArray);
mPortNumber=this->listen();
emit signalServerRuns(mPortNumber);
return 1;
}
int HttpServerPublisher::route(QString &serviceFolder, QMap<QString,QString> &contentBody)
{
qDebug() << Q_FUNC_INFO;
qDebug() << serviceFolder;
httpServer.route("/"+serviceFolder+"/Subscribe<arg>", [this](const QUrl &url,const QHttpServerRequest &request)
{
QString structure= QStringLiteral("%1").arg(url.path());
qDebug()<<"subscribe request "<<structure;
qDebug().noquote()<<request.body();
/*
QString textVysledek="true";
QString odpoved=vyrobSubscribeResponse(textVysledek);
*/
this->requestBody=request.body();
emit signalContentChanged(request.body(),structure);
return this->mContentSubscribe;
});
httpServer.route("/"+serviceFolder+"/Unsubscribe<arg>", [this](const QUrl &url,const QHttpServerRequest &request)
{
QString structure= QStringLiteral("%1").arg(url.path());
qDebug()<<"unsubscribe request "<<structure;
qDebug().noquote()<<request.body();
/*
QString textVysledek="true";
QString odpoved=vyrobSubscribeResponse(textVysledek);
*/
this->requestBody=request.body();
emit signalContentChanged(request.body(),structure);
return this->mContentUnsubscribe;
});
httpServer.route("/"+serviceFolder+"/Set<arg>", [this](const QUrl &url,const QHttpServerRequest &request)
{
QString struktura= QStringLiteral("%1").arg(url.path());
qDebug().noquote()<<"request Set"<<struktura;
this->requestBody=request.body();
emit signalContentChanged(request.body(),struktura);
return this->mContentSet;
});
httpServer.route("/"+serviceFolder+"/Get<arg>", [&contentBody](const QUrl &url,const QHttpServerRequest &request)
{
QString structure= QStringLiteral("%1").arg(url.path());
qDebug().noquote()<<"request Get"<<structure;
return contentBody.value(structure);
});
httpServer.route("/", [this](const QHttpServerRequest &request)
{
qDebug()<<"request HEAD "<<request.headers();
qDebug()<<"request BODY "<<request.body();
emit signalDataReceived(request.body());
return this->mContentRoot;
});
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
//not used on Qt5.15
#else
httpServer.afterRequest([](QHttpServerResponse &&resp)
{
resp.setHeader("Server", "Super server!");
resp.setHeader("Content-Type", "text/xml");
return std::move(resp);
});
#endif
return 1;
}
int HttpServerPublisher::listen()
{
qDebug() << Q_FUNC_INFO;
if (mPortNumber!=0)
{
/* manual port choice */
const auto port = httpServer.listen(QHostAddress::Any,mPortNumber);
if (!port)
{
qDebug() << QCoreApplication::translate(
"QHttpServerExample", "Server failed to listen on a port.");
}
qDebug()<<"Starting server at port:"<<QString::number(port);
return port;
qDebug() << QCoreApplication::translate("QHttpServerExample", "Running on http://127.0.0.1:%1/ (Press CTRL+C to quit)").arg(port);
}
else
{
/* automatic port selection */
const auto port = httpServer.listen(QHostAddress::Any);
if (!port) {
qDebug() << QCoreApplication::translate(
"QHttpServerExample", "Server failed to listen on a port.");
}
return port;
qDebug() << QCoreApplication::translate("QHttpServerExample", "Running on http://127.0.0.1:%1/ (Press CTRL+C to quit)").arg(port);
}
return 1;
}
void HttpServerPublisher::setGetContent(QString input)
{
qDebug() << Q_FUNC_INFO;
this->mServiceFolder=input;
}
void HttpServerPublisher::setSubscribeContent(QString input)
{
qDebug() << Q_FUNC_INFO;
this->mContentSubscribe=input;
}
int HttpServerPublisher::setBodyContent(QMap<QString,QString> input )
{
qDebug() << Q_FUNC_INFO;
mBodyContentArray=input;
return 1;
}
quint16 HttpServerPublisher::portNumber() const
{
return mPortNumber;
}
void HttpServerPublisher::setPortNumber(quint16 newPortNumber)
{
qDebug() << Q_FUNC_INFO <<QString::number(newPortNumber);
mPortNumber = newPortNumber;
}
QString HttpServerPublisher::createHeaderOk()
{
qDebug() << Q_FUNC_INFO;
QString hlavicka;
hlavicka+=("HTTP/1.1 200 OK\r\n"); // \r needs to be before \n
hlavicka+=("Content-Type: application/xml\r\n");
hlavicka+=("Connection: close\r\n");
hlavicka+=("Pragma: no-cache\r\n");
hlavicka+=("\r\n");
return hlavicka;
}
QString HttpServerPublisher::createSubscribeResponse(QString result)
{
QString response;
response+="<?xml version=\"1.0\" encoding=\"utf-8\"?>";
response+="<SubscribeResponse xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">";
response+="<Active><Value>";
response+=result;
response+="</Value></Active>";
response+="</SubscribeResponse>";
return response;
}
QString HttpServerPublisher::createUnsubscribeResponse(QString result)
{
QString response;
response+="<?xml version=\"1.0\" encoding=\"utf-8\"?>";
response+="<UnsubscribeResponse xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">";
response+="<Active><Value>";
response+=result;
response+="</Value></Active>";
response+="</UnsubscribeResponse>";
return response;
}
void HttpServerPublisher::slotTest(int port)
{
qDebug()<<Q_FUNC_INFO<<" "<<QString::number(port);
}