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