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

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