Skip to content

Commit becbbeb

Browse files
committed
Updates
1 parent fb1703f commit becbbeb

File tree

5 files changed

+56
-39
lines changed

5 files changed

+56
-39
lines changed

libsrc/webserver/QtHttpClientWrapper.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@ QtHttpClientWrapper::QtHttpClientWrapper (QTcpSocket * sock, const bool& localCo
2727
, m_localConnection(localConnection)
2828
, m_websocketClient(nullptr)
2929
, m_webJsonRpc (nullptr)
30+
, m_websocketServer (nullptr)
3031
{
31-
connect (m_sockClient, &QTcpSocket::readyRead, this, &QtHttpClientWrapper::onClientDataReceived);
32-
connect(&m_websocketServer, &QWebSocketServer::newConnection,
33-
this, &QtHttpClientWrapper::onNewWebSocketConnection);
32+
connect(m_sockClient, &QTcpSocket::readyRead, this, &QtHttpClientWrapper::onClientDataReceived);
3433
}
3534

3635
QString QtHttpClientWrapper::getGuid (void)
@@ -172,14 +171,19 @@ void QtHttpClientWrapper::onClientDataReceived (void)
172171
const auto& upgradeValue = m_currentRequest->getHeader(QtHttpHeader::Upgrade).toLower();
173172
if (upgradeValue.compare(QByteArrayLiteral("websocket"), Qt::CaseInsensitive) == 0) {
174173

175-
qDebug() << "WebSocket upgrade detected, passing to QWebSocketServer";
176-
177174
if(m_websocketClient == Q_NULLPTR)
178175
{
179176
// disconnect this slot from socket for further requests
180177
disconnect(m_sockClient, &QTcpSocket::readyRead, this, &QtHttpClientWrapper::onClientDataReceived);
181178
m_sockClient->rollbackTransaction();
182-
m_websocketServer.handleConnection(m_sockClient);
179+
180+
QString servername = QCoreApplication::applicationName() + QLatin1Char('/') + HYPERION_VERSION;
181+
QWebSocketServer::SslMode secureMode = m_serverHandle->isSecure() ? QWebSocketServer::SecureMode : QWebSocketServer::NonSecureMode;
182+
m_websocketServer.reset(new QWebSocketServer(servername, secureMode));
183+
connect(m_websocketServer.get(), &QWebSocketServer::newConnection,
184+
this, &QtHttpClientWrapper::onNewWebSocketConnection);
185+
186+
m_websocketServer->handleConnection(m_sockClient);
183187
emit m_sockClient->readyRead();
184188
return;
185189
}
@@ -381,10 +385,8 @@ void QtHttpClientWrapper::closeConnection()
381385
void QtHttpClientWrapper::onNewWebSocketConnection() {
382386

383387
// Handle the pending connection
384-
QWebSocket* webSocket = m_websocketServer.nextPendingConnection();
388+
QWebSocket* webSocket = m_websocketServer->nextPendingConnection();
385389
if (webSocket) {
386-
qDebug() << "New WebSocket connection established";
387-
388390
// Manage the WebSocketJsonHandler for this connection
389391
WebSocketJsonHandler* handler = new WebSocketJsonHandler(webSocket);
390392
connect(webSocket, &QWebSocket::disconnected, handler, &QObject::deleteLater);

libsrc/webserver/QtHttpClientWrapper.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <QString>
66
#include <QWebSocketServer>
77
#include <QCoreApplication>
8+
#include <QScopedPointer>
89

910
class QTcpSocket;
1011

@@ -41,11 +42,6 @@ class QtHttpClientWrapper : public QObject {
4142
///
4243
void closeConnection();
4344

44-
QWebSocketServer m_websocketServer{
45-
QCoreApplication::applicationName() + QLatin1Char('/') + QCoreApplication::applicationVersion(),
46-
QWebSocketServer::NonSecureMode
47-
};
48-
4945
signals:
5046
void newWebSocketConnection();
5147

@@ -70,6 +66,7 @@ protected slots:
7066
WebSocketClient * m_websocketClient;
7167
WebJsonRpc * m_webJsonRpc;
7268
QByteArray m_fragment;
69+
QScopedPointer<QWebSocketServer> m_websocketServer;
7370
};
7471

7572
#endif // QTHTTPCLIENTWRAPPER_H

libsrc/webserver/QtHttpServer.h

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,22 @@ class QtHttpServer : public QObject
4646

4747
typedef void (QSslSocket::* SslErrorSignal) (const QList<QSslError> &);
4848

49-
const QString & getServerName (void) const { return m_serverName; };
49+
const QString & getServerName (void) const { return m_serverName; }
5050

51-
quint16 getServerPort (void) const { return m_sockServer->serverPort(); };
52-
QString getErrorString (void) const { return m_sockServer->errorString(); };
53-
bool isListening() { return m_sockServer->isListening(); };
51+
quint16 getServerPort (void) const { return m_sockServer->serverPort(); }
52+
QString getErrorString (void) const { return m_sockServer->errorString(); }
53+
bool isListening() { return m_sockServer->isListening(); }
54+
bool isSecure() { return m_useSsl; }
5455

5556
public slots:
5657
void start (quint16 port = 0);
5758
void stop (void);
58-
void setUseSecure (const bool ssl = true);
59-
void setServerName (const QString & serverName) { m_serverName = serverName; };
60-
void setPrivateKey (const QSslKey & key) { m_sslKey = key; };
61-
void setCertificates (const QList<QSslCertificate> & certs) { m_sslCerts = certs; };
62-
QSslKey getPrivateKey() { return m_sslKey; };
63-
QList<QSslCertificate> getCertificates() { return m_sslCerts; };
59+
void setUseSecure (bool ssl = true);
60+
void setServerName (const QString & serverName) { m_serverName = serverName; }
61+
void setPrivateKey (const QSslKey & key) { m_sslKey = key; }
62+
void setCertificates (const QList<QSslCertificate> & certs) { m_sslCerts = certs; }
63+
QSslKey getPrivateKey() { return m_sslKey; }
64+
QList<QSslCertificate> getCertificates() { return m_sslCerts; }
6465

6566
signals:
6667
void started (quint16 port);
@@ -73,10 +74,10 @@ public slots:
7374
private slots:
7475
void onClientConnected (void);
7576
void onClientDisconnected (void);
76-
void onClientSslEncrypted (void) { };
77-
void onClientSslPeerVerifyError (const QSslError & err) { Q_UNUSED (err) };
78-
void onClientSslErrors (const QList<QSslError> & errors) { Q_UNUSED (errors) };
79-
void onClientSslModeChanged (QSslSocket::SslMode mode) { Q_UNUSED (mode) };
77+
void onClientSslEncrypted (void) { }
78+
void onClientSslPeerVerifyError (const QSslError & err) { Q_UNUSED (err) }
79+
void onClientSslErrors (const QList<QSslError> & errors) { Q_UNUSED (errors) }
80+
void onClientSslModeChanged (QSslSocket::SslMode mode) { Q_UNUSED (mode) }
8081

8182
private:
8283
bool m_useSsl;

libsrc/webserver/WebSocketJsonHandler.cpp

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,26 @@
55
#include <utils/JsonUtils.h>
66
#include <utils/NetOrigin.h>
77

8+
#define NO_TRACE_SEND
9+
#define NO_TRACE_RECEIVE
10+
811
WebSocketJsonHandler::WebSocketJsonHandler(QWebSocket* websocket, QObject* parent)
912
: QObject(parent)
1013
, _websocket(websocket)
1114
, _log(Logger::getInstance("WEBSOCKET"))
1215
{
1316
connect(_websocket, &QWebSocket::textMessageReceived, this, &WebSocketJsonHandler::onTextMessageReceived);
17+
connect(_websocket, &QWebSocket::binaryMessageReceived, this, &WebSocketJsonHandler::onBinaryMessageReceived);
1418
connect(_websocket, &QWebSocket::disconnected, this, &WebSocketJsonHandler::onDisconnected);
1519

16-
const QString client = _websocket->peerAddress().toString();
17-
Debug(_log, "New WebSocket connection from %s", QSTRING_CSTR(client));
20+
_peerAddress = _websocket->peerAddress().toString();
21+
_origin = websocket->origin();
22+
Debug(_log, "New WebSocket connection from %s initiated via: %s", QSTRING_CSTR(_peerAddress), QSTRING_CSTR(_origin));
1823

1924
bool localConnection = NetOrigin::getInstance()->isLocalAddress(_websocket->peerAddress(), _websocket->localAddress());
2025

2126
// Json processor
22-
_jsonAPI.reset(new JsonAPI(client, _log, localConnection, this));
27+
_jsonAPI.reset(new JsonAPI(_peerAddress, _log, localConnection, this));
2328

2429
connect(_jsonAPI.get(), &JsonAPI::callbackReady, this, &WebSocketJsonHandler::sendMessage);
2530
connect(_jsonAPI->getCallBack().get(), &JsonCallbacks::callbackReady, this, &WebSocketJsonHandler::sendMessage);
@@ -30,20 +35,29 @@ WebSocketJsonHandler::WebSocketJsonHandler(QWebSocket* websocket, QObject* paren
3035

3136
void WebSocketJsonHandler::onTextMessageReceived(const QString& message)
3237
{
33-
qDebug() << "WebSocket message received:" << message;
34-
_jsonAPI.get()->handleMessage(message);
38+
#ifdef RECEIVE_TRACE
39+
qDebug() << "[" << _peerAddress << "] WebSocket message received:" << message;
40+
#endif
41+
_jsonAPI->handleMessage(message);
42+
}
43+
44+
void WebSocketJsonHandler::onBinaryMessageReceived(const QByteArray& message)
45+
{
46+
#ifdef RECEIVE_TRACE
47+
qDebug() << "[" << _peerAddress << "] WebSocket message received:" << message.toHex();
48+
#endif
49+
Warning(_log,"Unexpected binary message received");
3550
}
3651

3752
qint64 WebSocketJsonHandler::sendMessage(QJsonObject obj)
3853
{
39-
QString const message = JsonUtils::jsonValueToQString(obj);
40-
qDebug() << "WebSocket send message: " << message;
41-
return _websocket->sendTextMessage(message);
54+
#ifdef TRACE_SEND
55+
qDebug() << "[" << _peerAddress << "] WebSocket send message: " << obj;
56+
#endif
57+
return _websocket->sendTextMessage(JsonUtils::jsonValueToQString(obj));
4258
}
4359

4460
void WebSocketJsonHandler::onDisconnected()
4561
{
46-
qDebug() << "WebSocket disconnected";
62+
Debug(_log, "WebSocket disconnected from %s initiated via: %s", QSTRING_CSTR(_peerAddress), QSTRING_CSTR(_origin));
4763
}
48-
49-

libsrc/webserver/WebSocketJsonHandler.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class WebSocketJsonHandler : public QObject
1717

1818
private slots:
1919
void onTextMessageReceived(const QString& message);
20+
void onBinaryMessageReceived(const QByteArray& message);
2021
void onDisconnected();
2122
qint64 sendMessage(QJsonObject obj);
2223

@@ -25,6 +26,8 @@ private slots:
2526

2627
Logger* _log;
2728
QScopedPointer<JsonAPI> _jsonAPI;
29+
QString _peerAddress;
30+
QString _origin;
2831
};
2932

3033
#endif // WEBSOCKETJSONHANDLER_H

0 commit comments

Comments
 (0)