|
| 1 | +#include <ESPAsyncHTTPClient.h> |
| 2 | + |
| 3 | +#define DEBUG(...) {} |
| 4 | + |
| 5 | +String& ByteString::copy(const void *data, unsigned int length) { |
| 6 | + if (!reserve(length)) { |
| 7 | + invalidate(); |
| 8 | + return *this; |
| 9 | + } |
| 10 | +#if defined (ESP32) || defined (NEW_WSTRING) |
| 11 | + setLen(length); |
| 12 | + memcpy(wbuffer(), data, length); |
| 13 | + wbuffer()[length] = 0; |
| 14 | +#else |
| 15 | + len = length; |
| 16 | + memcpy(buffer, data, length); |
| 17 | + buffer[length] = 0; |
| 18 | +#endif |
| 19 | + return *this; |
| 20 | +} |
| 21 | + |
| 22 | +void AsyncHTTPClient::initialize(String url) { |
| 23 | + // check for : (http: or https: |
| 24 | + int index = url.indexOf(':'); |
| 25 | + if (index < 0) { |
| 26 | + initialized = false; // This is not a URL |
| 27 | + } |
| 28 | + |
| 29 | + protocol = url.substring(0, index); |
| 30 | + DEBUG(protocol); |
| 31 | + port = 80; //Default |
| 32 | + if (index == 5) { |
| 33 | + port = 443; |
| 34 | + } |
| 35 | + |
| 36 | + url.remove(0, (index + 3)); // remove http:// or https:// |
| 37 | + |
| 38 | + index = url.indexOf('/'); |
| 39 | + String hostPart = url.substring(0, index); |
| 40 | + DEBUG(hostPart); |
| 41 | + url.remove(0, index); // remove hostPart part |
| 42 | + |
| 43 | + // get Authorization |
| 44 | + index = hostPart.indexOf('@'); |
| 45 | + |
| 46 | + if (index >= 0) { |
| 47 | + // auth info |
| 48 | + String auth = hostPart.substring(0, index); |
| 49 | + hostPart.remove(0, index + 1); // remove auth part including @ |
| 50 | + base64Authorization = base64::encode(auth); |
| 51 | + } |
| 52 | + |
| 53 | + // get port |
| 54 | + index = hostPart.indexOf(':'); |
| 55 | + if (index >= 0) { |
| 56 | + host = hostPart.substring(0, index); // hostname |
| 57 | + host.remove(0, (index + 1)); // remove hostname + : |
| 58 | + DEBUG(host); |
| 59 | + port = host.toInt(); // get port |
| 60 | + DEBUG(port); |
| 61 | + } else { |
| 62 | + host = hostPart; |
| 63 | + DEBUG(host); |
| 64 | + } |
| 65 | + uri = url; |
| 66 | +#if ASYNC_TCP_SSL_ENABLED |
| 67 | + initialized = protocol == "http" || protocol == "https"; |
| 68 | +#else |
| 69 | + initialized = protocol == "http"; |
| 70 | +#endif |
| 71 | + |
| 72 | + DEBUG(initialized); |
| 73 | + request = "GET " + uri + " HTTP/1.1\r\nHost: " + host + "\r\n\r\n"; |
| 74 | + |
| 75 | + DEBUG(request); |
| 76 | + initialized = true; |
| 77 | +} |
| 78 | + |
| 79 | +String AsyncHTTPClient::getBody() { |
| 80 | + if (statusCode == 200) { |
| 81 | + // This is not a good check for chunked transfer encoding - it should ignore case and spaces |
| 82 | + int chunkedIndex = response.indexOf("Transfer-Encoding: chunked"); |
| 83 | + int bodyStart = response.indexOf("\r\n\r\n") + 4; |
| 84 | + if (bodyStart <= 3) { |
| 85 | + return ""; |
| 86 | + } |
| 87 | + if (chunkedIndex != -1) { |
| 88 | + String body; |
| 89 | + const char *buf = response.c_str(); |
| 90 | + int chunkSize = strtol(buf + bodyStart, 0, 16); |
| 91 | + while (chunkSize) { |
| 92 | + // Move past chunk size |
| 93 | + bodyStart = response.indexOf("\r\n", bodyStart) + 2; |
| 94 | + body += response.substring(bodyStart, bodyStart + chunkSize); |
| 95 | + |
| 96 | + // Move past chunk |
| 97 | + bodyStart = response.indexOf("\r\n", bodyStart + chunkSize) + 2; |
| 98 | + chunkSize = strtol(buf + bodyStart, 0, 16); |
| 99 | + } |
| 100 | + |
| 101 | + return body; |
| 102 | + } else { |
| 103 | + return response.substring(bodyStart); |
| 104 | + } |
| 105 | + } else { |
| 106 | + return ""; |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +void AsyncHTTPClient::clientError(void *arg, AsyncClient *client, |
| 111 | + err_t error) { |
| 112 | + DEBUG("Connect Error"); |
| 113 | + AsyncHTTPClient *self = (AsyncHTTPClient*) arg; |
| 114 | + self->onFail("Connection error"); |
| 115 | + self->aClient = NULL; |
| 116 | + delete client; |
| 117 | +} |
| 118 | + |
| 119 | +void AsyncHTTPClient::clientDisconnect(void *arg, AsyncClient *client) { |
| 120 | + DEBUG("Disconnected"); |
| 121 | + AsyncHTTPClient *self = (AsyncHTTPClient*) arg; |
| 122 | + self->aClient = NULL; |
| 123 | + delete client; |
| 124 | +} |
| 125 | + |
| 126 | +void AsyncHTTPClient::clientData(void *arg, AsyncClient *client, void *data, size_t len) { |
| 127 | + DEBUG("Got response"); |
| 128 | + |
| 129 | + AsyncHTTPClient *self = (AsyncHTTPClient*) arg; |
| 130 | + self->response = ByteString(data, len); |
| 131 | + String status = self->response.substring(9, 12); |
| 132 | + self->statusCode = atoi(status.c_str()); |
| 133 | + DEBUG(status.c_str()); |
| 134 | + |
| 135 | + if (self->statusCode == 200) { |
| 136 | + self->onSuccess(); |
| 137 | + } else { |
| 138 | + self->onFail("Failed with code " + status); |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +void AsyncHTTPClient::clientConnect(void *arg, AsyncClient *client) { |
| 143 | + DEBUG("Connected"); |
| 144 | + |
| 145 | + AsyncHTTPClient *self = (AsyncHTTPClient*) arg; |
| 146 | + |
| 147 | + self->response.copy("", 0); |
| 148 | + self->statusCode = -1; |
| 149 | + |
| 150 | + // Clear oneError handler |
| 151 | + self->aClient->onError(NULL, NULL); |
| 152 | + |
| 153 | + // Set disconnect handler |
| 154 | + client->onDisconnect(clientDisconnect, self); |
| 155 | + |
| 156 | + client->onData(clientData, self); |
| 157 | + |
| 158 | + //send the request |
| 159 | + client->write(self->request.c_str()); |
| 160 | +} |
| 161 | + |
| 162 | +void AsyncHTTPClient::makeRequest(void (*success)(), void (*fail)(String msg)) { |
| 163 | + onFail = fail; |
| 164 | + |
| 165 | + if (!initialized) { |
| 166 | + fail("Not initialized"); |
| 167 | + return; |
| 168 | + } |
| 169 | + |
| 170 | + if (aClient) { //client already exists |
| 171 | + fail("Call taking forever"); |
| 172 | + return; |
| 173 | + } |
| 174 | + |
| 175 | + aClient = new AsyncClient(); |
| 176 | + if (!aClient) { //could not allocate client |
| 177 | + fail("Out of memory"); |
| 178 | + return; |
| 179 | + } |
| 180 | + |
| 181 | + onSuccess = success; |
| 182 | + |
| 183 | + aClient->onError(clientError, this); |
| 184 | + |
| 185 | + aClient->onConnect(clientConnect, this); |
| 186 | + bool connected = false; |
| 187 | +#if ASYNC_TCP_SSL_ENABLED |
| 188 | + if (protocol == "https") { |
| 189 | + DEBUG("Creating secure connection"); |
| 190 | + connected = aClient->connect(host.c_str(), port, true); |
| 191 | + } else { |
| 192 | + connected = aClient->connect(host.c_str(), port, false); |
| 193 | + } |
| 194 | +#else |
| 195 | + connected = aClient->connect(host.c_str(), port); |
| 196 | +#endif |
| 197 | + if (!connected) { |
| 198 | + DEBUG("Connect Fail"); |
| 199 | + fail("Connection failed"); |
| 200 | + AsyncClient * client = aClient; |
| 201 | + aClient = NULL; |
| 202 | + delete client; |
| 203 | + } |
| 204 | +} |
| 205 | + |
0 commit comments