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