source: ntrip/trunk/BNC/src/bncnetqueryv2.cpp@ 10989

Last change on this file since 10989 was 10984, checked in by stuerze, 3 weeks ago

First read after startRequest/reconnect now gets 5 × _timeOut (100 s) instead of the strict 20 s, giving Windows' slow WPAD/proxy‑autodetect + TLS handshake room to finish.

File size: 9.2 KB
RevLine 
[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
[10960]19#include <QTimer>
20
[1379]21#include "bncnetqueryv2.h"
[1535]22#include "bncsettings.h"
[2011]23#include "bncversion.h"
[3349]24#include "bncsslconfig.h"
[3359]25#include "bncsettings.h"
[1378]26
27// Constructor
28////////////////////////////////////////////////////////////////////////////
[3337]29bncNetQueryV2::bncNetQueryV2(bool secure) {
30 _secure = secure;
[1378]31 _manager = new QNetworkAccessManager(this);
[9706]32 connect(_manager, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)),
33 this, SLOT(slotProxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)));
[1378]34 _reply = 0;
35 _eventLoop = new QEventLoop(this);
[1583]36 _firstData = true;
[1378]37 _status = init;
[10960]38 _timeOut = 20000;
[3359]39
40 bncSettings settings;
[9795]41 _sslIgnoreErrors = (Qt::CheckState(settings.value("sslIgnoreErrors").toInt()) == Qt::Checked);
[3359]42
[9795]43 if (_secure ) {
44 if (!QSslSocket::supportsSsl()) {
45 BNC_CORE->slotMessage("No SSL support, install OpenSSL run-time libraries", true);
46 stop();
47 }
[3352]48 }
[9795]49
[1378]50}
51
52// Destructor
53////////////////////////////////////////////////////////////////////////////
54bncNetQueryV2::~bncNetQueryV2() {
55 delete _eventLoop;
[7704]56 if (_reply) {
57 _reply->abort();
58 delete _reply;
59 }
[1378]60 delete _manager;
61}
62
[1713]63// Stop (quit event loop)
[1390]64////////////////////////////////////////////////////////////////////////////
65void bncNetQueryV2::stop() {
[1393]66 if (_reply) {
[1395]67 _reply->abort();
[7704]68 delete _reply;
69 _reply = 0;
[1393]70 }
[1394]71 _eventLoop->quit();
[1398]72 _status = finished;
[1390]73}
74
[1389]75// End of Request
76////////////////////////////////////////////////////////////////////////////
[1378]77void bncNetQueryV2::slotFinished() {
[1704]78 _eventLoop->quit();
79 if (_reply && _reply->error() != QNetworkReply::NoError) {
80 _status = error;
[9723]81 if (!_reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toByteArray().isEmpty()) {
[8203]82 emit newMessage(_url.path().toLatin1().replace(0,1,"") +
[7612]83 ": NetQueryV2: server replied: " +
[1704]84 _reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toByteArray(),
85 true);
[9723]86 } else {
87 emit newMessage(_url.path().toLatin1().replace(0,1,"") +
88 ": NetQueryV2: server replied: " +
89 _reply->errorString().toLatin1(),
90 true);
91 }
[1704]92 }
93 else {
[1378]94 _status = finished;
95 }
96}
97
[7612]98//
[1405]99////////////////////////////////////////////////////////////////////////////
[7612]100void bncNetQueryV2::slotProxyAuthenticationRequired(const QNetworkProxy&,
[1405]101 QAuthenticator*) {
102 emit newMessage("slotProxyAuthenticationRequired", true);
103}
104
[1389]105// Start request, block till the next read
[1378]106////////////////////////////////////////////////////////////////////////////
[1380]107void bncNetQueryV2::startRequest(const QUrl& url, const QByteArray& gga) {
[1389]108 startRequestPrivate(url, gga, false);
[1378]109}
110
[6787]111// Start request, block till the next read
112////////////////////////////////////////////////////////////////////////////
113void bncNetQueryV2::keepAliveRequest(const QUrl& url, const QByteArray& gga) {
114 startRequestPrivate(url, gga, false);
115}
116
[1389]117// Start Request (Private Method)
[1378]118////////////////////////////////////////////////////////////////////////////
[1389]119void bncNetQueryV2::startRequestPrivate(const QUrl& url, const QByteArray& gga,
120 bool full) {
[1378]121
122 _status = running;
123
124 // Default scheme and path
125 // -----------------------
[1509]126 _url = url;
127 if (_url.scheme().isEmpty()) {
[3337]128 if (_secure) {
129 _url.setScheme("https");
130 }
131 else {
132 _url.setScheme("http");
133 }
[1378]134 }
[1509]135 if (_url.path().isEmpty()) {
136 _url.setPath("/");
[1378]137 }
138
139 // Network Request
140 // ---------------
[9795]141 bncSslConfig sslConfig = BNC_SSL_CONFIG;
142
143 if (_secure) {
[10802]144 bncSettings settings;
145 // Generate filenames to consider a potential client certificate
146 _crtFileName = settings.value("sslClientCertPath").toString() + _url.host() + QString(".%1.crt").arg(_url.port());
147 _keyFileName = settings.value("sslClientCertPath").toString() + _url.host() + QString(".%1.key").arg(_url.port());
[9795]148 QFile clientCrtFile(_crtFileName);
149 QFile privateKeyFile(_keyFileName);
150 if ( clientCrtFile.exists() && privateKeyFile.exists()) {
151 // set local certificate if available
152 clientCrtFile.open(QIODevice::ReadOnly);
153 QSslCertificate clientCrt(&clientCrtFile);
154 sslConfig.setLocalCertificate(clientCrt);
155 // set private key if available
156 privateKeyFile.open(QIODevice::ReadOnly);
157 QSslKey privateKey(&privateKeyFile, QSsl::Rsa);
158 sslConfig.setPrivateKey(privateKey);
159 }
160 }
161
[1716]162 QNetworkRequest request;
[9795]163 request.setSslConfiguration(sslConfig);
[1716]164 request.setUrl(_url);
[8203]165 request.setRawHeader("Host" , _url.host().toLatin1());
[1716]166 request.setRawHeader("Ntrip-Version", "Ntrip/2.0");
[8203]167 request.setRawHeader("User-Agent" , "NTRIP BNC/" BNCVERSION " (" BNC_OS ")");
[1509]168 if (!_url.userName().isEmpty()) {
[8203]169 QString uName = QUrl::fromPercentEncoding(_url.userName().toLatin1());
170 QString passW = QUrl::fromPercentEncoding(_url.password().toLatin1());
[7612]171 request.setRawHeader("Authorization", "Basic " +
[8203]172 (uName + ":" + passW).toLatin1().toBase64());
[7612]173 }
[1389]174 if (!gga.isEmpty()) {
[1716]175 request.setRawHeader("Ntrip-GGA", gga);
[1389]176 }
[1716]177 request.setRawHeader("Connection" , "close");
[1378]178
[7851]179 if (_reply) {
180 delete _reply;
181 _reply = 0;
182 }
[1716]183 _reply = _manager->get(request);
[1378]184
185 // Connect Signals
186 // ---------------
187 connect(_reply, SIGNAL(finished()), this, SLOT(slotFinished()));
[9722]188 connect(_reply, SIGNAL(finished()), _eventLoop, SLOT(quit()));
[9721]189 connect(_reply, SIGNAL(sslErrors(QList<QSslError>)),this, SLOT(slotSslErrors(QList<QSslError>)));
[1378]190 if (!full) {
[9722]191 connect(_reply, SIGNAL(readyRead()), _eventLoop, SLOT(quit()));
[1378]192 }
193}
194
195// Start Request, wait for its completion
196////////////////////////////////////////////////////////////////////////////
197void bncNetQueryV2::waitForRequestResult(const QUrl& url, QByteArray& outData) {
198
199 // Send Request
200 // ------------
[1389]201 startRequestPrivate(url, "", true);
[1378]202
203 // Wait Loop
204 // ---------
[1394]205 _eventLoop->exec();
[1378]206
207 // Copy Data and Return
208 // --------------------
[7813]209 if (_reply) {
210 outData = _reply->readAll();
211 }
[1378]212}
213
214// Wait for next data
215////////////////////////////////////////////////////////////////////////////
216void bncNetQueryV2::waitForReadyRead(QByteArray& outData) {
217
218 // Wait Loop
219 // ---------
[10960]220 bool timedOut = false;
[1378]221 if (!_reply->bytesAvailable()) {
[10960]222 QTimer timer;
223 timer.setSingleShot(true);
224 connect(&timer, SIGNAL(timeout()), _eventLoop, SLOT(quit()));
[10984]225 // The very first read of a request can take much longer than a
226 // steady-state read: it includes connection setup, TLS handshake and
227 // (on Windows) potentially slow system/WPAD proxy auto-detection. Only
228 // apply the strict outage-detection timeout once the stream is live.
229 timer.start(_firstData ? 5 * _timeOut : _timeOut);
[1394]230 _eventLoop->exec();
[10960]231 timedOut = !timer.isActive();
[1378]232 }
[9723]233 if (!_reply) {
234 return;
235 }
[1378]236
[10960]237 // Timeout: neither new data arrived nor did the request finish
238 // (e.g. connection silently dropped by a NAT/firewall)
239 // --------------------------------------------------------------
240 if (timedOut) {
241 _status = error;
242 emit newMessage(_url.path().toLatin1().replace(0,1,"")
243 + ": Read timeout", true);
244 disconnect(_reply, SIGNAL(finished()), this, SLOT(slotFinished()));
245 _reply->abort();
246 return;
247 }
248
[1701]249 // Check NTRIPv2 error code
250 // ------------------------
251 if (_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() != 200) {
252 _reply->abort();
253 }
254
[1378]255 // Append Data
256 // -----------
[1701]257 else {
258 outData.append(_reply->readAll());
[10984]259 _firstData = false;
[1583]260 }
[1378]261}
[1712]262
[7612]263// TSL/SSL
[3332]264////////////////////////////////////////////////////////////////////////////
[3349]265void bncNetQueryV2::slotSslErrors(QList<QSslError> errors) {
[3332]266
[9723]267 QString msg = "SSL Error: ";
[3351]268 QSslCertificate cert = _reply->sslConfiguration().peerCertificate();
[9708]269 if (!cert.isNull() &&
270 cert.issuerInfo(QSslCertificate::OrganizationalUnitName).count() &&
271 cert.issuerInfo(QSslCertificate::Organization).count()) {
[9719]272
[3354]273 msg += QString("Server Certificate Issued by:\n"
274 "%1\n%2\nCannot be verified\n")
[8203]275#if QT_VERSION >= 0x050000
276 .arg(cert.issuerInfo(QSslCertificate::OrganizationalUnitName).at(0))
277 .arg(cert.issuerInfo(QSslCertificate::Organization).at(0));
278#else
[3354]279 .arg(cert.issuerInfo(QSslCertificate::OrganizationalUnitName))
280 .arg(cert.issuerInfo(QSslCertificate::Organization));
[8203]281#endif
[3351]282 }
[9719]283
[9723]284 QListIterator<QSslError> it(errors);
[3363]285 while (it.hasNext()) {
286 const QSslError& err = it.next();
[9723]287 msg += err.errorString();
[3353]288 }
[3346]289
[7513]290 if (_sslIgnoreErrors) {
[3353]291 _reply->ignoreSslErrors();
[9723]292 BNC_CORE->slotMessage("BNC ignores SSL errors as configured", true);
[3353]293 }
294 else {
[9742]295 BNC_CORE->slotMessage(msg.toLatin1(), true);
[3353]296 stop();
297 }
[9723]298 return;
[3332]299}
Note: See TracBrowser for help on using the repository browser.