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

Last change on this file since 10802 was 10802, checked in by stuerze, 3 months 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 }
[3352]45 }
[9795]46
[1378]47}
48
49// Destructor
50////////////////////////////////////////////////////////////////////////////
51bncNetQueryV2::~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////////////////////////////////////////////////////////////////////////////
62void 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]74void bncNetQueryV2::slotFinished() {
[1704]75 _eventLoop->quit();
76 if (_reply && _reply->error() != QNetworkReply::NoError) {
77 _status = error;
[9723]78 if (!_reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toByteArray().isEmpty()) {
[8203]79 emit newMessage(_url.path().toLatin1().replace(0,1,"") +
[7612]80 ": NetQueryV2: server replied: " +
[1704]81 _reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toByteArray(),
82 true);
[9723]83 } else {
84 emit newMessage(_url.path().toLatin1().replace(0,1,"") +
85 ": NetQueryV2: server replied: " +
86 _reply->errorString().toLatin1(),
87 true);
88 }
[1704]89 }
90 else {
[1378]91 _status = finished;
92 }
93}
94
[7612]95//
[1405]96////////////////////////////////////////////////////////////////////////////
[7612]97void bncNetQueryV2::slotProxyAuthenticationRequired(const QNetworkProxy&,
[1405]98 QAuthenticator*) {
99 emit newMessage("slotProxyAuthenticationRequired", true);
100}
101
[1389]102// Start request, block till the next read
[1378]103////////////////////////////////////////////////////////////////////////////
[1380]104void bncNetQueryV2::startRequest(const QUrl& url, const QByteArray& gga) {
[1389]105 startRequestPrivate(url, gga, false);
[1378]106}
107
[6787]108// Start request, block till the next read
109////////////////////////////////////////////////////////////////////////////
110void bncNetQueryV2::keepAliveRequest(const QUrl& url, const QByteArray& gga) {
111 startRequestPrivate(url, gga, false);
112}
113
[1389]114// Start Request (Private Method)
[1378]115////////////////////////////////////////////////////////////////////////////
[1389]116void bncNetQueryV2::startRequestPrivate(const QUrl& url, const QByteArray& gga,
117 bool full) {
[1378]118
119 _status = running;
120
121 // Default scheme and path
122 // -----------------------
[1509]123 _url = url;
124 if (_url.scheme().isEmpty()) {
[3337]125 if (_secure) {
126 _url.setScheme("https");
127 }
128 else {
129 _url.setScheme("http");
130 }
[1378]131 }
[1509]132 if (_url.path().isEmpty()) {
133 _url.setPath("/");
[1378]134 }
135
136 // Network Request
137 // ---------------
[9795]138 bncSslConfig sslConfig = BNC_SSL_CONFIG;
139
140 if (_secure) {
[10802]141 bncSettings settings;
142 // Generate filenames to consider a potential client certificate
143 _crtFileName = settings.value("sslClientCertPath").toString() + _url.host() + QString(".%1.crt").arg(_url.port());
144 _keyFileName = settings.value("sslClientCertPath").toString() + _url.host() + QString(".%1.key").arg(_url.port());
[9795]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.