source: ntrip/trunk/BNC/bncnetqueryv2.cpp@ 4084

Last change on this file since 4084 was 3363, checked in by mervart, 13 years ago
File size: 6.6 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 *
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"
[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);
[1405]30 connect(_manager, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy&,
31 QAuthenticator*)),
32 this, SLOT(slotProxyAuthenticationRequired(const QNetworkProxy&,
33 QAuthenticator*)));
[1378]34 _reply = 0;
35 _eventLoop = new QEventLoop(this);
[1583]36 _firstData = true;
[1378]37 _status = init;
[3359]38
39 bncSettings settings;
40 _ignoreSslErrors =
41 (Qt::CheckState(settings.value("ignoreSslErrors").toInt()) == Qt::Checked);
42
[3352]43 if (_secure && !QSslSocket::supportsSsl()) {
44 ((bncApp*)qApp)->slotMessage("No SSL support, install OpenSSL run-time libraries", true);
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,"") +
[1713]74 ": NetQueryV2: server replied: " +
[1704]75 _reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toByteArray(),
76 true);
77 }
78 else {
[1378]79 _status = finished;
80 }
81}
82
[1405]83//
84////////////////////////////////////////////////////////////////////////////
85void bncNetQueryV2::slotProxyAuthenticationRequired(const QNetworkProxy&,
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
[1389]96// Start Request (Private Method)
[1378]97////////////////////////////////////////////////////////////////////////////
[1389]98void bncNetQueryV2::startRequestPrivate(const QUrl& url, const QByteArray& gga,
99 bool full) {
[1378]100
101 _status = running;
102
103 // Default scheme and path
104 // -----------------------
[1509]105 _url = url;
106 if (_url.scheme().isEmpty()) {
[3337]107 if (_secure) {
108 _url.setScheme("https");
109 }
110 else {
111 _url.setScheme("http");
112 }
[1378]113 }
[1509]114 if (_url.path().isEmpty()) {
115 _url.setPath("/");
[1378]116 }
117
[1404]118 // Proxy Settings
119 // --------------
[1535]120 bncSettings settings;
[1404]121 QString proxyHost = settings.value("proxyHost").toString();
122 int proxyPort = settings.value("proxyPort").toInt();
123
124 if (!proxyHost.isEmpty()) {
125 QNetworkProxy proxy(QNetworkProxy::HttpProxy, proxyHost, proxyPort);
126 _manager->setProxy(proxy);
127 }
128
[1378]129 // Network Request
130 // ---------------
[1716]131 QNetworkRequest request;
[3360]132 bncSslConfig sslConfig;
133 request.setSslConfiguration(sslConfig);
[1716]134 request.setUrl(_url);
135 request.setRawHeader("Host" , _url.host().toAscii());
136 request.setRawHeader("Ntrip-Version", "Ntrip/2.0");
[2012]137 request.setRawHeader("User-Agent" , "NTRIP BNC/"BNCVERSION);
[1509]138 if (!_url.userName().isEmpty()) {
139 QString uName = QUrl::fromPercentEncoding(_url.userName().toAscii());
140 QString passW = QUrl::fromPercentEncoding(_url.password().toAscii());
[1716]141 request.setRawHeader("Authorization", "Basic " +
[1509]142 (uName + ":" + passW).toAscii().toBase64());
[1378]143 }
[1389]144 if (!gga.isEmpty()) {
[1716]145 request.setRawHeader("Ntrip-GGA", gga);
[1389]146 }
[1716]147 request.setRawHeader("Connection" , "close");
[1378]148
[1716]149 delete _reply;
150 _reply = _manager->get(request);
[1378]151
152 // Connect Signals
153 // ---------------
154 connect(_reply, SIGNAL(finished()), this, SLOT(slotFinished()));
155 connect(_reply, SIGNAL(finished()), _eventLoop, SLOT(quit()));
[3332]156 connect(_reply, SIGNAL(sslErrors(QList<QSslError>)),
157 this, SLOT(slotSslErrors(QList<QSslError>)));
[1378]158 if (!full) {
159 connect(_reply, SIGNAL(readyRead()), _eventLoop, SLOT(quit()));
160 }
161}
162
163// Start Request, wait for its completion
164////////////////////////////////////////////////////////////////////////////
165void bncNetQueryV2::waitForRequestResult(const QUrl& url, QByteArray& outData) {
166
167 // Send Request
168 // ------------
[1389]169 startRequestPrivate(url, "", true);
[1378]170
171 // Wait Loop
172 // ---------
[1394]173 _eventLoop->exec();
[1378]174
175 // Copy Data and Return
176 // --------------------
177 outData = _reply->readAll();
178}
179
180// Wait for next data
181////////////////////////////////////////////////////////////////////////////
182void bncNetQueryV2::waitForReadyRead(QByteArray& outData) {
183
184 // Wait Loop
185 // ---------
186 if (!_reply->bytesAvailable()) {
[1394]187 _eventLoop->exec();
[1378]188 }
189
[1701]190 // Check NTRIPv2 error code
191 // ------------------------
192 if (_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() != 200) {
193 _reply->abort();
194 }
195
[1378]196 // Append Data
197 // -----------
[1701]198 else {
199 outData.append(_reply->readAll());
[1583]200 }
[1378]201}
[1712]202
[3332]203// TSL/SSL
204////////////////////////////////////////////////////////////////////////////
[3349]205void bncNetQueryV2::slotSslErrors(QList<QSslError> errors) {
[3332]206
[3353]207 QString msg = "SSL Error\n";
[3351]208 QSslCertificate cert = _reply->sslConfiguration().peerCertificate();
209 if (!cert.isNull()) {
[3354]210 msg += QString("Server Certificate Issued by:\n"
211 "%1\n%2\nCannot be verified\n")
212 .arg(cert.issuerInfo(QSslCertificate::OrganizationalUnitName))
213 .arg(cert.issuerInfo(QSslCertificate::Organization));
[3351]214 }
[3363]215 QListIterator<QSslError> it(errors);
216 while (it.hasNext()) {
217 const QSslError& err = it.next();
218 msg += "\n" + err.errorString();
[3353]219 }
[3351]220
[3349]221 ((bncApp*)qApp)->slotMessage(msg.toAscii(), true);
[3346]222
[3353]223 if (_ignoreSslErrors) {
224 _reply->ignoreSslErrors();
225 }
226 else {
227 stop();
228 }
[3332]229}
Note: See TracBrowser for help on using the repository browser.