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

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