-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathasync_http.cpp
178 lines (143 loc) · 5.17 KB
/
async_http.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
#include "async_http.h"
AsyncHTTP::AsyncHTTP() : sendID(0), requestOngoing(false), lastRequestTime(0) {}
AsyncHTTP::~AsyncHTTP() {}
unsigned long AsyncHTTP::send2http(const std::string auth, const std::string host, int port, const std::string query)
{
sendID++;
Request req = {auth, host, port, query, sendID};
requestQueue.push(req);
//LOG_V("[http] [%lu] %s:%i%s\n", sendID, host.c_str(), port, query.c_str());
return sendID;
}
unsigned long AsyncHTTP::send2http_url(const std::string url)
{
std::string protocol;
std::string host;
int port = 80;
std::string query = "/";
size_t protocolPos = url.find("://");
if (protocolPos != std::string::npos) {
protocol = url.substr(0, protocolPos);
if (protocol == "https") {
port = 443;
}
}
size_t hostStart = (protocolPos == std::string::npos) ? 0 : protocolPos + 3;
size_t hostEnd = url.find('/', hostStart);
size_t portPos = url.find(':', hostStart);
if (portPos != std::string::npos && portPos < hostEnd) {
host = url.substr(hostStart, portPos - hostStart);
port = std::stoi(url.substr(portPos + 1, hostEnd - portPos - 1));
} else {
host = url.substr(hostStart, hostEnd - hostStart);
}
if (hostEnd != std::string::npos) {
query = url.substr(hostEnd);
}
sendID++;
Request req = {"", host, port, query, sendID};
requestQueue.push(req);
//LOG_V("[http] [%lu] %s:%i%s\n", sendID, host.c_str(), port, query.c_str());
return sendID;
}
void AsyncHTTP::handleData(AsyncClient *c, unsigned long sendID, void *data, size_t len)
{
const char *payloadStart = static_cast<const char *>(data);
const char *jsonStart = strstr(payloadStart, "{");
if (!jsonStart) return;
const char *jsonEnd = strrchr(payloadStart, '}');
if (!jsonEnd || jsonEnd < jsonStart) return;
size_t jsonLen = (jsonEnd - jsonStart) + 1;
if (jsonLen > len) return;
std::string jsonData(jsonStart, jsonLen);
if (!jsonData.empty()) {
jsonData.erase(std::remove_if(jsonData.begin(), jsonData.end(), ::isspace), jsonData.end());
JsonDocument doc;
DeserializationError error = deserializeJson(doc, jsonData);
if (error) {
LOG_I("[http] #< [%lu] nonjson: %s\n", sendID, error.c_str());
} else {
LOG_I("[http] #< [%lu] json: %s\n", sendID, jsonData.c_str());
if (onDataCallbackJson_) {
onDataCallbackJson_(sendID, doc);
}
}
} else {
LOG_V("[http] #< [%lu] ERR response not JSON\n", sendID);
}
}
void AsyncHTTP::onDataCallback(std::function<void(unsigned long, const std::string &)> callback)
{
onDataCallback_ = callback;
}
void AsyncHTTP::onDataCallbackJson(std::function<void(unsigned long, JsonDocument &doc)> callback)
{
onDataCallbackJson_ = callback;
}
unsigned long AsyncHTTP::sendRequest(const Request &req)
{
AsyncClient *client = new AsyncClient();
if (!client)
return 0;
std::string request =
"GET " + req.query + " HTTP/1.1\r\n" +
"Host: " + req.host + ":" + std::to_string(req.port) + "\r\n" +
"Authorization: Basic " + req.auth + "\r\n" +
"User-Agent: iqESP\r\n" +
"Connection: close\r\n\r\n";
unsigned long requestID = req.id;
client->onError([this, requestID](void *arg, AsyncClient *client, int error) {
LOG_I("[http] ## [%lu] onError\n", requestID);
delete client;
requestOngoing = false;
},
nullptr);
client->onConnect([this, request, requestID](void *arg, AsyncClient *client) {
LOG_V("[http] ## [%lu] onConnect\n", requestID);
client->onError(nullptr, nullptr);
client->onDisconnect([this, requestID](void *arg, AsyncClient *client) {
LOG_V("[http] ## [%lu] onDisconnect\n", requestID);
delete client;
requestOngoing = false;
},
nullptr);
client->onData([this, requestID](void *arg, AsyncClient *client, void *data, size_t len) {
LOG_V("[http] #< [%lu] onData\n", requestID);
handleData(client, requestID, data, len);
client->close();
},
nullptr);
client->write(request.c_str());
lastRequestTime = millis();
delay(1);
},
nullptr);
LOG_I("[http] #> [%lu] %s:%i%s\n", requestID, req.host.c_str(), req.port, req.query.c_str());
if (!client->connect(req.host.c_str(), req.port)) {
LOG_I("[http] #> [%lu] Connect ERR\n", requestID);
delete client;
return 0;
}
return requestID;
}
void AsyncHTTP::loop()
{
if (!requestOngoing && !requestQueue.empty()) {
processQueue();
}
if (requestOngoing && millis() - lastRequestTime > 10000) {
LOG_I("[http] ## Timeout - closing connection");
requestOngoing = false;
processQueue();
}
}
void AsyncHTTP::processQueue()
{
if (!requestQueue.empty()) {
Request req = requestQueue.front();
requestQueue.pop();
sendRequest(req);
lastRequestTime = millis();
requestOngoing = true;
}
}