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