2013-08-17 15:39:29 +02:00
|
|
|
// stl includes
|
2013-08-17 19:20:19 +02:00
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
// Qt includes
|
2014-10-25 22:35:53 +02:00
|
|
|
#include <QCryptographicHash>
|
2013-08-18 12:21:07 +02:00
|
|
|
|
2013-08-17 19:20:19 +02:00
|
|
|
// project includes
|
2013-08-17 15:39:29 +02:00
|
|
|
#include "JsonClientConnection.h"
|
|
|
|
|
2016-07-31 22:21:35 +02:00
|
|
|
JsonClientConnection::JsonClientConnection(QTcpSocket *socket)
|
|
|
|
: QObject()
|
|
|
|
, _socket(socket)
|
|
|
|
, _hyperion(Hyperion::getInstance())
|
|
|
|
, _receiveBuffer()
|
|
|
|
, _webSocketHandshakeDone(false)
|
2016-11-26 22:46:16 +01:00
|
|
|
, _log(Logger::getInstance("JSONCLIENTCONNECTION"))
|
2017-03-01 15:23:53 +01:00
|
|
|
, _clientAddress(socket->peerAddress())
|
2013-08-17 15:39:29 +02:00
|
|
|
{
|
2013-08-17 19:20:19 +02:00
|
|
|
// connect internal signals and slots
|
2013-08-17 15:39:29 +02:00
|
|
|
connect(_socket, SIGNAL(disconnected()), this, SLOT(socketClosed()));
|
|
|
|
connect(_socket, SIGNAL(readyRead()), this, SLOT(readData()));
|
2016-11-26 22:46:16 +01:00
|
|
|
|
2017-06-24 11:52:22 +02:00
|
|
|
// create a new instance of JsonProcessor
|
|
|
|
_jsonProcessor = new JsonProcessor(_clientAddress.toString());
|
|
|
|
// get the callback messages from JsonProcessor and send it to the client
|
|
|
|
connect(_jsonProcessor,SIGNAL(callbackMessage(QJsonObject)),this,SLOT(sendMessage(QJsonObject)));
|
2013-08-17 15:39:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
JsonClientConnection::~JsonClientConnection()
|
|
|
|
{
|
|
|
|
delete _socket;
|
2017-06-24 11:52:22 +02:00
|
|
|
delete _jsonProcessor;
|
2013-08-17 15:39:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void JsonClientConnection::readData()
|
|
|
|
{
|
|
|
|
_receiveBuffer += _socket->readAll();
|
2017-06-17 23:29:04 +02:00
|
|
|
|
2014-11-08 21:01:46 +01:00
|
|
|
if (_webSocketHandshakeDone)
|
|
|
|
{
|
|
|
|
// websocket mode, data frame
|
|
|
|
handleWebSocketFrame();
|
2016-12-05 11:10:44 +01:00
|
|
|
}
|
|
|
|
else
|
2014-11-08 21:01:46 +01:00
|
|
|
{
|
|
|
|
// might be a handshake request or raw socket data
|
|
|
|
if(_receiveBuffer.contains("Upgrade: websocket"))
|
|
|
|
{
|
|
|
|
doWebSocketHandshake();
|
2017-06-24 11:52:22 +02:00
|
|
|
} else
|
2014-11-08 21:01:46 +01:00
|
|
|
{
|
|
|
|
// raw socket data, handling as usual
|
2014-10-25 22:35:53 +02:00
|
|
|
int bytes = _receiveBuffer.indexOf('\n') + 1;
|
|
|
|
while(bytes > 0)
|
|
|
|
{
|
|
|
|
// create message string
|
2017-03-04 22:17:42 +01:00
|
|
|
QString message(QByteArray(_receiveBuffer.data(), bytes));
|
2014-10-25 22:35:53 +02:00
|
|
|
|
|
|
|
// remove message data from buffer
|
|
|
|
_receiveBuffer = _receiveBuffer.mid(bytes);
|
|
|
|
|
|
|
|
// handle message
|
2017-06-24 11:52:22 +02:00
|
|
|
_jsonProcessor->handleMessage(message);
|
2014-10-25 22:35:53 +02:00
|
|
|
|
|
|
|
// try too look up '\n' again
|
|
|
|
bytes = _receiveBuffer.indexOf('\n') + 1;
|
2017-06-24 11:52:22 +02:00
|
|
|
}
|
2014-10-25 22:35:53 +02:00
|
|
|
}
|
|
|
|
}
|
2013-08-17 15:39:29 +02:00
|
|
|
}
|
|
|
|
|
2014-11-08 21:01:46 +01:00
|
|
|
void JsonClientConnection::handleWebSocketFrame()
|
|
|
|
{
|
2016-12-05 11:10:44 +01:00
|
|
|
if ((_receiveBuffer.at(0) & BHB0_FIN) == BHB0_FIN)
|
2014-11-08 21:01:46 +01:00
|
|
|
{
|
|
|
|
// final bit found, frame complete
|
|
|
|
quint8 * maskKey = NULL;
|
2016-12-05 11:10:44 +01:00
|
|
|
quint8 opCode = _receiveBuffer.at(0) & BHB0_OPCODE;
|
|
|
|
bool isMasked = (_receiveBuffer.at(1) & BHB0_FIN) == BHB0_FIN;
|
|
|
|
quint64 payloadLength = _receiveBuffer.at(1) & BHB1_PAYLOAD;
|
2014-11-08 21:01:46 +01:00
|
|
|
quint32 index = 2;
|
2017-06-24 11:52:22 +02:00
|
|
|
|
2014-11-08 21:01:46 +01:00
|
|
|
switch (payloadLength)
|
|
|
|
{
|
2016-12-05 11:10:44 +01:00
|
|
|
case payload_size_code_16bit:
|
2014-11-08 21:01:46 +01:00
|
|
|
payloadLength = ((_receiveBuffer.at(2) << 8) & 0xFF00) | (_receiveBuffer.at(3) & 0xFF);
|
|
|
|
index += 2;
|
|
|
|
break;
|
2016-12-05 11:10:44 +01:00
|
|
|
case payload_size_code_64bit:
|
2014-11-08 21:01:46 +01:00
|
|
|
payloadLength = 0;
|
2016-12-05 11:10:44 +01:00
|
|
|
for (uint i=0; i < 8; i++)
|
|
|
|
{
|
2014-11-08 21:01:46 +01:00
|
|
|
payloadLength |= ((quint64)(_receiveBuffer.at(index+i) & 0xFF)) << (8*(7-i));
|
|
|
|
}
|
|
|
|
index += 8;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2017-06-24 11:52:22 +02:00
|
|
|
|
2014-11-08 21:01:46 +01:00
|
|
|
if (isMasked)
|
|
|
|
{
|
|
|
|
// if the data is masked we need to get the key for unmasking
|
|
|
|
maskKey = new quint8[4];
|
|
|
|
for (uint i=0; i < 4; i++)
|
|
|
|
{
|
2017-06-24 11:52:22 +02:00
|
|
|
maskKey[i] = _receiveBuffer.at(index + i);
|
2014-11-08 21:01:46 +01:00
|
|
|
}
|
|
|
|
index += 4;
|
|
|
|
}
|
2017-06-24 11:52:22 +02:00
|
|
|
|
2014-11-08 21:01:46 +01:00
|
|
|
// check the type of data frame
|
|
|
|
switch (opCode)
|
|
|
|
{
|
2016-12-05 11:10:44 +01:00
|
|
|
case OPCODE::TEXT:
|
2014-11-08 21:01:46 +01:00
|
|
|
{
|
|
|
|
// frame contains text, extract it
|
|
|
|
QByteArray result = _receiveBuffer.mid(index, payloadLength);
|
|
|
|
_receiveBuffer.clear();
|
2017-06-24 11:52:22 +02:00
|
|
|
|
2014-11-08 21:01:46 +01:00
|
|
|
// unmask data if necessary
|
|
|
|
if (isMasked)
|
|
|
|
{
|
|
|
|
for (uint i=0; i < payloadLength; i++)
|
|
|
|
{
|
|
|
|
result[i] = (result[i] ^ maskKey[i % 4]);
|
|
|
|
}
|
|
|
|
if (maskKey != NULL)
|
|
|
|
{
|
|
|
|
delete[] maskKey;
|
|
|
|
maskKey = NULL;
|
|
|
|
}
|
|
|
|
}
|
2017-06-24 11:52:22 +02:00
|
|
|
|
|
|
|
_jsonProcessor->handleMessage(QString(result));
|
2014-11-08 21:01:46 +01:00
|
|
|
}
|
|
|
|
break;
|
2016-12-05 11:10:44 +01:00
|
|
|
case OPCODE::CLOSE:
|
2014-11-08 21:01:46 +01:00
|
|
|
{
|
|
|
|
// close request, confirm
|
2016-12-05 11:10:44 +01:00
|
|
|
quint8 close[] = {0x88, 0};
|
2014-11-08 21:01:46 +01:00
|
|
|
_socket->write((const char*)close, 2);
|
|
|
|
_socket->flush();
|
|
|
|
_socket->close();
|
|
|
|
}
|
|
|
|
break;
|
2016-12-05 11:10:44 +01:00
|
|
|
case OPCODE::PING:
|
2014-11-08 21:01:46 +01:00
|
|
|
{
|
|
|
|
// ping received, send pong
|
2016-12-05 11:10:44 +01:00
|
|
|
quint8 pong[] = {OPCODE::PONG, 0};
|
2014-11-08 21:01:46 +01:00
|
|
|
_socket->write((const char*)pong, 2);
|
|
|
|
_socket->flush();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-12-05 11:10:44 +01:00
|
|
|
}
|
|
|
|
else
|
2014-11-08 21:01:46 +01:00
|
|
|
{
|
2016-07-11 17:08:22 +02:00
|
|
|
Error(_log, "Someone is sending very big messages over several frames... it's not supported yet");
|
2017-06-24 11:52:22 +02:00
|
|
|
quint8 close[] = {0x88, 0};
|
2014-11-08 21:01:46 +01:00
|
|
|
_socket->write((const char*)close, 2);
|
|
|
|
_socket->flush();
|
|
|
|
_socket->close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsonClientConnection::doWebSocketHandshake()
|
|
|
|
{
|
|
|
|
// http header, might not be a very reliable check...
|
2016-07-11 17:08:22 +02:00
|
|
|
Debug(_log, "Websocket handshake");
|
2014-11-08 21:01:46 +01:00
|
|
|
|
|
|
|
// get the key to prepare an answer
|
|
|
|
int start = _receiveBuffer.indexOf("Sec-WebSocket-Key") + 19;
|
2017-03-04 22:17:42 +01:00
|
|
|
QByteArray value = _receiveBuffer.mid(start, _receiveBuffer.indexOf("\r\n", start) - start);
|
2014-11-08 21:01:46 +01:00
|
|
|
_receiveBuffer.clear();
|
|
|
|
|
|
|
|
// must be always appended
|
|
|
|
value += "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
|
|
|
|
|
|
|
|
// generate sha1 hash
|
2017-03-04 22:17:42 +01:00
|
|
|
QByteArray hash = QCryptographicHash::hash(value, QCryptographicHash::Sha1);
|
2014-11-08 21:01:46 +01:00
|
|
|
|
|
|
|
// prepare an answer
|
|
|
|
std::ostringstream h;
|
|
|
|
h << "HTTP/1.1 101 Switching Protocols\r\n" <<
|
|
|
|
"Upgrade: websocket\r\n" <<
|
2017-06-24 11:52:22 +02:00
|
|
|
"Connection: Upgrade\r\n" <<
|
2014-11-08 21:01:46 +01:00
|
|
|
"Sec-WebSocket-Accept: " << QString(hash.toBase64()).toStdString() << "\r\n\r\n";
|
|
|
|
|
|
|
|
_socket->write(h.str().c_str());
|
|
|
|
_socket->flush();
|
2017-06-17 23:29:04 +02:00
|
|
|
|
2014-11-08 21:01:46 +01:00
|
|
|
// we are in WebSocket mode, data frames should follow next
|
|
|
|
_webSocketHandshakeDone = true;
|
|
|
|
}
|
|
|
|
|
2013-08-17 15:39:29 +02:00
|
|
|
void JsonClientConnection::socketClosed()
|
|
|
|
{
|
2014-10-25 22:35:53 +02:00
|
|
|
_webSocketHandshakeDone = false;
|
2013-08-17 15:39:29 +02:00
|
|
|
emit connectionClosed(this);
|
|
|
|
}
|
|
|
|
|
2017-06-24 11:52:22 +02:00
|
|
|
void JsonClientConnection::sendMessage(QJsonObject message)
|
2013-08-17 15:39:29 +02:00
|
|
|
{
|
2016-10-09 22:22:17 +02:00
|
|
|
QJsonDocument writer(message);
|
|
|
|
QByteArray serializedReply = writer.toJson(QJsonDocument::Compact) + "\n";
|
2017-06-24 11:52:22 +02:00
|
|
|
|
2014-11-08 21:01:46 +01:00
|
|
|
if (!_webSocketHandshakeDone)
|
|
|
|
{
|
|
|
|
// raw tcp socket mode
|
2014-10-25 22:35:53 +02:00
|
|
|
_socket->write(serializedReply.data(), serializedReply.length());
|
2014-11-08 21:01:46 +01:00
|
|
|
} else
|
|
|
|
{
|
|
|
|
// websocket mode
|
2014-10-25 22:35:53 +02:00
|
|
|
quint32 size = serializedReply.length();
|
2017-06-24 11:52:22 +02:00
|
|
|
|
2014-10-25 22:35:53 +02:00
|
|
|
// prepare data frame
|
|
|
|
QByteArray response;
|
|
|
|
response.append(0x81);
|
2014-11-08 21:01:46 +01:00
|
|
|
if (size > 125)
|
|
|
|
{
|
2014-10-25 22:35:53 +02:00
|
|
|
response.append(0x7E);
|
|
|
|
response.append((size >> 8) & 0xFF);
|
|
|
|
response.append(size & 0xFF);
|
|
|
|
} else {
|
|
|
|
response.append(size);
|
|
|
|
}
|
2013-08-18 12:02:17 +02:00
|
|
|
|
2017-06-24 11:52:22 +02:00
|
|
|
response.append(serializedReply, serializedReply.length());
|
2016-12-18 19:00:14 +01:00
|
|
|
|
2017-06-24 11:52:22 +02:00
|
|
|
_socket->write(response.data(), response.length());
|
2016-12-18 19:00:14 +01:00
|
|
|
}
|
2016-09-07 20:10:37 +02:00
|
|
|
}
|