httpcommander: Add Qt6 support
parent
233b89d65e
commit
6091d24574
|
|
@ -1,8 +1,6 @@
|
|||
include(../plugins.pri)
|
||||
|
||||
QT += network
|
||||
|
||||
TARGET = $$qtLibraryTarget(nymea_integrationpluginhttpcommander)
|
||||
QT *= network
|
||||
|
||||
SOURCES += \
|
||||
integrationpluginhttpcommander.cpp \
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||
*
|
||||
* Copyright 2013 - 2020, nymea GmbH
|
||||
* Copyright 2013 - 2025, nymea GmbH
|
||||
* Contact: contact@nymea.io
|
||||
*
|
||||
* This file is part of nymea.
|
||||
|
|
@ -30,14 +30,13 @@
|
|||
|
||||
#include "httpsimpleserver.h"
|
||||
|
||||
#include "types/statetype.h"
|
||||
#include "extern-plugininfo.h"
|
||||
|
||||
#include <QTcpSocket>
|
||||
#include <QDebug>
|
||||
#include <QDateTime>
|
||||
#include <QUrlQuery>
|
||||
#include <QRegExp>
|
||||
#include <QRegularExpression>
|
||||
#include <QStringList>
|
||||
|
||||
HttpSimpleServer::HttpSimpleServer(quint16 port, QObject *parent):
|
||||
|
|
@ -58,8 +57,8 @@ void HttpSimpleServer::incomingConnection(qintptr socket)
|
|||
// works asynchronously, this means that all the communication is done
|
||||
// in the two slots readClient() and discardClient().
|
||||
QTcpSocket* tcpSocket = new QTcpSocket(this);
|
||||
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readClient()));
|
||||
connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(discardClient()));
|
||||
connect(tcpSocket, &QTcpSocket::readyRead, this, &HttpSimpleServer::readClient);
|
||||
connect(tcpSocket, &QTcpSocket::disconnected, this, &HttpSimpleServer::discardClient);
|
||||
tcpSocket->setSocketDescriptor(socket);
|
||||
|
||||
}
|
||||
|
|
@ -69,17 +68,16 @@ void HttpSimpleServer::readClient()
|
|||
// This slot is called when the client sent data to the server. The
|
||||
// server looks if it was a get request and sends a very simple HTML
|
||||
// document back.
|
||||
QTcpSocket* tcpSocket = static_cast<QTcpSocket*>(sender());
|
||||
QTcpSocket *tcpSocket = static_cast<QTcpSocket*>(sender());
|
||||
if (tcpSocket->canReadLine()) {
|
||||
|
||||
QByteArray data = tcpSocket->readAll();
|
||||
QStringList tokens = QString(data).split(QRegExp("[ \r\n][ \r\n]*"));
|
||||
QStringList tokens = QString(data).split(QRegularExpression("[ \r\n][ \r\n]*"));
|
||||
qCDebug(dcHttpCommander()) << "Http Request, type" << tokens[0] << "path" << tokens[1] << "body" << tokens.last();
|
||||
|
||||
if ((tokens[0] == "GET") ||
|
||||
(tokens[0] == "PUT") ||
|
||||
(tokens[0] == "POST") ||
|
||||
(tokens[0] == "DELETE")) {
|
||||
(tokens[0] == "PUT") ||
|
||||
(tokens[0] == "POST") ||
|
||||
(tokens[0] == "DELETE")) {
|
||||
|
||||
QTextStream os(tcpSocket);
|
||||
os.setAutoDetectUnicode(true);
|
||||
|
|
@ -96,16 +94,16 @@ void HttpSimpleServer::readClient()
|
|||
|
||||
void HttpSimpleServer::discardClient()
|
||||
{
|
||||
QTcpSocket* socket = static_cast<QTcpSocket*>(sender());
|
||||
QTcpSocket *socket = static_cast<QTcpSocket *>(sender());
|
||||
socket->deleteLater();
|
||||
}
|
||||
|
||||
QString HttpSimpleServer::generateHeader()
|
||||
{
|
||||
QString contentHeader(
|
||||
"HTTP/1.0 200 Ok\r\n"
|
||||
"Content-Type: text/html; charset=\"utf-8\"\r\n"
|
||||
"\r\n"
|
||||
);
|
||||
"HTTP/1.0 200 Ok\r\n"
|
||||
"Content-Type: text/html; charset=\"utf-8\"\r\n"
|
||||
"\r\n"
|
||||
);
|
||||
return contentHeader;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||
*
|
||||
* Copyright 2013 - 2020, nymea GmbH
|
||||
* Copyright 2013 - 2025, nymea GmbH
|
||||
* Contact: contact@nymea.io
|
||||
*
|
||||
* This file is part of nymea.
|
||||
|
|
@ -31,15 +31,12 @@
|
|||
#ifndef HTTPSIMPLESERVER1_H
|
||||
#define HTTPSIMPLESERVER1_H
|
||||
|
||||
#include "typeutils.h"
|
||||
|
||||
#include <QTcpServer>
|
||||
#include <QUuid>
|
||||
#include <QDateTime>
|
||||
#include <QUrl>
|
||||
|
||||
class Device;
|
||||
class DevicePlugin;
|
||||
#include <typeutils.h>
|
||||
|
||||
class HttpSimpleServer : public QTcpServer
|
||||
{
|
||||
|
|
@ -48,6 +45,7 @@ public:
|
|||
|
||||
HttpSimpleServer(quint16 port, QObject* parent = nullptr);
|
||||
~HttpSimpleServer() override;
|
||||
|
||||
void incomingConnection(qintptr socket) override;
|
||||
|
||||
signals:
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||
*
|
||||
* Copyright 2013 - 2020, nymea GmbH
|
||||
* Copyright 2013 - 2025, nymea GmbH
|
||||
* Contact: contact@nymea.io
|
||||
*
|
||||
* This file is part of nymea.
|
||||
|
|
@ -29,8 +29,12 @@
|
|||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
||||
|
||||
#include "integrationpluginhttpcommander.h"
|
||||
#include "network/networkaccessmanager.h"
|
||||
#include "plugininfo.h"
|
||||
|
||||
#include <network/networkaccessmanager.h>
|
||||
|
||||
#include <QHostInfo>
|
||||
#include <QNetworkReply>
|
||||
#include <QNetworkInterface>
|
||||
|
||||
IntegrationPluginHttpCommander::IntegrationPluginHttpCommander()
|
||||
|
|
@ -40,14 +44,14 @@ IntegrationPluginHttpCommander::IntegrationPluginHttpCommander()
|
|||
void IntegrationPluginHttpCommander::setupThing(ThingSetupInfo *info)
|
||||
{
|
||||
Thing *thing = info->thing();
|
||||
qDebug(dcHttpCommander()) << "Setup thing" << thing->name() << thing->params();
|
||||
qCDebug(dcHttpCommander()) << "Setup thing" << thing->name() << thing->params();
|
||||
|
||||
|
||||
if (thing->thingClassId() == httpRequestThingClassId) {
|
||||
QUrl url = thing->paramValue(httpRequestThingUrlParamTypeId).toUrl();
|
||||
|
||||
if (!url.isValid()) {
|
||||
qDebug(dcHttpCommander()) << "Given URL is not valid";
|
||||
qCDebug(dcHttpCommander()) << "Given URL is not valid";
|
||||
//: Error setting up thing
|
||||
return info->finish(Thing::ThingErrorInvalidParameter, QT_TR_NOOP("The given url is not valid."));
|
||||
}
|
||||
|
|
@ -92,9 +96,9 @@ void IntegrationPluginHttpCommander::executeAction(ThingActionInfo *info)
|
|||
info->finish(Thing::ThingErrorInvalidParameter);
|
||||
return;
|
||||
}
|
||||
connect(reply, &QNetworkReply::finished, this, [thing, reply, this](){
|
||||
|
||||
qDebug(dcHttpCommander()) << "POST reply finished";
|
||||
connect(reply, &QNetworkReply::finished, this, [thing, reply](){
|
||||
qCDebug(dcHttpCommander()) << "POST reply finished";
|
||||
QByteArray data = reply->readAll();
|
||||
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||
*
|
||||
* Copyright 2013 - 2020, nymea GmbH
|
||||
* Copyright 2013 - 2025, nymea GmbH
|
||||
* Contact: contact@nymea.io
|
||||
*
|
||||
* This file is part of nymea.
|
||||
|
|
@ -31,13 +31,9 @@
|
|||
#ifndef INTEGRATIONPLUGINHTTPCOMMANDER_H
|
||||
#define INTEGRATIONPLUGINHTTPCOMMANDER_H
|
||||
|
||||
#include "integrations/integrationplugin.h"
|
||||
#include "plugintimer.h"
|
||||
#include <integrations/integrationplugin.h>
|
||||
#include "httpsimpleserver.h"
|
||||
|
||||
#include <QNetworkReply>
|
||||
#include <QHostInfo>
|
||||
|
||||
class IntegrationPluginHttpCommander : public IntegrationPlugin
|
||||
{
|
||||
Q_OBJECT
|
||||
|
|
@ -46,7 +42,6 @@ class IntegrationPluginHttpCommander : public IntegrationPlugin
|
|||
Q_INTERFACES(IntegrationPlugin)
|
||||
|
||||
public:
|
||||
|
||||
explicit IntegrationPluginHttpCommander();
|
||||
|
||||
void setupThing(ThingSetupInfo *info) override;
|
||||
|
|
|
|||
Loading…
Reference in New Issue