123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645 |
- /*
- This file is part of JQLibrary
- Copyright: Jason
- Contact email: 188080501@qq.com
- GNU Lesser General Public License Usage
- Alternatively, this file may be used under the terms of the GNU Lesser
- General Public License version 2.1 or version 3 as published by the Free
- Software Foundation and appearing in the file LICENSE.LGPLv21 and
- LICENSE.LGPLv3 included in the packaging of this file. Please review the
- following information to ensure the GNU Lesser General Public License
- requirements will be met: https://www.gnu.org/licenses/lgpl.html and
- http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
- */
- #include "JQNet.h"
- // Qt lib import
- #include <QEventLoop>
- #include <QTimer>
- #include <QUrl>
- #include <QHostInfo>
- // JQLibrary lib import
- #ifdef JQFOUNDATION_LIB
- # include "JQFoundation.h"
- #endif
- QNetworkAddressEntry JQNet::getNetworkAddressEntry()
- {
- return getNetworkAddressEntryWithNetworkInterface().first;
- }
- QPair< QNetworkAddressEntry, QNetworkInterface > JQNet::getNetworkAddressEntryWithNetworkInterface(const bool &ridVm)
- {
- for ( const auto &interface: static_cast< const QList< QNetworkInterface > >( QNetworkInterface::allInterfaces() ) )
- {
- if ( interface.flags() != ( QNetworkInterface::IsUp |
- QNetworkInterface::IsRunning |
- QNetworkInterface::CanBroadcast |
- QNetworkInterface::CanMulticast ) ) { continue; }
- if ( ridVm && interface.humanReadableName().startsWith( "vm" ) ) { continue; }
- for ( const auto &entry: static_cast< QList<QNetworkAddressEntry> >( interface.addressEntries() ) )
- {
- if ( entry.ip().toIPv4Address() )
- {
- return { entry, interface };
- }
- }
- }
- return { };
- }
- QString JQNet::getHostName()
- {
- #if ( defined Q_OS_MAC )
- return QHostInfo::localHostName().replace( ".local", "" );
- #else
- return QHostInfo::localHostName();
- #endif
- }
- #ifdef JQFOUNDATION_LIB
- bool JQNet::pingIp(const QHostAddress &hostAddress)
- {
- // qDebug() << "NetworkManage::pingIp:" << hostAddress.toString();
- QPair< int, QByteArray > pingResult = { -1, { } };
- #ifdef Q_OS_MAC
- pingResult = JQFoundation::startProcessAndReadOutput( "ping", { "-c1", "-W300", hostAddress.toString() } );
- #endif
- #ifdef Q_OS_WIN
- pingResult = JQFoundation::startProcessAndReadOutput( "ping", { "-n", "1", "-w", "300", hostAddress.toString() } );
- #endif
- return ( pingResult.first == 0 ) && ( pingResult.second.size() > 20 ) && ( pingResult.second.count( hostAddress.toString().toUtf8() ) > 1 );
- }
- #endif
- // HTTP
- bool JQNet::HTTP::get(
- const QNetworkRequest &request,
- QByteArray &target, const int &timeout
- )
- {
- target.clear();
- QEventLoop eventLoop;
- auto reply = manage_.get( request );
- bool failFlag = false;
- this->handle(
- reply,
- timeout,
- [ & ](const QByteArray &data)
- {
- target = data;
- eventLoop.exit( 1 );
- },
- [ & ](const QNetworkReply::NetworkError &, const QByteArray &data)
- {
- target = data;
- eventLoop.exit( 0 );
- },
- [ & ]()
- {
- failFlag = true;
- eventLoop.exit( 0 );
- }
- );
- return eventLoop.exec() && !failFlag;
- }
- void JQNet::HTTP::get(
- const QNetworkRequest &request,
- const std::function<void (const QByteArray &)> &onFinished,
- const std::function<void (const QNetworkReply::NetworkError &, const QByteArray &)> &onError,
- const int &timeout
- )
- {
- auto reply = manage_.get( request );
- this->handle(
- reply,
- timeout,
- onFinished,
- onError,
- [ onError ]()
- {
- onError( QNetworkReply::TimeoutError, { } );
- }
- );
- }
- bool JQNet::HTTP::deleteResource(
- const QNetworkRequest &request,
- QByteArray &target,
- const int &timeout
- )
- {
- target.clear();
- QEventLoop eventLoop;
- auto reply = manage_.deleteResource( request );
- bool failFlag = false;
- this->handle(
- reply,
- timeout,
- [ & ](const QByteArray &data)
- {
- target = data;
- eventLoop.exit( 1 );
- },
- [ & ](const QNetworkReply::NetworkError &, const QByteArray &data)
- {
- target = data;
- eventLoop.exit( 0 );
- },
- [ & ]()
- {
- failFlag = true;
- eventLoop.exit( 0 );
- }
- );
- return eventLoop.exec() && !failFlag;
- }
- void JQNet::HTTP::deleteResource(
- const QNetworkRequest &request,
- const std::function<void (const QByteArray &)> &onFinished,
- const std::function<void (const QNetworkReply::NetworkError &, const QByteArray &)> &onError,
- const int &timeout
- )
- {
- auto reply = manage_.deleteResource( request );
- this->handle(
- reply,
- timeout,
- onFinished,
- onError,
- [ onError ]()
- {
- onError( QNetworkReply::TimeoutError, { } );
- }
- );
- }
- bool JQNet::HTTP::post(
- const QNetworkRequest &request,
- const QByteArray &body,
- QByteArray &target,
- const int &timeout
- )
- {
- target.clear();
- QEventLoop eventLoop;
- auto reply = manage_.post( request, body );
- bool failFlag = false;
- this->handle(
- reply,
- timeout,
- [ &target, &eventLoop ](const QByteArray &data)
- {
- target = data;
- eventLoop.exit( true );
- },
- [ &target, &eventLoop ](const QNetworkReply::NetworkError &, const QByteArray &data)
- {
- target = data;
- eventLoop.exit( false );
- },
- [ &failFlag, &eventLoop ]()
- {
- failFlag = true;
- eventLoop.exit( false );
- }
- );
- return eventLoop.exec() && !failFlag;
- }
- bool JQNet::HTTP::post(
- const QNetworkRequest &request,
- const QSharedPointer< QHttpMultiPart > &multiPart,
- QByteArray &target,
- const int &timeout
- )
- {
- target.clear();
- QEventLoop eventLoop;
- auto reply = manage_.post( request, multiPart.data() );
- bool failFlag = false;
- this->handle(
- reply,
- timeout,
- [ &target, &eventLoop ](const QByteArray &data)
- {
- target = data;
- eventLoop.exit( true );
- },
- [ &target, &eventLoop ](const QNetworkReply::NetworkError &, const QByteArray &data)
- {
- target = data;
- eventLoop.exit( false );
- },
- [ &failFlag, &eventLoop ]()
- {
- failFlag = true;
- eventLoop.exit( false );
- }
- );
- return eventLoop.exec() && !failFlag;
- }
- void JQNet::HTTP::post(
- const QNetworkRequest &request,
- const QByteArray &body,
- const std::function<void (const QByteArray &)> &onFinished,
- const std::function<void (const QNetworkReply::NetworkError &, const QByteArray &)> &onError,
- const int &timeout
- )
- {
- auto reply = manage_.post( request, body );
- this->handle(
- reply,
- timeout,
- onFinished,
- onError,
- [ onError ]()
- {
- onError( QNetworkReply::TimeoutError, { } );
- }
- );
- }
- bool JQNet::HTTP::put(
- const QNetworkRequest &request,
- const QByteArray &body,
- QByteArray &target,
- const int &timeout
- )
- {
- target.clear();
- QEventLoop eventLoop;
- auto reply = manage_.put( request, body );
- bool failFlag = false;
- this->handle(
- reply,
- timeout,
- [ &target, &eventLoop ](const QByteArray &data)
- {
- target = data;
- eventLoop.exit( true );
- },
- [ &target, &eventLoop ](const QNetworkReply::NetworkError &, const QByteArray &data)
- {
- target = data;
- eventLoop.exit( false );
- },
- [ &failFlag, &eventLoop ]()
- {
- failFlag = true;
- eventLoop.exit( false );
- }
- );
- return eventLoop.exec() && !failFlag;
- }
- bool JQNet::HTTP::put(
- const QNetworkRequest &request,
- const QSharedPointer< QHttpMultiPart > &multiPart,
- QByteArray &target,
- const int &timeout
- )
- {
- target.clear();
- QEventLoop eventLoop;
- auto reply = manage_.put( request, multiPart.data() );
- bool failFlag = false;
- this->handle(
- reply,
- timeout,
- [ &target, &eventLoop ](const QByteArray &data)
- {
- target = data;
- eventLoop.exit( true );
- },
- [ &target, &eventLoop ](const QNetworkReply::NetworkError &e, const QByteArray &data)
- {
- qDebug() << e;
- target = data;
- eventLoop.exit( false );
- },
- [ &failFlag, &eventLoop ]()
- {
- failFlag = true;
- eventLoop.exit( false );
- }
- );
- return eventLoop.exec() && !failFlag;
- }
- void JQNet::HTTP::put(
- const QNetworkRequest &request,
- const QByteArray &body,
- const std::function<void (const QByteArray &)> &onFinished,
- const std::function<void (const QNetworkReply::NetworkError &, const QByteArray &)> &onError,
- const int &timeout
- )
- {
- auto reply = manage_.put( request, body );
- this->handle(
- reply,
- timeout,
- onFinished,
- onError,
- [ onError ]()
- {
- onError( QNetworkReply::TimeoutError, { } );
- }
- );
- }
- #if !( defined Q_OS_LINUX ) && ( QT_VERSION >= QT_VERSION_CHECK( 5, 9, 0 ) )
- bool JQNet::HTTP::patch(
- const QNetworkRequest &request,
- const QByteArray &body,
- QByteArray &target,
- const int &timeout
- )
- {
- target.clear();
- QEventLoop eventLoop;
- auto reply = manage_.sendCustomRequest( request, "PATCH", body );
- bool failFlag = false;
- this->handle(
- reply,
- timeout,
- [ &target, &eventLoop ](const QByteArray &data)
- {
- target = data;
- eventLoop.exit( true );
- },
- [ &target, &eventLoop ](const QNetworkReply::NetworkError &, const QByteArray &data)
- {
- target = data;
- eventLoop.exit( false );
- },
- [ &failFlag, &eventLoop ]()
- {
- failFlag = true;
- eventLoop.exit( false );
- }
- );
- return eventLoop.exec() && !failFlag;
- }
- void JQNet::HTTP::patch(
- const QNetworkRequest &request,
- const QByteArray &body,
- const std::function<void (const QByteArray &)> &onFinished,
- const std::function<void (const QNetworkReply::NetworkError &, const QByteArray &)> &onError,
- const int &timeout
- )
- {
- auto reply = manage_.sendCustomRequest( request, "PATCH", body );
- this->handle(
- reply,
- timeout,
- onFinished,
- onError,
- [ onError ]()
- {
- onError( QNetworkReply::TimeoutError, { } );
- }
- );
- }
- #endif
- QPair< bool, QByteArray > JQNet::HTTP::get(const QString &url, const int &timeout)
- {
- QNetworkRequest networkRequest( ( QUrl( url ) ) );
- QByteArray buf;
- const auto &&flag = HTTP().get( networkRequest, buf, timeout );
- return { flag, buf };
- }
- QPair< bool, QByteArray > JQNet::HTTP::get(const QNetworkRequest &request, const int &timeout)
- {
- QByteArray buf;
- HTTP http;
- const auto &&flag = http.get( request, buf, timeout );
- return { flag, buf };
- }
- QPair< bool, QByteArray > JQNet::HTTP::deleteResource(const QString &url, const int &timeout)
- {
- QNetworkRequest networkRequest( ( QUrl( url ) ) );
- QByteArray buf;
- const auto &&flag = HTTP().deleteResource( networkRequest, buf, timeout );
- return { flag, buf };
- }
- QPair< bool, QByteArray > JQNet::HTTP::deleteResource(const QNetworkRequest &request, const int &timeout)
- {
- QByteArray buf;
- HTTP http;
- const auto &&flag = http.deleteResource( request, buf, timeout );
- return { flag, buf };
- }
- QPair< bool, QByteArray > JQNet::HTTP::post(const QString &url, const QByteArray &body, const int &timeout)
- {
- QNetworkRequest networkRequest( ( QUrl( url ) ) );
- QByteArray buf;
- networkRequest.setRawHeader( "Content-Type", "application/x-www-form-urlencoded" );
- const auto &&flag = HTTP().post( networkRequest, body, buf, timeout );
- return { flag, buf };
- }
- QPair< bool, QByteArray > JQNet::HTTP::post(const QNetworkRequest &request, const QByteArray &body, const int &timeout)
- {
- QByteArray buf;
- HTTP http;
- const auto &&flag = http.post( request, body, buf, timeout );
- return { flag, buf };
- }
- QPair< bool, QByteArray > JQNet::HTTP::post(const QNetworkRequest &request, const QSharedPointer<QHttpMultiPart> &multiPart, const int &timeout)
- {
- QByteArray buf;
- HTTP http;
- const auto &&flag = http.post( request, multiPart, buf, timeout );
- return { flag, buf };
- }
- QPair< bool, QByteArray > JQNet::HTTP::put(const QString &url, const QByteArray &body, const int &timeout)
- {
- QNetworkRequest networkRequest( ( QUrl( url ) ) );
- QByteArray buf;
- networkRequest.setRawHeader( "Content-Type", "application/x-www-form-urlencoded" );
- const auto &&flag = HTTP().put( networkRequest, body, buf, timeout );
- return { flag, buf };
- }
- QPair< bool, QByteArray > JQNet::HTTP::put(const QNetworkRequest &request, const QByteArray &body, const int &timeout)
- {
- QByteArray buf;
- HTTP http;
- const auto &&flag = http.put( request, body, buf, timeout );
- return { flag, buf };
- }
- QPair< bool, QByteArray > JQNet::HTTP::put(const QNetworkRequest &request, const QSharedPointer< QHttpMultiPart > &multiPart, const int &timeout)
- {
- QByteArray buf;
- HTTP http;
- const auto &&flag = http.put( request, multiPart, buf, timeout );
- return { flag, buf };
- }
- #if !( defined Q_OS_LINUX ) && ( QT_VERSION >= QT_VERSION_CHECK( 5, 9, 0 ) )
- QPair< bool, QByteArray > JQNet::HTTP::patch(const QString &url, const QByteArray &body, const int &timeout)
- {
- QNetworkRequest networkRequest( ( QUrl( url ) ) );
- QByteArray buf;
- networkRequest.setRawHeader( "Content-Type", "application/x-www-form-urlencoded" );
- const auto &&flag = HTTP().patch( networkRequest, body, buf, timeout );
- return { flag, buf };
- }
- QPair< bool, QByteArray > JQNet::HTTP::patch(const QNetworkRequest &request, const QByteArray &body, const int &timeout)
- {
- QByteArray buf;
- HTTP http;
- const auto &&flag = http.patch( request, body, buf, timeout );
- return { flag, buf };
- }
- #endif
- void JQNet::HTTP::handle(
- QNetworkReply *reply, const int &timeout,
- const std::function<void (const QByteArray &)> &onFinished,
- const std::function<void (const QNetworkReply::NetworkError &, const QByteArray &data)> &onError,
- const std::function<void ()> &onTimeout
- )
- {
- QSharedPointer< bool > isCalled( new bool( false ) );
- QTimer *timer = nullptr;
- if ( timeout )
- {
- timer = new QTimer;
- timer->setSingleShot(true);
- QObject::connect( timer, &QTimer::timeout, [ timer, onTimeout, isCalled ]()
- {
- if ( *isCalled ) { return; }
- *isCalled = true;
- onTimeout();
- timer->deleteLater();
- } );
- timer->start( timeout );
- }
- QObject::connect( reply, &QNetworkReply::finished, [ reply, timer, onFinished, isCalled ]()
- {
- if ( *isCalled ) { return; }
- *isCalled = true;
- if ( timer )
- {
- timer->deleteLater();
- }
- const auto &&acceptedData = reply->readAll();
- // qDebug() << acceptedData;
- onFinished( acceptedData );
- } );
- #ifndef QT_NO_SSL
- if ( reply->url().toString().toLower().startsWith( "https" ) )
- {
- QObject::connect( reply, static_cast< void( QNetworkReply::* )( const QList< QSslError > & ) >( &QNetworkReply::sslErrors ), [ reply ](const QList< QSslError > &errors)
- {
- qDebug() << "HTTP::handle: ignoreSslErrors:" << errors;
- reply->ignoreSslErrors();
- } );
- }
- #endif
- QObject::connect( reply, static_cast< void( QNetworkReply::* )( QNetworkReply::NetworkError ) >( &QNetworkReply::error ), [ reply, timer, onError, isCalled ](const QNetworkReply::NetworkError &code)
- {
- if ( *isCalled ) { return; }
- *isCalled = true;
- if ( timer )
- {
- timer->deleteLater();
- }
- const auto &&acceptedData = reply->readAll();
- // qDebug() << acceptedData;
- onError( code, acceptedData );
- } );
- }
|