source: ntrip/branches/BNC_LM/bncnetqueryv2.cpp@ 6199

Last change on this file since 6199 was 3363, checked in by mervart, 13 years ago
File size: 6.6 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 _ignoreSslErrors =
41 (Qt::CheckState(settings.value("ignoreSslErrors").toInt()) == Qt::Checked);
42
43 if (_secure && !QSslSocket::supportsSsl()) {
44 ((bncApp*)qApp)->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 (Private Method)
97////////////////////////////////////////////////////////////////////////////
98void bncNetQueryV2::startRequestPrivate(const QUrl& url, const QByteArray& gga,
99 bool full) {
100
101 _status = running;
102
103 // Default scheme and path
104 // -----------------------
105 _url = url;
106 if (_url.scheme().isEmpty()) {
107 if (_secure) {
108 _url.setScheme("https");
109 }
110 else {
111 _url.setScheme("http");
112 }
113 }
114 if (_url.path().isEmpty()) {
115 _url.setPath("/");
116 }
117
118 // Proxy Settings
119 // --------------
120 bncSettings settings;
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
129 // Network Request
130 // ---------------
131 QNetworkRequest request;
132 bncSslConfig sslConfig;
133 request.setSslConfiguration(sslConfig);
134 request.setUrl(_url);
135 request.setRawHeader("Host" , _url.host().toAscii());
136 request.setRawHeader("Ntrip-Version", "Ntrip/2.0");
137 request.setRawHeader("User-Agent" , "NTRIP BNC/"BNCVERSION);
138 if (!_url.userName().isEmpty()) {
139 QString uName = QUrl::fromPercentEncoding(_url.userName().toAscii());
140 QString passW = QUrl::fromPercentEncoding(_url.password().toAscii());
141 request.setRawHeader("Authorization", "Basic " +
142 (uName + ":" + passW).toAscii().toBase64());
143 }
144 if (!gga.isEmpty()) {
145 request.setRawHeader("Ntrip-GGA", gga);
146 }
147 request.setRawHeader("Connection" , "close");
148
149 delete _reply;
150 _reply = _manager->get(request);
151
152 // Connect Signals
153 // ---------------
154 connect(_reply, SIGNAL(finished()), this, SLOT(slotFinished()));
155 connect(_reply, SIGNAL(finished()), _eventLoop, SLOT(quit()));
156 connect(_reply, SIGNAL(sslErrors(QList<QSslError>)),
157 this, SLOT(slotSslErrors(QList<QSslError>)));
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 // ------------
169 startRequestPrivate(url, "", true);
170
171 // Wait Loop
172 // ---------
173 _eventLoop->exec();
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()) {
187 _eventLoop->exec();
188 }
189
190 // Check NTRIPv2 error code
191 // ------------------------
192 if (_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() != 200) {
193 _reply->abort();
194 }
195
196 // Append Data
197 // -----------
198 else {
199 outData.append(_reply->readAll());
200 }
201}
202
203// TSL/SSL
204////////////////////////////////////////////////////////////////////////////
205void bncNetQueryV2::slotSslErrors(QList<QSslError> errors) {
206
207 QString msg = "SSL Error\n";
208 QSslCertificate cert = _reply->sslConfiguration().peerCertificate();
209 if (!cert.isNull()) {
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));
214 }
215 QListIterator<QSslError> it(errors);
216 while (it.hasNext()) {
217 const QSslError& err = it.next();
218 msg += "\n" + err.errorString();
219 }
220
221 ((bncApp*)qApp)->slotMessage(msg.toAscii(), true);
222
223 if (_ignoreSslErrors) {
224 _reply->ignoreSslErrors();
225 }
226 else {
227 stop();
228 }
229}
Note: See TracBrowser for help on using the repository browser.