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

Last change on this file since 7623 was 7623, checked in by stuerze, 8 years ago

minor changes

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