mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-05 01:05:34 +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>
|
||||
|
||||
using namespace BlackMisc;
|
||||
using namespace BlackMisc::Network;
|
||||
using namespace BlackMisc::Simulation;
|
||||
|
||||
namespace BlackCore
|
||||
@@ -47,21 +48,9 @@ namespace BlackCore
|
||||
|
||||
QUrl url(m_modelUrl.toQUrl());
|
||||
QNetworkRequest request(url);
|
||||
const QByteArray jsonData(QJsonDocument(model.toJson()).toJson(QJsonDocument::Compact));
|
||||
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
|
||||
QHttpPart textPart;
|
||||
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);
|
||||
}
|
||||
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType, this);
|
||||
multiPart->append(CNetworkUtils::getJsonTextMutlipart(model.toJson()));
|
||||
if (m_setup.get().dbDebugFlag()) { multiPart->append(CNetworkUtils::getMultipartWithDebugFlag()); }
|
||||
|
||||
m_pendingReply = this->m_networkManager->post(request, multiPart);
|
||||
multiPart->setParent(m_pendingReply);
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
//! \file
|
||||
|
||||
#include "blackcore/blackcoreexport.h"
|
||||
#include "blackcore/data/globalsetup.h"
|
||||
#include "blackmisc/threadedreader.h"
|
||||
#include "blackmisc/simulation/aircraftmodel.h"
|
||||
|
||||
@@ -42,11 +43,11 @@ namespace BlackCore
|
||||
void ps_postResponse(QNetworkReply *nwReplyPtr);
|
||||
|
||||
private:
|
||||
BlackMisc::Network::CUrl m_modelUrl;
|
||||
QNetworkAccessManager *m_networkManager = nullptr;
|
||||
QNetworkReply *m_pendingReply = nullptr;
|
||||
bool m_shutdown = false;
|
||||
bool m_phpDebug = true;
|
||||
CData<BlackCore::Data::GlobalSetup> m_setup {this}; //!< data cache
|
||||
BlackMisc::Network::CUrl m_modelUrl;
|
||||
QNetworkAccessManager *m_networkManager = nullptr;
|
||||
QNetworkReply *m_pendingReply = nullptr;
|
||||
bool m_shutdown = false;
|
||||
|
||||
//! URL model web service
|
||||
static BlackMisc::Network::CUrl getModelWriteUrl(const BlackMisc::Network::CUrl &baseUrl);
|
||||
|
||||
@@ -181,5 +181,44 @@ namespace BlackMisc
|
||||
conf.setPeerVerifyMode(QSslSocket::VerifyNone);
|
||||
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
|
||||
} // namespacee
|
||||
|
||||
@@ -16,8 +16,11 @@
|
||||
#include "blackmisc/network/server.h"
|
||||
#include "blackmisc/network/url.h"
|
||||
#include <QUrl>
|
||||
#include <QHttpPart>
|
||||
#include <QStringList>
|
||||
#include <QUrlQuery>
|
||||
#include <QNetworkRequest>
|
||||
#include <QJsonObject>
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
@@ -27,6 +30,15 @@ namespace BlackMisc
|
||||
class BLACKMISC_EXPORT CNetworkUtils
|
||||
{
|
||||
public:
|
||||
|
||||
//! Request type
|
||||
enum RequestType
|
||||
{
|
||||
Get,
|
||||
PostUrlEncoded,
|
||||
Multipart
|
||||
};
|
||||
|
||||
//! Is a connected interface available?
|
||||
//! \param withDebugOutput enables some debugging output
|
||||
//! \return
|
||||
@@ -74,6 +86,18 @@ namespace BlackMisc
|
||||
//! Ignore SSL verification such as self signed certificates
|
||||
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:
|
||||
//! Deleted constructor
|
||||
CNetworkUtils() {}
|
||||
|
||||
@@ -100,7 +100,6 @@ namespace BlackMisc
|
||||
|
||||
QString CUrl::getFullUrl() const
|
||||
{
|
||||
Q_ASSERT_X(!m_host.isEmpty(), Q_FUNC_INFO, "missing address");
|
||||
if (m_host.isEmpty()) { return ""; }
|
||||
|
||||
QString qn(m_host);
|
||||
@@ -138,6 +137,16 @@ namespace BlackMisc
|
||||
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
|
||||
{
|
||||
if (query.isEmpty()) { return *this; }
|
||||
|
||||
@@ -119,6 +119,9 @@ namespace BlackMisc
|
||||
//! Append path
|
||||
CUrl withAppendedQuery(const QString &query) const;
|
||||
|
||||
//! Switch protocol
|
||||
CUrl withSwitchedScheme(const QString &protocol, int port) const;
|
||||
|
||||
//! \copydoc CValueObject::propertyByIndex
|
||||
CVariant propertyByIndex(const BlackMisc::CPropertyIndex &index) const;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user