source: ntrip/trunk/BNC/bncnetqueryv2.cpp@ 3359

Last change on this file since 3359 was 3359, 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 request.setSslConfiguration(bncSslConfig::Instance());
133 request.setUrl(_url);
134 request.setRawHeader("Host" , _url.host().toAscii());
135 request.setRawHeader("Ntrip-Version", "Ntrip/2.0");
136 request.setRawHeader("User-Agent" , "NTRIP BNC/"BNCVERSION);
137 if (!_url.userName().isEmpty()) {
138 QString uName = QUrl::fromPercentEncoding(_url.userName().toAscii());
139 QString passW = QUrl::fromPercentEncoding(_url.password().toAscii());
140 request.setRawHeader("Authorization", "Basic " +
141 (uName + ":" + passW).toAscii().toBase64());
142 }
143 if (!gga.isEmpty()) {
144 request.setRawHeader("Ntrip-GGA", gga);
145 }
146 request.setRawHeader("Connection" , "close");
147
148 delete _reply;
149 _reply = _manager->get(request);
150
151 // Connect Signals
152 // ---------------
153 connect(_reply, SIGNAL(finished()), this, SLOT(slotFinished()));
154 connect(_reply, SIGNAL(finished()), _eventLoop, SLOT(quit()));
155 connect(_reply, SIGNAL(sslErrors(QList<QSslError>)),
156 this, SLOT(slotSslErrors(QList<QSslError>)));
157 if (!full) {
158 connect(_reply, SIGNAL(readyRead()), _eventLoop, SLOT(quit()));
159 }
160}
161
162// Start Request, wait for its completion
163////////////////////////////////////////////////////////////////////////////
164void bncNetQueryV2::waitForRequestResult(const QUrl& url, QByteArray& outData) {
165
166 // Send Request
167 // ------------
168 startRequestPrivate(url, "", true);
169
170 // Wait Loop
171 // ---------
172 _eventLoop->exec();
173
174 // Copy Data and Return
175 // --------------------
176 outData = _reply->readAll();
177}
178
179// Wait for next data
180////////////////////////////////////////////////////////////////////////////
181void bncNetQueryV2::waitForReadyRead(QByteArray& outData) {
182
183 // Wait Loop
184 // ---------
185 if (!_reply->bytesAvailable()) {
186 _eventLoop->exec();
187 }
188
189 // Check NTRIPv2 error code
190 // ------------------------
191 if (_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() != 200) {
192 _reply->abort();
193 }
194
195 // Append Data
196 // -----------
197 else {
198 outData.append(_reply->readAll());
199 }
200}
201
202// TSL/SSL
203////////////////////////////////////////////////////////////////////////////
204void bncNetQueryV2::slotSslErrors(QList<QSslError> errors) {
205
206 QString msg = "SSL Error\n";
207 QSslCertificate cert = _reply->sslConfiguration().peerCertificate();
208 if (!cert.isNull()) {
209 msg += QString("Server Certificate Issued by:\n"
210 "%1\n%2\nCannot be verified\n")
211 .arg(cert.issuerInfo(QSslCertificate::OrganizationalUnitName))
212 .arg(cert.issuerInfo(QSslCertificate::Organization));
213 }
214 else {
215 QListIterator<QSslError> it(errors);
216 while (it.hasNext()) {
217 const QSslError& err = it.next();
218 msg += "\n" + err.errorString();
219 }
220 }
221
222 ((bncApp*)qApp)->slotMessage(msg.toAscii(), true);
223
224 if (_ignoreSslErrors) {
225 _reply->ignoreSslErrors();
226 }
227 else {
228 stop();
229 }
230}
Note: See TracBrowser for help on using the repository browser.