source: ntrip/trunk/BNC/bncnetqueryv1.cpp@ 1602

Last change on this file since 1602 was 1597, checked in by mervart, 16 years ago

* empty log message *

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