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

Last change on this file since 10417 was 9852, checked in by stuerze, 2 years ago

minor changes

File size: 8.1 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
[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]27bncNetQueryV2::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;
[9795]38 _sslIgnoreErrors = (Qt::CheckState(settings.value("sslIgnoreErrors").toInt()) == Qt::Checked);
[3359]39
[9795]40 if (_secure ) {
41 if (!QSslSocket::supportsSsl()) {
42 BNC_CORE->slotMessage("No SSL support, install OpenSSL run-time libraries", true);
43 stop();
44 }
45 // Generate filenames to consider a potential client certificate
46 _crtFileName = settings.value("sslClientCertPath").toString() + _url.host() + QString(".%1.crt").arg(_url.port());
47 _keyFileName = settings.value("sslClientCertPath").toString() + _url.host() + QString(".%1.key").arg(_url.port());
[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) {
[9795]129 _url.setPort(443);
[3337]130 _url.setScheme("https");
131 }
132 else {
133 _url.setScheme("http");
134 }
[1378]135 }
[1509]136 if (_url.path().isEmpty()) {
137 _url.setPath("/");
[1378]138 }
139
140 // Network Request
141 // ---------------
[9795]142 bncSslConfig sslConfig = BNC_SSL_CONFIG;
143
144 if (_secure) {
145 QFile clientCrtFile(_crtFileName);
146 QFile privateKeyFile(_keyFileName);
147 if ( clientCrtFile.exists() && privateKeyFile.exists()) {
148 // set local certificate if available
149 clientCrtFile.open(QIODevice::ReadOnly);
150 QSslCertificate clientCrt(&clientCrtFile);
151 sslConfig.setLocalCertificate(clientCrt);
152 // set private key if available
153 privateKeyFile.open(QIODevice::ReadOnly);
154 QSslKey privateKey(&privateKeyFile, QSsl::Rsa);
155 sslConfig.setPrivateKey(privateKey);
156 }
157 }
158
[1716]159 QNetworkRequest request;
[9795]160 request.setSslConfiguration(sslConfig);
[1716]161 request.setUrl(_url);
[8203]162 request.setRawHeader("Host" , _url.host().toLatin1());
[1716]163 request.setRawHeader("Ntrip-Version", "Ntrip/2.0");
[8203]164 request.setRawHeader("User-Agent" , "NTRIP BNC/" BNCVERSION " (" BNC_OS ")");
[1509]165 if (!_url.userName().isEmpty()) {
[8203]166 QString uName = QUrl::fromPercentEncoding(_url.userName().toLatin1());
167 QString passW = QUrl::fromPercentEncoding(_url.password().toLatin1());
[7612]168 request.setRawHeader("Authorization", "Basic " +
[8203]169 (uName + ":" + passW).toLatin1().toBase64());
[7612]170 }
[1389]171 if (!gga.isEmpty()) {
[1716]172 request.setRawHeader("Ntrip-GGA", gga);
[1389]173 }
[1716]174 request.setRawHeader("Connection" , "close");
[1378]175
[7851]176 if (_reply) {
177 delete _reply;
178 _reply = 0;
179 }
[1716]180 _reply = _manager->get(request);
[1378]181
182 // Connect Signals
183 // ---------------
184 connect(_reply, SIGNAL(finished()), this, SLOT(slotFinished()));
[9722]185 connect(_reply, SIGNAL(finished()), _eventLoop, SLOT(quit()));
[9721]186 connect(_reply, SIGNAL(sslErrors(QList<QSslError>)),this, SLOT(slotSslErrors(QList<QSslError>)));
[1378]187 if (!full) {
[9722]188 connect(_reply, SIGNAL(readyRead()), _eventLoop, SLOT(quit()));
[1378]189 }
190}
191
192// Start Request, wait for its completion
193////////////////////////////////////////////////////////////////////////////
194void bncNetQueryV2::waitForRequestResult(const QUrl& url, QByteArray& outData) {
195
196 // Send Request
197 // ------------
[1389]198 startRequestPrivate(url, "", true);
[1378]199
200 // Wait Loop
201 // ---------
[1394]202 _eventLoop->exec();
[1378]203
204 // Copy Data and Return
205 // --------------------
[7813]206 if (_reply) {
207 outData = _reply->readAll();
208 }
[1378]209}
210
211// Wait for next data
212////////////////////////////////////////////////////////////////////////////
213void bncNetQueryV2::waitForReadyRead(QByteArray& outData) {
214
215 // Wait Loop
216 // ---------
217 if (!_reply->bytesAvailable()) {
[1394]218 _eventLoop->exec();
[1378]219 }
[9723]220 if (!_reply) {
221 return;
222 }
[1378]223
[1701]224 // Check NTRIPv2 error code
225 // ------------------------
226 if (_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() != 200) {
227 _reply->abort();
228 }
229
[1378]230 // Append Data
231 // -----------
[1701]232 else {
233 outData.append(_reply->readAll());
[1583]234 }
[1378]235}
[1712]236
[7612]237// TSL/SSL
[3332]238////////////////////////////////////////////////////////////////////////////
[3349]239void bncNetQueryV2::slotSslErrors(QList<QSslError> errors) {
[3332]240
[9723]241 QString msg = "SSL Error: ";
[3351]242 QSslCertificate cert = _reply->sslConfiguration().peerCertificate();
[9708]243 if (!cert.isNull() &&
244 cert.issuerInfo(QSslCertificate::OrganizationalUnitName).count() &&
245 cert.issuerInfo(QSslCertificate::Organization).count()) {
[9719]246
[3354]247 msg += QString("Server Certificate Issued by:\n"
248 "%1\n%2\nCannot be verified\n")
[8203]249#if QT_VERSION >= 0x050000
250 .arg(cert.issuerInfo(QSslCertificate::OrganizationalUnitName).at(0))
251 .arg(cert.issuerInfo(QSslCertificate::Organization).at(0));
252#else
[3354]253 .arg(cert.issuerInfo(QSslCertificate::OrganizationalUnitName))
254 .arg(cert.issuerInfo(QSslCertificate::Organization));
[8203]255#endif
[3351]256 }
[9719]257
[9723]258 QListIterator<QSslError> it(errors);
[3363]259 while (it.hasNext()) {
260 const QSslError& err = it.next();
[9723]261 msg += err.errorString();
[3353]262 }
[3346]263
[7513]264 if (_sslIgnoreErrors) {
[3353]265 _reply->ignoreSslErrors();
[9723]266 BNC_CORE->slotMessage("BNC ignores SSL errors as configured", true);
[3353]267 }
268 else {
[9742]269 BNC_CORE->slotMessage(msg.toLatin1(), true);
[3353]270 stop();
271 }
[9723]272 return;
[3332]273}
Note: See TracBrowser for help on using the repository browser.