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

Last change on this file since 9723 was 9723, checked in by stuerze, 3 months ago

minor changes to handle SSL errors

File size: 7.3 KB
Line 
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
17#include <iostream>
18
19#include "bncnetqueryv2.h"
20#include "bncsettings.h"
21#include "bncversion.h"
22#include "bncsslconfig.h"
23#include "bncsettings.h"
24
25// Constructor
26////////////////////////////////////////////////////////////////////////////
27bncNetQueryV2::bncNetQueryV2(bool secure) {
28 _secure = secure;
29 _manager = new QNetworkAccessManager(this);
30 connect(_manager, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)),
31 this, SLOT(slotProxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)));
32 _reply = 0;
33 _eventLoop = new QEventLoop(this);
34 _firstData = true;
35 _status = init;
36
37 bncSettings settings;
38 _sslIgnoreErrors =
39 (Qt::CheckState(settings.value("sslIgnoreErrors").toInt()) == Qt::Checked);
40
41 if (_secure && !QSslSocket::supportsSsl()) {
42 BNC_CORE->slotMessage("No SSL support, install OpenSSL run-time libraries", true);
43 stop();
44 }
45}
46
47// Destructor
48////////////////////////////////////////////////////////////////////////////
49bncNetQueryV2::~bncNetQueryV2() {
50 delete _eventLoop;
51 if (_reply) {
52 _reply->abort();
53 delete _reply;
54 }
55 delete _manager;
56}
57
58// Stop (quit event loop)
59////////////////////////////////////////////////////////////////////////////
60void bncNetQueryV2::stop() {
61 if (_reply) {
62 _reply->abort();
63 delete _reply;
64 _reply = 0;
65 }
66 _eventLoop->quit();
67 _status = finished;
68}
69
70// End of Request
71////////////////////////////////////////////////////////////////////////////
72void bncNetQueryV2::slotFinished() {
73 _eventLoop->quit();
74 if (_reply && _reply->error() != QNetworkReply::NoError) {
75 _status = error;
76 if (!_reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toByteArray().isEmpty()) {
77 emit newMessage(_url.path().toLatin1().replace(0,1,"") +
78 ": NetQueryV2: server replied: " +
79 _reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toByteArray(),
80 true);
81 } else {
82 emit newMessage(_url.path().toLatin1().replace(0,1,"") +
83 ": NetQueryV2: server replied: " +
84 _reply->errorString().toLatin1(),
85 true);
86 }
87 }
88 else {
89 _status = finished;
90 }
91}
92
93//
94////////////////////////////////////////////////////////////////////////////
95void bncNetQueryV2::slotProxyAuthenticationRequired(const QNetworkProxy&,
96 QAuthenticator*) {
97 emit newMessage("slotProxyAuthenticationRequired", true);
98}
99
100// Start request, block till the next read
101////////////////////////////////////////////////////////////////////////////
102void bncNetQueryV2::startRequest(const QUrl& url, const QByteArray& gga) {
103 startRequestPrivate(url, gga, false);
104}
105
106// Start request, block till the next read
107////////////////////////////////////////////////////////////////////////////
108void bncNetQueryV2::keepAliveRequest(const QUrl& url, const QByteArray& gga) {
109 startRequestPrivate(url, gga, false);
110}
111
112// Start Request (Private Method)
113////////////////////////////////////////////////////////////////////////////
114void bncNetQueryV2::startRequestPrivate(const QUrl& url, const QByteArray& gga,
115 bool full) {
116
117 _status = running;
118
119 // Default scheme and path
120 // -----------------------
121 _url = url;
122 if (_url.scheme().isEmpty()) {
123 if (_secure) {
124 _url.setScheme("https");
125 }
126 else {
127 _url.setScheme("http");
128 }
129 }
130 if (_url.path().isEmpty()) {
131 _url.setPath("/");
132 }
133
134 // Network Request
135 // ---------------
136 QNetworkRequest request;
137 bncSslConfig sslConfig;
138 request.setSslConfiguration(sslConfig);
139 request.setUrl(_url);
140 request.setRawHeader("Host" , _url.host().toLatin1());
141 request.setRawHeader("Ntrip-Version", "Ntrip/2.0");
142 request.setRawHeader("User-Agent" , "NTRIP BNC/" BNCVERSION " (" BNC_OS ")");
143 if (!_url.userName().isEmpty()) {
144 QString uName = QUrl::fromPercentEncoding(_url.userName().toLatin1());
145 QString passW = QUrl::fromPercentEncoding(_url.password().toLatin1());
146 request.setRawHeader("Authorization", "Basic " +
147 (uName + ":" + passW).toLatin1().toBase64());
148 }
149 if (!gga.isEmpty()) {
150 request.setRawHeader("Ntrip-GGA", gga);
151 }
152 request.setRawHeader("Connection" , "close");
153
154 if (_reply) {
155 delete _reply;
156 _reply = 0;
157 }
158 _reply = _manager->get(request);
159
160 // Connect Signals
161 // ---------------
162 connect(_reply, SIGNAL(finished()), this, SLOT(slotFinished()));
163 connect(_reply, SIGNAL(finished()), _eventLoop, SLOT(quit()));
164 connect(_reply, SIGNAL(sslErrors(QList<QSslError>)),this, SLOT(slotSslErrors(QList<QSslError>)));
165 if (!full) {
166 connect(_reply, SIGNAL(readyRead()), _eventLoop, SLOT(quit()));
167 }
168}
169
170// Start Request, wait for its completion
171////////////////////////////////////////////////////////////////////////////
172void bncNetQueryV2::waitForRequestResult(const QUrl& url, QByteArray& outData) {
173
174 // Send Request
175 // ------------
176 startRequestPrivate(url, "", true);
177
178 // Wait Loop
179 // ---------
180 _eventLoop->exec();
181
182 // Copy Data and Return
183 // --------------------
184 if (_reply) {
185 outData = _reply->readAll();
186 }
187}
188
189// Wait for next data
190////////////////////////////////////////////////////////////////////////////
191void bncNetQueryV2::waitForReadyRead(QByteArray& outData) {
192
193 // Wait Loop
194 // ---------
195 if (!_reply->bytesAvailable()) {
196 _eventLoop->exec();
197 }
198 if (!_reply) {
199 return;
200 }
201
202 // Check NTRIPv2 error code
203 // ------------------------
204 if (_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() != 200) {
205 _reply->abort();
206 }
207
208
209 // Append Data
210 // -----------
211 else {
212 outData.append(_reply->readAll());
213 }
214}
215
216// TSL/SSL
217////////////////////////////////////////////////////////////////////////////
218void bncNetQueryV2::slotSslErrors(QList<QSslError> errors) {
219
220 QString msg = "SSL Error: ";
221 QSslCertificate cert = _reply->sslConfiguration().peerCertificate();
222 if (!cert.isNull() &&
223 cert.issuerInfo(QSslCertificate::OrganizationalUnitName).count() &&
224 cert.issuerInfo(QSslCertificate::Organization).count()) {
225
226 msg += QString("Server Certificate Issued by:\n"
227 "%1\n%2\nCannot be verified\n")
228#if QT_VERSION >= 0x050000
229 .arg(cert.issuerInfo(QSslCertificate::OrganizationalUnitName).at(0))
230 .arg(cert.issuerInfo(QSslCertificate::Organization).at(0));
231#else
232 .arg(cert.issuerInfo(QSslCertificate::OrganizationalUnitName))
233 .arg(cert.issuerInfo(QSslCertificate::Organization));
234#endif
235 }
236
237 QListIterator<QSslError> it(errors);
238 while (it.hasNext()) {
239 const QSslError& err = it.next();
240 msg += err.errorString();
241 }
242 BNC_CORE->slotMessage(msg.toLatin1(), true);
243
244 if (_sslIgnoreErrors) {
245 _reply->ignoreSslErrors();
246 BNC_CORE->slotMessage("BNC ignores SSL errors as configured", true);
247 }
248 else {
249 stop();
250 }
251 return;
252}
Note: See TracBrowser for help on using the repository browser.