[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 | *
|
---|
| 13 | * Changes:
|
---|
| 14 | *
|
---|
| 15 | * -----------------------------------------------------------------------*/
|
---|
| 16 |
|
---|
[1583] | 17 | #include <iostream>
|
---|
| 18 |
|
---|
[1379] | 19 | #include "bncnetqueryv2.h"
|
---|
[1535] | 20 | #include "bncsettings.h"
|
---|
[2011] | 21 | #include "bncversion.h"
|
---|
[1378] | 22 |
|
---|
| 23 | // Constructor
|
---|
| 24 | ////////////////////////////////////////////////////////////////////////////
|
---|
[3337] | 25 | bncNetQueryV2::bncNetQueryV2(bool secure) {
|
---|
| 26 | _secure = secure;
|
---|
[1378] | 27 | _manager = new QNetworkAccessManager(this);
|
---|
[1405] | 28 | connect(_manager, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy&,
|
---|
| 29 | QAuthenticator*)),
|
---|
| 30 | this, SLOT(slotProxyAuthenticationRequired(const QNetworkProxy&,
|
---|
| 31 | QAuthenticator*)));
|
---|
[1378] | 32 | _reply = 0;
|
---|
| 33 | _eventLoop = new QEventLoop(this);
|
---|
[1583] | 34 | _firstData = true;
|
---|
[1378] | 35 | _status = init;
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | // Destructor
|
---|
| 39 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 40 | bncNetQueryV2::~bncNetQueryV2() {
|
---|
| 41 | delete _eventLoop;
|
---|
| 42 | delete _reply;
|
---|
| 43 | delete _manager;
|
---|
| 44 | }
|
---|
| 45 |
|
---|
[1713] | 46 | // Stop (quit event loop)
|
---|
[1390] | 47 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 48 | void bncNetQueryV2::stop() {
|
---|
[1393] | 49 | if (_reply) {
|
---|
[1395] | 50 | _reply->abort();
|
---|
[1393] | 51 | }
|
---|
[1394] | 52 | _eventLoop->quit();
|
---|
[1398] | 53 | _status = finished;
|
---|
[1390] | 54 | }
|
---|
| 55 |
|
---|
[1389] | 56 | // End of Request
|
---|
| 57 | ////////////////////////////////////////////////////////////////////////////
|
---|
[1378] | 58 | void bncNetQueryV2::slotFinished() {
|
---|
[1704] | 59 | _eventLoop->quit();
|
---|
| 60 | if (_reply && _reply->error() != QNetworkReply::NoError) {
|
---|
| 61 | _status = error;
|
---|
| 62 | emit newMessage(_url.path().toAscii().replace(0,1,"") +
|
---|
[1713] | 63 | ": NetQueryV2: server replied: " +
|
---|
[1704] | 64 | _reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toByteArray(),
|
---|
| 65 | true);
|
---|
| 66 | }
|
---|
| 67 | else {
|
---|
[1378] | 68 | _status = finished;
|
---|
| 69 | }
|
---|
| 70 | }
|
---|
| 71 |
|
---|
[1405] | 72 | //
|
---|
| 73 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 74 | void bncNetQueryV2::slotProxyAuthenticationRequired(const QNetworkProxy&,
|
---|
| 75 | QAuthenticator*) {
|
---|
| 76 | emit newMessage("slotProxyAuthenticationRequired", true);
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[1389] | 79 | // Start request, block till the next read
|
---|
[1378] | 80 | ////////////////////////////////////////////////////////////////////////////
|
---|
[1380] | 81 | void bncNetQueryV2::startRequest(const QUrl& url, const QByteArray& gga) {
|
---|
[1389] | 82 | startRequestPrivate(url, gga, false);
|
---|
[1378] | 83 | }
|
---|
| 84 |
|
---|
[1389] | 85 | // Start Request (Private Method)
|
---|
[1378] | 86 | ////////////////////////////////////////////////////////////////////////////
|
---|
[1389] | 87 | void bncNetQueryV2::startRequestPrivate(const QUrl& url, const QByteArray& gga,
|
---|
| 88 | bool full) {
|
---|
[1378] | 89 |
|
---|
| 90 | _status = running;
|
---|
| 91 |
|
---|
| 92 | // Default scheme and path
|
---|
| 93 | // -----------------------
|
---|
[1509] | 94 | _url = url;
|
---|
| 95 | if (_url.scheme().isEmpty()) {
|
---|
[3337] | 96 | if (_secure) {
|
---|
| 97 | _url.setScheme("https");
|
---|
| 98 | }
|
---|
| 99 | else {
|
---|
| 100 | _url.setScheme("http");
|
---|
| 101 | }
|
---|
[1378] | 102 | }
|
---|
[1509] | 103 | if (_url.path().isEmpty()) {
|
---|
| 104 | _url.setPath("/");
|
---|
[1378] | 105 | }
|
---|
| 106 |
|
---|
[1404] | 107 | // Proxy Settings
|
---|
| 108 | // --------------
|
---|
[1535] | 109 | bncSettings settings;
|
---|
[1404] | 110 | QString proxyHost = settings.value("proxyHost").toString();
|
---|
| 111 | int proxyPort = settings.value("proxyPort").toInt();
|
---|
| 112 |
|
---|
| 113 | if (!proxyHost.isEmpty()) {
|
---|
| 114 | QNetworkProxy proxy(QNetworkProxy::HttpProxy, proxyHost, proxyPort);
|
---|
| 115 | _manager->setProxy(proxy);
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[1378] | 118 | // Network Request
|
---|
| 119 | // ---------------
|
---|
[1716] | 120 | QNetworkRequest request;
|
---|
| 121 | request.setUrl(_url);
|
---|
| 122 | request.setRawHeader("Host" , _url.host().toAscii());
|
---|
| 123 | request.setRawHeader("Ntrip-Version", "Ntrip/2.0");
|
---|
[2012] | 124 | request.setRawHeader("User-Agent" , "NTRIP BNC/"BNCVERSION);
|
---|
[1509] | 125 | if (!_url.userName().isEmpty()) {
|
---|
| 126 | QString uName = QUrl::fromPercentEncoding(_url.userName().toAscii());
|
---|
| 127 | QString passW = QUrl::fromPercentEncoding(_url.password().toAscii());
|
---|
[1716] | 128 | request.setRawHeader("Authorization", "Basic " +
|
---|
[1509] | 129 | (uName + ":" + passW).toAscii().toBase64());
|
---|
[1378] | 130 | }
|
---|
[1389] | 131 | if (!gga.isEmpty()) {
|
---|
[1716] | 132 | request.setRawHeader("Ntrip-GGA", gga);
|
---|
[1389] | 133 | }
|
---|
[1716] | 134 | request.setRawHeader("Connection" , "close");
|
---|
[1378] | 135 |
|
---|
[1716] | 136 | delete _reply;
|
---|
| 137 | _reply = _manager->get(request);
|
---|
[1378] | 138 |
|
---|
| 139 | // Connect Signals
|
---|
| 140 | // ---------------
|
---|
| 141 | connect(_reply, SIGNAL(finished()), this, SLOT(slotFinished()));
|
---|
| 142 | connect(_reply, SIGNAL(finished()), _eventLoop, SLOT(quit()));
|
---|
[3332] | 143 | connect(_reply, SIGNAL(sslErrors(QList<QSslError>)),
|
---|
| 144 | this, SLOT(slotSslErrors(QList<QSslError>)));
|
---|
[1378] | 145 | if (!full) {
|
---|
| 146 | connect(_reply, SIGNAL(readyRead()), _eventLoop, SLOT(quit()));
|
---|
| 147 | }
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | // Start Request, wait for its completion
|
---|
| 151 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 152 | void bncNetQueryV2::waitForRequestResult(const QUrl& url, QByteArray& outData) {
|
---|
| 153 |
|
---|
| 154 | // Send Request
|
---|
| 155 | // ------------
|
---|
[1389] | 156 | startRequestPrivate(url, "", true);
|
---|
[1378] | 157 |
|
---|
| 158 | // Wait Loop
|
---|
| 159 | // ---------
|
---|
[1394] | 160 | _eventLoop->exec();
|
---|
[1378] | 161 |
|
---|
| 162 | // Copy Data and Return
|
---|
| 163 | // --------------------
|
---|
| 164 | outData = _reply->readAll();
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | // Wait for next data
|
---|
| 168 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 169 | void bncNetQueryV2::waitForReadyRead(QByteArray& outData) {
|
---|
| 170 |
|
---|
| 171 | // Wait Loop
|
---|
| 172 | // ---------
|
---|
| 173 | if (!_reply->bytesAvailable()) {
|
---|
[1394] | 174 | _eventLoop->exec();
|
---|
[1378] | 175 | }
|
---|
| 176 |
|
---|
[1701] | 177 | // Check NTRIPv2 error code
|
---|
| 178 | // ------------------------
|
---|
| 179 | if (_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() != 200) {
|
---|
| 180 | _reply->abort();
|
---|
| 181 | }
|
---|
| 182 |
|
---|
[1378] | 183 | // Append Data
|
---|
| 184 | // -----------
|
---|
[1701] | 185 | else {
|
---|
| 186 | outData.append(_reply->readAll());
|
---|
[1583] | 187 | }
|
---|
[1378] | 188 | }
|
---|
[1712] | 189 |
|
---|
[3332] | 190 | // TSL/SSL
|
---|
| 191 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 192 | void bncNetQueryV2::slotSslErrors(QList<QSslError>) {
|
---|
| 193 |
|
---|
[3338] | 194 | //// std::cout << "slotSslErrors" << std::endl;
|
---|
[3332] | 195 | _reply->ignoreSslErrors();
|
---|
| 196 | }
|
---|