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