-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterfacehttp.cpp
134 lines (117 loc) · 4.83 KB
/
interfacehttp.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
/*
This file is part of HTTP2OSC
Copyright (C) 2014-2015 — Buzzing Light
Development: Guillaume Jacquemin (http://www.buzzinglight.com)
This file was written by Guillaume Jacquemin.
HTTP2OSC is a free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "interfacehttp.h"
InterfaceHttp::InterfaceHttp(QObject *parent, quint16 port) :
QObject(parent) {
//HTTP server
httpServer = new InterfaceHttpServer(this, port);
connect(httpServer, SIGNAL(parseRequest(QNetworkReply*)), SLOT(parseRequest(QNetworkReply*)));
connect(httpServer, SIGNAL(parseSocket(QTcpSocket*)), SLOT(parseSocket(QTcpSocket*)));
}
InterfaceHttpServer::InterfaceHttpServer(QObject *parent, quint16 port) :
QTcpServer(parent) {
http = new QNetworkAccessManager(this);
connect(http, SIGNAL(finished(QNetworkReply*)), SLOT(parse(QNetworkReply*)));
setPort(port);
}
void InterfaceHttpServer::setPort(quint16 port) {
if(isListening())
close();
listen(QHostAddress::Any, port);
}
QString InterfaceHttpServer::send(const QString &ip, quint16 port, const QString &destination, const QList<QVariant> &) {
QUrl url;
url.setScheme("http");
url.setHost(ip);
url.setPort(port);
url.setPath(destination);
/*
QUrlQuery query;
for(quint16 i = 0 ; i < valeurs.count() ; i++)
query.addQueryItem(QString("value%1").arg(i), valeurs.at(i).toString());
url.setQuery(query);
*/
http->get(QNetworkRequest(url));
return QString("%2: HTTP sent on %1").arg(url.toString()).arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));
}
void InterfaceHttpServer::parse(QNetworkReply *reply) {
emit(parseRequest(reply));
}
void InterfaceHttp::parseRequest(QNetworkReply *reply) {
//QString(reply->readAll());
QString replyStr = reply->readAll();
}
//HTTP reception
void InterfaceHttpServer::incomingConnection(qintptr socketDescriptor) {
QTcpSocket *socket = new QTcpSocket(this);
connect(socket, SIGNAL(readyRead()), this, SLOT(readClient()));
connect(socket, SIGNAL(disconnected()), this, SLOT(discardClient()));
socket->setSocketDescriptor(socketDescriptor);
}
void InterfaceHttpServer::readClient() {
QTcpSocket *socket = (QTcpSocket*)sender();
if(socket->canReadLine())
emit(parseSocket(socket));
}
void InterfaceHttp::parseSocket(QTcpSocket *socket) {
QStringList tokens = QString(socket->readLine()).split(QRegExp("[ \r\n][ \r\n]*"));
if((tokens.count() > 1) && (tokens.at(0) == "GET")) {
#ifdef QT4
QUrl url(tokens.at(1));
#else
QUrlQuery url(QUrl(tokens.at(1)));
#endif
QTextStream os(socket);
os.setAutoDetectUnicode(true);
os << "HTTP/1.0 200 Ok\r\n"
"Content-Type: text/plain; charset=\"utf-8\"\r\n"
"Access-Control-Allow-Origin: *\r\n"
"Access-Control-Allow-Credentials: true\r\n"
"Access-Control-Allow-Headers: origin, content-type, accept\r\n"
"\r\n";
QString ip = "127.0.0.1", destination = "/http";
quint16 port = 57120;
QList<QVariant> valeurs = QList<QVariant>() << socket->peerAddress().toString() << socket->peerPort();
for(quint16 index = 0 ; index < url.queryItems().count() ; index++) {
QString param = url.queryItems().at(index).first.toLower();
QString value = url.queryItems().at(index).second.toLower();
if (param == "ip") ip = value;
else if(param == "destination") destination = value;
else if(param == "port") port = value.toDouble();
else {
bool ok = false;
qreal valueReal = value.toDouble(&ok);
if(ok) valeurs.append(valueReal);
else valeurs.append(value);
}
}
QString log = MainWindowInterface::main->udp->send(ip, port, destination, valeurs);
emit(outgoingMessage(log));
emit(outgoingData(ip, port, destination, valeurs));
os << log;
os.flush();
socket->close();
if(socket->state() == QTcpSocket::UnconnectedState)
delete socket;
}
}
void InterfaceHttpServer::discardClient() {
QTcpSocket* socket = (QTcpSocket*)sender();
socket->deleteLater();
}
InterfaceHttp::~InterfaceHttp() {
}