-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBridgeHttpClient.cpp
189 lines (148 loc) · 4.36 KB
/
BridgeHttpClient.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
Copyright (c) 2016, 2021 Imre Horvath. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "BridgeHttpClient.h"
BridgeHttpClient::BridgeHttpClient()
: _isInsecureEnabled(false),
_user(NULL),
_passwd(NULL),
_extraHeaderIdx(0) {
}
void BridgeHttpClient::get(const char *url) {
request("GET", url, NULL);
}
void BridgeHttpClient::post(const char *url, const char *data) {
request("POST", url, data);
}
void BridgeHttpClient::put(const char *url, const char *data) {
request("PUT", url, data);
}
void BridgeHttpClient::del(const char *url) {
request("DELETE", url, NULL);
}
void BridgeHttpClient::getAsync(const char *url) {
request("GET", url, NULL, true);
}
void BridgeHttpClient::postAsync(const char *url, const char *data) {
request("POST", url, data, true);
}
void BridgeHttpClient::putAsync(const char *url, const char *data) {
request("PUT", url, data, true);
}
void BridgeHttpClient::delAsync(const char *url) {
request("DELETE", url, NULL, true);
}
bool BridgeHttpClient::finished() {
return !running();
}
unsigned int BridgeHttpClient::getResponseCode() {
Process p;
p.runShellCommand("head -n 1 " + _tempFileName + " | cut -d' ' -f2");
return p.parseInt();
}
String BridgeHttpClient::getResponseHeaders() {
String responseHeaders;
Process p;
p.runShellCommand("tail -n +2 " + _tempFileName);
while (p.available() > 0) {
char c = p.read();
responseHeaders += c;
}
responseHeaders.trim();
return responseHeaders;
}
void BridgeHttpClient::enableInsecure() {
_isInsecureEnabled = true;
}
int BridgeHttpClient::addHeader(const char *header) {
if (_extraHeaderIdx < maxNumberOfExtraHeaders) {
_extraHeaders[_extraHeaderIdx++] = header;
return 0;
}
return -1;
}
void BridgeHttpClient::basicAuth(const char *user, const char *passwd) {
_user = user;
_passwd = passwd;
}
void BridgeHttpClient::clearHeaders() {
_extraHeaderIdx = 0;
}
void BridgeHttpClient::clearAuth() {
_user = _passwd = NULL;
}
bool BridgeHttpClient::getResponseHeaderValue(const String& header, String& value) {
// Cache the response headers for subsequent calls
if (_cachedRespHeaders.length() == 0) {
_cachedRespHeaders = getResponseHeaders();
}
// Handle the case when the header is not found
int startOfHeader = _cachedRespHeaders.indexOf(header);
if (startOfHeader == -1) {
return false; // Return false if the header was not found
}
// Get the header value
int startOfValue = _cachedRespHeaders.indexOf(':', startOfHeader) + 1;
String respValue = _cachedRespHeaders.substring(startOfValue,
_cachedRespHeaders.indexOf('\n', startOfValue));
respValue.trim();
value = respValue;
return true;
}
void BridgeHttpClient::startRequest() {
clearHeaders();
clearAuth();
_isInsecureEnabled = false;
}
void BridgeHttpClient::request(const char *verb, const char *url, const char *data, bool async) {
Process p;
p.runShellCommand("mktemp");
_tempFileName = p.readStringUntil('\n');
clearCachedRespHeaders();
begin("curl");
addParameter("-D");
addParameter(_tempFileName);
if (verb != "GET") {
addParameter("-X");
addParameter(verb);
}
if (_isInsecureEnabled) {
addParameter("-k");
}
if (_user && _passwd) {
String auth;
auth += _user;
auth += ":";
auth += _passwd;
addParameter("-u");
addParameter(auth);
}
for (int i = 0; i < _extraHeaderIdx; i++) {
addParameter("-H");
addParameter(_extraHeaders[i]);
}
if (data != NULL) {
addParameter("-d");
addParameter(data);
}
addParameter(url);
if (async) {
runAsynchronously();
} else {
(void) run();
}
}
void BridgeHttpClient::clearCachedRespHeaders() {
_cachedRespHeaders = "";
}