mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-05-04 00:45:46 +08:00
refs #497, improved URL and network utils
* utility functions * URL, allow to switch to https * Adjusted database writer
This commit is contained in:
committed by
Mathew Sutcliffe
parent
03f4aa6889
commit
7cb4c6a6c6
@@ -18,6 +18,7 @@
|
|||||||
#include <QHttpMultiPart>
|
#include <QHttpMultiPart>
|
||||||
|
|
||||||
using namespace BlackMisc;
|
using namespace BlackMisc;
|
||||||
|
using namespace BlackMisc::Network;
|
||||||
using namespace BlackMisc::Simulation;
|
using namespace BlackMisc::Simulation;
|
||||||
|
|
||||||
namespace BlackCore
|
namespace BlackCore
|
||||||
@@ -47,21 +48,9 @@ namespace BlackCore
|
|||||||
|
|
||||||
QUrl url(m_modelUrl.toQUrl());
|
QUrl url(m_modelUrl.toQUrl());
|
||||||
QNetworkRequest request(url);
|
QNetworkRequest request(url);
|
||||||
const QByteArray jsonData(QJsonDocument(model.toJson()).toJson(QJsonDocument::Compact));
|
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType, this);
|
||||||
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
|
multiPart->append(CNetworkUtils::getJsonTextMutlipart(model.toJson()));
|
||||||
QHttpPart textPart;
|
if (m_setup.get().dbDebugFlag()) { multiPart->append(CNetworkUtils::getMultipartWithDebugFlag()); }
|
||||||
QString name("form-data; name=\"swiftjson\"");
|
|
||||||
textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(name));
|
|
||||||
textPart.setBody(jsonData);
|
|
||||||
multiPart->append(textPart);
|
|
||||||
|
|
||||||
if (m_phpDebug && CProject::isRunningInDeveloperEnvironment())
|
|
||||||
{
|
|
||||||
QHttpPart textPartDebug;
|
|
||||||
textPartDebug.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"XDEBUG_SESSION_START\""));
|
|
||||||
textPartDebug.setBody(QString("ECLIPSE_DBGP").toUtf8());
|
|
||||||
multiPart->append(textPartDebug);
|
|
||||||
}
|
|
||||||
|
|
||||||
m_pendingReply = this->m_networkManager->post(request, multiPart);
|
m_pendingReply = this->m_networkManager->post(request, multiPart);
|
||||||
multiPart->setParent(m_pendingReply);
|
multiPart->setParent(m_pendingReply);
|
||||||
|
|||||||
@@ -13,6 +13,7 @@
|
|||||||
//! \file
|
//! \file
|
||||||
|
|
||||||
#include "blackcore/blackcoreexport.h"
|
#include "blackcore/blackcoreexport.h"
|
||||||
|
#include "blackcore/data/globalsetup.h"
|
||||||
#include "blackmisc/threadedreader.h"
|
#include "blackmisc/threadedreader.h"
|
||||||
#include "blackmisc/simulation/aircraftmodel.h"
|
#include "blackmisc/simulation/aircraftmodel.h"
|
||||||
|
|
||||||
@@ -42,11 +43,11 @@ namespace BlackCore
|
|||||||
void ps_postResponse(QNetworkReply *nwReplyPtr);
|
void ps_postResponse(QNetworkReply *nwReplyPtr);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
BlackMisc::Network::CUrl m_modelUrl;
|
CData<BlackCore::Data::GlobalSetup> m_setup {this}; //!< data cache
|
||||||
QNetworkAccessManager *m_networkManager = nullptr;
|
BlackMisc::Network::CUrl m_modelUrl;
|
||||||
QNetworkReply *m_pendingReply = nullptr;
|
QNetworkAccessManager *m_networkManager = nullptr;
|
||||||
bool m_shutdown = false;
|
QNetworkReply *m_pendingReply = nullptr;
|
||||||
bool m_phpDebug = true;
|
bool m_shutdown = false;
|
||||||
|
|
||||||
//! URL model web service
|
//! URL model web service
|
||||||
static BlackMisc::Network::CUrl getModelWriteUrl(const BlackMisc::Network::CUrl &baseUrl);
|
static BlackMisc::Network::CUrl getModelWriteUrl(const BlackMisc::Network::CUrl &baseUrl);
|
||||||
|
|||||||
@@ -181,5 +181,44 @@ namespace BlackMisc
|
|||||||
conf.setPeerVerifyMode(QSslSocket::VerifyNone);
|
conf.setPeerVerifyMode(QSslSocket::VerifyNone);
|
||||||
request.setSslConfiguration(conf);
|
request.setSslConfiguration(conf);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QHttpPart CNetworkUtils::getMultipartWithDebugFlag()
|
||||||
|
{
|
||||||
|
QHttpPart textPartDebug;
|
||||||
|
textPartDebug.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"XDEBUG_SESSION_START\""));
|
||||||
|
textPartDebug.setBody(QString("ECLIPSE_DBGP").toUtf8());
|
||||||
|
return textPartDebug;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CNetworkUtils::addDebugFlag(QUrlQuery &qurl)
|
||||||
|
{
|
||||||
|
qurl.addQueryItem("XDEBUG_SESSION_START", "ECLIPSE_DBGP");
|
||||||
|
}
|
||||||
|
|
||||||
|
QHttpPart CNetworkUtils::getJsonTextMutlipart(const QJsonObject &json)
|
||||||
|
{
|
||||||
|
const QByteArray jsonData(QJsonDocument(json).toJson(QJsonDocument::Compact));
|
||||||
|
QHttpPart textPart;
|
||||||
|
QString name("form-data; name=\"swiftjson\"");
|
||||||
|
textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(name));
|
||||||
|
textPart.setBody(jsonData);
|
||||||
|
return textPart;
|
||||||
|
}
|
||||||
|
|
||||||
|
QNetworkRequest CNetworkUtils::getNetworkRequest(const CUrl &url, RequestType type)
|
||||||
|
{
|
||||||
|
QNetworkRequest request(url.toQUrl());
|
||||||
|
switch (type)
|
||||||
|
{
|
||||||
|
case PostUrlEncoded:
|
||||||
|
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
CNetworkUtils::ignoreSslVerification(request);
|
||||||
|
return request;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
} // namespacee
|
} // namespacee
|
||||||
|
|||||||
@@ -16,8 +16,11 @@
|
|||||||
#include "blackmisc/network/server.h"
|
#include "blackmisc/network/server.h"
|
||||||
#include "blackmisc/network/url.h"
|
#include "blackmisc/network/url.h"
|
||||||
#include <QUrl>
|
#include <QUrl>
|
||||||
|
#include <QHttpPart>
|
||||||
#include <QStringList>
|
#include <QStringList>
|
||||||
|
#include <QUrlQuery>
|
||||||
#include <QNetworkRequest>
|
#include <QNetworkRequest>
|
||||||
|
#include <QJsonObject>
|
||||||
|
|
||||||
namespace BlackMisc
|
namespace BlackMisc
|
||||||
{
|
{
|
||||||
@@ -27,6 +30,15 @@ namespace BlackMisc
|
|||||||
class BLACKMISC_EXPORT CNetworkUtils
|
class BLACKMISC_EXPORT CNetworkUtils
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
//! Request type
|
||||||
|
enum RequestType
|
||||||
|
{
|
||||||
|
Get,
|
||||||
|
PostUrlEncoded,
|
||||||
|
Multipart
|
||||||
|
};
|
||||||
|
|
||||||
//! Is a connected interface available?
|
//! Is a connected interface available?
|
||||||
//! \param withDebugOutput enables some debugging output
|
//! \param withDebugOutput enables some debugging output
|
||||||
//! \return
|
//! \return
|
||||||
@@ -74,6 +86,18 @@ namespace BlackMisc
|
|||||||
//! Ignore SSL verification such as self signed certificates
|
//! Ignore SSL verification such as self signed certificates
|
||||||
static void ignoreSslVerification(QNetworkRequest &request);
|
static void ignoreSslVerification(QNetworkRequest &request);
|
||||||
|
|
||||||
|
//! Multipart with DEBUG FLAG for server
|
||||||
|
static QHttpPart getMultipartWithDebugFlag();
|
||||||
|
|
||||||
|
//! Add debug flag
|
||||||
|
static void addDebugFlag(QUrlQuery &qurl);
|
||||||
|
|
||||||
|
//! Multipart for JSON
|
||||||
|
static QHttpPart getJsonTextMutlipart(const QJsonObject &json);
|
||||||
|
|
||||||
|
//! Our tweakes network request
|
||||||
|
static QNetworkRequest getNetworkRequest(const CUrl &url, RequestType type = Get);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
//! Deleted constructor
|
//! Deleted constructor
|
||||||
CNetworkUtils() {}
|
CNetworkUtils() {}
|
||||||
|
|||||||
@@ -100,7 +100,6 @@ namespace BlackMisc
|
|||||||
|
|
||||||
QString CUrl::getFullUrl() const
|
QString CUrl::getFullUrl() const
|
||||||
{
|
{
|
||||||
Q_ASSERT_X(!m_host.isEmpty(), Q_FUNC_INFO, "missing address");
|
|
||||||
if (m_host.isEmpty()) { return ""; }
|
if (m_host.isEmpty()) { return ""; }
|
||||||
|
|
||||||
QString qn(m_host);
|
QString qn(m_host);
|
||||||
@@ -138,6 +137,16 @@ namespace BlackMisc
|
|||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CUrl CUrl::withSwitchedScheme(const QString &scheme, int port) const
|
||||||
|
{
|
||||||
|
if (getPort() == port && getScheme() == scheme) { return *this; }
|
||||||
|
QUrl qurl(this->toQUrl());
|
||||||
|
qurl.setPort(port);
|
||||||
|
qurl.setScheme(scheme);
|
||||||
|
CUrl url(qurl);
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
|
||||||
CUrl CUrl::withAppendedQuery(const QString &query) const
|
CUrl CUrl::withAppendedQuery(const QString &query) const
|
||||||
{
|
{
|
||||||
if (query.isEmpty()) { return *this; }
|
if (query.isEmpty()) { return *this; }
|
||||||
|
|||||||
@@ -119,6 +119,9 @@ namespace BlackMisc
|
|||||||
//! Append path
|
//! Append path
|
||||||
CUrl withAppendedQuery(const QString &query) const;
|
CUrl withAppendedQuery(const QString &query) const;
|
||||||
|
|
||||||
|
//! Switch protocol
|
||||||
|
CUrl withSwitchedScheme(const QString &protocol, int port) const;
|
||||||
|
|
||||||
//! \copydoc CValueObject::propertyByIndex
|
//! \copydoc CValueObject::propertyByIndex
|
||||||
CVariant propertyByIndex(const BlackMisc::CPropertyIndex &index) const;
|
CVariant propertyByIndex(const BlackMisc::CPropertyIndex &index) const;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user