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

Last change on this file since 7533 was 7513, checked in by weber, 9 years ago

Keyname 'ignoreSslErrors' changed to 'sslIngoreErrors'

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