The slot in the websocket client will now run through until there are no more data in the buffer

Signed-off-by: Paulchen-Panther <Paulchen-Panter@protonmail.com>
This commit is contained in:
Paulchen-Panther 2019-07-21 15:03:50 +02:00
parent 370e1b5f45
commit 96d79cdef6
No known key found for this signature in database
GPG Key ID: 84E3B692456B6840
14 changed files with 652 additions and 661 deletions

View File

@ -298,7 +298,7 @@ $(document).ready(function() {
createCP('cp2', cpcolor, function(rgbT,hex){ createCP('cp2', cpcolor, function(rgbT,hex){
rgb = rgbT; rgb = rgbT;
sendColor() sendColor();
setStorage('rmcpcolor', hex); setStorage('rmcpcolor', hex);
}); });

View File

@ -180,8 +180,6 @@ void LinearColorSmoothing::componentStateChange(const hyperion::Components compo
void LinearColorSmoothing::setEnable(bool enable) void LinearColorSmoothing::setEnable(bool enable)
{ {
LedDevice::setEnable(enable);
if (!enable) if (!enable)
{ {
_timer->stop(); _timer->stop();

View File

@ -30,8 +30,10 @@ QtHttpClientWrapper::QtHttpClientWrapper (QTcpSocket * sock, const bool& localCo
connect (m_sockClient, &QTcpSocket::readyRead, this, &QtHttpClientWrapper::onClientDataReceived); connect (m_sockClient, &QTcpSocket::readyRead, this, &QtHttpClientWrapper::onClientDataReceived);
} }
QString QtHttpClientWrapper::getGuid (void) { QString QtHttpClientWrapper::getGuid (void)
if (m_guid.isEmpty ()) { {
if (m_guid.isEmpty ())
{
m_guid = QString::fromLocal8Bit ( m_guid = QString::fromLocal8Bit (
QCryptographicHash::hash ( QCryptographicHash::hash (
QByteArray::number ((quint64) (this)), QByteArray::number ((quint64) (this)),
@ -39,81 +41,117 @@ QString QtHttpClientWrapper::getGuid (void) {
).toHex () ).toHex ()
); );
} }
return m_guid; return m_guid;
} }
void QtHttpClientWrapper::onClientDataReceived (void) { void QtHttpClientWrapper::onClientDataReceived (void)
if (m_sockClient != Q_NULLPTR) { {
while (m_sockClient->bytesAvailable ()) { if (m_sockClient != Q_NULLPTR)
{
while (m_sockClient->bytesAvailable ())
{
QByteArray line = m_sockClient->readLine (); QByteArray line = m_sockClient->readLine ();
switch (m_parsingStatus) { // handle parsing steps
case AwaitingRequest: { // "command url version" × 1 switch (m_parsingStatus) // handle parsing steps
{
case AwaitingRequest: // "command url version" × 1
{
QString str = QString::fromUtf8 (line).trimmed (); QString str = QString::fromUtf8 (line).trimmed ();
QStringList parts = str.split (SPACE, QString::SkipEmptyParts); QStringList parts = str.split (SPACE, QString::SkipEmptyParts);
if (parts.size () == 3) {
if (parts.size () == 3)
{
QString command = parts.at (0); QString command = parts.at (0);
QString url = parts.at (1); QString url = parts.at (1);
QString version = parts.at (2); QString version = parts.at (2);
if (version == QtHttpServer::HTTP_VERSION) {
if (version == QtHttpServer::HTTP_VERSION)
{
m_currentRequest = new QtHttpRequest (this, m_serverHandle); m_currentRequest = new QtHttpRequest (this, m_serverHandle);
m_currentRequest->setClientInfo(m_sockClient->localAddress(), m_sockClient->peerAddress()); m_currentRequest->setClientInfo(m_sockClient->localAddress(), m_sockClient->peerAddress());
m_currentRequest->setUrl (QUrl (url)); m_currentRequest->setUrl (QUrl (url));
m_currentRequest->setCommand (command); m_currentRequest->setCommand (command);
m_parsingStatus = AwaitingHeaders; m_parsingStatus = AwaitingHeaders;
} }
else { else
{
m_parsingStatus = ParsingError; m_parsingStatus = ParsingError;
//qWarning () << "Error : unhandled HTTP version :" << version; //qWarning () << "Error : unhandled HTTP version :" << version;
} }
} }
else { else
{
m_parsingStatus = ParsingError; m_parsingStatus = ParsingError;
//qWarning () << "Error : incorrect HTTP command line :" << line; //qWarning () << "Error : incorrect HTTP command line :" << line;
} }
break; break;
} }
case AwaitingHeaders: { // "header: value" × N (until empty line) case AwaitingHeaders: // "header: value" × N (until empty line)
{
QByteArray raw = line.trimmed (); QByteArray raw = line.trimmed ();
if (!raw.isEmpty ()) { // parse headers
if (!raw.isEmpty ()) // parse headers
{
int pos = raw.indexOf (COLON); int pos = raw.indexOf (COLON);
if (pos > 0) {
if (pos > 0)
{
QByteArray header = raw.left (pos).trimmed (); QByteArray header = raw.left (pos).trimmed ();
QByteArray value = raw.mid (pos +1).trimmed (); QByteArray value = raw.mid (pos +1).trimmed ();
m_currentRequest->addHeader (header, value); m_currentRequest->addHeader (header, value);
if (header == QtHttpHeader::ContentLength) { if (header == QtHttpHeader::ContentLength)
{
bool ok = false; bool ok = false;
const int len = value.toInt (&ok, 10); const int len = value.toInt (&ok, 10);
if (ok) { if (ok)
{
m_currentRequest->addHeader (QtHttpHeader::ContentLength, QByteArray::number (len)); m_currentRequest->addHeader (QtHttpHeader::ContentLength, QByteArray::number (len));
} }
} }
} }
else { else
{
m_parsingStatus = ParsingError; m_parsingStatus = ParsingError;
qWarning () << "Error : incorrect HTTP headers line :" << line; qWarning () << "Error : incorrect HTTP headers line :" << line;
} }
} }
else { // end of headers else // end of headers
if (m_currentRequest->getHeader (QtHttpHeader::ContentLength).toInt () > 0) { {
if (m_currentRequest->getHeader (QtHttpHeader::ContentLength).toInt () > 0)
{
m_parsingStatus = AwaitingContent; m_parsingStatus = AwaitingContent;
} }
else { else
{
m_parsingStatus = RequestParsed; m_parsingStatus = RequestParsed;
} }
} }
break; break;
} }
case AwaitingContent: { // raw data × N (until EOF ??) case AwaitingContent: // raw data × N (until EOF ??)
{
m_currentRequest->appendRawData (line); m_currentRequest->appendRawData (line);
if (m_currentRequest->getRawDataSize () == m_currentRequest->getHeader (QtHttpHeader::ContentLength).toInt ()) {
if (m_currentRequest->getRawDataSize () == m_currentRequest->getHeader (QtHttpHeader::ContentLength).toInt ())
{
m_parsingStatus = RequestParsed; m_parsingStatus = RequestParsed;
} }
break; break;
} }
default: { break; } default:
{
break;
} }
switch (m_parsingStatus) { // handle parsing status end/error }
case RequestParsed: { // a valid request has ben fully parsed
switch (m_parsingStatus) // handle parsing status end/error
{
case RequestParsed: // a valid request has ben fully parsed
{
// Catch websocket header "Upgrade" // Catch websocket header "Upgrade"
if(m_currentRequest->getHeader(QtHttpHeader::Upgrade) == "websocket") if(m_currentRequest->getHeader(QtHttpHeader::Upgrade) == "websocket")
{ {
@ -121,69 +159,87 @@ void QtHttpClientWrapper::onClientDataReceived (void) {
{ {
// disconnect this slot from socket for further requests // disconnect this slot from socket for further requests
disconnect(m_sockClient, &QTcpSocket::readyRead, this, &QtHttpClientWrapper::onClientDataReceived); disconnect(m_sockClient, &QTcpSocket::readyRead, this, &QtHttpClientWrapper::onClientDataReceived);
// disabling packet bunching
m_sockClient->setSocketOption(QAbstractSocket::LowDelayOption, 1);
m_sockClient->setSocketOption(QAbstractSocket::KeepAliveOption, 1);
m_websocketClient = new WebSocketClient(m_currentRequest, m_sockClient, m_localConnection, this); m_websocketClient = new WebSocketClient(m_currentRequest, m_sockClient, m_localConnection, this);
} }
break; break;
} }
// add post data to request and catch /jsonrpc subroute url // add post data to request and catch /jsonrpc subroute url
if ( m_currentRequest->getCommand() == "POST") if ( m_currentRequest->getCommand() == "POST")
{ {
QtHttpPostData postData; QtHttpPostData postData;
QByteArray data = m_currentRequest->getRawData(); QByteArray data = m_currentRequest->getRawData();
QList<QByteArray> parts = data.split('&'); QList<QByteArray> parts = data.split('&');
for (int i = 0; i < parts.size(); ++i) for (int i = 0; i < parts.size(); ++i)
{ {
QList<QByteArray> keyValue = parts.at(i).split('='); QList<QByteArray> keyValue = parts.at(i).split('=');
QByteArray value; QByteArray value;
if (keyValue.size()>1) if (keyValue.size()>1)
{ {
value = QByteArray::fromPercentEncoding(keyValue.at(1)); value = QByteArray::fromPercentEncoding(keyValue.at(1));
} }
postData.insert(QString::fromUtf8(keyValue.at(0)),value); postData.insert(QString::fromUtf8(keyValue.at(0)),value);
} }
m_currentRequest->setPostData(postData); m_currentRequest->setPostData(postData);
// catch /jsonrpc in url, we need async callback, StaticFileServing is sync // catch /jsonrpc in url, we need async callback, StaticFileServing is sync
QString path = m_currentRequest->getUrl ().path (); QString path = m_currentRequest->getUrl ().path ();
QStringList uri_parts = path.split('/', QString::SkipEmptyParts); QStringList uri_parts = path.split('/', QString::SkipEmptyParts);
if ( ! uri_parts.empty() && uri_parts.at(0) == "json-rpc" ) if ( ! uri_parts.empty() && uri_parts.at(0) == "json-rpc" )
{ {
if(m_webJsonRpc == Q_NULLPTR) if(m_webJsonRpc == Q_NULLPTR)
{ {
m_webJsonRpc = new WebJsonRpc(m_currentRequest, m_serverHandle, m_localConnection, this); m_webJsonRpc = new WebJsonRpc(m_currentRequest, m_serverHandle, m_localConnection, this);
} }
m_webJsonRpc->handleMessage(m_currentRequest); m_webJsonRpc->handleMessage(m_currentRequest);
break; break;
} }
} }
QtHttpReply reply (m_serverHandle); QtHttpReply reply (m_serverHandle);
connect (&reply, &QtHttpReply::requestSendHeaders, connect (&reply, &QtHttpReply::requestSendHeaders, this, &QtHttpClientWrapper::onReplySendHeadersRequested);
this, &QtHttpClientWrapper::onReplySendHeadersRequested); connect (&reply, &QtHttpReply::requestSendData, this, &QtHttpClientWrapper::onReplySendDataRequested);
connect (&reply, &QtHttpReply::requestSendData,
this, &QtHttpClientWrapper::onReplySendDataRequested);
emit m_serverHandle->requestNeedsReply (m_currentRequest, &reply); // allow app to handle request emit m_serverHandle->requestNeedsReply (m_currentRequest, &reply); // allow app to handle request
m_parsingStatus = sendReplyToClient (&reply); m_parsingStatus = sendReplyToClient (&reply);
break; break;
} }
case ParsingError: { // there was an error durin one of parsing steps case ParsingError: // there was an error durin one of parsing steps
{
m_sockClient->readAll (); // clear remaining buffer to ignore content m_sockClient->readAll (); // clear remaining buffer to ignore content
QtHttpReply reply (m_serverHandle); QtHttpReply reply (m_serverHandle);
reply.setStatusCode (QtHttpReply::BadRequest); reply.setStatusCode (QtHttpReply::BadRequest);
reply.appendRawData (QByteArrayLiteral ("<h1>Bad Request (HTTP parsing error) !</h1>")); reply.appendRawData (QByteArrayLiteral ("<h1>Bad Request (HTTP parsing error) !</h1>"));
reply.appendRawData (CRLF); reply.appendRawData (CRLF);
m_parsingStatus = sendReplyToClient (&reply); m_parsingStatus = sendReplyToClient (&reply);
break;
}
default:
{
break; break;
} }
default: { break; }
} }
} }
} }
} }
void QtHttpClientWrapper::onReplySendHeadersRequested (void) { void QtHttpClientWrapper::onReplySendHeadersRequested (void)
{
QtHttpReply * reply = qobject_cast<QtHttpReply *> (sender ()); QtHttpReply * reply = qobject_cast<QtHttpReply *> (sender ());
if (reply != Q_NULLPTR) {
if (reply != Q_NULLPTR)
{
QByteArray data; QByteArray data;
// HTTP Version + Status Code + Status Msg // HTTP Version + Status Code + Status Msg
data.append (QtHttpServer::HTTP_VERSION); data.append (QtHttpServer::HTTP_VERSION);
@ -192,22 +248,28 @@ void QtHttpClientWrapper::onReplySendHeadersRequested (void) {
data.append (SPACE); data.append (SPACE);
data.append (QtHttpReply::getStatusTextForCode (reply->getStatusCode ())); data.append (QtHttpReply::getStatusTextForCode (reply->getStatusCode ()));
data.append (CRLF); data.append (CRLF);
// Header name: header value
if (reply->useChunked ()) { if (reply->useChunked ()) // Header name: header value
{
static const QByteArray & CHUNKED = QByteArrayLiteral ("chunked"); static const QByteArray & CHUNKED = QByteArrayLiteral ("chunked");
reply->addHeader (QtHttpHeader::TransferEncoding, CHUNKED); reply->addHeader (QtHttpHeader::TransferEncoding, CHUNKED);
} }
else { else
{
reply->addHeader (QtHttpHeader::ContentLength, QByteArray::number (reply->getRawDataSize ())); reply->addHeader (QtHttpHeader::ContentLength, QByteArray::number (reply->getRawDataSize ()));
} }
const QList<QByteArray> & headersList = reply->getHeadersList (); const QList<QByteArray> & headersList = reply->getHeadersList ();
foreach (const QByteArray & header, headersList) {
foreach (const QByteArray & header, headersList)
{
data.append (header); data.append (header);
data.append (COLON); data.append (COLON);
data.append (SPACE); data.append (SPACE);
data.append (reply->getHeader (header)); data.append (reply->getHeader (header));
data.append (CRLF); data.append (CRLF);
} }
// empty line // empty line
data.append (CRLF); data.append (CRLF);
m_sockClient->write (data); m_sockClient->write (data);
@ -215,52 +277,66 @@ void QtHttpClientWrapper::onReplySendHeadersRequested (void) {
} }
} }
void QtHttpClientWrapper::onReplySendDataRequested (void) { void QtHttpClientWrapper::onReplySendDataRequested (void)
{
QtHttpReply * reply = qobject_cast<QtHttpReply *> (sender ()); QtHttpReply * reply = qobject_cast<QtHttpReply *> (sender ());
if (reply != Q_NULLPTR) { if (reply != Q_NULLPTR)
{
// content raw data // content raw data
QByteArray data = reply->getRawData (); QByteArray data = reply->getRawData ();
if (reply->useChunked ()) {
if (reply->useChunked ())
{
data.prepend (QByteArray::number (data.size (), 16) % CRLF); data.prepend (QByteArray::number (data.size (), 16) % CRLF);
data.append (CRLF); data.append (CRLF);
reply->resetRawData (); reply->resetRawData ();
} }
// write to socket // write to socket
m_sockClient->write (data); m_sockClient->write (data);
m_sockClient->flush (); m_sockClient->flush ();
} }
} }
void QtHttpClientWrapper::sendToClientWithReply(QtHttpReply * reply) { void QtHttpClientWrapper::sendToClientWithReply(QtHttpReply * reply)
connect (reply, &QtHttpReply::requestSendHeaders, {
this, &QtHttpClientWrapper::onReplySendHeadersRequested); connect (reply, &QtHttpReply::requestSendHeaders, this, &QtHttpClientWrapper::onReplySendHeadersRequested);
connect (reply, &QtHttpReply::requestSendData, connect (reply, &QtHttpReply::requestSendData, this, &QtHttpClientWrapper::onReplySendDataRequested);
this, &QtHttpClientWrapper::onReplySendDataRequested);
m_parsingStatus = sendReplyToClient (reply); m_parsingStatus = sendReplyToClient (reply);
} }
QtHttpClientWrapper::ParsingStatus QtHttpClientWrapper::sendReplyToClient (QtHttpReply * reply) { QtHttpClientWrapper::ParsingStatus QtHttpClientWrapper::sendReplyToClient (QtHttpReply * reply)
if (reply != Q_NULLPTR) { {
if (!reply->useChunked ()) { if (reply != Q_NULLPTR)
{
if (!reply->useChunked ())
{
//reply->appendRawData (CRLF); //reply->appendRawData (CRLF);
// send all headers and all data in one shot // send all headers and all data in one shot
reply->requestSendHeaders (); reply->requestSendHeaders ();
reply->requestSendData (); reply->requestSendData ();
} }
else { else
{
// last chunk // last chunk
m_sockClient->write ("0" % CRLF % CRLF); m_sockClient->write ("0" % CRLF % CRLF);
m_sockClient->flush (); m_sockClient->flush ();
} }
if (m_currentRequest != Q_NULLPTR) {
if (m_currentRequest != Q_NULLPTR)
{
static const QByteArray & CLOSE = QByteArrayLiteral ("close"); static const QByteArray & CLOSE = QByteArrayLiteral ("close");
if (m_currentRequest->getHeader (QtHttpHeader::Connection).toLower () == CLOSE) {
if (m_currentRequest->getHeader (QtHttpHeader::Connection).toLower () == CLOSE)
{
// must close connection after this request // must close connection after this request
m_sockClient->close (); m_sockClient->close ();
} }
m_currentRequest->deleteLater (); m_currentRequest->deleteLater ();
m_currentRequest = Q_NULLPTR; m_currentRequest = Q_NULLPTR;
} }
} }
return AwaitingRequest; return AwaitingRequest;
} }

View File

@ -3,7 +3,8 @@
class QByteArray; class QByteArray;
class QtHttpHeader { class QtHttpHeader
{
public: public:
static const QByteArray & Server; static const QByteArray & Server;
static const QByteArray & Date; static const QByteArray & Date;

View File

@ -17,32 +17,10 @@ QtHttpReply::QtHttpReply (QtHttpServer * parent)
addHeader (QtHttpHeader::Server, m_serverHandle->getServerName ().toUtf8 ()); addHeader (QtHttpHeader::Server, m_serverHandle->getServerName ().toUtf8 ());
} }
int QtHttpReply::getRawDataSize (void) const { const QByteArray QtHttpReply::getStatusTextForCode (QtHttpReply::StatusCode statusCode)
return m_data.size (); {
} switch (statusCode)
{
bool QtHttpReply::useChunked (void) const {
return m_useChunked;
}
QtHttpReply::StatusCode QtHttpReply::getStatusCode (void) const {
return m_statusCode;
}
QByteArray QtHttpReply::getRawData (void) const {
return m_data;
}
QList<QByteArray> QtHttpReply::getHeadersList (void) const {
return m_headersHash.keys ();
}
QByteArray QtHttpReply::getHeader (const QByteArray & header) const {
return m_headersHash.value (header, QByteArray ());
}
const QByteArray QtHttpReply::getStatusTextForCode (QtHttpReply::StatusCode statusCode) {
switch (statusCode) {
case Ok: return QByteArrayLiteral ("OK."); case Ok: return QByteArrayLiteral ("OK.");
case BadRequest: return QByteArrayLiteral ("Bad request !"); case BadRequest: return QByteArrayLiteral ("Bad request !");
case Forbidden: return QByteArrayLiteral ("Forbidden !"); case Forbidden: return QByteArrayLiteral ("Forbidden !");
@ -51,25 +29,12 @@ const QByteArray QtHttpReply::getStatusTextForCode (QtHttpReply::StatusCode stat
} }
} }
void QtHttpReply::setUseChunked (bool chunked){ void QtHttpReply::addHeader (const QByteArray & header, const QByteArray & value)
m_useChunked = chunked; {
}
void QtHttpReply::setStatusCode (QtHttpReply::StatusCode statusCode) {
m_statusCode = statusCode;
}
void QtHttpReply::appendRawData (const QByteArray & data) {
m_data.append (data);
}
void QtHttpReply::addHeader (const QByteArray & header, const QByteArray & value) {
QByteArray key = header.trimmed (); QByteArray key = header.trimmed ();
if (!key.isEmpty ()) {
if (!key.isEmpty ())
{
m_headersHash.insert (key, value); m_headersHash.insert (key, value);
} }
} }
void QtHttpReply::resetRawData (void) {
m_data.clear ();
}

View File

@ -8,14 +8,16 @@
class QtHttpServer; class QtHttpServer;
class QtHttpReply : public QObject { class QtHttpReply : public QObject
{
Q_OBJECT Q_OBJECT
Q_ENUMS (StatusCode) Q_ENUMS (StatusCode)
public: public:
explicit QtHttpReply (QtHttpServer * parent); explicit QtHttpReply (QtHttpServer * parent);
enum StatusCode { enum StatusCode
{
Ok = 200, Ok = 200,
SeeOther = 303, SeeOther = 303,
BadRequest = 400, BadRequest = 400,
@ -28,22 +30,25 @@ public:
ServiceUnavailable = 503, ServiceUnavailable = 503,
}; };
int getRawDataSize (void) const; int getRawDataSize (void) const { return m_data.size(); };
bool useChunked (void) const; bool useChunked (void) const { return m_useChunked; };
StatusCode getStatusCode (void) const; StatusCode getStatusCode (void) const { return m_statusCode; };
QByteArray getRawData (void) const; QByteArray getRawData (void) const { return m_data; };
QList<QByteArray> getHeadersList (void) const; QList<QByteArray> getHeadersList (void) const { return m_headersHash.keys (); };
QByteArray getHeader (const QByteArray & header) const; QByteArray getHeader (const QByteArray & header) const
{
return m_headersHash.value (header, QByteArray ());
};
static const QByteArray getStatusTextForCode (StatusCode statusCode); static const QByteArray getStatusTextForCode (StatusCode statusCode);
public slots: public slots:
void setUseChunked (bool chunked = false); void setUseChunked (bool chunked = false) { m_useChunked = chunked; };
void setStatusCode (StatusCode statusCode); void setStatusCode (StatusCode statusCode) { m_statusCode = statusCode; };
void appendRawData (const QByteArray & data); void appendRawData (const QByteArray & data) { m_data.append(data); };
void addHeader (const QByteArray & header, const QByteArray & value); void addHeader (const QByteArray & header, const QByteArray & value);
void resetRawData (void); void resetRawData (void) { m_data.clear (); };
signals: signals:
void requestSendHeaders (void); void requestSendHeaders (void);

View File

@ -17,67 +17,18 @@ QtHttpRequest::QtHttpRequest (QtHttpClientWrapper * client, QtHttpServer * paren
addHeader (QtHttpHeader::Connection, QByteArrayLiteral ("Keep-Alive")); addHeader (QtHttpHeader::Connection, QByteArrayLiteral ("Keep-Alive"));
} }
QUrl QtHttpRequest::getUrl (void) const { void QtHttpRequest::setClientInfo (const QHostAddress & server, const QHostAddress & client)
return m_url; {
}
QString QtHttpRequest::getCommand (void) const {
return m_command;
}
QtHttpRequest::ClientInfo QtHttpRequest::getClientInfo (void) const {
return m_clientInfo;
}
int QtHttpRequest::getRawDataSize (void) const {
return m_data.size ();
}
QByteArray QtHttpRequest::getRawData (void) const {
return m_data;
}
QtHttpPostData QtHttpRequest::getPostData (void) const {
return m_postData;
}
QList<QByteArray> QtHttpRequest::getHeadersList (void) const {
return m_headersHash.keys ();
}
QtHttpClientWrapper * QtHttpRequest::getClient (void) const {
return m_clientHandle;
}
QByteArray QtHttpRequest::getHeader (const QByteArray & header) const {
return m_headersHash.value (header, QByteArray ());
}
void QtHttpRequest::setUrl (const QUrl & url) {
m_url = url;
}
void QtHttpRequest::setCommand (const QString & command) {
m_command = command;
}
void QtHttpRequest::setClientInfo (const QHostAddress & server, const QHostAddress & client) {
m_clientInfo.serverAddress = server; m_clientInfo.serverAddress = server;
m_clientInfo.clientAddress = client; m_clientInfo.clientAddress = client;
} }
void QtHttpRequest::addHeader (const QByteArray & header, const QByteArray & value) { void QtHttpRequest::addHeader (const QByteArray & header, const QByteArray & value)
{
QByteArray key = header.trimmed (); QByteArray key = header.trimmed ();
if (!key.isEmpty ()) {
if (!key.isEmpty ())
{
m_headersHash.insert (key, value); m_headersHash.insert (key, value);
} }
} }
void QtHttpRequest::appendRawData (const QByteArray & data) {
m_data.append (data);
}
void QtHttpRequest::setPostData (const QtHttpPostData & data) {
m_postData = data;
}

View File

@ -14,36 +14,41 @@ class QtHttpClientWrapper;
using QtHttpPostData = QMap<QString,QByteArray>; using QtHttpPostData = QMap<QString,QByteArray>;
class QtHttpRequest : public QObject { class QtHttpRequest : public QObject
{
Q_OBJECT Q_OBJECT
public: public:
explicit QtHttpRequest (QtHttpClientWrapper * client, QtHttpServer * parent); explicit QtHttpRequest (QtHttpClientWrapper * client, QtHttpServer * parent);
struct ClientInfo { struct ClientInfo
{
QHostAddress serverAddress; QHostAddress serverAddress;
QHostAddress clientAddress; QHostAddress clientAddress;
}; };
int getRawDataSize (void) const; int getRawDataSize (void) const { return m_data.size (); };
QUrl getUrl (void) const; QUrl getUrl (void) const { return m_url; };
QString getCommand (void) const; QString getCommand (void) const { return m_command; };
QByteArray getRawData (void) const; QByteArray getRawData (void) const { return m_data; };
QList<QByteArray> getHeadersList (void) const; QList<QByteArray> getHeadersList (void) const { return m_headersHash.keys (); };
QtHttpClientWrapper * getClient (void) const; QtHttpClientWrapper * getClient (void) const { return m_clientHandle; };
QtHttpPostData getPostData (void) const { return m_postData; };
ClientInfo getClientInfo (void) const { return m_clientInfo; };
QByteArray getHeader (const QByteArray & header) const; QByteArray getHeader (const QByteArray & header) const
QtHttpPostData getPostData (void) const; {
return m_headersHash.value (header, QByteArray ());
ClientInfo getClientInfo (void) const; };
public slots: public slots:
void setUrl (const QUrl & url); void setUrl (const QUrl & url) { m_url = url; };
void setCommand (const QString & command); void setCommand (const QString & command) { m_command = command; };
void appendRawData (const QByteArray & data) { m_data.append (data); };
void setPostData (const QtHttpPostData & data) { m_postData = data; };
void setClientInfo (const QHostAddress & server, const QHostAddress & client); void setClientInfo (const QHostAddress & server, const QHostAddress & client);
void addHeader (const QByteArray & header, const QByteArray & value); void addHeader (const QByteArray & header, const QByteArray & value);
void appendRawData (const QByteArray & data);
void setPostData (const QtHttpPostData & data);
private: private:
QUrl m_url; QUrl m_url;

View File

@ -13,24 +13,24 @@ const QString & QtHttpServer::HTTP_VERSION = QStringLiteral ("HTTP/1.1");
QtHttpServerWrapper::QtHttpServerWrapper (QObject * parent) QtHttpServerWrapper::QtHttpServerWrapper (QObject * parent)
: QTcpServer (parent) : QTcpServer (parent)
, m_useSsl (false) , m_useSsl (false)
{ } {
QtHttpServerWrapper::~QtHttpServerWrapper (void) { } }
void QtHttpServerWrapper::setUseSecure (const bool ssl) { QtHttpServerWrapper::~QtHttpServerWrapper (void)
{
}
void QtHttpServerWrapper::setUseSecure (const bool ssl)
{
m_useSsl = ssl; m_useSsl = ssl;
} }
void QtHttpServerWrapper::incomingConnection (qintptr handle) { void QtHttpServerWrapper::incomingConnection (qintptr handle)
QTcpSocket * sock = (m_useSsl {
? new QSslSocket (this) QTcpSocket * sock = (m_useSsl ? new QSslSocket (this) : new QTcpSocket (this));
: new QTcpSocket (this)); sock->setSocketDescriptor(handle) ? addPendingConnection (sock) : delete sock;
if (sock->setSocketDescriptor (handle)) {
addPendingConnection (sock);
}
else {
delete sock;
}
} }
QtHttpServer::QtHttpServer (QObject * parent) QtHttpServer::QtHttpServer (QObject * parent)
@ -43,32 +43,20 @@ QtHttpServer::QtHttpServer (QObject * parent)
connect (m_sockServer, &QtHttpServerWrapper::newConnection, this, &QtHttpServer::onClientConnected); connect (m_sockServer, &QtHttpServerWrapper::newConnection, this, &QtHttpServer::onClientConnected);
} }
const QString & QtHttpServer::getServerName (void) const { void QtHttpServer::start (quint16 port)
return m_serverName; {
}
quint16 QtHttpServer::getServerPort (void) const {
return m_sockServer->serverPort ();
}
QString QtHttpServer::getErrorString (void) const {
return m_sockServer->errorString ();
}
void QtHttpServer::start (quint16 port) {
if(!m_sockServer->isListening()) if(!m_sockServer->isListening())
{ {
if (m_sockServer->listen (QHostAddress::Any, port)) { m_sockServer->listen (QHostAddress::Any, port)
emit started (m_sockServer->serverPort ()); ? emit started (m_sockServer->serverPort ())
} : emit error (m_sockServer->errorString ());
else {
emit error (m_sockServer->errorString ());
}
} }
} }
void QtHttpServer::stop (void) { void QtHttpServer::stop (void)
if (m_sockServer->isListening ()) { {
if (m_sockServer->isListening ())
{
m_sockServer->close (); m_sockServer->close ();
// disconnect clients // disconnect clients
const QList<QTcpSocket*> socks = m_socksClientsHash.keys(); const QList<QTcpSocket*> socks = m_socksClientsHash.keys();
@ -76,35 +64,31 @@ void QtHttpServer::stop (void) {
{ {
sock->close(); sock->close();
} }
emit stopped (); emit stopped ();
} }
} }
void QtHttpServer::setServerName (const QString & serverName) { void QtHttpServer::setUseSecure (const bool ssl)
m_serverName = serverName; {
}
void QtHttpServer::setUseSecure (const bool ssl) {
m_useSsl = ssl; m_useSsl = ssl;
m_sockServer->setUseSecure (m_useSsl); m_sockServer->setUseSecure (m_useSsl);
} }
void QtHttpServer::setPrivateKey (const QSslKey & key) { void QtHttpServer::onClientConnected (void)
m_sslKey = key; {
} while (m_sockServer->hasPendingConnections ())
{
void QtHttpServer::setCertificates (const QList<QSslCertificate> & certs) { if (QTcpSocket * sock = m_sockServer->nextPendingConnection ())
m_sslCerts = certs; {
}
void QtHttpServer::onClientConnected (void) {
while (m_sockServer->hasPendingConnections ()) {
if (QTcpSocket * sock = m_sockServer->nextPendingConnection ()) {
if(m_netOrigin->accessAllowed(sock->peerAddress(), sock->localAddress())) if(m_netOrigin->accessAllowed(sock->peerAddress(), sock->localAddress()))
{ {
connect (sock, &QTcpSocket::disconnected, this, &QtHttpServer::onClientDisconnected); connect (sock, &QTcpSocket::disconnected, this, &QtHttpServer::onClientDisconnected);
if (m_useSsl) {
if (QSslSocket * ssl = qobject_cast<QSslSocket *> (sock)) { if (m_useSsl)
{
if (QSslSocket * ssl = qobject_cast<QSslSocket *> (sock))
{
connect (ssl, SslErrorSignal (&QSslSocket::sslErrors), this, &QtHttpServer::onClientSslErrors); connect (ssl, SslErrorSignal (&QSslSocket::sslErrors), this, &QtHttpServer::onClientSslErrors);
connect (ssl, &QSslSocket::encrypted, this, &QtHttpServer::onClientSslEncrypted); connect (ssl, &QSslSocket::encrypted, this, &QtHttpServer::onClientSslEncrypted);
connect (ssl, &QSslSocket::peerVerifyError, this, &QtHttpServer::onClientSslPeerVerifyError); connect (ssl, &QSslSocket::peerVerifyError, this, &QtHttpServer::onClientSslPeerVerifyError);
@ -115,6 +99,7 @@ void QtHttpServer::onClientConnected (void) {
ssl->startServerEncryption (); ssl->startServerEncryption ();
} }
} }
QtHttpClientWrapper * wrapper = new QtHttpClientWrapper (sock, m_netOrigin->isLocalAddress(sock->peerAddress(), sock->localAddress()), this); QtHttpClientWrapper * wrapper = new QtHttpClientWrapper (sock, m_netOrigin->isLocalAddress(sock->peerAddress(), sock->localAddress()), this);
m_socksClientsHash.insert (sock, wrapper); m_socksClientsHash.insert (sock, wrapper);
emit clientConnected (wrapper->getGuid ()); emit clientConnected (wrapper->getGuid ());
@ -127,23 +112,12 @@ void QtHttpServer::onClientConnected (void) {
} }
} }
void QtHttpServer::onClientSslEncrypted (void) { } void QtHttpServer::onClientDisconnected (void)
{
void QtHttpServer::onClientSslPeerVerifyError (const QSslError & err) { if (QTcpSocket * sockClient = qobject_cast<QTcpSocket *> (sender ()))
Q_UNUSED (err) {
} if (QtHttpClientWrapper * wrapper = m_socksClientsHash.value (sockClient, Q_NULLPTR))
{
void QtHttpServer::onClientSslErrors (const QList<QSslError> & errors) {
Q_UNUSED (errors)
}
void QtHttpServer::onClientSslModeChanged (QSslSocket::SslMode mode) {
Q_UNUSED (mode)
}
void QtHttpServer::onClientDisconnected (void) {
if (QTcpSocket * sockClient = qobject_cast<QTcpSocket *> (sender ())) {
if (QtHttpClientWrapper * wrapper = m_socksClientsHash.value (sockClient, Q_NULLPTR)) {
emit clientDisconnected (wrapper->getGuid ()); emit clientDisconnected (wrapper->getGuid ());
wrapper->deleteLater (); wrapper->deleteLater ();
m_socksClientsHash.remove (sockClient); m_socksClientsHash.remove (sockClient);

View File

@ -13,13 +13,13 @@
class QTcpSocket; class QTcpSocket;
class QTcpServer; class QTcpServer;
class QtHttpRequest; class QtHttpRequest;
class QtHttpReply; class QtHttpReply;
class QtHttpClientWrapper; class QtHttpClientWrapper;
class NetOrigin; class NetOrigin;
class QtHttpServerWrapper : public QTcpServer { class QtHttpServerWrapper : public QTcpServer
{
Q_OBJECT Q_OBJECT
public: public:
@ -35,7 +35,8 @@ private:
bool m_useSsl; bool m_useSsl;
}; };
class QtHttpServer : public QObject { class QtHttpServer : public QObject
{
Q_OBJECT Q_OBJECT
public: public:
@ -45,19 +46,19 @@ public:
typedef void (QSslSocket::* SslErrorSignal) (const QList<QSslError> &); typedef void (QSslSocket::* SslErrorSignal) (const QList<QSslError> &);
const QString & getServerName (void) const; const QString & getServerName (void) const { return m_serverName; };
quint16 getServerPort (void) const; quint16 getServerPort (void) const { return m_sockServer->serverPort(); };
QString getErrorString (void) const; QString getErrorString (void) const { return m_sockServer->errorString(); };
bool isListening() { return m_sockServer->isListening(); }; bool isListening() { return m_sockServer->isListening(); };
public slots: public slots:
void start (quint16 port = 0); void start (quint16 port = 0);
void stop (void); void stop (void);
void setServerName (const QString & serverName);
void setUseSecure (const bool ssl = true); void setUseSecure (const bool ssl = true);
void setPrivateKey (const QSslKey & key); void setServerName (const QString & serverName) { m_serverName = serverName; };
void setCertificates (const QList<QSslCertificate> & certs); void setPrivateKey (const QSslKey & key) { m_sslKey = key; };
void setCertificates (const QList<QSslCertificate> & certs) { m_sslCerts = certs; };
signals: signals:
void started (quint16 port); void started (quint16 port);
@ -70,10 +71,10 @@ signals:
private slots: private slots:
void onClientConnected (void); void onClientConnected (void);
void onClientDisconnected (void); void onClientDisconnected (void);
void onClientSslEncrypted (void); void onClientSslEncrypted (void) { };
void onClientSslPeerVerifyError (const QSslError & err); void onClientSslPeerVerifyError (const QSslError & err) { Q_UNUSED (err) };
void onClientSslErrors (const QList<QSslError> & errors); void onClientSslErrors (const QList<QSslError> & errors) { Q_UNUSED (errors) };
void onClientSslModeChanged (QSslSocket::SslMode mode); void onClientSslModeChanged (QSslSocket::SslMode mode) { Q_UNUSED (mode) };
private: private:
bool m_useSsl; bool m_useSsl;

View File

@ -63,7 +63,8 @@ void WebServer::onServerStarted (quint16 port)
emit stateChange(true); emit stateChange(true);
} }
void WebServer::onServerStopped () { void WebServer::onServerStopped ()
{
Info(_log, "Stopped %s", _server->getServerName().toStdString().c_str()); Info(_log, "Stopped %s", _server->getServerName().toStdString().c_str());
emit stateChange(false); emit stateChange(false);
} }

View File

@ -14,7 +14,6 @@ WebSocketClient::WebSocketClient(QtHttpRequest* request, QTcpSocket* sock, const
: QObject(parent) : QObject(parent)
, _socket(sock) , _socket(sock)
, _log(Logger::getInstance("WEBSOCKET")) , _log(Logger::getInstance("WEBSOCKET"))
// , _hyperion(Hyperion::getInstance())
{ {
// connect socket; disconnect handled from QtHttpServer // connect socket; disconnect handled from QtHttpServer
connect(_socket, &QTcpSocket::readyRead , this, &WebSocketClient::handleWebSocketFrame); connect(_socket, &QTcpSocket::readyRead , this, &WebSocketClient::handleWebSocketFrame);
@ -45,6 +44,8 @@ WebSocketClient::WebSocketClient(QtHttpRequest* request, QTcpSocket* sock, const
void WebSocketClient::handleWebSocketFrame(void) void WebSocketClient::handleWebSocketFrame(void)
{ {
while (_socket->bytesAvailable())
{
// we are on no continious reading from socket from call before // we are on no continious reading from socket from call before
if (!_notEnoughData) if (!_notEnoughData)
{ {
@ -70,6 +71,7 @@ void WebSocketClient::handleWebSocketFrame(void)
// check the type of data frame // check the type of data frame
bool isContinuation=false; bool isContinuation=false;
switch (_wsh.opCode) switch (_wsh.opCode)
{ {
case OPCODE::CONTINUATION: case OPCODE::CONTINUATION:
@ -79,7 +81,7 @@ void WebSocketClient::handleWebSocketFrame(void)
case OPCODE::BINARY: case OPCODE::BINARY:
case OPCODE::TEXT: case OPCODE::TEXT:
{ {
// check for protocal violations // check for protocol violations
if (_onContinuation && !isContinuation) if (_onContinuation && !isContinuation)
{ {
sendClose(CLOSECODE::VIOLATION, "protocol violation, somebody sends frames in between continued frames"); sendClose(CLOSECODE::VIOLATION, "protocol violation, somebody sends frames in between continued frames");
@ -113,6 +115,7 @@ void WebSocketClient::handleWebSocketFrame(void)
_onContinuation = false; _onContinuation = false;
if (_wsh.opCode == OPCODE::TEXT) if (_wsh.opCode == OPCODE::TEXT)
{ {
_jsonAPI->handleMessage(QString(_wsReceiveBuffer)); _jsonAPI->handleMessage(QString(_wsReceiveBuffer));
} }
else else
@ -120,6 +123,7 @@ void WebSocketClient::handleWebSocketFrame(void)
handleBinaryMessage(_wsReceiveBuffer); handleBinaryMessage(_wsReceiveBuffer);
} }
_wsReceiveBuffer.clear(); _wsReceiveBuffer.clear();
} }
} }
break; break;
@ -147,6 +151,7 @@ void WebSocketClient::handleWebSocketFrame(void)
default: default:
Warning(_log, "strange %d\n%s\n", _wsh.opCode, QSTRING_CSTR(QString(buf))); Warning(_log, "strange %d\n%s\n", _wsh.opCode, QSTRING_CSTR(QString(buf)));
} }
}
} }
void WebSocketClient::getWsFrameHeader(WebSocketHeader* header) void WebSocketClient::getWsFrameHeader(WebSocketHeader* header)
@ -221,6 +226,7 @@ void WebSocketClient::sendClose(int status, QString reason)
_socket->close(); _socket->close();
} }
void WebSocketClient::handleBinaryMessage(QByteArray &data) void WebSocketClient::handleBinaryMessage(QByteArray &data)
{ {
//uint8_t priority = data.at(0); //uint8_t priority = data.at(0);
@ -243,6 +249,7 @@ void WebSocketClient::handleBinaryMessage(QByteArray &data)
//_hyperion->setInputImage(priority, image, duration_s*1000); //_hyperion->setInputImage(priority, image, duration_s*1000);
} }
qint64 WebSocketClient::sendMessage(QJsonObject obj) qint64 WebSocketClient::sendMessage(QJsonObject obj)
{ {
QJsonDocument writer(obj); QJsonDocument writer(obj);

View File

@ -4,8 +4,10 @@
/** /**
* WebSocket Opcodes are 4 bits. See RFC6455 section 5.2. * WebSocket Opcodes are 4 bits. See RFC6455 section 5.2.
*/ */
namespace OPCODE { namespace OPCODE
enum value { {
enum value
{
CONTINUATION = 0x0, CONTINUATION = 0x0,
TEXT = 0x1, TEXT = 0x1,
BINARY = 0x2, BINARY = 0x2,
@ -29,7 +31,8 @@ namespace OPCODE {
* @param v The opcode to test. * @param v The opcode to test.
* @return Whether or not the opcode is reserved. * @return Whether or not the opcode is reserved.
*/ */
inline bool reserved(value v) { inline bool reserved(value v)
{
return (v >= RSV3 && v <= RSV7) || (v >= CONTROL_RSVB && v <= CONTROL_RSVF); return (v >= RSV3 && v <= RSV7) || (v >= CONTROL_RSVB && v <= CONTROL_RSVF);
} }
@ -40,7 +43,8 @@ namespace OPCODE {
* @param v The opcode to test. * @param v The opcode to test.
* @return Whether or not the opcode is invalid. * @return Whether or not the opcode is invalid.
*/ */
inline bool invalid(value v) { inline bool invalid(value v)
{
return (v > 0xF || v < 0); return (v > 0xF || v < 0);
} }
@ -49,13 +53,16 @@ namespace OPCODE {
* @param v The opcode to test. * @param v The opcode to test.
* @return Whether or not the opcode is a control opcode. * @return Whether or not the opcode is a control opcode.
*/ */
inline bool is_control(value v) { inline bool is_control(value v)
{
return v >= 0x8; return v >= 0x8;
} }
} }
namespace CLOSECODE { namespace CLOSECODE
enum value { {
enum value
{
NORMAL = 1000, NORMAL = 1000,
AWAY = 1001, AWAY = 1001,
TERM = 1002, TERM = 1002,