1 | /* -------------------------------------------------------------------------
|
---|
2 | * BKG NTRIP Client
|
---|
3 | * -------------------------------------------------------------------------
|
---|
4 | *
|
---|
5 | * Class: bncNetQueryV1
|
---|
6 | *
|
---|
7 | * Purpose: Blocking Network Requests (NTRIP Version 1)
|
---|
8 | *
|
---|
9 | * Author: L. Mervart
|
---|
10 | *
|
---|
11 | * Created: 27-Dec-2008
|
---|
12 | *
|
---|
13 | * Changes:
|
---|
14 | *
|
---|
15 | * -----------------------------------------------------------------------*/
|
---|
16 |
|
---|
17 | #include <iostream>
|
---|
18 | #include <iomanip>
|
---|
19 |
|
---|
20 | #include "bncnetqueryv1.h"
|
---|
21 | #include "bncsettings.h"
|
---|
22 | #include "bncversion.h"
|
---|
23 |
|
---|
24 | using namespace std;
|
---|
25 |
|
---|
26 | // Constructor
|
---|
27 | ////////////////////////////////////////////////////////////////////////////
|
---|
28 | bncNetQueryV1::bncNetQueryV1() {
|
---|
29 | _socket = 0;
|
---|
30 | _eventLoop = new QEventLoop(this);
|
---|
31 | _timeOut = 20000;
|
---|
32 | }
|
---|
33 |
|
---|
34 | // Destructor
|
---|
35 | ////////////////////////////////////////////////////////////////////////////
|
---|
36 | bncNetQueryV1::~bncNetQueryV1() {
|
---|
37 | delete _socket;
|
---|
38 | delete _eventLoop;
|
---|
39 | }
|
---|
40 |
|
---|
41 | //
|
---|
42 | ////////////////////////////////////////////////////////////////////////////
|
---|
43 | void bncNetQueryV1::stop() {
|
---|
44 | _eventLoop->quit();
|
---|
45 | #ifndef sparc
|
---|
46 | if (_socket) {
|
---|
47 | _socket->abort();
|
---|
48 | }
|
---|
49 | #endif
|
---|
50 | _status = finished;
|
---|
51 | }
|
---|
52 |
|
---|
53 | //
|
---|
54 | ////////////////////////////////////////////////////////////////////////////
|
---|
55 | void bncNetQueryV1::waitForRequestResult(const QUrl& url, QByteArray& outData){
|
---|
56 |
|
---|
57 | delete _socket;
|
---|
58 | _socket = new QTcpSocket();
|
---|
59 |
|
---|
60 | connect(_socket, SIGNAL(disconnected()), _eventLoop, SLOT(quit()));
|
---|
61 |
|
---|
62 | startRequestPrivate(url, "", true);
|
---|
63 |
|
---|
64 | QTimer::singleShot(10000, _eventLoop, SLOT(quit()));
|
---|
65 |
|
---|
66 | _eventLoop->exec();
|
---|
67 |
|
---|
68 | if (_socket) {
|
---|
69 | outData = _socket->readAll();
|
---|
70 | delete _socket; _socket = 0;
|
---|
71 | _status = finished;
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | //
|
---|
76 | ////////////////////////////////////////////////////////////////////////////
|
---|
77 | void bncNetQueryV1::waitForReadyRead(QByteArray& outData) {
|
---|
78 | if (_socket && _socket->state() == QAbstractSocket::ConnectedState) {
|
---|
79 | while (true) {
|
---|
80 | int nBytes = _socket->bytesAvailable();
|
---|
81 | if (nBytes > 0) {
|
---|
82 | outData = _socket->readAll();
|
---|
83 | return;
|
---|
84 | }
|
---|
85 | else if (!_socket->waitForReadyRead(_timeOut)) {
|
---|
86 | QString errStr = _socket->errorString();
|
---|
87 | if (errStr.isEmpty()) {
|
---|
88 | errStr = "Read timeout";
|
---|
89 | }
|
---|
90 | delete _socket;
|
---|
91 | _socket = 0;
|
---|
92 | _status = error;
|
---|
93 | emit newMessage(_url.path().toAscii().replace(0,1,"")
|
---|
94 | + ": " + errStr.toAscii(), true);
|
---|
95 | return;
|
---|
96 | }
|
---|
97 | }
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | // Connect to Caster, send the Request
|
---|
102 | ////////////////////////////////////////////////////////////////////////////
|
---|
103 | void bncNetQueryV1::startRequest(const QUrl& url, const QByteArray& gga) {
|
---|
104 | startRequestPrivate(url, gga, false);
|
---|
105 | }
|
---|
106 |
|
---|
107 | // Connect to Caster, send the Request
|
---|
108 | ////////////////////////////////////////////////////////////////////////////
|
---|
109 | void bncNetQueryV1::startRequestPrivate(const QUrl& url,
|
---|
110 | const QByteArray& gga,
|
---|
111 | bool sendRequestOnly) {
|
---|
112 |
|
---|
113 | _status = running;
|
---|
114 |
|
---|
115 | if (!sendRequestOnly) {
|
---|
116 | delete _socket;
|
---|
117 | _socket = new QTcpSocket();
|
---|
118 | }
|
---|
119 |
|
---|
120 | // Default scheme and path
|
---|
121 | // -----------------------
|
---|
122 | _url = url;
|
---|
123 | if (_url.scheme().isEmpty()) {
|
---|
124 | _url.setScheme("http");
|
---|
125 | }
|
---|
126 | if (_url.path().isEmpty()) {
|
---|
127 | _url.setPath("/");
|
---|
128 | }
|
---|
129 |
|
---|
130 | // Connect the Socket
|
---|
131 | // ------------------
|
---|
132 | bncSettings settings;
|
---|
133 | QString proxyHost = settings.value("proxyHost").toString();
|
---|
134 | int proxyPort = settings.value("proxyPort").toInt();
|
---|
135 |
|
---|
136 | if ( proxyHost.isEmpty() ) {
|
---|
137 | _socket->connectToHost(_url.host(), _url.port());
|
---|
138 | }
|
---|
139 | else {
|
---|
140 | _socket->connectToHost(proxyHost, proxyPort);
|
---|
141 | }
|
---|
142 | if (!_socket->waitForConnected(_timeOut)) {
|
---|
143 | delete _socket;
|
---|
144 | _socket = 0;
|
---|
145 | _status = error;
|
---|
146 | return;
|
---|
147 | }
|
---|
148 |
|
---|
149 | // Send Request
|
---|
150 | // ------------
|
---|
151 | QString uName = QUrl::fromPercentEncoding(_url.userName().toAscii());
|
---|
152 | QString passW = QUrl::fromPercentEncoding(_url.password().toAscii());
|
---|
153 | QByteArray userAndPwd;
|
---|
154 |
|
---|
155 | if(!uName.isEmpty() || !passW.isEmpty()) {
|
---|
156 | userAndPwd = "Authorization: Basic " + (uName.toAscii() + ":" +
|
---|
157 | passW.toAscii()).toBase64() + "\r\n";
|
---|
158 | }
|
---|
159 |
|
---|
160 | QByteArray reqStr;
|
---|
161 | if ( proxyHost.isEmpty() ) {
|
---|
162 | if (_url.path().indexOf("/") != 0) _url.setPath("/");
|
---|
163 | reqStr = "GET " + _url.path().toAscii() + " HTTP/1.0\r\n"
|
---|
164 | + "User-Agent: NTRIP BNC/" BNCVERSION " (" BNC_OS ")\r\n"
|
---|
165 | + "Host: " + _url.host().toAscii() + "\r\n"
|
---|
166 | + userAndPwd + "\r\n";
|
---|
167 | } else {
|
---|
168 | reqStr = "GET " + _url.toEncoded() + " HTTP/1.0\r\n"
|
---|
169 | + "User-Agent: NTRIP BNC/" BNCVERSION " (" BNC_OS ")\r\n"
|
---|
170 | + "Host: " + _url.host().toAscii() + "\r\n"
|
---|
171 | + userAndPwd + "\r\n";
|
---|
172 | }
|
---|
173 |
|
---|
174 | // NMEA string to handle VRS stream
|
---|
175 | // --------------------------------
|
---|
176 | if (!gga.isEmpty()) {
|
---|
177 | reqStr += gga + "\r\n";
|
---|
178 | }
|
---|
179 |
|
---|
180 | _socket->write(reqStr, reqStr.length());
|
---|
181 |
|
---|
182 | if (!_socket->waitForBytesWritten(_timeOut)) {
|
---|
183 | delete _socket;
|
---|
184 | _socket = 0;
|
---|
185 | _status = error;
|
---|
186 | emit newMessage(_url.path().toAscii().replace(0,1,"")
|
---|
187 | + ": Write timeout", true);
|
---|
188 | return;
|
---|
189 | }
|
---|
190 |
|
---|
191 | // Read Caster Response
|
---|
192 | // --------------------
|
---|
193 | if (!sendRequestOnly) {
|
---|
194 | bool proxyResponse = false;
|
---|
195 | QStringList response;
|
---|
196 | while (true) {
|
---|
197 | if (_socket->canReadLine()) {
|
---|
198 | QString line = _socket->readLine();
|
---|
199 |
|
---|
200 | if (line.indexOf("ICY 200 OK") == -1 &&
|
---|
201 | line.indexOf("HTTP") != -1 &&
|
---|
202 | line.indexOf("200 OK") != -1 ) {
|
---|
203 | proxyResponse = true;
|
---|
204 | }
|
---|
205 |
|
---|
206 | if (!proxyResponse && !line.trimmed().isEmpty()) {
|
---|
207 | response.push_back(line);
|
---|
208 | }
|
---|
209 |
|
---|
210 | if (line.trimmed().isEmpty()) {
|
---|
211 | if (proxyResponse) {
|
---|
212 | proxyResponse = false;
|
---|
213 | }
|
---|
214 | else {
|
---|
215 | break;
|
---|
216 | }
|
---|
217 | }
|
---|
218 |
|
---|
219 | if (line.indexOf("Unauthorized") != -1) {
|
---|
220 | break;
|
---|
221 | }
|
---|
222 |
|
---|
223 | if (!proxyResponse &&
|
---|
224 | line.indexOf("200 OK") != -1 &&
|
---|
225 | line.indexOf("SOURCETABLE") == -1) {
|
---|
226 | response.clear();
|
---|
227 | if (_socket->canReadLine()) {
|
---|
228 | _socket->readLine();
|
---|
229 | }
|
---|
230 | break;
|
---|
231 | }
|
---|
232 | }
|
---|
233 | else if (!_socket->waitForReadyRead(_timeOut)) {
|
---|
234 | delete _socket;
|
---|
235 | _socket = 0;
|
---|
236 | _status = error;
|
---|
237 | emit newMessage(_url.path().toAscii().replace(0,1,"")
|
---|
238 | + ": Response timeout", true);
|
---|
239 | return;
|
---|
240 | }
|
---|
241 | }
|
---|
242 | if (response.size() > 0) {
|
---|
243 | delete _socket;
|
---|
244 | _socket = 0;
|
---|
245 | _status = error;
|
---|
246 | emit newMessage(_url.path().toAscii().replace(0,1,"")
|
---|
247 | + ": Wrong caster response\n"
|
---|
248 | + response.join("").toAscii(), true);
|
---|
249 | }
|
---|
250 | }
|
---|
251 | }
|
---|
252 |
|
---|