2013-08-17 19:20:19 +02:00
|
|
|
// Qt includes
|
2014-10-25 22:35:53 +02:00
|
|
|
#include <QCryptographicHash>
|
2017-08-06 21:50:30 +02:00
|
|
|
#include <QtEndian>
|
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"
|
|
|
|
|
2017-08-06 21:50:30 +02:00
|
|
|
const quint64 FRAME_SIZE_IN_BYTES = 512 * 512 * 2; //maximum size of a frame when sending a message
|
|
|
|
|
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-08-06 21:50:30 +02:00
|
|
|
//printf("%ld\n", payloadLength);
|
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);
|
2017-08-06 21:50:30 +02:00
|
|
|
QByteArray hashB64 = hash.toBase64();
|
2014-11-08 21:01:46 +01:00
|
|
|
|
|
|
|
// prepare an answer
|
2017-08-06 21:50:30 +02:00
|
|
|
QString data
|
|
|
|
= QString("HTTP/1.1 101 Switching Protocols\r\n")
|
|
|
|
+ QString("Upgrade: websocket\r\n")
|
|
|
|
+ QString("Connection: Upgrade\r\n")
|
|
|
|
+ QString("Sec-WebSocket-Accept: ")
|
|
|
|
+ QString(hashB64.data()) + "\r\n\r\n";
|
2014-11-08 21:01:46 +01:00
|
|
|
|
2017-08-06 21:50:30 +02:00
|
|
|
_socket->write(QSTRING_CSTR(data), data.size());
|
2014-11-08 21:01:46 +01:00
|
|
|
_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-08-06 21:50:30 +02:00
|
|
|
QByteArray JsonClientConnection::getFrameHeader(quint8 opCode, quint64 payloadLength, bool lastFrame)
|
|
|
|
{
|
|
|
|
QByteArray header;
|
|
|
|
bool ok = payloadLength <= 0x7FFFFFFFFFFFFFFFULL;
|
|
|
|
|
|
|
|
if (ok)
|
|
|
|
{
|
|
|
|
//FIN, RSV1-3, opcode (RSV-1, RSV-2 and RSV-3 are zero)
|
|
|
|
quint8 byte = static_cast<quint8>((opCode & 0x0F) | (lastFrame ? 0x80 : 0x00));
|
|
|
|
header.append(static_cast<char>(byte));
|
|
|
|
|
|
|
|
byte = 0x00;
|
|
|
|
if (payloadLength <= 125)
|
|
|
|
{
|
|
|
|
byte |= static_cast<quint8>(payloadLength);
|
|
|
|
header.append(static_cast<char>(byte));
|
|
|
|
}
|
|
|
|
else if (payloadLength <= 0xFFFFU)
|
|
|
|
{
|
|
|
|
byte |= 126;
|
|
|
|
header.append(static_cast<char>(byte));
|
|
|
|
quint16 swapped = qToBigEndian<quint16>(static_cast<quint16>(payloadLength));
|
|
|
|
header.append(static_cast<const char *>(static_cast<const void *>(&swapped)), 2);
|
|
|
|
}
|
|
|
|
else if (payloadLength <= 0x7FFFFFFFFFFFFFFFULL)
|
|
|
|
{
|
|
|
|
byte |= 127;
|
|
|
|
header.append(static_cast<char>(byte));
|
|
|
|
quint64 swapped = qToBigEndian<quint64>(payloadLength);
|
|
|
|
header.append(static_cast<const char *>(static_cast<const void *>(&swapped)), 8);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Error(_log, "JsonClientConnection::getHeader: payload too big!");
|
|
|
|
}
|
|
|
|
|
|
|
|
return header;
|
|
|
|
}
|
|
|
|
|
|
|
|
qint64 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
|
|
|
|
2017-08-06 21:50:30 +02:00
|
|
|
if (!_socket || (_socket->state() != QAbstractSocket::ConnectedState)) return 0;
|
|
|
|
if (_webSocketHandshakeDone) return sendMessage_Websockets(serializedReply);
|
|
|
|
|
|
|
|
return sendMessage_Raw(serializedReply);
|
|
|
|
}
|
|
|
|
|
|
|
|
qint64 JsonClientConnection::sendMessage_Raw(const char* data, quint64 size)
|
|
|
|
{
|
|
|
|
return _socket->write(data, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
qint64 JsonClientConnection::sendMessage_Raw(QByteArray data)
|
|
|
|
{
|
|
|
|
return _socket->write(data.data(), data.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
qint64 JsonClientConnection::sendMessage_Websockets(QByteArray &data)
|
|
|
|
{
|
|
|
|
qint64 payloadWritten = 0;
|
|
|
|
quint32 payloadSize = data.size();
|
|
|
|
const char * payload = data.data();
|
|
|
|
|
|
|
|
qint32 numFrames = payloadSize / FRAME_SIZE_IN_BYTES + ((quint64(payloadSize) % FRAME_SIZE_IN_BYTES) > 0 ? 1 : 0);
|
|
|
|
|
|
|
|
for (int i = 0; i < numFrames; ++i)
|
2014-11-08 21:01:46 +01:00
|
|
|
{
|
2017-08-06 21:50:30 +02:00
|
|
|
const bool isLastFrame = (i == (numFrames - 1));
|
2017-06-24 11:52:22 +02:00
|
|
|
|
2017-08-06 21:50:30 +02:00
|
|
|
quint64 position = i * FRAME_SIZE_IN_BYTES;
|
|
|
|
quint32 frameSize = (payloadSize-position >= FRAME_SIZE_IN_BYTES) ? FRAME_SIZE_IN_BYTES : (payloadSize-position);
|
|
|
|
|
|
|
|
sendMessage_Raw(getFrameHeader(OPCODE::TEXT, frameSize, isLastFrame));
|
|
|
|
qint64 written = sendMessage_Raw(payload+position,frameSize);
|
|
|
|
if (written > 0)
|
2014-11-08 21:01:46 +01:00
|
|
|
{
|
2017-08-06 21:50:30 +02:00
|
|
|
payloadWritten += written;
|
2014-10-25 22:35:53 +02:00
|
|
|
}
|
2017-08-06 21:50:30 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
_socket->flush();
|
|
|
|
Error(_log, "Error writing bytes to socket: %s", QSTRING_CSTR(_socket->errorString()));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-08-18 12:02:17 +02:00
|
|
|
|
2017-08-06 21:50:30 +02:00
|
|
|
if (payloadSize != payloadWritten)
|
|
|
|
{
|
|
|
|
Error(_log, "Error writing bytes to socket %d bytes from %d writte", payloadWritten, payloadSize);
|
|
|
|
return -1;
|
2016-12-18 19:00:14 +01:00
|
|
|
}
|
2017-08-06 21:50:30 +02:00
|
|
|
return payloadWritten;
|
2016-09-07 20:10:37 +02:00
|
|
|
}
|
2017-08-06 21:50:30 +02:00
|
|
|
|
|
|
|
|