mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2023-10-10 13:36:59 +02:00
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:
parent
370e1b5f45
commit
96d79cdef6
@ -298,7 +298,7 @@ $(document).ready(function() {
|
||||
|
||||
createCP('cp2', cpcolor, function(rgbT,hex){
|
||||
rgb = rgbT;
|
||||
sendColor()
|
||||
sendColor();
|
||||
setStorage('rmcpcolor', hex);
|
||||
});
|
||||
|
||||
|
@ -180,8 +180,6 @@ void LinearColorSmoothing::componentStateChange(const hyperion::Components compo
|
||||
|
||||
void LinearColorSmoothing::setEnable(bool enable)
|
||||
{
|
||||
LedDevice::setEnable(enable);
|
||||
|
||||
if (!enable)
|
||||
{
|
||||
_timer->stop();
|
||||
|
@ -30,8 +30,10 @@ QtHttpClientWrapper::QtHttpClientWrapper (QTcpSocket * sock, const bool& localCo
|
||||
connect (m_sockClient, &QTcpSocket::readyRead, this, &QtHttpClientWrapper::onClientDataReceived);
|
||||
}
|
||||
|
||||
QString QtHttpClientWrapper::getGuid (void) {
|
||||
if (m_guid.isEmpty ()) {
|
||||
QString QtHttpClientWrapper::getGuid (void)
|
||||
{
|
||||
if (m_guid.isEmpty ())
|
||||
{
|
||||
m_guid = QString::fromLocal8Bit (
|
||||
QCryptographicHash::hash (
|
||||
QByteArray::number ((quint64) (this)),
|
||||
@ -39,81 +41,117 @@ QString QtHttpClientWrapper::getGuid (void) {
|
||||
).toHex ()
|
||||
);
|
||||
}
|
||||
|
||||
return m_guid;
|
||||
}
|
||||
|
||||
void QtHttpClientWrapper::onClientDataReceived (void) {
|
||||
if (m_sockClient != Q_NULLPTR) {
|
||||
while (m_sockClient->bytesAvailable ()) {
|
||||
void QtHttpClientWrapper::onClientDataReceived (void)
|
||||
{
|
||||
if (m_sockClient != Q_NULLPTR)
|
||||
{
|
||||
while (m_sockClient->bytesAvailable ())
|
||||
{
|
||||
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 ();
|
||||
QStringList parts = str.split (SPACE, QString::SkipEmptyParts);
|
||||
if (parts.size () == 3) {
|
||||
|
||||
if (parts.size () == 3)
|
||||
{
|
||||
QString command = parts.at (0);
|
||||
QString url = parts.at (1);
|
||||
QString version = parts.at (2);
|
||||
if (version == QtHttpServer::HTTP_VERSION) {
|
||||
|
||||
if (version == QtHttpServer::HTTP_VERSION)
|
||||
{
|
||||
m_currentRequest = new QtHttpRequest (this, m_serverHandle);
|
||||
m_currentRequest->setClientInfo(m_sockClient->localAddress(), m_sockClient->peerAddress());
|
||||
m_currentRequest->setUrl (QUrl (url));
|
||||
m_currentRequest->setCommand (command);
|
||||
m_parsingStatus = AwaitingHeaders;
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
m_parsingStatus = ParsingError;
|
||||
//qWarning () << "Error : unhandled HTTP version :" << version;
|
||||
}
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
m_parsingStatus = ParsingError;
|
||||
//qWarning () << "Error : incorrect HTTP command line :" << line;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case AwaitingHeaders: { // "header: value" × N (until empty line)
|
||||
case AwaitingHeaders: // "header: value" × N (until empty line)
|
||||
{
|
||||
QByteArray raw = line.trimmed ();
|
||||
if (!raw.isEmpty ()) { // parse headers
|
||||
|
||||
if (!raw.isEmpty ()) // parse headers
|
||||
{
|
||||
int pos = raw.indexOf (COLON);
|
||||
if (pos > 0) {
|
||||
|
||||
if (pos > 0)
|
||||
{
|
||||
QByteArray header = raw.left (pos).trimmed ();
|
||||
QByteArray value = raw.mid (pos +1).trimmed ();
|
||||
m_currentRequest->addHeader (header, value);
|
||||
if (header == QtHttpHeader::ContentLength) {
|
||||
if (header == QtHttpHeader::ContentLength)
|
||||
{
|
||||
bool ok = false;
|
||||
const int len = value.toInt (&ok, 10);
|
||||
if (ok) {
|
||||
if (ok)
|
||||
{
|
||||
m_currentRequest->addHeader (QtHttpHeader::ContentLength, QByteArray::number (len));
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
m_parsingStatus = ParsingError;
|
||||
qWarning () << "Error : incorrect HTTP headers line :" << line;
|
||||
}
|
||||
}
|
||||
else { // end of headers
|
||||
if (m_currentRequest->getHeader (QtHttpHeader::ContentLength).toInt () > 0) {
|
||||
else // end of headers
|
||||
{
|
||||
if (m_currentRequest->getHeader (QtHttpHeader::ContentLength).toInt () > 0)
|
||||
{
|
||||
m_parsingStatus = AwaitingContent;
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
m_parsingStatus = RequestParsed;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case AwaitingContent: { // raw data × N (until EOF ??)
|
||||
case AwaitingContent: // raw data × N (until EOF ??)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
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"
|
||||
if(m_currentRequest->getHeader(QtHttpHeader::Upgrade) == "websocket")
|
||||
{
|
||||
@ -121,69 +159,87 @@ void QtHttpClientWrapper::onClientDataReceived (void) {
|
||||
{
|
||||
// disconnect this slot from socket for further requests
|
||||
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);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
// add post data to request and catch /jsonrpc subroute url
|
||||
if ( m_currentRequest->getCommand() == "POST")
|
||||
{
|
||||
QtHttpPostData postData;
|
||||
QByteArray data = m_currentRequest->getRawData();
|
||||
QList<QByteArray> parts = data.split('&');
|
||||
|
||||
for (int i = 0; i < parts.size(); ++i)
|
||||
{
|
||||
QList<QByteArray> keyValue = parts.at(i).split('=');
|
||||
QByteArray value;
|
||||
|
||||
if (keyValue.size()>1)
|
||||
{
|
||||
value = QByteArray::fromPercentEncoding(keyValue.at(1));
|
||||
}
|
||||
|
||||
postData.insert(QString::fromUtf8(keyValue.at(0)),value);
|
||||
}
|
||||
|
||||
m_currentRequest->setPostData(postData);
|
||||
|
||||
// catch /jsonrpc in url, we need async callback, StaticFileServing is sync
|
||||
QString path = m_currentRequest->getUrl ().path ();
|
||||
QStringList uri_parts = path.split('/', QString::SkipEmptyParts);
|
||||
|
||||
if ( ! uri_parts.empty() && uri_parts.at(0) == "json-rpc" )
|
||||
{
|
||||
if(m_webJsonRpc == Q_NULLPTR)
|
||||
{
|
||||
m_webJsonRpc = new WebJsonRpc(m_currentRequest, m_serverHandle, m_localConnection, this);
|
||||
}
|
||||
|
||||
m_webJsonRpc->handleMessage(m_currentRequest);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
QtHttpReply reply (m_serverHandle);
|
||||
connect (&reply, &QtHttpReply::requestSendHeaders,
|
||||
this, &QtHttpClientWrapper::onReplySendHeadersRequested);
|
||||
connect (&reply, &QtHttpReply::requestSendData,
|
||||
this, &QtHttpClientWrapper::onReplySendDataRequested);
|
||||
connect (&reply, &QtHttpReply::requestSendHeaders, this, &QtHttpClientWrapper::onReplySendHeadersRequested);
|
||||
connect (&reply, &QtHttpReply::requestSendData, this, &QtHttpClientWrapper::onReplySendDataRequested);
|
||||
emit m_serverHandle->requestNeedsReply (m_currentRequest, &reply); // allow app to handle request
|
||||
m_parsingStatus = sendReplyToClient (&reply);
|
||||
|
||||
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
|
||||
QtHttpReply reply (m_serverHandle);
|
||||
reply.setStatusCode (QtHttpReply::BadRequest);
|
||||
reply.appendRawData (QByteArrayLiteral ("<h1>Bad Request (HTTP parsing error) !</h1>"));
|
||||
reply.appendRawData (CRLF);
|
||||
m_parsingStatus = sendReplyToClient (&reply);
|
||||
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
break;
|
||||
}
|
||||
default: { break; }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void QtHttpClientWrapper::onReplySendHeadersRequested (void) {
|
||||
void QtHttpClientWrapper::onReplySendHeadersRequested (void)
|
||||
{
|
||||
QtHttpReply * reply = qobject_cast<QtHttpReply *> (sender ());
|
||||
if (reply != Q_NULLPTR) {
|
||||
|
||||
if (reply != Q_NULLPTR)
|
||||
{
|
||||
QByteArray data;
|
||||
// HTTP Version + Status Code + Status Msg
|
||||
data.append (QtHttpServer::HTTP_VERSION);
|
||||
@ -192,22 +248,28 @@ void QtHttpClientWrapper::onReplySendHeadersRequested (void) {
|
||||
data.append (SPACE);
|
||||
data.append (QtHttpReply::getStatusTextForCode (reply->getStatusCode ()));
|
||||
data.append (CRLF);
|
||||
// Header name: header value
|
||||
if (reply->useChunked ()) {
|
||||
|
||||
if (reply->useChunked ()) // Header name: header value
|
||||
{
|
||||
static const QByteArray & CHUNKED = QByteArrayLiteral ("chunked");
|
||||
reply->addHeader (QtHttpHeader::TransferEncoding, CHUNKED);
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
reply->addHeader (QtHttpHeader::ContentLength, QByteArray::number (reply->getRawDataSize ()));
|
||||
}
|
||||
|
||||
const QList<QByteArray> & headersList = reply->getHeadersList ();
|
||||
foreach (const QByteArray & header, headersList) {
|
||||
|
||||
foreach (const QByteArray & header, headersList)
|
||||
{
|
||||
data.append (header);
|
||||
data.append (COLON);
|
||||
data.append (SPACE);
|
||||
data.append (reply->getHeader (header));
|
||||
data.append (CRLF);
|
||||
}
|
||||
|
||||
// empty line
|
||||
data.append (CRLF);
|
||||
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 ());
|
||||
if (reply != Q_NULLPTR) {
|
||||
if (reply != Q_NULLPTR)
|
||||
{
|
||||
// content raw data
|
||||
QByteArray data = reply->getRawData ();
|
||||
if (reply->useChunked ()) {
|
||||
|
||||
if (reply->useChunked ())
|
||||
{
|
||||
data.prepend (QByteArray::number (data.size (), 16) % CRLF);
|
||||
data.append (CRLF);
|
||||
reply->resetRawData ();
|
||||
}
|
||||
|
||||
// write to socket
|
||||
m_sockClient->write (data);
|
||||
m_sockClient->flush ();
|
||||
}
|
||||
}
|
||||
|
||||
void QtHttpClientWrapper::sendToClientWithReply(QtHttpReply * reply) {
|
||||
connect (reply, &QtHttpReply::requestSendHeaders,
|
||||
this, &QtHttpClientWrapper::onReplySendHeadersRequested);
|
||||
connect (reply, &QtHttpReply::requestSendData,
|
||||
this, &QtHttpClientWrapper::onReplySendDataRequested);
|
||||
void QtHttpClientWrapper::sendToClientWithReply(QtHttpReply * reply)
|
||||
{
|
||||
connect (reply, &QtHttpReply::requestSendHeaders, this, &QtHttpClientWrapper::onReplySendHeadersRequested);
|
||||
connect (reply, &QtHttpReply::requestSendData, this, &QtHttpClientWrapper::onReplySendDataRequested);
|
||||
m_parsingStatus = sendReplyToClient (reply);
|
||||
}
|
||||
|
||||
QtHttpClientWrapper::ParsingStatus QtHttpClientWrapper::sendReplyToClient (QtHttpReply * reply) {
|
||||
if (reply != Q_NULLPTR) {
|
||||
if (!reply->useChunked ()) {
|
||||
QtHttpClientWrapper::ParsingStatus QtHttpClientWrapper::sendReplyToClient (QtHttpReply * reply)
|
||||
{
|
||||
if (reply != Q_NULLPTR)
|
||||
{
|
||||
if (!reply->useChunked ())
|
||||
{
|
||||
//reply->appendRawData (CRLF);
|
||||
// send all headers and all data in one shot
|
||||
reply->requestSendHeaders ();
|
||||
reply->requestSendData ();
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
// last chunk
|
||||
m_sockClient->write ("0" % CRLF % CRLF);
|
||||
m_sockClient->flush ();
|
||||
}
|
||||
if (m_currentRequest != Q_NULLPTR) {
|
||||
|
||||
if (m_currentRequest != Q_NULLPTR)
|
||||
{
|
||||
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
|
||||
m_sockClient->close ();
|
||||
}
|
||||
|
||||
m_currentRequest->deleteLater ();
|
||||
m_currentRequest = Q_NULLPTR;
|
||||
}
|
||||
}
|
||||
|
||||
return AwaitingRequest;
|
||||
}
|
||||
|
@ -3,7 +3,8 @@
|
||||
|
||||
class QByteArray;
|
||||
|
||||
class QtHttpHeader {
|
||||
class QtHttpHeader
|
||||
{
|
||||
public:
|
||||
static const QByteArray & Server;
|
||||
static const QByteArray & Date;
|
||||
|
@ -17,32 +17,10 @@ QtHttpReply::QtHttpReply (QtHttpServer * parent)
|
||||
addHeader (QtHttpHeader::Server, m_serverHandle->getServerName ().toUtf8 ());
|
||||
}
|
||||
|
||||
int QtHttpReply::getRawDataSize (void) const {
|
||||
return m_data.size ();
|
||||
}
|
||||
|
||||
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) {
|
||||
const QByteArray QtHttpReply::getStatusTextForCode (QtHttpReply::StatusCode statusCode)
|
||||
{
|
||||
switch (statusCode)
|
||||
{
|
||||
case Ok: return QByteArrayLiteral ("OK.");
|
||||
case BadRequest: return QByteArrayLiteral ("Bad request !");
|
||||
case Forbidden: return QByteArrayLiteral ("Forbidden !");
|
||||
@ -51,25 +29,12 @@ const QByteArray QtHttpReply::getStatusTextForCode (QtHttpReply::StatusCode stat
|
||||
}
|
||||
}
|
||||
|
||||
void QtHttpReply::setUseChunked (bool chunked){
|
||||
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) {
|
||||
void QtHttpReply::addHeader (const QByteArray & header, const QByteArray & value)
|
||||
{
|
||||
QByteArray key = header.trimmed ();
|
||||
if (!key.isEmpty ()) {
|
||||
|
||||
if (!key.isEmpty ())
|
||||
{
|
||||
m_headersHash.insert (key, value);
|
||||
}
|
||||
}
|
||||
|
||||
void QtHttpReply::resetRawData (void) {
|
||||
m_data.clear ();
|
||||
}
|
||||
|
@ -8,14 +8,16 @@
|
||||
|
||||
class QtHttpServer;
|
||||
|
||||
class QtHttpReply : public QObject {
|
||||
class QtHttpReply : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_ENUMS (StatusCode)
|
||||
|
||||
public:
|
||||
explicit QtHttpReply (QtHttpServer * parent);
|
||||
|
||||
enum StatusCode {
|
||||
enum StatusCode
|
||||
{
|
||||
Ok = 200,
|
||||
SeeOther = 303,
|
||||
BadRequest = 400,
|
||||
@ -28,22 +30,25 @@ public:
|
||||
ServiceUnavailable = 503,
|
||||
};
|
||||
|
||||
int getRawDataSize (void) const;
|
||||
bool useChunked (void) const;
|
||||
StatusCode getStatusCode (void) const;
|
||||
QByteArray getRawData (void) const;
|
||||
QList<QByteArray> getHeadersList (void) const;
|
||||
int getRawDataSize (void) const { return m_data.size(); };
|
||||
bool useChunked (void) const { return m_useChunked; };
|
||||
StatusCode getStatusCode (void) const { return m_statusCode; };
|
||||
QByteArray getRawData (void) const { return m_data; };
|
||||
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);
|
||||
|
||||
public slots:
|
||||
void setUseChunked (bool chunked = false);
|
||||
void setStatusCode (StatusCode statusCode);
|
||||
void appendRawData (const QByteArray & data);
|
||||
void setUseChunked (bool chunked = false) { m_useChunked = chunked; };
|
||||
void setStatusCode (StatusCode statusCode) { m_statusCode = statusCode; };
|
||||
void appendRawData (const QByteArray & data) { m_data.append(data); };
|
||||
void addHeader (const QByteArray & header, const QByteArray & value);
|
||||
void resetRawData (void);
|
||||
void resetRawData (void) { m_data.clear (); };
|
||||
|
||||
signals:
|
||||
void requestSendHeaders (void);
|
||||
|
@ -17,67 +17,18 @@ QtHttpRequest::QtHttpRequest (QtHttpClientWrapper * client, QtHttpServer * paren
|
||||
addHeader (QtHttpHeader::Connection, QByteArrayLiteral ("Keep-Alive"));
|
||||
}
|
||||
|
||||
QUrl QtHttpRequest::getUrl (void) const {
|
||||
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) {
|
||||
void QtHttpRequest::setClientInfo (const QHostAddress & server, const QHostAddress & client)
|
||||
{
|
||||
m_clientInfo.serverAddress = server;
|
||||
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 ();
|
||||
if (!key.isEmpty ()) {
|
||||
|
||||
if (!key.isEmpty ())
|
||||
{
|
||||
m_headersHash.insert (key, value);
|
||||
}
|
||||
}
|
||||
|
||||
void QtHttpRequest::appendRawData (const QByteArray & data) {
|
||||
m_data.append (data);
|
||||
}
|
||||
|
||||
void QtHttpRequest::setPostData (const QtHttpPostData & data) {
|
||||
m_postData = data;
|
||||
}
|
||||
|
@ -14,36 +14,41 @@ class QtHttpClientWrapper;
|
||||
|
||||
using QtHttpPostData = QMap<QString,QByteArray>;
|
||||
|
||||
class QtHttpRequest : public QObject {
|
||||
class QtHttpRequest : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit QtHttpRequest (QtHttpClientWrapper * client, QtHttpServer * parent);
|
||||
|
||||
struct ClientInfo {
|
||||
struct ClientInfo
|
||||
{
|
||||
QHostAddress serverAddress;
|
||||
QHostAddress clientAddress;
|
||||
};
|
||||
|
||||
int getRawDataSize (void) const;
|
||||
QUrl getUrl (void) const;
|
||||
QString getCommand (void) const;
|
||||
QByteArray getRawData (void) const;
|
||||
QList<QByteArray> getHeadersList (void) const;
|
||||
QtHttpClientWrapper * getClient (void) const;
|
||||
int getRawDataSize (void) const { return m_data.size (); };
|
||||
QUrl getUrl (void) const { return m_url; };
|
||||
QString getCommand (void) const { return m_command; };
|
||||
QByteArray getRawData (void) const { return m_data; };
|
||||
QList<QByteArray> getHeadersList (void) const { return m_headersHash.keys (); };
|
||||
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;
|
||||
QtHttpPostData getPostData (void) const;
|
||||
|
||||
ClientInfo getClientInfo (void) const;
|
||||
QByteArray getHeader (const QByteArray & header) const
|
||||
{
|
||||
return m_headersHash.value (header, QByteArray ());
|
||||
};
|
||||
|
||||
public slots:
|
||||
void setUrl (const QUrl & url);
|
||||
void setCommand (const QString & command);
|
||||
void setUrl (const QUrl & url) { m_url = url; };
|
||||
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 addHeader (const QByteArray & header, const QByteArray & value);
|
||||
void appendRawData (const QByteArray & data);
|
||||
void setPostData (const QtHttpPostData & data);
|
||||
|
||||
private:
|
||||
QUrl m_url;
|
||||
|
@ -13,24 +13,24 @@ const QString & QtHttpServer::HTTP_VERSION = QStringLiteral ("HTTP/1.1");
|
||||
QtHttpServerWrapper::QtHttpServerWrapper (QObject * parent)
|
||||
: QTcpServer (parent)
|
||||
, m_useSsl (false)
|
||||
{ }
|
||||
{
|
||||
|
||||
QtHttpServerWrapper::~QtHttpServerWrapper (void) { }
|
||||
}
|
||||
|
||||
void QtHttpServerWrapper::setUseSecure (const bool ssl) {
|
||||
QtHttpServerWrapper::~QtHttpServerWrapper (void)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void QtHttpServerWrapper::setUseSecure (const bool ssl)
|
||||
{
|
||||
m_useSsl = ssl;
|
||||
}
|
||||
|
||||
void QtHttpServerWrapper::incomingConnection (qintptr handle) {
|
||||
QTcpSocket * sock = (m_useSsl
|
||||
? new QSslSocket (this)
|
||||
: new QTcpSocket (this));
|
||||
if (sock->setSocketDescriptor (handle)) {
|
||||
addPendingConnection (sock);
|
||||
}
|
||||
else {
|
||||
delete sock;
|
||||
}
|
||||
void QtHttpServerWrapper::incomingConnection (qintptr handle)
|
||||
{
|
||||
QTcpSocket * sock = (m_useSsl ? new QSslSocket (this) : new QTcpSocket (this));
|
||||
sock->setSocketDescriptor(handle) ? addPendingConnection (sock) : delete sock;
|
||||
}
|
||||
|
||||
QtHttpServer::QtHttpServer (QObject * parent)
|
||||
@ -43,32 +43,20 @@ QtHttpServer::QtHttpServer (QObject * parent)
|
||||
connect (m_sockServer, &QtHttpServerWrapper::newConnection, this, &QtHttpServer::onClientConnected);
|
||||
}
|
||||
|
||||
const QString & QtHttpServer::getServerName (void) const {
|
||||
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) {
|
||||
void QtHttpServer::start (quint16 port)
|
||||
{
|
||||
if(!m_sockServer->isListening())
|
||||
{
|
||||
if (m_sockServer->listen (QHostAddress::Any, port)) {
|
||||
emit started (m_sockServer->serverPort ());
|
||||
}
|
||||
else {
|
||||
emit error (m_sockServer->errorString ());
|
||||
}
|
||||
m_sockServer->listen (QHostAddress::Any, port)
|
||||
? emit started (m_sockServer->serverPort ())
|
||||
: emit error (m_sockServer->errorString ());
|
||||
}
|
||||
}
|
||||
|
||||
void QtHttpServer::stop (void) {
|
||||
if (m_sockServer->isListening ()) {
|
||||
void QtHttpServer::stop (void)
|
||||
{
|
||||
if (m_sockServer->isListening ())
|
||||
{
|
||||
m_sockServer->close ();
|
||||
// disconnect clients
|
||||
const QList<QTcpSocket*> socks = m_socksClientsHash.keys();
|
||||
@ -76,35 +64,31 @@ void QtHttpServer::stop (void) {
|
||||
{
|
||||
sock->close();
|
||||
}
|
||||
|
||||
emit stopped ();
|
||||
}
|
||||
}
|
||||
|
||||
void QtHttpServer::setServerName (const QString & serverName) {
|
||||
m_serverName = serverName;
|
||||
}
|
||||
|
||||
void QtHttpServer::setUseSecure (const bool ssl) {
|
||||
void QtHttpServer::setUseSecure (const bool ssl)
|
||||
{
|
||||
m_useSsl = ssl;
|
||||
m_sockServer->setUseSecure (m_useSsl);
|
||||
}
|
||||
|
||||
void QtHttpServer::setPrivateKey (const QSslKey & key) {
|
||||
m_sslKey = key;
|
||||
}
|
||||
|
||||
void QtHttpServer::setCertificates (const QList<QSslCertificate> & certs) {
|
||||
m_sslCerts = certs;
|
||||
}
|
||||
|
||||
void QtHttpServer::onClientConnected (void) {
|
||||
while (m_sockServer->hasPendingConnections ()) {
|
||||
if (QTcpSocket * sock = m_sockServer->nextPendingConnection ()) {
|
||||
void QtHttpServer::onClientConnected (void)
|
||||
{
|
||||
while (m_sockServer->hasPendingConnections ())
|
||||
{
|
||||
if (QTcpSocket * sock = m_sockServer->nextPendingConnection ())
|
||||
{
|
||||
if(m_netOrigin->accessAllowed(sock->peerAddress(), sock->localAddress()))
|
||||
{
|
||||
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, &QSslSocket::encrypted, this, &QtHttpServer::onClientSslEncrypted);
|
||||
connect (ssl, &QSslSocket::peerVerifyError, this, &QtHttpServer::onClientSslPeerVerifyError);
|
||||
@ -115,6 +99,7 @@ void QtHttpServer::onClientConnected (void) {
|
||||
ssl->startServerEncryption ();
|
||||
}
|
||||
}
|
||||
|
||||
QtHttpClientWrapper * wrapper = new QtHttpClientWrapper (sock, m_netOrigin->isLocalAddress(sock->peerAddress(), sock->localAddress()), this);
|
||||
m_socksClientsHash.insert (sock, wrapper);
|
||||
emit clientConnected (wrapper->getGuid ());
|
||||
@ -127,23 +112,12 @@ void QtHttpServer::onClientConnected (void) {
|
||||
}
|
||||
}
|
||||
|
||||
void QtHttpServer::onClientSslEncrypted (void) { }
|
||||
|
||||
void QtHttpServer::onClientSslPeerVerifyError (const QSslError & err) {
|
||||
Q_UNUSED (err)
|
||||
}
|
||||
|
||||
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)) {
|
||||
void QtHttpServer::onClientDisconnected (void)
|
||||
{
|
||||
if (QTcpSocket * sockClient = qobject_cast<QTcpSocket *> (sender ()))
|
||||
{
|
||||
if (QtHttpClientWrapper * wrapper = m_socksClientsHash.value (sockClient, Q_NULLPTR))
|
||||
{
|
||||
emit clientDisconnected (wrapper->getGuid ());
|
||||
wrapper->deleteLater ();
|
||||
m_socksClientsHash.remove (sockClient);
|
||||
|
@ -13,13 +13,13 @@
|
||||
|
||||
class QTcpSocket;
|
||||
class QTcpServer;
|
||||
|
||||
class QtHttpRequest;
|
||||
class QtHttpReply;
|
||||
class QtHttpClientWrapper;
|
||||
class NetOrigin;
|
||||
|
||||
class QtHttpServerWrapper : public QTcpServer {
|
||||
class QtHttpServerWrapper : public QTcpServer
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
@ -35,7 +35,8 @@ private:
|
||||
bool m_useSsl;
|
||||
};
|
||||
|
||||
class QtHttpServer : public QObject {
|
||||
class QtHttpServer : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
@ -45,19 +46,19 @@ public:
|
||||
|
||||
typedef void (QSslSocket::* SslErrorSignal) (const QList<QSslError> &);
|
||||
|
||||
const QString & getServerName (void) const;
|
||||
const QString & getServerName (void) const { return m_serverName; };
|
||||
|
||||
quint16 getServerPort (void) const;
|
||||
QString getErrorString (void) const;
|
||||
quint16 getServerPort (void) const { return m_sockServer->serverPort(); };
|
||||
QString getErrorString (void) const { return m_sockServer->errorString(); };
|
||||
bool isListening() { return m_sockServer->isListening(); };
|
||||
|
||||
public slots:
|
||||
void start (quint16 port = 0);
|
||||
void stop (void);
|
||||
void setServerName (const QString & serverName);
|
||||
void setUseSecure (const bool ssl = true);
|
||||
void setPrivateKey (const QSslKey & key);
|
||||
void setCertificates (const QList<QSslCertificate> & certs);
|
||||
void setServerName (const QString & serverName) { m_serverName = serverName; };
|
||||
void setPrivateKey (const QSslKey & key) { m_sslKey = key; };
|
||||
void setCertificates (const QList<QSslCertificate> & certs) { m_sslCerts = certs; };
|
||||
|
||||
signals:
|
||||
void started (quint16 port);
|
||||
@ -70,10 +71,10 @@ signals:
|
||||
private slots:
|
||||
void onClientConnected (void);
|
||||
void onClientDisconnected (void);
|
||||
void onClientSslEncrypted (void);
|
||||
void onClientSslPeerVerifyError (const QSslError & err);
|
||||
void onClientSslErrors (const QList<QSslError> & errors);
|
||||
void onClientSslModeChanged (QSslSocket::SslMode mode);
|
||||
void onClientSslEncrypted (void) { };
|
||||
void onClientSslPeerVerifyError (const QSslError & err) { Q_UNUSED (err) };
|
||||
void onClientSslErrors (const QList<QSslError> & errors) { Q_UNUSED (errors) };
|
||||
void onClientSslModeChanged (QSslSocket::SslMode mode) { Q_UNUSED (mode) };
|
||||
|
||||
private:
|
||||
bool m_useSsl;
|
||||
|
@ -63,7 +63,8 @@ void WebServer::onServerStarted (quint16 port)
|
||||
emit stateChange(true);
|
||||
}
|
||||
|
||||
void WebServer::onServerStopped () {
|
||||
void WebServer::onServerStopped ()
|
||||
{
|
||||
Info(_log, "Stopped %s", _server->getServerName().toStdString().c_str());
|
||||
emit stateChange(false);
|
||||
}
|
||||
|
@ -14,7 +14,6 @@ WebSocketClient::WebSocketClient(QtHttpRequest* request, QTcpSocket* sock, const
|
||||
: QObject(parent)
|
||||
, _socket(sock)
|
||||
, _log(Logger::getInstance("WEBSOCKET"))
|
||||
// , _hyperion(Hyperion::getInstance())
|
||||
{
|
||||
// connect socket; disconnect handled from QtHttpServer
|
||||
connect(_socket, &QTcpSocket::readyRead , this, &WebSocketClient::handleWebSocketFrame);
|
||||
@ -44,6 +43,8 @@ WebSocketClient::WebSocketClient(QtHttpRequest* request, QTcpSocket* sock, const
|
||||
}
|
||||
|
||||
void WebSocketClient::handleWebSocketFrame(void)
|
||||
{
|
||||
while (_socket->bytesAvailable())
|
||||
{
|
||||
// we are on no continious reading from socket from call before
|
||||
if (!_notEnoughData)
|
||||
@ -70,6 +71,7 @@ void WebSocketClient::handleWebSocketFrame(void)
|
||||
|
||||
// check the type of data frame
|
||||
bool isContinuation=false;
|
||||
|
||||
switch (_wsh.opCode)
|
||||
{
|
||||
case OPCODE::CONTINUATION:
|
||||
@ -79,7 +81,7 @@ void WebSocketClient::handleWebSocketFrame(void)
|
||||
case OPCODE::BINARY:
|
||||
case OPCODE::TEXT:
|
||||
{
|
||||
// check for protocal violations
|
||||
// check for protocol violations
|
||||
if (_onContinuation && !isContinuation)
|
||||
{
|
||||
sendClose(CLOSECODE::VIOLATION, "protocol violation, somebody sends frames in between continued frames");
|
||||
@ -113,6 +115,7 @@ void WebSocketClient::handleWebSocketFrame(void)
|
||||
_onContinuation = false;
|
||||
if (_wsh.opCode == OPCODE::TEXT)
|
||||
{
|
||||
|
||||
_jsonAPI->handleMessage(QString(_wsReceiveBuffer));
|
||||
}
|
||||
else
|
||||
@ -120,6 +123,7 @@ void WebSocketClient::handleWebSocketFrame(void)
|
||||
handleBinaryMessage(_wsReceiveBuffer);
|
||||
}
|
||||
_wsReceiveBuffer.clear();
|
||||
|
||||
}
|
||||
}
|
||||
break;
|
||||
@ -148,6 +152,7 @@ void WebSocketClient::handleWebSocketFrame(void)
|
||||
Warning(_log, "strange %d\n%s\n", _wsh.opCode, QSTRING_CSTR(QString(buf)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void WebSocketClient::getWsFrameHeader(WebSocketHeader* header)
|
||||
{
|
||||
@ -221,6 +226,7 @@ void WebSocketClient::sendClose(int status, QString reason)
|
||||
_socket->close();
|
||||
}
|
||||
|
||||
|
||||
void WebSocketClient::handleBinaryMessage(QByteArray &data)
|
||||
{
|
||||
//uint8_t priority = data.at(0);
|
||||
@ -243,6 +249,7 @@ void WebSocketClient::handleBinaryMessage(QByteArray &data)
|
||||
//_hyperion->setInputImage(priority, image, duration_s*1000);
|
||||
}
|
||||
|
||||
|
||||
qint64 WebSocketClient::sendMessage(QJsonObject obj)
|
||||
{
|
||||
QJsonDocument writer(obj);
|
||||
|
@ -4,8 +4,10 @@
|
||||
/**
|
||||
* WebSocket Opcodes are 4 bits. See RFC6455 section 5.2.
|
||||
*/
|
||||
namespace OPCODE {
|
||||
enum value {
|
||||
namespace OPCODE
|
||||
{
|
||||
enum value
|
||||
{
|
||||
CONTINUATION = 0x0,
|
||||
TEXT = 0x1,
|
||||
BINARY = 0x2,
|
||||
@ -29,7 +31,8 @@ namespace OPCODE {
|
||||
* @param v The opcode to test.
|
||||
* @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);
|
||||
}
|
||||
|
||||
@ -40,7 +43,8 @@ namespace OPCODE {
|
||||
* @param v The opcode to test.
|
||||
* @return Whether or not the opcode is invalid.
|
||||
*/
|
||||
inline bool invalid(value v) {
|
||||
inline bool invalid(value v)
|
||||
{
|
||||
return (v > 0xF || v < 0);
|
||||
}
|
||||
|
||||
@ -49,13 +53,16 @@ namespace OPCODE {
|
||||
* @param v The opcode to test.
|
||||
* @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;
|
||||
}
|
||||
}
|
||||
|
||||
namespace CLOSECODE {
|
||||
enum value {
|
||||
namespace CLOSECODE
|
||||
{
|
||||
enum value
|
||||
{
|
||||
NORMAL = 1000,
|
||||
AWAY = 1001,
|
||||
TERM = 1002,
|
||||
|
Loading…
Reference in New Issue
Block a user