[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;
|
---|
[8203] | 76 | emit newMessage(_url.path().toLatin1().replace(0,1,"") +
|
---|
[7612] | 77 | ": NetQueryV2: server replied: " +
|
---|
[1704] | 78 | _reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toByteArray(),
|
---|
| 79 | true);
|
---|
| 80 | }
|
---|
| 81 | else {
|
---|
[1378] | 82 | _status = finished;
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[7612] | 86 | //
|
---|
[1405] | 87 | ////////////////////////////////////////////////////////////////////////////
|
---|
[7612] | 88 | void bncNetQueryV2::slotProxyAuthenticationRequired(const QNetworkProxy&,
|
---|
[1405] | 89 | QAuthenticator*) {
|
---|
| 90 | emit newMessage("slotProxyAuthenticationRequired", true);
|
---|
| 91 | }
|
---|
| 92 |
|
---|
[1389] | 93 | // Start request, block till the next read
|
---|
[1378] | 94 | ////////////////////////////////////////////////////////////////////////////
|
---|
[1380] | 95 | void bncNetQueryV2::startRequest(const QUrl& url, const QByteArray& gga) {
|
---|
[1389] | 96 | startRequestPrivate(url, gga, false);
|
---|
[1378] | 97 | }
|
---|
| 98 |
|
---|
[6787] | 99 | // Start request, block till the next read
|
---|
| 100 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 101 | void bncNetQueryV2::keepAliveRequest(const QUrl& url, const QByteArray& gga) {
|
---|
| 102 | startRequestPrivate(url, gga, false);
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[1389] | 105 | // Start Request (Private Method)
|
---|
[1378] | 106 | ////////////////////////////////////////////////////////////////////////////
|
---|
[1389] | 107 | void bncNetQueryV2::startRequestPrivate(const QUrl& url, const QByteArray& gga,
|
---|
| 108 | bool full) {
|
---|
[1378] | 109 |
|
---|
| 110 | _status = running;
|
---|
| 111 |
|
---|
| 112 | // Default scheme and path
|
---|
| 113 | // -----------------------
|
---|
[1509] | 114 | _url = url;
|
---|
| 115 | if (_url.scheme().isEmpty()) {
|
---|
[3337] | 116 | if (_secure) {
|
---|
| 117 | _url.setScheme("https");
|
---|
| 118 | }
|
---|
| 119 | else {
|
---|
| 120 | _url.setScheme("http");
|
---|
| 121 | }
|
---|
[1378] | 122 | }
|
---|
[1509] | 123 | if (_url.path().isEmpty()) {
|
---|
| 124 | _url.setPath("/");
|
---|
[1378] | 125 | }
|
---|
| 126 |
|
---|
| 127 | // Network Request
|
---|
| 128 | // ---------------
|
---|
[1716] | 129 | QNetworkRequest request;
|
---|
[3360] | 130 | bncSslConfig sslConfig;
|
---|
| 131 | request.setSslConfiguration(sslConfig);
|
---|
[1716] | 132 | request.setUrl(_url);
|
---|
[8203] | 133 | request.setRawHeader("Host" , _url.host().toLatin1());
|
---|
[1716] | 134 | request.setRawHeader("Ntrip-Version", "Ntrip/2.0");
|
---|
[8203] | 135 | request.setRawHeader("User-Agent" , "NTRIP BNC/" BNCVERSION " (" BNC_OS ")");
|
---|
[1509] | 136 | if (!_url.userName().isEmpty()) {
|
---|
[8203] | 137 | QString uName = QUrl::fromPercentEncoding(_url.userName().toLatin1());
|
---|
| 138 | QString passW = QUrl::fromPercentEncoding(_url.password().toLatin1());
|
---|
[7612] | 139 | request.setRawHeader("Authorization", "Basic " +
|
---|
[8203] | 140 | (uName + ":" + passW).toLatin1().toBase64());
|
---|
[7612] | 141 | }
|
---|
[1389] | 142 | if (!gga.isEmpty()) {
|
---|
[1716] | 143 | request.setRawHeader("Ntrip-GGA", gga);
|
---|
[1389] | 144 | }
|
---|
[1716] | 145 | request.setRawHeader("Connection" , "close");
|
---|
[1378] | 146 |
|
---|
[7851] | 147 | if (_reply) {
|
---|
| 148 | delete _reply;
|
---|
| 149 | _reply = 0;
|
---|
| 150 | }
|
---|
[1716] | 151 | _reply = _manager->get(request);
|
---|
[1378] | 152 |
|
---|
| 153 | // Connect Signals
|
---|
| 154 | // ---------------
|
---|
| 155 | connect(_reply, SIGNAL(finished()), this, SLOT(slotFinished()));
|
---|
[9720] | 156 | connect(_reply, SIGNAL(finished()), _eventLoop, SLOT(terminate()));
|
---|
[9721] | 157 | connect(_reply, SIGNAL(sslErrors(QList<QSslError>)),this, SLOT(slotSslErrors(QList<QSslError>)));
|
---|
[1378] | 158 | if (!full) {
|
---|
[9720] | 159 | connect(_reply, SIGNAL(readyRead()), _eventLoop, SLOT(terminate()));
|
---|
[1378] | 160 | }
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | // Start Request, wait for its completion
|
---|
| 164 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 165 | void bncNetQueryV2::waitForRequestResult(const QUrl& url, QByteArray& outData) {
|
---|
| 166 |
|
---|
| 167 | // Send Request
|
---|
| 168 | // ------------
|
---|
[1389] | 169 | startRequestPrivate(url, "", true);
|
---|
[1378] | 170 |
|
---|
| 171 | // Wait Loop
|
---|
| 172 | // ---------
|
---|
[1394] | 173 | _eventLoop->exec();
|
---|
[1378] | 174 |
|
---|
| 175 | // Copy Data and Return
|
---|
| 176 | // --------------------
|
---|
[7813] | 177 | if (_reply) {
|
---|
| 178 | outData = _reply->readAll();
|
---|
| 179 | }
|
---|
[1378] | 180 | }
|
---|
| 181 |
|
---|
| 182 | // Wait for next data
|
---|
| 183 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 184 | void bncNetQueryV2::waitForReadyRead(QByteArray& outData) {
|
---|
| 185 |
|
---|
| 186 | // Wait Loop
|
---|
| 187 | // ---------
|
---|
| 188 | if (!_reply->bytesAvailable()) {
|
---|
[1394] | 189 | _eventLoop->exec();
|
---|
[1378] | 190 | }
|
---|
| 191 |
|
---|
[1701] | 192 | // Check NTRIPv2 error code
|
---|
| 193 | // ------------------------
|
---|
| 194 | if (_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() != 200) {
|
---|
| 195 | _reply->abort();
|
---|
| 196 | }
|
---|
| 197 |
|
---|
[1378] | 198 | // Append Data
|
---|
| 199 | // -----------
|
---|
[1701] | 200 | else {
|
---|
| 201 | outData.append(_reply->readAll());
|
---|
[1583] | 202 | }
|
---|
[1378] | 203 | }
|
---|
[1712] | 204 |
|
---|
[7612] | 205 | // TSL/SSL
|
---|
[3332] | 206 | ////////////////////////////////////////////////////////////////////////////
|
---|
[3349] | 207 | void bncNetQueryV2::slotSslErrors(QList<QSslError> errors) {
|
---|
[3332] | 208 |
|
---|
[3353] | 209 | QString msg = "SSL Error\n";
|
---|
[3351] | 210 | QSslCertificate cert = _reply->sslConfiguration().peerCertificate();
|
---|
[9708] | 211 | if (!cert.isNull() &&
|
---|
| 212 | cert.issuerInfo(QSslCertificate::OrganizationalUnitName).count() &&
|
---|
| 213 | cert.issuerInfo(QSslCertificate::Organization).count()) {
|
---|
[9719] | 214 |
|
---|
[3354] | 215 | msg += QString("Server Certificate Issued by:\n"
|
---|
| 216 | "%1\n%2\nCannot be verified\n")
|
---|
[8203] | 217 | #if QT_VERSION >= 0x050000
|
---|
| 218 | .arg(cert.issuerInfo(QSslCertificate::OrganizationalUnitName).at(0))
|
---|
| 219 | .arg(cert.issuerInfo(QSslCertificate::Organization).at(0));
|
---|
| 220 | #else
|
---|
[3354] | 221 | .arg(cert.issuerInfo(QSslCertificate::OrganizationalUnitName))
|
---|
| 222 | .arg(cert.issuerInfo(QSslCertificate::Organization));
|
---|
[8203] | 223 | #endif
|
---|
[3351] | 224 | }
|
---|
[9719] | 225 |
|
---|
[3363] | 226 | QListIterator<QSslError> it(errors);
|
---|
| 227 | while (it.hasNext()) {
|
---|
| 228 | const QSslError& err = it.next();
|
---|
| 229 | msg += "\n" + err.errorString();
|
---|
[3353] | 230 | }
|
---|
[8203] | 231 | BNC_CORE->slotMessage(msg.toLatin1(), true);
|
---|
[3346] | 232 |
|
---|
[7513] | 233 | if (_sslIgnoreErrors) {
|
---|
[3353] | 234 | _reply->ignoreSslErrors();
|
---|
| 235 | }
|
---|
| 236 | else {
|
---|
| 237 | stop();
|
---|
| 238 | }
|
---|
[3332] | 239 | }
|
---|