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

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

* empty log message *

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