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 |
|
---|
23 | using namespace std;
|
---|
24 |
|
---|
25 | #define BNCVERSION "1.7"
|
---|
26 |
|
---|
27 | // Constructor
|
---|
28 | ////////////////////////////////////////////////////////////////////////////
|
---|
29 | bncNetQueryV1::bncNetQueryV1() {
|
---|
30 | _socket = 0;
|
---|
31 | _timeOut = 20000;
|
---|
32 | }
|
---|
33 |
|
---|
34 | // Destructor
|
---|
35 | ////////////////////////////////////////////////////////////////////////////
|
---|
36 | bncNetQueryV1::~bncNetQueryV1() {
|
---|
37 | delete _socket;
|
---|
38 | }
|
---|
39 |
|
---|
40 | //
|
---|
41 | ////////////////////////////////////////////////////////////////////////////
|
---|
42 | void bncNetQueryV1::stop() {
|
---|
43 | #ifndef sparc
|
---|
44 | if (_socket) {
|
---|
45 | _socket->abort();
|
---|
46 | }
|
---|
47 | #endif
|
---|
48 | _status = finished;
|
---|
49 | }
|
---|
50 |
|
---|
51 | //
|
---|
52 | ////////////////////////////////////////////////////////////////////////////
|
---|
53 | void bncNetQueryV1::waitForRequestResult(const QUrl&, QByteArray&) {
|
---|
54 | }
|
---|
55 |
|
---|
56 | //
|
---|
57 | ////////////////////////////////////////////////////////////////////////////
|
---|
58 | void bncNetQueryV1::waitForReadyRead(QByteArray& outData) {
|
---|
59 | if (_socket && _socket->state() == QAbstractSocket::ConnectedState) {
|
---|
60 | while (true) {
|
---|
61 | int nBytes = _socket->bytesAvailable();
|
---|
62 | if (nBytes > 0) {
|
---|
63 | outData = _socket->readAll();
|
---|
64 | return;
|
---|
65 | }
|
---|
66 | else if (!_socket->waitForReadyRead(_timeOut)) {
|
---|
67 | QString errStr = _socket->errorString();
|
---|
68 | if (errStr.isEmpty()) {
|
---|
69 | errStr = "Read timeout";
|
---|
70 | }
|
---|
71 | delete _socket;
|
---|
72 | _socket = 0;
|
---|
73 | _status = error;
|
---|
74 | emit newMessage(_url.path().toAscii().replace(0,1,"")
|
---|
75 | + ": " + errStr.toAscii(), true);
|
---|
76 | return;
|
---|
77 | }
|
---|
78 | }
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | // Connect to Caster, send the Request
|
---|
83 | ////////////////////////////////////////////////////////////////////////////
|
---|
84 | void bncNetQueryV1::startRequest(const QUrl& url, const QByteArray& gga) {
|
---|
85 |
|
---|
86 | _status = running;
|
---|
87 |
|
---|
88 | delete _socket;
|
---|
89 | _socket = new QTcpSocket();
|
---|
90 |
|
---|
91 | // Default scheme and path
|
---|
92 | // -----------------------
|
---|
93 | _url = url;
|
---|
94 | if (_url.scheme().isEmpty()) {
|
---|
95 | _url.setScheme("http");
|
---|
96 | }
|
---|
97 | if (_url.path().isEmpty()) {
|
---|
98 | _url.setPath("/");
|
---|
99 | }
|
---|
100 |
|
---|
101 | // Connect the Socket
|
---|
102 | // ------------------
|
---|
103 | bncSettings settings;
|
---|
104 | QString proxyHost = settings.value("proxyHost").toString();
|
---|
105 | int proxyPort = settings.value("proxyPort").toInt();
|
---|
106 |
|
---|
107 | if ( proxyHost.isEmpty() ) {
|
---|
108 | _socket->connectToHost(_url.host(), _url.port());
|
---|
109 | }
|
---|
110 | else {
|
---|
111 | _socket->connectToHost(proxyHost, proxyPort);
|
---|
112 | }
|
---|
113 | if (!_socket->waitForConnected(_timeOut)) {
|
---|
114 | delete _socket;
|
---|
115 | _socket = 0;
|
---|
116 | _status = error;
|
---|
117 | return;
|
---|
118 | }
|
---|
119 |
|
---|
120 | // Send Request
|
---|
121 | // ------------
|
---|
122 | QString uName = QUrl::fromPercentEncoding(_url.userName().toAscii());
|
---|
123 | QString passW = QUrl::fromPercentEncoding(_url.password().toAscii());
|
---|
124 | QByteArray userAndPwd;
|
---|
125 |
|
---|
126 | if(!uName.isEmpty() || !passW.isEmpty()) {
|
---|
127 | userAndPwd = "Authorization: Basic " + (uName.toAscii() + ":" +
|
---|
128 | passW.toAscii()).toBase64() + "\r\n";
|
---|
129 | }
|
---|
130 |
|
---|
131 | QByteArray reqStr;
|
---|
132 | if ( proxyHost.isEmpty() ) {
|
---|
133 | if (_url.path().indexOf("/") != 0) _url.setPath("/");
|
---|
134 | reqStr = "GET " + _url.path().toAscii() + " HTTP/1.0\r\n"
|
---|
135 | + "User-Agent: NTRIP BNC/" BNCVERSION "\r\n"
|
---|
136 | + userAndPwd + "\r\n";
|
---|
137 | } else {
|
---|
138 | reqStr = "GET " + _url.toEncoded() + " HTTP/1.0\r\n"
|
---|
139 | + "User-Agent: NTRIP BNC/" BNCVERSION "\r\n"
|
---|
140 | + "Host: " + _url.host().toAscii() + "\r\n"
|
---|
141 | + userAndPwd + "\r\n";
|
---|
142 | }
|
---|
143 |
|
---|
144 | // NMEA string to handle VRS stream
|
---|
145 | // --------------------------------
|
---|
146 | if (!gga.isEmpty()) {
|
---|
147 | reqStr += gga + "\r\n";
|
---|
148 | }
|
---|
149 |
|
---|
150 | _socket->write(reqStr, reqStr.length());
|
---|
151 |
|
---|
152 | if (!_socket->waitForBytesWritten(_timeOut)) {
|
---|
153 | delete _socket;
|
---|
154 | _socket = 0;
|
---|
155 | _status = error;
|
---|
156 | emit newMessage(_url.path().toAscii().replace(0,1,"")
|
---|
157 | + ": Write timeout", true);
|
---|
158 | return;
|
---|
159 | }
|
---|
160 |
|
---|
161 | // Read Caster Response
|
---|
162 | // --------------------
|
---|
163 | bool proxyResponse = false;
|
---|
164 | QStringList response;
|
---|
165 | while (true) {
|
---|
166 | if (_socket->canReadLine()) {
|
---|
167 | QString line = _socket->readLine();
|
---|
168 |
|
---|
169 | if (line.indexOf("ICY 200 OK") == -1 &&
|
---|
170 | line.indexOf("HTTP") != -1 &&
|
---|
171 | line.indexOf("200 OK") != -1 ) {
|
---|
172 | proxyResponse = true;
|
---|
173 | }
|
---|
174 |
|
---|
175 | if (!proxyResponse && !line.trimmed().isEmpty()) {
|
---|
176 | response.push_back(line);
|
---|
177 | }
|
---|
178 |
|
---|
179 | if (line.trimmed().isEmpty()) {
|
---|
180 | if (proxyResponse) {
|
---|
181 | proxyResponse = false;
|
---|
182 | }
|
---|
183 | else {
|
---|
184 | break;
|
---|
185 | }
|
---|
186 | }
|
---|
187 |
|
---|
188 | if (line.indexOf("Unauthorized") != -1) {
|
---|
189 | break;
|
---|
190 | }
|
---|
191 |
|
---|
192 | if (!proxyResponse &&
|
---|
193 | line.indexOf("200 OK") != -1 &&
|
---|
194 | line.indexOf("SOURCETABLE") == -1) {
|
---|
195 | response.clear();
|
---|
196 | if (_socket->canReadLine()) {
|
---|
197 | _socket->readLine();
|
---|
198 | }
|
---|
199 | break;
|
---|
200 | }
|
---|
201 | }
|
---|
202 | else if (!_socket->waitForReadyRead(_timeOut)) {
|
---|
203 | delete _socket;
|
---|
204 | _socket = 0;
|
---|
205 | _status = error;
|
---|
206 | emit newMessage(_url.path().toAscii().replace(0,1,"")
|
---|
207 | + ": Response timeout", true);
|
---|
208 | return;
|
---|
209 | }
|
---|
210 | }
|
---|
211 | if (response.size() > 0) {
|
---|
212 | delete _socket;
|
---|
213 | _socket = 0;
|
---|
214 | _status = error;
|
---|
215 | emit newMessage(_url.path().toAscii().replace(0,1,"")
|
---|
216 | + ": Wrong caster response\n"
|
---|
217 | + response.join("").toAscii(), true);
|
---|
218 | }
|
---|
219 | }
|
---|
220 |
|
---|