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

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