-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathESPAsyncHTTPClient.cpp
205 lines (171 loc) · 4.9 KB
/
ESPAsyncHTTPClient.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <ESPAsyncHTTPClient.h>
#define DEBUG(...) {}
String& ByteString::copy(const void *data, unsigned int length) {
if (!reserve(length)) {
invalidate();
return *this;
}
#if defined (ESP32) || defined (NEW_WSTRING)
setLen(length);
memcpy(wbuffer(), data, length);
wbuffer()[length] = 0;
#else
len = length;
memcpy(buffer, data, length);
buffer[length] = 0;
#endif
return *this;
}
void AsyncHTTPClient::initialize(String url) {
// check for : (http: or https:
int index = url.indexOf(':');
if (index < 0) {
initialized = false; // This is not a URL
}
protocol = url.substring(0, index);
DEBUG(protocol);
port = 80; //Default
if (index == 5) {
port = 443;
}
url.remove(0, (index + 3)); // remove http:// or https://
index = url.indexOf('/');
String hostPart = url.substring(0, index);
DEBUG(hostPart);
url.remove(0, index); // remove hostPart part
// get Authorization
index = hostPart.indexOf('@');
if (index >= 0) {
// auth info
String auth = hostPart.substring(0, index);
hostPart.remove(0, index + 1); // remove auth part including @
base64Authorization = base64::encode(auth);
}
// get port
index = hostPart.indexOf(':');
if (index >= 0) {
host = hostPart.substring(0, index); // hostname
host.remove(0, (index + 1)); // remove hostname + :
DEBUG(host);
port = host.toInt(); // get port
DEBUG(port);
} else {
host = hostPart;
DEBUG(host);
}
uri = url;
#if ASYNC_TCP_SSL_ENABLED
initialized = protocol == "http" || protocol == "https";
#else
initialized = protocol == "http";
#endif
DEBUG(initialized);
request = "GET " + uri + " HTTP/1.1\r\nHost: " + host + "\r\nConnection: close\r\n\r\n";
DEBUG(request);
initialized = true;
}
String AsyncHTTPClient::getBody() {
if (statusCode == 200) {
// This is not a good check for chunked transfer encoding - it should ignore case and spaces
int chunkedIndex = response.indexOf("Transfer-Encoding: chunked");
int bodyStart = response.indexOf("\r\n\r\n") + 4;
if (bodyStart <= 3) {
return "";
}
if (chunkedIndex != -1) {
String body;
const char *buf = response.c_str();
int chunkSize = strtol(buf + bodyStart, 0, 16);
while (chunkSize) {
// Move past chunk size
bodyStart = response.indexOf("\r\n", bodyStart) + 2;
body += response.substring(bodyStart, bodyStart + chunkSize);
// Move past chunk
bodyStart = response.indexOf("\r\n", bodyStart + chunkSize) + 2;
chunkSize = strtol(buf + bodyStart, 0, 16);
}
return body;
} else {
return response.substring(bodyStart);
}
} else {
return "";
}
}
void AsyncHTTPClient::clientError(void *arg, AsyncClient *client,
err_t error) {
DEBUG("Connect Error");
AsyncHTTPClient *self = (AsyncHTTPClient*) arg;
self->onFail("Connection error");
self->aClient = NULL;
delete client;
}
void AsyncHTTPClient::clientDisconnect(void *arg, AsyncClient *client) {
DEBUG("Disconnected");
AsyncHTTPClient *self = (AsyncHTTPClient*) arg;
self->aClient = NULL;
delete client;
}
void AsyncHTTPClient::clientData(void *arg, AsyncClient *client, void *data, size_t len) {
DEBUG("Got response");
AsyncHTTPClient *self = (AsyncHTTPClient*) arg;
self->response = ByteString(data, len);
String status = self->response.substring(9, 12);
self->statusCode = atoi(status.c_str());
DEBUG(status.c_str());
if (self->statusCode == 200) {
self->onSuccess();
} else {
self->onFail("Failed with code " + status);
}
}
void AsyncHTTPClient::clientConnect(void *arg, AsyncClient *client) {
DEBUG("Connected");
AsyncHTTPClient *self = (AsyncHTTPClient*) arg;
self->response.copy("", 0);
self->statusCode = -1;
// Clear oneError handler
self->aClient->onError(NULL, NULL);
// Set disconnect handler
client->onDisconnect(clientDisconnect, self);
client->onData(clientData, self);
//send the request
client->write(self->request.c_str());
}
void AsyncHTTPClient::makeRequest(void (*success)(), void (*fail)(String msg)) {
onFail = fail;
if (!initialized) {
fail("Not initialized");
return;
}
if (aClient) { //client already exists
fail("Call taking forever");
return;
}
aClient = new AsyncClient();
if (!aClient) { //could not allocate client
fail("Out of memory");
return;
}
onSuccess = success;
aClient->onError(clientError, this);
aClient->onConnect(clientConnect, this);
bool connected = false;
#if ASYNC_TCP_SSL_ENABLED
if (protocol == "https") {
DEBUG("Creating secure connection");
connected = aClient->connect(host.c_str(), port, true);
} else {
connected = aClient->connect(host.c_str(), port, false);
}
#else
connected = aClient->connect(host.c_str(), port);
#endif
if (!connected) {
DEBUG("Connect Fail");
fail("Connection failed");
AsyncClient * client = aClient;
aClient = NULL;
delete client;
}
}