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

Last change on this file since 9706 was 9706, checked in by stuerze, 2 years ago

minor changes

File size: 6.7 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 emit newMessage(_url.path().toLatin1().replace(0,1,"") +
77 ": NetQueryV2: server replied: " +
78 _reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toByteArray(),
79 true);
80 }
81 else {
82 _status = finished;
83 }
84}
85
86//
87////////////////////////////////////////////////////////////////////////////
88void bncNetQueryV2::slotProxyAuthenticationRequired(const QNetworkProxy&,
89 QAuthenticator*) {
90 emit newMessage("slotProxyAuthenticationRequired", true);
91}
92
93// Start request, block till the next read
94////////////////////////////////////////////////////////////////////////////
95void bncNetQueryV2::startRequest(const QUrl& url, const QByteArray& gga) {
96 startRequestPrivate(url, gga, false);
97}
98
99// Start request, block till the next read
100////////////////////////////////////////////////////////////////////////////
101void bncNetQueryV2::keepAliveRequest(const QUrl& url, const QByteArray& gga) {
102 startRequestPrivate(url, gga, false);
103}
104
105// Start Request (Private Method)
106////////////////////////////////////////////////////////////////////////////
107void bncNetQueryV2::startRequestPrivate(const QUrl& url, const QByteArray& gga,
108 bool full) {
109
110 _status = running;
111
112 // Default scheme and path
113 // -----------------------
114 _url = url;
115 if (_url.scheme().isEmpty()) {
116 if (_secure) {
117 _url.setScheme("https");
118 }
119 else {
120 _url.setScheme("http");
121 }
122 }
123 if (_url.path().isEmpty()) {
124 _url.setPath("/");
125 }
126
127 // Network Request
128 // ---------------
129 QNetworkRequest request;
130 bncSslConfig sslConfig;
131 request.setSslConfiguration(sslConfig);
132 request.setUrl(_url);
133 request.setRawHeader("Host" , _url.host().toLatin1());
134 request.setRawHeader("Ntrip-Version", "Ntrip/2.0");
135 request.setRawHeader("User-Agent" , "NTRIP BNC/" BNCVERSION " (" BNC_OS ")");
136 if (!_url.userName().isEmpty()) {
137 QString uName = QUrl::fromPercentEncoding(_url.userName().toLatin1());
138 QString passW = QUrl::fromPercentEncoding(_url.password().toLatin1());
139 request.setRawHeader("Authorization", "Basic " +
140 (uName + ":" + passW).toLatin1().toBase64());
141 }
142 if (!gga.isEmpty()) {
143 request.setRawHeader("Ntrip-GGA", gga);
144 }
145 request.setRawHeader("Connection" , "close");
146
147 if (_reply) {
148 delete _reply;
149 _reply = 0;
150 }
151 _reply = _manager->get(request);
152
153 // Connect Signals
154 // ---------------
155 connect(_reply, SIGNAL(finished()), this, SLOT(slotFinished()));
156 connect(_reply, SIGNAL(finished()), _eventLoop, SLOT(quit()));
157 connect(_reply, SIGNAL(sslErrors(QList<QSslError>)),
158 this, SLOT(slotSslErrors(QList<QSslError>)));
159 if (!full) {
160 connect(_reply, SIGNAL(readyRead()), _eventLoop, SLOT(quit()));
161 }
162}
163
164// Start Request, wait for its completion
165////////////////////////////////////////////////////////////////////////////
166void bncNetQueryV2::waitForRequestResult(const QUrl& url, QByteArray& outData) {
167
168 // Send Request
169 // ------------
170 startRequestPrivate(url, "", true);
171
172 // Wait Loop
173 // ---------
174 _eventLoop->exec();
175
176 // Copy Data and Return
177 // --------------------
178 if (_reply) {
179 outData = _reply->readAll();
180 }
181}
182
183// Wait for next data
184////////////////////////////////////////////////////////////////////////////
185void bncNetQueryV2::waitForReadyRead(QByteArray& outData) {
186
187 // Wait Loop
188 // ---------
189 if (!_reply->bytesAvailable()) {
190 _eventLoop->exec();
191 }
192
193 // Check NTRIPv2 error code
194 // ------------------------
195 if (_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() != 200) {
196 _reply->abort();
197 }
198
199 // Append Data
200 // -----------
201 else {
202 outData.append(_reply->readAll());
203 }
204}
205
206// TSL/SSL
207////////////////////////////////////////////////////////////////////////////
208void bncNetQueryV2::slotSslErrors(QList<QSslError> errors) {
209
210 QString msg = "SSL Error\n";
211 QSslCertificate cert = _reply->sslConfiguration().peerCertificate();
212 if (!cert.isNull()) {
213 msg += QString("Server Certificate Issued by:\n"
214 "%1\n%2\nCannot be verified\n")
215#if QT_VERSION >= 0x050000
216 .arg(cert.issuerInfo(QSslCertificate::OrganizationalUnitName).at(0))
217 .arg(cert.issuerInfo(QSslCertificate::Organization).at(0));
218#else
219 .arg(cert.issuerInfo(QSslCertificate::OrganizationalUnitName))
220 .arg(cert.issuerInfo(QSslCertificate::Organization));
221#endif
222 }
223 QListIterator<QSslError> it(errors);
224 while (it.hasNext()) {
225 const QSslError& err = it.next();
226 msg += "\n" + err.errorString();
227 }
228
229 BNC_CORE->slotMessage(msg.toLatin1(), true);
230
231 if (_sslIgnoreErrors) {
232 _reply->ignoreSslErrors();
233 }
234 else {
235 stop();
236 }
237}
Note: See TracBrowser for help on using the repository browser.