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 | delete _socket;
|
---|
68 | _socket = 0;
|
---|
69 | _status = error;
|
---|
70 | emit newMessage(_url.path().toAscii() + ": Read timeout", true);
|
---|
71 | return;
|
---|
72 | }
|
---|
73 | }
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | // Connect to Caster, send the Request
|
---|
78 | ////////////////////////////////////////////////////////////////////////////
|
---|
79 | void bncNetQueryV1::startRequest(const QUrl& url, const QByteArray& gga) {
|
---|
80 |
|
---|
81 | _status = running;
|
---|
82 |
|
---|
83 | delete _socket;
|
---|
84 | _socket = new QTcpSocket();
|
---|
85 |
|
---|
86 | // Default scheme and path
|
---|
87 | // -----------------------
|
---|
88 | _url = url;
|
---|
89 | if (_url.scheme().isEmpty()) {
|
---|
90 | _url.setScheme("http");
|
---|
91 | }
|
---|
92 | if (_url.path().isEmpty()) {
|
---|
93 | _url.setPath("/");
|
---|
94 | }
|
---|
95 |
|
---|
96 | // Connect the Socket
|
---|
97 | // ------------------
|
---|
98 | bncSettings settings;
|
---|
99 | QString proxyHost = settings.value("proxyHost").toString();
|
---|
100 | int proxyPort = settings.value("proxyPort").toInt();
|
---|
101 |
|
---|
102 | if ( proxyHost.isEmpty() ) {
|
---|
103 | _socket->connectToHost(_url.host(), _url.port());
|
---|
104 | }
|
---|
105 | else {
|
---|
106 | _socket->connectToHost(proxyHost, proxyPort);
|
---|
107 | }
|
---|
108 | if (!_socket->waitForConnected(_timeOut)) {
|
---|
109 | delete _socket;
|
---|
110 | _socket = 0;
|
---|
111 | _status = error;
|
---|
112 | return;
|
---|
113 | }
|
---|
114 |
|
---|
115 | // Send Request
|
---|
116 | // ------------
|
---|
117 | QString uName = QUrl::fromPercentEncoding(_url.userName().toAscii());
|
---|
118 | QString passW = QUrl::fromPercentEncoding(_url.password().toAscii());
|
---|
119 | QByteArray userAndPwd;
|
---|
120 |
|
---|
121 | if(!uName.isEmpty() || !passW.isEmpty()) {
|
---|
122 | userAndPwd = "Authorization: Basic " + (uName.toAscii() + ":" +
|
---|
123 | passW.toAscii()).toBase64() + "\r\n";
|
---|
124 | }
|
---|
125 |
|
---|
126 | QByteArray reqStr;
|
---|
127 | if ( proxyHost.isEmpty() ) {
|
---|
128 | if (_url.path().indexOf("/") != 0) _url.setPath("/");
|
---|
129 | reqStr = "GET " + _url.path().toAscii() + " HTTP/1.0\r\n"
|
---|
130 | + "User-Agent: NTRIP BNC/" BNCVERSION "\r\n"
|
---|
131 | + userAndPwd + "\r\n";
|
---|
132 | } else {
|
---|
133 | reqStr = "GET " + _url.toEncoded() + " HTTP/1.0\r\n"
|
---|
134 | + "User-Agent: NTRIP BNC/" BNCVERSION "\r\n"
|
---|
135 | + "Host: " + _url.host().toAscii() + "\r\n"
|
---|
136 | + userAndPwd + "\r\n";
|
---|
137 | }
|
---|
138 |
|
---|
139 | // NMEA string to handle VRS stream
|
---|
140 | // --------------------------------
|
---|
141 | if (!gga.isEmpty()) {
|
---|
142 | reqStr += gga + "\r\n";
|
---|
143 | }
|
---|
144 |
|
---|
145 | _socket->write(reqStr, reqStr.length());
|
---|
146 |
|
---|
147 | if (!_socket->waitForBytesWritten(_timeOut)) {
|
---|
148 | delete _socket;
|
---|
149 | _socket = 0;
|
---|
150 | _status = error;
|
---|
151 | emit newMessage(_url.path().toAscii() + ": Write timeout", true);
|
---|
152 | return;
|
---|
153 | }
|
---|
154 |
|
---|
155 | // Read Caster Response
|
---|
156 | // --------------------
|
---|
157 | bool proxyResponse = false;
|
---|
158 | QStringList response;
|
---|
159 | while (true) {
|
---|
160 | QByteArray line = this->readNextLine();
|
---|
161 | if (_status == error) {
|
---|
162 | break;
|
---|
163 | }
|
---|
164 |
|
---|
165 | if (line.indexOf("ICY 200 OK") == -1 &&
|
---|
166 | line.indexOf("HTTP") != -1 &&
|
---|
167 | line.indexOf("200 OK") != -1 ) {
|
---|
168 | proxyResponse = true;
|
---|
169 | }
|
---|
170 |
|
---|
171 | cout << proxyResponse << ": " << line.data(); cout.flush();
|
---|
172 |
|
---|
173 | if (!proxyResponse && !line.trimmed().isEmpty()) {
|
---|
174 | response.push_back(line);
|
---|
175 | }
|
---|
176 |
|
---|
177 | if (line.indexOf("Unauthorized") != -1) {
|
---|
178 | break;
|
---|
179 | }
|
---|
180 |
|
---|
181 | if (line.trimmed().isEmpty()) {
|
---|
182 | if (proxyResponse) {
|
---|
183 | proxyResponse = false;
|
---|
184 | }
|
---|
185 | else {
|
---|
186 | break;
|
---|
187 | }
|
---|
188 | }
|
---|
189 |
|
---|
190 | if (!proxyResponse &&
|
---|
191 | line.indexOf("200 OK") != -1 &&
|
---|
192 | line.indexOf("SOURCETABLE") == -1) {
|
---|
193 | response.clear();
|
---|
194 | break;
|
---|
195 | }
|
---|
196 | }
|
---|
197 | if (response.size() > 0) {
|
---|
198 | delete _socket;
|
---|
199 | _socket = 0;
|
---|
200 | _status = error;
|
---|
201 | emit newMessage(_url.path().toAscii() + ": Wrong caster response\n" +
|
---|
202 | response.join("").toAscii(), true);
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 | //
|
---|
207 | ////////////////////////////////////////////////////////////////////////////
|
---|
208 | QByteArray bncNetQueryV1::readNextLine() {
|
---|
209 | QByteArray buffer;
|
---|
210 | char ch1 = ' ';
|
---|
211 | while (true) {
|
---|
212 | if (_socket->bytesAvailable()) {
|
---|
213 | char ch2;
|
---|
214 | _socket->read(&ch2, 1);
|
---|
215 | if (ch2 != '\r') {
|
---|
216 | buffer += ch2;
|
---|
217 | }
|
---|
218 | if ( (ch2 == '\n' && ch1 == '\r') ||
|
---|
219 | (ch2 == '\r' && ch1 == '\n') ) {
|
---|
220 | return buffer;
|
---|
221 | }
|
---|
222 | ch1 = ch2;
|
---|
223 | }
|
---|
224 | else if (!_socket->waitForReadyRead(_timeOut)) {
|
---|
225 | delete _socket;
|
---|
226 | _socket = 0;
|
---|
227 | _status = error;
|
---|
228 | emit newMessage(_url.path().toAscii() + ": Response timeout", true);
|
---|
229 | return "";
|
---|
230 | }
|
---|
231 | }
|
---|
232 | }
|
---|
233 |
|
---|