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 | }
|
---|
31 |
|
---|
32 | // Destructor
|
---|
33 | ////////////////////////////////////////////////////////////////////////////
|
---|
34 | bncNetQueryV1::~bncNetQueryV1() {
|
---|
35 | delete _socket;
|
---|
36 | }
|
---|
37 |
|
---|
38 | //
|
---|
39 | ////////////////////////////////////////////////////////////////////////////
|
---|
40 | void bncNetQueryV1::waitForRequestResult(const QUrl& url, QByteArray& outData) {
|
---|
41 | }
|
---|
42 |
|
---|
43 | //
|
---|
44 | ////////////////////////////////////////////////////////////////////////////
|
---|
45 | void bncNetQueryV1::waitForReadyRead(QByteArray& outData) {
|
---|
46 |
|
---|
47 | }
|
---|
48 |
|
---|
49 | // Connect to Caster, send the Request
|
---|
50 | ////////////////////////////////////////////////////////////////////////////
|
---|
51 | void bncNetQueryV1::startRequest(const QUrl& url, const QByteArray& gga) {
|
---|
52 |
|
---|
53 | const int timeOut = 5000;
|
---|
54 |
|
---|
55 | _status = running;
|
---|
56 |
|
---|
57 | delete _socket;
|
---|
58 | _socket = new QTcpSocket();
|
---|
59 |
|
---|
60 | // Default scheme and path
|
---|
61 | // -----------------------
|
---|
62 | QUrl urlLoc(url);
|
---|
63 | if (urlLoc.scheme().isEmpty()) {
|
---|
64 | urlLoc.setScheme("http");
|
---|
65 | }
|
---|
66 | if (urlLoc.path().isEmpty()) {
|
---|
67 | urlLoc.setPath("/");
|
---|
68 | }
|
---|
69 |
|
---|
70 | // Connect the Socket
|
---|
71 | // ------------------
|
---|
72 | QSettings settings;
|
---|
73 | QString proxyHost = settings.value("proxyHost").toString();
|
---|
74 | int proxyPort = settings.value("proxyPort").toInt();
|
---|
75 |
|
---|
76 | if ( proxyHost.isEmpty() ) {
|
---|
77 | _socket->connectToHost(urlLoc.host(), urlLoc.port());
|
---|
78 | }
|
---|
79 | else {
|
---|
80 | _socket->connectToHost(proxyHost, proxyPort);
|
---|
81 | }
|
---|
82 | if (!_socket->waitForConnected(timeOut)) {
|
---|
83 | delete _socket;
|
---|
84 | _socket = 0;
|
---|
85 | _status = error;
|
---|
86 | return;
|
---|
87 | }
|
---|
88 |
|
---|
89 | // Send Request
|
---|
90 | // ------------
|
---|
91 | QString uName = QUrl::fromPercentEncoding(urlLoc.userName().toAscii());
|
---|
92 | QString passW = QUrl::fromPercentEncoding(urlLoc.password().toAscii());
|
---|
93 | QByteArray userAndPwd;
|
---|
94 |
|
---|
95 | if(!uName.isEmpty() || !passW.isEmpty()) {
|
---|
96 | userAndPwd = "Authorization: Basic " + (uName.toAscii() + ":" +
|
---|
97 | passW.toAscii()).toBase64() + "\r\n";
|
---|
98 | }
|
---|
99 |
|
---|
100 | QByteArray reqStr;
|
---|
101 | if ( proxyHost.isEmpty() ) {
|
---|
102 | if (urlLoc.path().indexOf("/") != 0) urlLoc.setPath("/");
|
---|
103 | reqStr = "GET " + urlLoc.path().toAscii() + " HTTP/1.0\r\n"
|
---|
104 | + "User-Agent: NTRIP BNC/" BNCVERSION "\r\n"
|
---|
105 | + userAndPwd + "\r\n";
|
---|
106 | } else {
|
---|
107 | reqStr = "GET " + urlLoc.toEncoded() + " HTTP/1.0\r\n"
|
---|
108 | + "User-Agent: NTRIP BNC/" BNCVERSION "\r\n"
|
---|
109 | + "Host: " + urlLoc.host().toAscii() + "\r\n"
|
---|
110 | + userAndPwd + "\r\n";
|
---|
111 | }
|
---|
112 |
|
---|
113 | // NMEA string to handle VRS stream
|
---|
114 | // --------------------------------
|
---|
115 | if (!gga.isEmpty()) {
|
---|
116 | reqStr += gga + "\r\n";
|
---|
117 | }
|
---|
118 |
|
---|
119 | _socket->write(reqStr, reqStr.length());
|
---|
120 |
|
---|
121 | if (!_socket->waitForBytesWritten(timeOut)) {
|
---|
122 | delete _socket;
|
---|
123 | _socket = 0;
|
---|
124 | _status = error;
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|