| [1378] | 1 | /* -------------------------------------------------------------------------
|
|---|
| 2 | * BKG NTRIP Client
|
|---|
| 3 | * -------------------------------------------------------------------------
|
|---|
| 4 | *
|
|---|
| 5 | * Class: bncNetQueryV2
|
|---|
| 6 | *
|
|---|
| 7 | * Purpose: Blocking Network Requests (NTRIP Version 2)
|
|---|
| 8 | *
|
|---|
| 9 | * Author: L. Mervart
|
|---|
| 10 | *
|
|---|
| 11 | * Created: 27-Dec-2008
|
|---|
| 12 | *
|
|---|
| [7612] | 13 | * Changes:
|
|---|
| [1378] | 14 | *
|
|---|
| 15 | * -----------------------------------------------------------------------*/
|
|---|
| 16 |
|
|---|
| [1583] | 17 | #include <iostream>
|
|---|
| 18 |
|
|---|
| [1379] | 19 | #include "bncnetqueryv2.h"
|
|---|
| [1535] | 20 | #include "bncsettings.h"
|
|---|
| [2011] | 21 | #include "bncversion.h"
|
|---|
| [3349] | 22 | #include "bncsslconfig.h"
|
|---|
| [3359] | 23 | #include "bncsettings.h"
|
|---|
| [1378] | 24 |
|
|---|
| 25 | // Constructor
|
|---|
| 26 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| [3337] | 27 | bncNetQueryV2::bncNetQueryV2(bool secure) {
|
|---|
| 28 | _secure = secure;
|
|---|
| [1378] | 29 | _manager = new QNetworkAccessManager(this);
|
|---|
| [9706] | 30 | connect(_manager, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)),
|
|---|
| 31 | this, SLOT(slotProxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)));
|
|---|
| [1378] | 32 | _reply = 0;
|
|---|
| 33 | _eventLoop = new QEventLoop(this);
|
|---|
| [1583] | 34 | _firstData = true;
|
|---|
| [1378] | 35 | _status = init;
|
|---|
| [3359] | 36 |
|
|---|
| 37 | bncSettings settings;
|
|---|
| [7612] | 38 | _sslIgnoreErrors =
|
|---|
| [7513] | 39 | (Qt::CheckState(settings.value("sslIgnoreErrors").toInt()) == Qt::Checked);
|
|---|
| [3359] | 40 |
|
|---|
| [3352] | 41 | if (_secure && !QSslSocket::supportsSsl()) {
|
|---|
| [5068] | 42 | BNC_CORE->slotMessage("No SSL support, install OpenSSL run-time libraries", true);
|
|---|
| [3352] | 43 | stop();
|
|---|
| 44 | }
|
|---|
| [1378] | 45 | }
|
|---|
| 46 |
|
|---|
| 47 | // Destructor
|
|---|
| 48 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 49 | bncNetQueryV2::~bncNetQueryV2() {
|
|---|
| 50 | delete _eventLoop;
|
|---|
| [7704] | 51 | if (_reply) {
|
|---|
| 52 | _reply->abort();
|
|---|
| 53 | delete _reply;
|
|---|
| 54 | }
|
|---|
| [1378] | 55 | delete _manager;
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| [1713] | 58 | // Stop (quit event loop)
|
|---|
| [1390] | 59 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 60 | void bncNetQueryV2::stop() {
|
|---|
| [1393] | 61 | if (_reply) {
|
|---|
| [1395] | 62 | _reply->abort();
|
|---|
| [7704] | 63 | delete _reply;
|
|---|
| 64 | _reply = 0;
|
|---|
| [1393] | 65 | }
|
|---|
| [1394] | 66 | _eventLoop->quit();
|
|---|
| [1398] | 67 | _status = finished;
|
|---|
| [1390] | 68 | }
|
|---|
| 69 |
|
|---|
| [1389] | 70 | // End of Request
|
|---|
| 71 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| [1378] | 72 | void bncNetQueryV2::slotFinished() {
|
|---|
| [1704] | 73 | _eventLoop->quit();
|
|---|
| 74 | if (_reply && _reply->error() != QNetworkReply::NoError) {
|
|---|
| 75 | _status = error;
|
|---|
| [9723] | 76 | if (!_reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toByteArray().isEmpty()) {
|
|---|
| [8203] | 77 | emit newMessage(_url.path().toLatin1().replace(0,1,"") +
|
|---|
| [7612] | 78 | ": NetQueryV2: server replied: " +
|
|---|
| [1704] | 79 | _reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toByteArray(),
|
|---|
| 80 | true);
|
|---|
| [9723] | 81 | } else {
|
|---|
| 82 | emit newMessage(_url.path().toLatin1().replace(0,1,"") +
|
|---|
| 83 | ": NetQueryV2: server replied: " +
|
|---|
| 84 | _reply->errorString().toLatin1(),
|
|---|
| 85 | true);
|
|---|
| 86 | }
|
|---|
| [1704] | 87 | }
|
|---|
| 88 | else {
|
|---|
| [1378] | 89 | _status = finished;
|
|---|
| 90 | }
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| [7612] | 93 | //
|
|---|
| [1405] | 94 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| [7612] | 95 | void bncNetQueryV2::slotProxyAuthenticationRequired(const QNetworkProxy&,
|
|---|
| [1405] | 96 | QAuthenticator*) {
|
|---|
| 97 | emit newMessage("slotProxyAuthenticationRequired", true);
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| [1389] | 100 | // Start request, block till the next read
|
|---|
| [1378] | 101 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| [1380] | 102 | void bncNetQueryV2::startRequest(const QUrl& url, const QByteArray& gga) {
|
|---|
| [1389] | 103 | startRequestPrivate(url, gga, false);
|
|---|
| [1378] | 104 | }
|
|---|
| 105 |
|
|---|
| [6787] | 106 | // Start request, block till the next read
|
|---|
| 107 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 108 | void bncNetQueryV2::keepAliveRequest(const QUrl& url, const QByteArray& gga) {
|
|---|
| 109 | startRequestPrivate(url, gga, false);
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| [1389] | 112 | // Start Request (Private Method)
|
|---|
| [1378] | 113 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| [1389] | 114 | void bncNetQueryV2::startRequestPrivate(const QUrl& url, const QByteArray& gga,
|
|---|
| 115 | bool full) {
|
|---|
| [1378] | 116 |
|
|---|
| 117 | _status = running;
|
|---|
| 118 |
|
|---|
| 119 | // Default scheme and path
|
|---|
| 120 | // -----------------------
|
|---|
| [1509] | 121 | _url = url;
|
|---|
| 122 | if (_url.scheme().isEmpty()) {
|
|---|
| [3337] | 123 | if (_secure) {
|
|---|
| 124 | _url.setScheme("https");
|
|---|
| 125 | }
|
|---|
| 126 | else {
|
|---|
| 127 | _url.setScheme("http");
|
|---|
| 128 | }
|
|---|
| [1378] | 129 | }
|
|---|
| [1509] | 130 | if (_url.path().isEmpty()) {
|
|---|
| 131 | _url.setPath("/");
|
|---|
| [1378] | 132 | }
|
|---|
| 133 |
|
|---|
| 134 | // Network Request
|
|---|
| 135 | // ---------------
|
|---|
| [1716] | 136 | QNetworkRequest request;
|
|---|
| [3360] | 137 | bncSslConfig sslConfig;
|
|---|
| 138 | request.setSslConfiguration(sslConfig);
|
|---|
| [1716] | 139 | request.setUrl(_url);
|
|---|
| [8203] | 140 | request.setRawHeader("Host" , _url.host().toLatin1());
|
|---|
| [1716] | 141 | request.setRawHeader("Ntrip-Version", "Ntrip/2.0");
|
|---|
| [8203] | 142 | request.setRawHeader("User-Agent" , "NTRIP BNC/" BNCVERSION " (" BNC_OS ")");
|
|---|
| [1509] | 143 | if (!_url.userName().isEmpty()) {
|
|---|
| [8203] | 144 | QString uName = QUrl::fromPercentEncoding(_url.userName().toLatin1());
|
|---|
| 145 | QString passW = QUrl::fromPercentEncoding(_url.password().toLatin1());
|
|---|
| [7612] | 146 | request.setRawHeader("Authorization", "Basic " +
|
|---|
| [8203] | 147 | (uName + ":" + passW).toLatin1().toBase64());
|
|---|
| [7612] | 148 | }
|
|---|
| [1389] | 149 | if (!gga.isEmpty()) {
|
|---|
| [1716] | 150 | request.setRawHeader("Ntrip-GGA", gga);
|
|---|
| [1389] | 151 | }
|
|---|
| [1716] | 152 | request.setRawHeader("Connection" , "close");
|
|---|
| [1378] | 153 |
|
|---|
| [7851] | 154 | if (_reply) {
|
|---|
| 155 | delete _reply;
|
|---|
| 156 | _reply = 0;
|
|---|
| 157 | }
|
|---|
| [1716] | 158 | _reply = _manager->get(request);
|
|---|
| [1378] | 159 |
|
|---|
| 160 | // Connect Signals
|
|---|
| 161 | // ---------------
|
|---|
| 162 | connect(_reply, SIGNAL(finished()), this, SLOT(slotFinished()));
|
|---|
| [9722] | 163 | connect(_reply, SIGNAL(finished()), _eventLoop, SLOT(quit()));
|
|---|
| [9721] | 164 | connect(_reply, SIGNAL(sslErrors(QList<QSslError>)),this, SLOT(slotSslErrors(QList<QSslError>)));
|
|---|
| [1378] | 165 | if (!full) {
|
|---|
| [9722] | 166 | connect(_reply, SIGNAL(readyRead()), _eventLoop, SLOT(quit()));
|
|---|
| [1378] | 167 | }
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | // Start Request, wait for its completion
|
|---|
| 171 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 172 | void bncNetQueryV2::waitForRequestResult(const QUrl& url, QByteArray& outData) {
|
|---|
| 173 |
|
|---|
| 174 | // Send Request
|
|---|
| 175 | // ------------
|
|---|
| [1389] | 176 | startRequestPrivate(url, "", true);
|
|---|
| [1378] | 177 |
|
|---|
| 178 | // Wait Loop
|
|---|
| 179 | // ---------
|
|---|
| [1394] | 180 | _eventLoop->exec();
|
|---|
| [1378] | 181 |
|
|---|
| 182 | // Copy Data and Return
|
|---|
| 183 | // --------------------
|
|---|
| [7813] | 184 | if (_reply) {
|
|---|
| 185 | outData = _reply->readAll();
|
|---|
| 186 | }
|
|---|
| [1378] | 187 | }
|
|---|
| 188 |
|
|---|
| 189 | // Wait for next data
|
|---|
| 190 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 191 | void bncNetQueryV2::waitForReadyRead(QByteArray& outData) {
|
|---|
| 192 |
|
|---|
| 193 | // Wait Loop
|
|---|
| 194 | // ---------
|
|---|
| 195 | if (!_reply->bytesAvailable()) {
|
|---|
| [1394] | 196 | _eventLoop->exec();
|
|---|
| [1378] | 197 | }
|
|---|
| [9723] | 198 | if (!_reply) {
|
|---|
| 199 | return;
|
|---|
| 200 | }
|
|---|
| [1378] | 201 |
|
|---|
| [1701] | 202 | // Check NTRIPv2 error code
|
|---|
| 203 | // ------------------------
|
|---|
| 204 | if (_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() != 200) {
|
|---|
| 205 | _reply->abort();
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| [1378] | 208 | // Append Data
|
|---|
| 209 | // -----------
|
|---|
| [1701] | 210 | else {
|
|---|
| 211 | outData.append(_reply->readAll());
|
|---|
| [1583] | 212 | }
|
|---|
| [1378] | 213 | }
|
|---|
| [1712] | 214 |
|
|---|
| [7612] | 215 | // TSL/SSL
|
|---|
| [3332] | 216 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| [3349] | 217 | void bncNetQueryV2::slotSslErrors(QList<QSslError> errors) {
|
|---|
| [3332] | 218 |
|
|---|
| [9723] | 219 | QString msg = "SSL Error: ";
|
|---|
| [3351] | 220 | QSslCertificate cert = _reply->sslConfiguration().peerCertificate();
|
|---|
| [9708] | 221 | if (!cert.isNull() &&
|
|---|
| 222 | cert.issuerInfo(QSslCertificate::OrganizationalUnitName).count() &&
|
|---|
| 223 | cert.issuerInfo(QSslCertificate::Organization).count()) {
|
|---|
| [9719] | 224 |
|
|---|
| [3354] | 225 | msg += QString("Server Certificate Issued by:\n"
|
|---|
| 226 | "%1\n%2\nCannot be verified\n")
|
|---|
| [8203] | 227 | #if QT_VERSION >= 0x050000
|
|---|
| 228 | .arg(cert.issuerInfo(QSslCertificate::OrganizationalUnitName).at(0))
|
|---|
| 229 | .arg(cert.issuerInfo(QSslCertificate::Organization).at(0));
|
|---|
| 230 | #else
|
|---|
| [3354] | 231 | .arg(cert.issuerInfo(QSslCertificate::OrganizationalUnitName))
|
|---|
| 232 | .arg(cert.issuerInfo(QSslCertificate::Organization));
|
|---|
| [8203] | 233 | #endif
|
|---|
| [3351] | 234 | }
|
|---|
| [9719] | 235 |
|
|---|
| [9723] | 236 | QListIterator<QSslError> it(errors);
|
|---|
| [3363] | 237 | while (it.hasNext()) {
|
|---|
| 238 | const QSslError& err = it.next();
|
|---|
| [9723] | 239 | msg += err.errorString();
|
|---|
| [3353] | 240 | }
|
|---|
| [8203] | 241 | BNC_CORE->slotMessage(msg.toLatin1(), true);
|
|---|
| [3346] | 242 |
|
|---|
| [7513] | 243 | if (_sslIgnoreErrors) {
|
|---|
| [3353] | 244 | _reply->ignoreSslErrors();
|
|---|
| [9723] | 245 | BNC_CORE->slotMessage("BNC ignores SSL errors as configured", true);
|
|---|
| [3353] | 246 | }
|
|---|
| 247 | else {
|
|---|
| 248 | stop();
|
|---|
| 249 | }
|
|---|
| [9723] | 250 | return;
|
|---|
| [3332] | 251 | }
|
|---|