diff --git a/changelog.md b/changelog.md index 0b26cfb..c3e5f1d 100644 --- a/changelog.md +++ b/changelog.md @@ -12,6 +12,7 @@ ## Table of Contents * [Changelog](#changelog) + * [Releases v1.10.0](#releases-v1100) * [Releases v1.9.2](#releases-v192) * [Releases v1.9.1](#releases-v191) * [Releases v1.9.0](#releases-v190) @@ -42,6 +43,11 @@ ## Changelog +### Releases v1.10.0 + +1. Fix bug. +2. Clean up + ### Releases v1.9.2 1. Not try to reconnect to the same host:port after connected. Check [setReuse feature #12](https://github.com/khoih-prog/AsyncHTTPSRequest_Generic/issues/12) diff --git a/examples/AsyncCustomHeader_STM32/AsyncCustomHeader_STM32.ino b/examples/AsyncCustomHeader_STM32/AsyncCustomHeader_STM32.ino index 1d72e82..c32fece 100644 --- a/examples/AsyncCustomHeader_STM32/AsyncCustomHeader_STM32.ino +++ b/examples/AsyncCustomHeader_STM32/AsyncCustomHeader_STM32.ino @@ -24,8 +24,8 @@ //char GET_ServerAddress[] = "192.168.2.110/"; char GET_ServerAddress[] = "http://worldtimeapi.org/api/timezone/America/Toronto.txt"; -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.9.2" -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1009002 +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.10.0" +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1010000 // 600s = 10 minutes to not flooding, 60s in testing #define HTTP_REQUEST_INTERVAL_MS 60000 //600000 diff --git a/examples/AsyncDweetGet_STM32/AsyncDweetGet_STM32.ino b/examples/AsyncDweetGet_STM32/AsyncDweetGet_STM32.ino index 5429b9c..6618e18 100644 --- a/examples/AsyncDweetGet_STM32/AsyncDweetGet_STM32.ino +++ b/examples/AsyncDweetGet_STM32/AsyncDweetGet_STM32.ino @@ -35,8 +35,8 @@ const char GET_ServerAddress[] = "dweet.io"; // use your own thing name here String dweetName = "/dweet/for/currentSecond?second="; -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.9.2" -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1009002 +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.10.0" +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1010000 // 600s = 10 minutes to not flooding, 60s in testing #define HTTP_REQUEST_INTERVAL_MS 60000 //600000 diff --git a/examples/AsyncDweetPost_STM32/AsyncDweetPost_STM32.ino b/examples/AsyncDweetPost_STM32/AsyncDweetPost_STM32.ino index 6663518..f804bd1 100644 --- a/examples/AsyncDweetPost_STM32/AsyncDweetPost_STM32.ino +++ b/examples/AsyncDweetPost_STM32/AsyncDweetPost_STM32.ino @@ -29,8 +29,8 @@ const char POST_ServerAddress[] = "dweet.io"; // use your own thing name here String dweetName = "/dweet/for/pinA0-Read?"; -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.9.2" -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1009002 +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.10.0" +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1010000 // 600s = 10 minutes to not flooding, 60s in testing #define HTTP_REQUEST_INTERVAL_MS 60000 //600000 diff --git a/examples/AsyncHTTPMultiRequests_ESP/AsyncHTTPMultiRequests_ESP.ino b/examples/AsyncHTTPMultiRequests_ESP/AsyncHTTPMultiRequests_ESP.ino index 7e4bd2f..d0c25ff 100644 --- a/examples/AsyncHTTPMultiRequests_ESP/AsyncHTTPMultiRequests_ESP.ino +++ b/examples/AsyncHTTPMultiRequests_ESP/AsyncHTTPMultiRequests_ESP.ino @@ -44,8 +44,8 @@ #error This code is intended to run on the ESP8266 or ESP32 platform! Please check your Tools->Board setting. #endif -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.9.2" -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1009002 +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.10.0" +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1010000 // Level from 0-4 #define ASYNC_HTTP_DEBUG_PORT Serial diff --git a/examples/AsyncHTTPRequest_ESP/AsyncHTTPRequest_ESP.ino b/examples/AsyncHTTPRequest_ESP/AsyncHTTPRequest_ESP.ino index f382366..cb1eb67 100644 --- a/examples/AsyncHTTPRequest_ESP/AsyncHTTPRequest_ESP.ino +++ b/examples/AsyncHTTPRequest_ESP/AsyncHTTPRequest_ESP.ino @@ -46,7 +46,7 @@ // Level from 0-4 #define ASYNC_HTTP_DEBUG_PORT Serial -#define _ASYNC_HTTP_LOGLEVEL_ 3 +#define _ASYNC_HTTP_LOGLEVEL_ 4 // 300s = 5 minutes to not flooding #define HTTP_REQUEST_INTERVAL 60 //300 @@ -65,8 +65,8 @@ const char* password = "your_pass"; #include #endif -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.9.2" -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1009002 +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.10.0" +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1010000 // To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error #include // https://github.com/khoih-prog/AsyncHTTPRequest_Generic diff --git a/examples/AsyncHTTPRequest_ESP8266_Ethernet/AsyncHTTPRequest_ESP8266_Ethernet.ino b/examples/AsyncHTTPRequest_ESP8266_Ethernet/AsyncHTTPRequest_ESP8266_Ethernet.ino index 3fe3e69..f4330f1 100644 --- a/examples/AsyncHTTPRequest_ESP8266_Ethernet/AsyncHTTPRequest_ESP8266_Ethernet.ino +++ b/examples/AsyncHTTPRequest_ESP8266_Ethernet/AsyncHTTPRequest_ESP8266_Ethernet.ino @@ -95,8 +95,8 @@ using TCPClient = WiFiClient; ////////////////////////////////////////////////////////// -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.9.2" -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1009002 +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.10.0" +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1010000 // To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error #include // https://github.com/khoih-prog/AsyncHTTPRequest_Generic diff --git a/examples/AsyncHTTPRequest_ESP_WiFiManager/AsyncHTTPRequest_ESP_WiFiManager.ino b/examples/AsyncHTTPRequest_ESP_WiFiManager/AsyncHTTPRequest_ESP_WiFiManager.ino index 5e7991d..f34712b 100644 --- a/examples/AsyncHTTPRequest_ESP_WiFiManager/AsyncHTTPRequest_ESP_WiFiManager.ino +++ b/examples/AsyncHTTPRequest_ESP_WiFiManager/AsyncHTTPRequest_ESP_WiFiManager.ino @@ -49,8 +49,8 @@ #define ESP_ASYNC_WIFIMANAGER_VERSION_MIN_TARGET "ESPAsync_WiFiManager v1.15.0" #define ESP_ASYNC_WIFIMANAGER_VERSION_MIN 1015000 -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.9.2" -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1009002 +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.10.0" +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1010000 // Level from 0-4 #define ASYNC_HTTP_DEBUG_PORT Serial diff --git a/examples/AsyncHTTPRequest_STM32/AsyncHTTPRequest_STM32.ino b/examples/AsyncHTTPRequest_STM32/AsyncHTTPRequest_STM32.ino index 6e070d6..593aa1d 100644 --- a/examples/AsyncHTTPRequest_STM32/AsyncHTTPRequest_STM32.ino +++ b/examples/AsyncHTTPRequest_STM32/AsyncHTTPRequest_STM32.ino @@ -42,8 +42,8 @@ #include "defines.h" -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.9.2" -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1009002 +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.10.0" +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1010000 // 600s = 10 minutes to not flooding, 60s in testing #define HTTP_REQUEST_INTERVAL_MS 60000 //600000 diff --git a/examples/AsyncSimpleGET_STM32/AsyncSimpleGET_STM32.ino b/examples/AsyncSimpleGET_STM32/AsyncSimpleGET_STM32.ino index 1c3bc71..f035d6d 100644 --- a/examples/AsyncSimpleGET_STM32/AsyncSimpleGET_STM32.ino +++ b/examples/AsyncSimpleGET_STM32/AsyncSimpleGET_STM32.ino @@ -24,8 +24,8 @@ //char GET_ServerAddress[] = "ipv4bot.whatismyipaddress.com/"; char GET_ServerAddress[] = "http://worldtimeapi.org/api/timezone/America/Toronto.txt"; -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.9.2" -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1009002 +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.10.0" +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1010000 // 600s = 10 minutes to not flooding, 60s in testing #define HTTP_REQUEST_INTERVAL_MS 60000 //600000 diff --git a/examples/AsyncWebClientRepeating_STM32/AsyncWebClientRepeating_STM32.ino b/examples/AsyncWebClientRepeating_STM32/AsyncWebClientRepeating_STM32.ino index da4cf3e..33eb434 100644 --- a/examples/AsyncWebClientRepeating_STM32/AsyncWebClientRepeating_STM32.ino +++ b/examples/AsyncWebClientRepeating_STM32/AsyncWebClientRepeating_STM32.ino @@ -26,8 +26,8 @@ const char GET_ServerAddress[] = "arduino.tips"; // GET location String GET_Location = "/asciilogo.txt"; -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.9.2" -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1009002 +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.10.0" +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1010000 // 600s = 10 minutes to not flooding, 60s in testing #define HTTP_REQUEST_INTERVAL_MS 60000 //600000 diff --git a/examples/WT32_ETH01/AsyncHTTPMultiRequests_WT32_ETH01/AsyncHTTPMultiRequests_WT32_ETH01.ino b/examples/WT32_ETH01/AsyncHTTPMultiRequests_WT32_ETH01/AsyncHTTPMultiRequests_WT32_ETH01.ino index 2fb494e..78216ef 100644 --- a/examples/WT32_ETH01/AsyncHTTPMultiRequests_WT32_ETH01/AsyncHTTPMultiRequests_WT32_ETH01.ino +++ b/examples/WT32_ETH01/AsyncHTTPMultiRequests_WT32_ETH01/AsyncHTTPMultiRequests_WT32_ETH01.ino @@ -57,8 +57,8 @@ #include // https://github.com/khoih-prog/WebServer_WT32_ETH01 -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.9.2" -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1009002 +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.10.0" +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1010000 // To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error #include // https://github.com/khoih-prog/AsyncHTTPRequest_Generic diff --git a/examples/WT32_ETH01/AsyncHTTPRequest_WT32_ETH01/AsyncHTTPRequest_WT32_ETH01.ino b/examples/WT32_ETH01/AsyncHTTPRequest_WT32_ETH01/AsyncHTTPRequest_WT32_ETH01.ino index c011a93..44cd9b4 100644 --- a/examples/WT32_ETH01/AsyncHTTPRequest_WT32_ETH01/AsyncHTTPRequest_WT32_ETH01.ino +++ b/examples/WT32_ETH01/AsyncHTTPRequest_WT32_ETH01/AsyncHTTPRequest_WT32_ETH01.ino @@ -57,9 +57,8 @@ #include // https://github.com/khoih-prog/WebServer_WT32_ETH01 -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.9.2" -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1009002 - +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.10.0" +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1010000 // To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error #include // https://github.com/khoih-prog/AsyncHTTPRequest_Generic diff --git a/examples/multiFileProject/multiFileProject.ino b/examples/multiFileProject/multiFileProject.ino index 85b77a1..e2a2156 100644 --- a/examples/multiFileProject/multiFileProject.ino +++ b/examples/multiFileProject/multiFileProject.ino @@ -19,8 +19,8 @@ #error This code is intended to run on the ESP8266, ESP32 or STM32 platform! Please check your Tools->Board setting. #endif -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.9.2" -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1009002 +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.10.0" +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1010000 #include "multiFileProject.h" diff --git a/library.json b/library.json index 8f7b8ab..6878a91 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name":"AsyncHTTPRequest_Generic", - "version": "1.9.2", + "version": "1.10.0", "description":"Simple Async HTTP Request library, supporting GET, POST, PUT, PATCH, DELETE and HEAD, on top of AsyncTCP libraries, such as AsyncTCP, ESPAsyncTCP, AsyncTCP_STM32, etc.. for ESP32 (including ESP32_S2, ESP32_S3 and ESP32_C3), WT32_ETH01 (ESP32 + LAN8720), ESP8266 (WiFi, W5x00 or ENC28J60) and currently STM32 with built-in LAN8742A Ethernet.", "keywords":"communication, async, tcp, http, ESP8266, ESP32, ESP32-S2, ESP32-S3, ESP32-C3, wt32-eth01, stm32, ethernet, wifi, lan8742a, nucleo-144, stm32f7, W5x00, ENC28J60", "authors": [ diff --git a/library.properties b/library.properties index dbbaa2c..450ff5e 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=AsyncHTTPRequest_Generic -version=1.9.2 +version=1.10.0 author=Bob Lemaire,Khoi Hoang maintainer=Khoi Hoang license=GPLv3 diff --git a/src/AsyncHTTPRequest_Debug_Generic.h b/src/AsyncHTTPRequest_Debug_Generic.h index af25dd8..6f5fbf6 100644 --- a/src/AsyncHTTPRequest_Debug_Generic.h +++ b/src/AsyncHTTPRequest_Debug_Generic.h @@ -18,7 +18,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . - Version: 1.9.2 + Version: 1.10.0 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -32,6 +32,7 @@ 1.9.0 K Hoang 30/08/2022 Fix bug. Improve debug messages. Optimize code 1.9.1 K Hoang 09/09/2022 Fix ESP32 chipID for example `AsyncHTTPRequest_ESP_WiFiManager` 1.9.2 K Hoang 18/10/2022 Not try to reconnect to the same host:port after connected + 1.10.0 K Hoang 20/10/2022 Fix bug. Clean up *****************************************************************************************************************************/ #pragma once diff --git a/src/AsyncHTTPRequest_Generic.h b/src/AsyncHTTPRequest_Generic.h index 5cf57f1..a47d3ec 100644 --- a/src/AsyncHTTPRequest_Generic.h +++ b/src/AsyncHTTPRequest_Generic.h @@ -18,7 +18,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . - Version: 1.9.2 + Version: 1.10.0 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -32,6 +32,7 @@ 1.9.0 K Hoang 30/08/2022 Fix bug. Improve debug messages. Optimize code 1.9.1 K Hoang 09/09/2022 Fix ESP32 chipID for example `AsyncHTTPRequest_ESP_WiFiManager` 1.9.2 K Hoang 18/10/2022 Not try to reconnect to the same host:port after connected + 1.10.0 K Hoang 20/10/2022 Fix bug. Clean up *****************************************************************************************************************************/ #pragma once diff --git a/src/AsyncHTTPRequest_Generic.hpp b/src/AsyncHTTPRequest_Generic.hpp index 40200bd..53fe091 100644 --- a/src/AsyncHTTPRequest_Generic.hpp +++ b/src/AsyncHTTPRequest_Generic.hpp @@ -18,7 +18,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . - Version: 1.9.2 + Version: 1.10.0 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -32,6 +32,7 @@ 1.9.0 K Hoang 30/08/2022 Fix bug. Improve debug messages. Optimize code 1.9.1 K Hoang 09/09/2022 Fix ESP32 chipID for example `AsyncHTTPRequest_ESP_WiFiManager` 1.9.2 K Hoang 18/10/2022 Not try to reconnect to the same host:port after connected + 1.10.0 K Hoang 20/10/2022 Fix bug. Clean up *****************************************************************************************************************************/ #pragma once @@ -39,18 +40,24 @@ #ifndef ASYNC_HTTP_REQUEST_GENERIC_HPP #define ASYNC_HTTP_REQUEST_GENERIC_HPP -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION "AsyncHTTPRequest_Generic v1.9.2" +//////////////////////////////////////// + +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION "AsyncHTTPRequest_Generic v1.10.0" #define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MAJOR 1 -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MINOR 9 -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_PATCH 2 +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MINOR 10 +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_PATCH 0 -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_INT 1009002 +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_INT 1010000 + +//////////////////////////////////////// #include #include "AsyncHTTPRequest_Debug_Generic.h" +//////////////////////////////////////// + #ifndef DEBUG_IOTA_PORT #define DEBUG_IOTA_PORT Serial #endif @@ -61,11 +68,20 @@ #define DEBUG_IOTA_HTTP_SET false #endif +//////////////////////////////////////// // KH add #define SAFE_DELETE(object) if (object) { delete object;} #define SAFE_DELETE_ARRAY(object) if (object) { delete[] object;} +#define ASYNC_HTTP_PREFIX "HTTP://" +#define ASYNC_HTTP_PORT 80 + +#define ASYNC_HTTPS_PREFIX "HTTPS://" +#define ASYNC_HTTPS_PORT 443 + +//////////////////////////////////////// + #if ESP32 #include @@ -87,9 +103,9 @@ #define _AHTTP_lock #define _AHTTP_unlock -#elif ( defined(STM32F0) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) ||defined(STM32F4) || defined(STM32F7) || \ - defined(STM32L0) || defined(STM32L1) || defined(STM32L4) || defined(STM32H7) ||defined(STM32G0) || defined(STM32G4) || \ - defined(STM32WB) || defined(STM32MP1) ) +#elif ( defined(STM32F0) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) ||defined(STM32F4) || \ + defined(STM32F7) || defined(STM32L0) || defined(STM32L1) || defined(STM32L4) || defined(STM32H7) || \ + defined(STM32G0) || defined(STM32G4) || defined(STM32WB) || defined(STM32MP1) ) #include "STM32AsyncTCP.h" @@ -100,10 +116,12 @@ #endif +//////////////////////////////////////// + #include // Merge xbuf -//////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////// struct xseg { @@ -111,6 +129,8 @@ struct xseg uint8_t data[]; }; +//////////////////////////////////////// + class xbuf: public Print { public: @@ -204,7 +224,7 @@ class xbuf: public Print }; -//////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////// #define DEBUG_HTTP(format,...) if(_debug){\ DEBUG_IOTA_PORT.printf("Debug(%3ld): ", millis()-_requestStartTime);\ @@ -212,6 +232,8 @@ class xbuf: public Print #define DEFAULT_RX_TIMEOUT 3 // Seconds for timeout +//////////////////////////////////////// + #define HTTPCODE_CONNECTION_REFUSED (-1) #define HTTPCODE_SEND_HEADER_FAILED (-2) #define HTTPCODE_SEND_PAYLOAD_FAILED (-3) @@ -224,6 +246,8 @@ class xbuf: public Print #define HTTPCODE_STREAM_WRITE (-10) #define HTTPCODE_TIMEOUT (-11) +//////////////////////////////////////// + typedef enum { readyStateUnsent = 0, // Client created, open not yet called @@ -232,62 +256,68 @@ typedef enum readyStateLoading = 3, // receiving, partial data available readyStateDone = 4 // Request complete, all data available. } reqStates; - + +//////////////////////////////////////// + class AsyncHTTPRequest { - struct header + struct header + { + header* next; + char* name; + char* value; + + header(): next(nullptr), name(nullptr), value(nullptr) + {}; + + ~header() { - header* next; - char* name; - char* value; - - header(): next(nullptr), name(nullptr), value(nullptr) - {}; - - ~header() - { - SAFE_DELETE_ARRAY(name) - SAFE_DELETE_ARRAY(value) - SAFE_DELETE(next) - //delete[] name; - //delete[] value; - //delete next; - } - }; + SAFE_DELETE_ARRAY(name) + SAFE_DELETE_ARRAY(value) + SAFE_DELETE(next) + //delete[] name; + //delete[] value; + //delete next; + } + }; struct URL { - char *buffer; - char *scheme; - char *host; - int port; - char *path; - char *query; + char *buffer; + char *scheme; + char *host; + int port; + char *path; + char *query; + + URL(): buffer(nullptr), scheme(nullptr), host(nullptr), + port(80), path(nullptr), query(nullptr) + {}; - URL(): buffer(nullptr), scheme(nullptr), host(nullptr), - port(80), path(nullptr), query(nullptr) - {}; - - ~URL() - { - SAFE_DELETE_ARRAY(buffer) - SAFE_DELETE_ARRAY(scheme) - SAFE_DELETE_ARRAY(host) - SAFE_DELETE_ARRAY(path) - SAFE_DELETE_ARRAY(query) - } - }; - - typedef std::function readyStateChangeCB; - typedef std::function onDataCB; + ~URL() + { + SAFE_DELETE_ARRAY(buffer) + SAFE_DELETE_ARRAY(scheme) + SAFE_DELETE_ARRAY(host) + SAFE_DELETE_ARRAY(path) + SAFE_DELETE_ARRAY(query) + } + }; + typedef std::function readyStateChangeCB; + typedef std::function onDataCB; + + //////////////////////////////////////// + public: AsyncHTTPRequest(); ~AsyncHTTPRequest(); //External functions in typical order of use: - //__________________________________________________________________________________________________________*/ + + //////////////////////////////////////// + void setDebug(bool); // Turn debug message on/off bool debug(); // is debug on or off? @@ -341,16 +371,23 @@ class AsyncHTTPRequest size_t responseRead(uint8_t* buffer, size_t len); // Read response into buffer uint32_t elapsedTime(); // Elapsed time of in progress transaction or last completed (ms) String version(); // Version of AsyncHTTPRequest - //___________________________________________________________________________________________________________________________________ + + //////////////////////////////////////// private: - // New in v1.1.1 bool _requestReadyToSend; - ////// - - // New in v1.1.0 - typedef enum { HTTPmethodGET, HTTPmethodPOST, HTTPmethodPUT, HTTPmethodPATCH, HTTPmethodDELETE, HTTPmethodHEAD, HTTPmethodMAX } HTTPmethod; + + typedef enum + { + HTTPmethodGET, + HTTPmethodPOST, + HTTPmethodPUT, + HTTPmethodPATCH, + HTTPmethodDELETE, + HTTPmethodHEAD, + HTTPmethodMAX + } HTTPmethod; HTTPmethod _HTTPmethod; @@ -415,4 +452,6 @@ class AsyncHTTPRequest bool _collectHeaders(); }; +//////////////////////////////////////// + #endif // ASYNC_HTTP_REQUEST_GENERIC_HPP diff --git a/src/AsyncHTTPRequest_Impl_Generic.h b/src/AsyncHTTPRequest_Impl_Generic.h index 597b1cc..0357671 100644 --- a/src/AsyncHTTPRequest_Impl_Generic.h +++ b/src/AsyncHTTPRequest_Impl_Generic.h @@ -18,7 +18,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . - Version: 1.9.2 + Version: 1.10.0 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -32,6 +32,7 @@ 1.9.0 K Hoang 30/08/2022 Fix bug. Improve debug messages. Optimize code 1.9.1 K Hoang 09/09/2022 Fix ESP32 chipID for example `AsyncHTTPRequest_ESP_WiFiManager` 1.9.2 K Hoang 18/10/2022 Not try to reconnect to the same host:port after connected + 1.10.0 K Hoang 20/10/2022 Fix bug. Clean up *****************************************************************************************************************************/ #pragma once @@ -42,38 +43,43 @@ #define CANT_SEND_BAD_REQUEST F("Can't send() bad request") // Merge xbuf -//////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////// xbuf::xbuf(const uint16_t segSize) : _head(nullptr), _tail(nullptr), _used(0), _free(0), _offset(0) { _segSize = (segSize + 3) & -4;//((segSize + 3) >> 2) << 2; } -//******************************************************************************************************************* +//////////////////////////////////////// + xbuf::~xbuf() { flush(); } -//******************************************************************************************************************* +//////////////////////////////////////// + size_t xbuf::write(const uint8_t byte) { return write((uint8_t*) &byte, 1); } -//******************************************************************************************************************* +//////////////////////////////////////// + size_t xbuf::write(const char* buf) { return write((uint8_t*)buf, strlen(buf)); } -//******************************************************************************************************************* +//////////////////////////////////////// + size_t xbuf::write(const String& string) { return write((uint8_t*)string.c_str(), string.length()); } -//******************************************************************************************************************* +//////////////////////////////////////// + size_t xbuf::write(const uint8_t* buf, const size_t len) { size_t supply = len; @@ -95,7 +101,8 @@ size_t xbuf::write(const uint8_t* buf, const size_t len) return len; } -//******************************************************************************************************************* +//////////////////////////////////////// + size_t xbuf::write(xbuf* buf, const size_t len) { size_t supply = len; @@ -124,7 +131,8 @@ size_t xbuf::write(xbuf* buf, const size_t len) return read; } -//******************************************************************************************************************* +//////////////////////////////////////// + uint8_t xbuf::read() { uint8_t byte = 0; @@ -133,7 +141,8 @@ uint8_t xbuf::read() return byte; } -//******************************************************************************************************************* +//////////////////////////////////////// + uint8_t xbuf::peek() { uint8_t byte = 0; @@ -142,7 +151,8 @@ uint8_t xbuf::peek() return byte; } -//******************************************************************************************************************* +//////////////////////////////////////// + size_t xbuf::read(uint8_t* buf, const size_t len) { size_t read = 0; @@ -172,7 +182,8 @@ size_t xbuf::read(uint8_t* buf, const size_t len) return read; } -//******************************************************************************************************************* +//////////////////////////////////////// + size_t xbuf::peek(uint8_t* buf, const size_t len) { size_t read = 0; @@ -202,13 +213,15 @@ size_t xbuf::peek(uint8_t* buf, const size_t len) return read; } -//******************************************************************************************************************* +//////////////////////////////////////// + size_t xbuf::available() { return _used; } -//******************************************************************************************************************* +//////////////////////////////////////// + int xbuf::indexOf(const char target, const size_t begin) { char targetstr[2] = " "; @@ -217,7 +230,8 @@ int xbuf::indexOf(const char target, const size_t begin) return indexOf(targetstr, begin); } -//******************************************************************************************************************* +//////////////////////////////////////// + int xbuf::indexOf(const char* target, const size_t begin) { size_t targetLen = strlen(target); @@ -281,13 +295,15 @@ int xbuf::indexOf(const char* target, const size_t begin) return -1; } -//******************************************************************************************************************* +//////////////////////////////////////// + String xbuf::readStringUntil(const char target) { return readString(indexOf(target) + 1); } -//******************************************************************************************************************* +//////////////////////////////////////// + String xbuf::readStringUntil(const char* target) { int index = indexOf(target); @@ -298,24 +314,17 @@ String xbuf::readStringUntil(const char* target) return readString(index + strlen(target)); } -//******************************************************************************************************************* +//////////////////////////////////////// + String xbuf::readString(int endPos) { String result; if ( ! result.reserve(endPos + 1)) - { - // KH, to remove - //AHTTP_LOGDEBUG1(F("xbuf::readString: can't reserve size ="), endPos + 1); - /////// - + { return result; } - - // KH, to remove - //AHTTP_LOGDEBUG1(F("xbuf::readString: Reserved size ="), endPos + 1); - /////// - + if (endPos > _used) { endPos = _used; @@ -338,7 +347,8 @@ String xbuf::readString(int endPos) return result; } -//******************************************************************************************************************* +//////////////////////////////////////// + String xbuf::peekString(int endPos) { String result; @@ -368,7 +378,8 @@ String xbuf::peekString(int endPos) return result; } -//******************************************************************************************************************* +//////////////////////////////////////// + void xbuf::flush() { while (_head) @@ -380,7 +391,8 @@ void xbuf::flush() _free = 0; } -//******************************************************************************************************************* +//////////////////////////////////////// + void xbuf::addSeg() { if (_tail) @@ -389,7 +401,7 @@ void xbuf::addSeg() if (_tail->next == NULL) { - AHTTP_LOGDEBUG(F("xbuf::addSeg: error new 1")); + AHTTP_LOGERROR(F("xbuf::addSeg: error new 1")); } else { @@ -403,7 +415,7 @@ void xbuf::addSeg() _tail = _head = (xseg*) new uint32_t[_segSize / 4 + 1]; if (_tail == NULL) - AHTTP_LOGDEBUG(F("xbuf::addSeg: error new 2")); + AHTTP_LOGERROR(F("xbuf::addSeg: error new 2")); } // KH, Must check NULL here @@ -413,7 +425,8 @@ void xbuf::addSeg() _free += _segSize; } -//******************************************************************************************************************* +//////////////////////////////////////// + void xbuf::remSeg() { if (_head) @@ -431,7 +444,8 @@ void xbuf::remSeg() _offset = 0; } -//////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////// +//////////////////////////////////////// //************************************************************************************************************** AsyncHTTPRequest::AsyncHTTPRequest(): _readyState(readyStateUnsent), _HTTPcode(0), _chunked(false), _debug(DEBUG_IOTA_HTTP_SET) @@ -445,7 +459,8 @@ AsyncHTTPRequest::AsyncHTTPRequest(): _readyState(readyStateUnsent), _HTTPcode(0 #endif } -//************************************************************************************************************** +//////////////////////////////////////// + AsyncHTTPRequest::~AsyncHTTPRequest() { if (_client) @@ -468,33 +483,34 @@ AsyncHTTPRequest::~AsyncHTTPRequest() #endif } -//************************************************************************************************************** +//////////////////////////////////////// + void AsyncHTTPRequest::setDebug(bool debug) { if (_debug || debug) { _debug = true; - - AHTTP_LOGDEBUG3(F("setDebug("), debug ? F("on") : F("off"), F(") version"), ASYNC_HTTP_REQUEST_GENERIC_VERSION); } _debug = debug; } -//************************************************************************************************************** +//////////////////////////////////////// + bool AsyncHTTPRequest::debug() { return (_debug); } -//************************************************************************************************************** +//////////////////////////////////////// + bool AsyncHTTPRequest::open(const char* method, const char* URL) { AHTTP_LOGDEBUG3(F("open("), method, F(", url ="), URL); if (_readyState != readyStateUnsent && _readyState != readyStateDone) { - AHTTP_LOGWARN(F("open: not ready")); + AHTTP_LOGERROR(F("open: not ready")); return false; } @@ -516,9 +532,7 @@ bool AsyncHTTPRequest::open(const char* method, const char* URL) _contentRead = 0; _readyState = readyStateUnsent; - // New in v1.1.1 _requestReadyToSend = false; - ////// if (strcmp(method, "GET") == 0) { @@ -528,7 +542,6 @@ bool AsyncHTTPRequest::open(const char* method, const char* URL) { _HTTPmethod = HTTPmethodPOST; } - // New in v1.1.0 else if (strcmp(method, "PUT") == 0) { _HTTPmethod = HTTPmethodPUT; @@ -545,7 +558,6 @@ bool AsyncHTTPRequest::open(const char* method, const char* URL) { _HTTPmethod = HTTPmethodHEAD; } - ////// else { AHTTP_LOGERROR(F("open: Bad method")); @@ -553,7 +565,6 @@ bool AsyncHTTPRequest::open(const char* method, const char* URL) return false; } - if (!_parseURL(URL)) { AHTTP_LOGERROR(F("open: error parsing URL")); @@ -588,15 +599,13 @@ bool AsyncHTTPRequest::open(const char* method, const char* URL) sprintf(hostName, "%s:%d", _URL->host, _URL->port); _addHeader("host", hostName); - AHTTP_LOGDEBUG1(F("open: connecting to hostname ="), hostName); + AHTTP_LOGINFO1(F("open: connecting to hostname ="), hostName); SAFE_DELETE_ARRAY(hostName) _lastActivity = millis(); - // New in v1.1.1 _requestReadyToSend = true; - ////// return _connect(); } @@ -607,36 +616,32 @@ bool AsyncHTTPRequest::open(const char* method, const char* URL) return false; } } -//************************************************************************************************************** + +//////////////////////////////////////// + void AsyncHTTPRequest::onReadyStateChange(readyStateChangeCB cb, void* arg) { - _readyStateChangeCB = cb; + _readyStateChangeCB = cb; _readyStateChangeCBarg = arg; } -//************************************************************************************************************** +//////////////////////////////////////// + void AsyncHTTPRequest::setTimeout(int seconds) { - AHTTP_LOGDEBUG1(F("setTimeout ="), seconds); - _timeout = seconds; } -//************************************************************************************************************** +//////////////////////////////////////// + bool AsyncHTTPRequest::send() { - // New in v1.1.1 - if (_requestReadyToSend) + if (!_requestReadyToSend) { - AHTTP_LOGDEBUG(F("send()")); - } - else - { - AHTTP_LOGWARN(CANT_SEND_BAD_REQUEST); + AHTTP_LOGERROR(CANT_SEND_BAD_REQUEST); return false; } - ////// MUTEX_LOCK(false) @@ -650,28 +655,19 @@ bool AsyncHTTPRequest::send() return true; } -//************************************************************************************************************** +//////////////////////////////////////// + bool AsyncHTTPRequest::send(const String& body) { - // New in v1.1.1 - if (_requestReadyToSend) + if (!_requestReadyToSend) { - AHTTP_LOGDEBUG3(F("send(String)"), body.substring(0, 16).c_str(), F(", length ="), body.length()); - } - else - { - AHTTP_LOGWARN(CANT_SEND_BAD_REQUEST); + AHTTP_LOGERROR(CANT_SEND_BAD_REQUEST); return false; } - ////// - AHTTP_LOGDEBUG1(F("01) send String body ="), body); - MUTEX_LOCK(false) - - AHTTP_LOGDEBUG1(F("02) send String body ="), body); - + _addHeader("Content-Length", String(body.length()).c_str()); if ( ! _buildRequest()) @@ -680,35 +676,25 @@ bool AsyncHTTPRequest::send(const String& body) return false; } - - AHTTP_LOGDEBUG1(F("1) send String body ="), body); - + _request->write(body); - - AHTTP_LOGDEBUG1(F("2) send String body ="), body); _send(); - - AHTTP_LOGDEBUG1(F("3) send String body ="), body); - + _AHTTP_unlock; return true; } -//************************************************************************************************************** +//////////////////////////////////////// + bool AsyncHTTPRequest::send(const char* body) { - // New in v1.1.1 - if (_requestReadyToSend) + if (!_requestReadyToSend) { - AHTTP_LOGDEBUG3(F("send(char)"), body, F(", length ="), strlen(body)); - } - else - { - AHTTP_LOGWARN(CANT_SEND_BAD_REQUEST); + AHTTP_LOGERROR(CANT_SEND_BAD_REQUEST); + return false; } - ////// MUTEX_LOCK(false) @@ -729,20 +715,16 @@ bool AsyncHTTPRequest::send(const char* body) return true; } -//************************************************************************************************************** +//////////////////////////////////////// + bool AsyncHTTPRequest::send(const uint8_t* body, size_t len) { - // New in v1.1.1 - if (_requestReadyToSend) + if (!_requestReadyToSend) { - AHTTP_LOGDEBUG3(F("send(char)"), (char*) body, F(", length ="), len); - } - else - { - AHTTP_LOGWARN(CANT_SEND_BAD_REQUEST); + AHTTP_LOGERROR(CANT_SEND_BAD_REQUEST); + return false; } - ////// MUTEX_LOCK(false) @@ -763,21 +745,16 @@ bool AsyncHTTPRequest::send(const uint8_t* body, size_t len) return true; } -//************************************************************************************************************** +//////////////////////////////////////// + bool AsyncHTTPRequest::send(xbuf* body, size_t len) { - // New in v1.1.1 - if (_requestReadyToSend) + if (!_requestReadyToSend) { - AHTTP_LOGDEBUG3(F("send(char)"), body->peekString(16).c_str(), F(", length ="), len); - } - else - { - AHTTP_LOGWARN(CANT_SEND_BAD_REQUEST); + AHTTP_LOGERROR(CANT_SEND_BAD_REQUEST); return false; } - ////// MUTEX_LOCK(false) @@ -798,7 +775,8 @@ bool AsyncHTTPRequest::send(xbuf* body, size_t len) return true; } -//************************************************************************************************************** +//////////////////////////////////////// + void AsyncHTTPRequest::abort() { AHTTP_LOGERROR(F("abort()")); @@ -814,19 +792,23 @@ void AsyncHTTPRequest::abort() _AHTTP_unlock; } -//************************************************************************************************************** + +//////////////////////////////////////// + reqStates AsyncHTTPRequest::readyState() { return _readyState; } -//************************************************************************************************************** +//////////////////////////////////////// + int AsyncHTTPRequest::responseHTTPcode() { return _HTTPcode; } -//************************************************************************************************************** +//////////////////////////////////////// + String AsyncHTTPRequest::responseHTTPString() { switch(_HTTPcode) @@ -897,15 +879,14 @@ String AsyncHTTPRequest::responseHTTPString() case 503: return F("Service Unavailable"); case 504: return F("Gateway Time-out"); case 505: return F("HTTP Version not supported"); - default: return "UNKNOWN"; + default: return "UNKNOWN"; } } -//************************************************************************************************************** +//////////////////////////////////////// + String AsyncHTTPRequest::responseText() { - AHTTP_LOGDEBUG(F("responseText()")); - MUTEX_LOCK(String()) if ( ! _response || _readyState < readyStateLoading || ! available()) @@ -932,15 +913,13 @@ String AsyncHTTPRequest::responseText() } _contentRead += localString.length(); - - AHTTP_LOGDEBUG3(F("responseText() ="), localString.substring(0,16), F(", size ="), avail); - + _AHTTP_unlock; return localString; } -//************************************************************************************************************** +//////////////////////////////////////// #if (ESP32) #define GLOBAL_STR_LEN (32 * 1024) @@ -950,12 +929,14 @@ String AsyncHTTPRequest::responseText() #define GLOBAL_STR_LEN (4 * 1024) #endif +//////////////////////////////////////// + char globalLongString[GLOBAL_STR_LEN + 1]; +//////////////////////////////////////// + char* AsyncHTTPRequest::responseLongText() { - AHTTP_LOGDEBUG(F("responseLongText()")); - MUTEX_LOCK(NULL) if ( ! _response || _readyState < readyStateLoading || ! available()) @@ -964,11 +945,9 @@ char* AsyncHTTPRequest::responseLongText() _AHTTP_unlock; - //return String(); return NULL; } - // String localString; size_t avail = available(); size_t lenToCopy = (avail <= GLOBAL_STR_LEN) ? avail : GLOBAL_STR_LEN; @@ -976,15 +955,14 @@ char* AsyncHTTPRequest::responseLongText() globalLongString[ lenToCopy + 1 ] = 0; _contentRead += _response->readString(avail).length(); - - AHTTP_LOGDEBUG3(F("responseLongText(char)"), globalLongString, F(", avail ="), avail); - + _AHTTP_unlock; return globalLongString; } -//************************************************************************************************************** +//////////////////////////////////////// + size_t AsyncHTTPRequest::responseRead(uint8_t* buf, size_t len) { if ( ! _response || _readyState < readyStateLoading || ! available()) @@ -999,8 +977,6 @@ size_t AsyncHTTPRequest::responseRead(uint8_t* buf, size_t len) size_t avail = available() > len ? len : available(); _response->read(buf, avail); - AHTTP_LOGDEBUG3(F("responseRead(char)"), (char*) buf, F(", avail ="), avail); - _contentRead += avail; _AHTTP_unlock; @@ -1008,7 +984,8 @@ size_t AsyncHTTPRequest::responseRead(uint8_t* buf, size_t len) return avail; } -//************************************************************************************************************** +//////////////////////////////////////// + size_t AsyncHTTPRequest::available() { if (_readyState < readyStateLoading) @@ -1022,7 +999,8 @@ size_t AsyncHTTPRequest::available() return _response->available(); } -//************************************************************************************************************** +//////////////////////////////////////// + size_t AsyncHTTPRequest::responseLength() { if (_readyState < readyStateLoading) @@ -1031,16 +1009,16 @@ size_t AsyncHTTPRequest::responseLength() return _contentLength; } -//************************************************************************************************************** +//////////////////////////////////////// + void AsyncHTTPRequest::onData(onDataCB cb, void* arg) { - AHTTP_LOGDEBUG(F("onData() CB set")); - _onDataCB = cb; _onDataCBarg = arg; } -//************************************************************************************************************** +//////////////////////////////////////// + uint32_t AsyncHTTPRequest::elapsedTime() { if (_readyState <= readyStateOpened) @@ -1054,7 +1032,8 @@ uint32_t AsyncHTTPRequest::elapsedTime() return _requestEndTime - _requestStartTime; } -//************************************************************************************************************** +//////////////////////////////////////// + String AsyncHTTPRequest::version() { return String(ASYNC_HTTP_REQUEST_GENERIC_VERSION); @@ -1069,13 +1048,15 @@ String AsyncHTTPRequest::version() P R R OOO T EEEEE CCC T EEEEE DDDD _______________________________________________________________________________________________________________*/ -//************************************************************************************************************** +//////////////////////////////////////// + bool AsyncHTTPRequest::_parseURL(const char* url) { return _parseURL(String(url)); } -//************************************************************************************************************** +//////////////////////////////////////// + bool AsyncHTTPRequest::_parseURL(const String& url) { SAFE_DELETE(_URL) @@ -1086,7 +1067,7 @@ bool AsyncHTTPRequest::_parseURL(const String& url) if (_URL) { - _URL->scheme = new char[8]; + _URL->scheme = new char[strlen(ASYNC_HTTP_PREFIX) + 1]; if (! (_URL->scheme) ) return false; @@ -1094,13 +1075,13 @@ bool AsyncHTTPRequest::_parseURL(const String& url) else return false; - strcpy(_URL->scheme, "HTTP://"); + strcpy(_URL->scheme, ASYNC_HTTP_PREFIX); - if (url.substring(0, 7).equalsIgnoreCase("HTTP://")) + if (url.substring(0, strlen(ASYNC_HTTP_PREFIX)).equalsIgnoreCase(ASYNC_HTTP_PREFIX)) { - hostBeg += 7; + hostBeg += strlen(ASYNC_HTTP_PREFIX); } - else if (url.substring(0, 8).equalsIgnoreCase("HTTPS://")) + else if (url.substring(0, strlen(ASYNC_HTTPS_PREFIX)).equalsIgnoreCase(ASYNC_HTTPS_PREFIX) ) { return false; } @@ -1145,17 +1126,13 @@ bool AsyncHTTPRequest::_parseURL(const String& url) strcpy(_URL->query, url.substring(queryBeg).c_str()); - AHTTP_LOGDEBUG2(F("_parseURL(): scheme+host"), _URL->scheme, _URL->host); - AHTTP_LOGDEBUG3(F("_parseURL(): port+path+query"), _URL->port, _URL->path, _URL->query); - return true; } -//************************************************************************************************************** +//////////////////////////////////////// + bool AsyncHTTPRequest::_connect() { - AHTTP_LOGDEBUG(F("_connect()")); - if ( ! _client) { _client = new AsyncClient(); @@ -1219,16 +1196,13 @@ bool AsyncHTTPRequest::_connect() _lastActivity = millis(); - AHTTP_LOGDEBUG(F("_connect() true")); - return true; } -//************************************************************************************************************** +//////////////////////////////////////// + bool AsyncHTTPRequest::_buildRequest() { - AHTTP_LOGDEBUG(F("_buildRequest()")); - // Build the header. if ( ! _request) { @@ -1238,17 +1212,10 @@ bool AsyncHTTPRequest::_buildRequest() return false; } - // New in v1.1.1 - AHTTP_LOGDEBUG1(F("_HTTPmethod ="), _HTTPmethod); - AHTTP_LOGDEBUG3(_HTTPmethodStringwithSpace[_HTTPmethod], _URL->path, _URL->query, F("HTTP/1.1\r\n") ); - ////// - - // New in v1.1.0 - _request->write(_HTTPmethodStringwithSpace[_HTTPmethod]); - ////// - + _request->write(_HTTPmethodStringwithSpace[_HTTPmethod]); _request->write(_URL->path); _request->write(_URL->query); + _request->write(" HTTP/1.1\r\n"); SAFE_DELETE(_URL) @@ -1262,9 +1229,7 @@ bool AsyncHTTPRequest::_buildRequest() _request->write(':'); _request->write(hdr->value); _request->write("\r\n"); - - AHTTP_LOGDEBUG3(hdr->name, F(":"), hdr->value, F("\r\n")); - + hdr = hdr->next; } @@ -1276,18 +1241,15 @@ bool AsyncHTTPRequest::_buildRequest() return true; } -//************************************************************************************************************** +//////////////////////////////////////// + size_t AsyncHTTPRequest::_send() { if ( ! _request) return 0; - AHTTP_LOGDEBUG1(F("_send(), _request->available ="), _request->available()); - if ( ! _client->connected()) - { - AHTTP_LOGDEBUG(F("!connected yet")); - + { // KH fix bug https://github.com/khoih-prog/AsyncHTTPRequest_Generic/issues/38 _HTTPcode = HTTPCODE_NOT_CONNECTED; _setReadyState(readyStateDone); @@ -1298,8 +1260,6 @@ size_t AsyncHTTPRequest::_send() } else if ( ! _client->canSend()) { - AHTTP_LOGDEBUG(F("*can't send")); - return 0; } @@ -1310,16 +1270,20 @@ size_t AsyncHTTPRequest::_send() supply = demand; size_t sent = 0; - uint8_t* temp = new uint8_t[100]; + + #define TEMP_SIZE 255 + + uint8_t* temp = new uint8_t[TEMP_SIZE + 1]; if (!temp) return 0; while (supply) { - size_t chunk = supply < 100 ? supply : 100; - - supply -= _request->read(temp, chunk); + size_t chunk = supply < TEMP_SIZE ? supply : TEMP_SIZE; + + memset(temp, 0, TEMP_SIZE + 1); + supply -= _request->read(temp, chunk); sent += _client->add((char*)temp, chunk); } @@ -1328,30 +1292,29 @@ size_t AsyncHTTPRequest::_send() if (_request->available() == 0) { - //delete _request; SAFE_DELETE(_request) _request = nullptr; + + // KH fix crash bug + return 0; } _client->send(); - AHTTP_LOGDEBUG1(F("*send"), sent); - _lastActivity = millis(); return sent; } -//************************************************************************************************************** +//////////////////////////////////////// + void AsyncHTTPRequest::_setReadyState(reqStates newState) { if (_readyState != newState) { _readyState = newState; - AHTTP_LOGDEBUG1(F("_setReadyState :"), _readyState); - if (_readyStateChangeCB) { _readyStateChangeCB(_readyStateChangeCBarg, this, _readyState); @@ -1359,13 +1322,12 @@ void AsyncHTTPRequest::_setReadyState(reqStates newState) } } -//************************************************************************************************************** +//////////////////////////////////////// + void AsyncHTTPRequest::_processChunks() { while (_chunks->available()) { - AHTTP_LOGDEBUG3(F("_processChunks()"), _chunks->peekString(16).c_str(), F(", chunks available ="), _chunks->available()); - size_t _chunkRemaining = _contentLength - _contentRead - _response->available(); _chunkRemaining -= _response->write(_chunks, _chunkRemaining); @@ -1376,8 +1338,6 @@ void AsyncHTTPRequest::_processChunks() String chunkHeader = _chunks->readStringUntil("\r\n"); - AHTTP_LOGDEBUG3(F("*getChunkHeader"), chunkHeader.c_str(), F(", chunkHeader length ="), chunkHeader.length()); - size_t chunkLength = strtol(chunkHeader.c_str(), nullptr, 16); _contentLength += chunkLength; @@ -1387,14 +1347,8 @@ void AsyncHTTPRequest::_processChunks() if (connectionHdr && (strcasecmp_P(connectionHdr, PSTR("close")) == 0)) { - AHTTP_LOGDEBUG(F("*all chunks received - closing TCP")); - _client->close(); } - else - { - AHTTP_LOGDEBUG(F("*all chunks received - no disconnect")); - } _requestEndTime = millis(); _lastActivity = 0; @@ -1415,35 +1369,27 @@ void AsyncHTTPRequest::_processChunks() EEEEE V EEEEE N N T H H A A N N DDDD LLLLL EEEEE R R SSS _______________________________________________________________________________________________________________*/ -//************************************************************************************************************** +//////////////////////////////////////// + void AsyncHTTPRequest::_onConnect(AsyncClient* client) { - AHTTP_LOGDEBUG(F("_onConnect handler")); - MUTEX_LOCK_NR _client = client; _setReadyState(readyStateOpened); - // KH test _response = new xbuf; - //_response = new xbuf(256); - ////// if (!_response) { _AHTTP_unlock; - - // KH, to remove - //AHTTP_LOGWARN(F("_onConnect: Can't new _response")); - /////// - + return; } _contentLength = 0; - _contentRead = 0; - _chunked = false; + _contentRead = 0; + _chunked = false; _client->onAck([](void* obj, AsyncClient * client, size_t len, uint32_t time) { @@ -1471,7 +1417,8 @@ void AsyncHTTPRequest::_onConnect(AsyncClient* client) _AHTTP_unlock; } -//************************************************************************************************************** +//////////////////////////////////////// + void AsyncHTTPRequest::_onPoll(AsyncClient* client) { (void) client; @@ -1482,8 +1429,6 @@ void AsyncHTTPRequest::_onPoll(AsyncClient* client) { _client->close(); _HTTPcode = HTTPCODE_TIMEOUT; - - AHTTP_LOGDEBUG(F("_onPoll timeout")); } if (_onDataCB && available()) @@ -1494,7 +1439,8 @@ void AsyncHTTPRequest::_onPoll(AsyncClient* client) _AHTTP_unlock; } -//************************************************************************************************************** +//////////////////////////////////////// + void AsyncHTTPRequest::_onError(AsyncClient* client, int8_t error) { (void) client; @@ -1504,30 +1450,30 @@ void AsyncHTTPRequest::_onError(AsyncClient* client, int8_t error) _HTTPcode = error; } -//************************************************************************************************************** +//////////////////////////////////////// + void AsyncHTTPRequest::_onDisconnect(AsyncClient* client) { (void) client; - AHTTP_LOGDEBUG(F("\n_onDisconnect handler")); - MUTEX_LOCK_NR if (_readyState < readyStateOpened) { - AHTTP_LOGERROR(F("HTTPCODE_NOT_CONNECTED")); _HTTPcode = HTTPCODE_NOT_CONNECTED; } else if (_HTTPcode > 0 && - (_readyState < readyStateHdrsRecvd || (_contentRead + _response->available()) < _contentLength)) + (_readyState < readyStateHdrsRecvd || (_contentRead + _response->available()) < _contentLength)) { AHTTP_LOGDEBUG(F("_onDisconnect: HTTPCODE_CONNECTION_LOST")); _HTTPcode = HTTPCODE_CONNECTION_LOST; } - - AHTTP_LOGDEBUG1(F("_onDisconnect: _HTTPcode ="), _HTTPcode); - AHTTP_LOGDEBUG1(F("_onDisconnect: _HTTPcode ="), responseHTTPString()); + // KH, New test in AsyncHTTPRequest, v1.10.0 + SAFE_DELETE(_client) + + _client = nullptr; + ////// SAFE_DELETE_ARRAY(_connectedHost) @@ -1541,11 +1487,10 @@ void AsyncHTTPRequest::_onDisconnect(AsyncClient* client) _AHTTP_unlock; } -//************************************************************************************************************** +//////////////////////////////////////// + void AsyncHTTPRequest::_onData(void* Vbuf, size_t len) { - AHTTP_LOGDEBUG3(F("_onData handler"), (char*) Vbuf, F(", len ="), len); - MUTEX_LOCK_NR _lastActivity = millis(); @@ -1554,11 +1499,7 @@ void AsyncHTTPRequest::_onData(void* Vbuf, size_t len) if (_chunks) { _chunks->write((uint8_t*)Vbuf, len); - - // KH, to remove - //AHTTP_LOGDEBUG(F("_onData: _processChunks")); - /////// - + _processChunks(); } else @@ -1573,10 +1514,6 @@ void AsyncHTTPRequest::_onData(void* Vbuf, size_t len) { _AHTTP_unlock; - // KH, to remove - //AHTTP_LOGWARN(F("_onData: headers not complete")); - /////// - return; } } @@ -1594,14 +1531,8 @@ void AsyncHTTPRequest::_onData(void* Vbuf, size_t len) if (connectionHdr && (strcasecmp_P(connectionHdr, PSTR("close")) == 0)) { - AHTTP_LOGDEBUG(F("*all data received - closing TCP")); - _client->close(); } - else - { - AHTTP_LOGDEBUG(F("*all data received - no disconnect")); - } _requestEndTime = millis(); _lastActivity = 0; @@ -1619,11 +1550,10 @@ void AsyncHTTPRequest::_onData(void* Vbuf, size_t len) } -//************************************************************************************************************** +//////////////////////////////////////// + bool AsyncHTTPRequest::_collectHeaders() { - AHTTP_LOGDEBUG(F("_collectHeaders()")); - // Loop to parse off each header line. Drop out and return false if no \r\n (incomplete) do { @@ -1696,7 +1626,8 @@ bool AsyncHTTPRequest::_collectHeaders() H H EEEEE A A DDDD EEEEE R R SSS ______________________________________________________________________________________________________________*/ -//************************************************************************************************************** +//////////////////////////////////////// + void AsyncHTTPRequest::setReqHeader(const char* name, const char* value) { if (_readyState <= readyStateOpened && _headers) @@ -1705,7 +1636,8 @@ void AsyncHTTPRequest::setReqHeader(const char* name, const char* value) } } -//************************************************************************************************************** +//////////////////////////////////////// + void AsyncHTTPRequest::setReqHeader(const char* name, int32_t value) { if (_readyState <= readyStateOpened && _headers) @@ -1713,10 +1645,12 @@ void AsyncHTTPRequest::setReqHeader(const char* name, int32_t value) setReqHeader(name, String(value).c_str()); } } +//////////////////////////////////////// -#if (ESP32 || ESP8266) +#if ( defined(ESP32) || defined(ESP8266) ) + +//////////////////////////////////////// -//************************************************************************************************************** void AsyncHTTPRequest::setReqHeader(const char* name, const __FlashStringHelper* value) { if (_readyState <= readyStateOpened && _headers) @@ -1728,7 +1662,8 @@ void AsyncHTTPRequest::setReqHeader(const char* name, const __FlashStringHelper* } } -//************************************************************************************************************** +//////////////////////////////////////// + void AsyncHTTPRequest::setReqHeader(const __FlashStringHelper *name, const char* value) { if (_readyState <= readyStateOpened && _headers) @@ -1740,7 +1675,8 @@ void AsyncHTTPRequest::setReqHeader(const __FlashStringHelper *name, const char* } } -//************************************************************************************************************** +//////////////////////////////////////// + void AsyncHTTPRequest::setReqHeader(const __FlashStringHelper *name, const __FlashStringHelper* value) { if (_readyState <= readyStateOpened && _headers) @@ -1754,7 +1690,8 @@ void AsyncHTTPRequest::setReqHeader(const __FlashStringHelper *name, const __Fla } } -//************************************************************************************************************** +//////////////////////////////////////// + void AsyncHTTPRequest::setReqHeader(const __FlashStringHelper *name, int32_t value) { if (_readyState <= readyStateOpened && _headers) @@ -1765,10 +1702,12 @@ void AsyncHTTPRequest::setReqHeader(const __FlashStringHelper *name, int32_t val SAFE_DELETE_ARRAY(_name) } } +//////////////////////////////////////// #endif -//************************************************************************************************************** +//////////////////////////////////////// + int AsyncHTTPRequest::respHeaderCount() { if (_readyState < readyStateHdrsRecvd) @@ -1786,7 +1725,8 @@ int AsyncHTTPRequest::respHeaderCount() return count; } -//************************************************************************************************************** +//////////////////////////////////////// + char* AsyncHTTPRequest::respHeaderName(int ndx) { if (_readyState < readyStateHdrsRecvd) @@ -1800,7 +1740,8 @@ char* AsyncHTTPRequest::respHeaderName(int ndx) return hdr->name; } -//************************************************************************************************************** +//////////////////////////////////////// + char* AsyncHTTPRequest::respHeaderValue(const char* name) { if (_readyState < readyStateHdrsRecvd) @@ -1814,7 +1755,8 @@ char* AsyncHTTPRequest::respHeaderValue(const char* name) return hdr->value; } -//************************************************************************************************************** +//////////////////////////////////////// + char* AsyncHTTPRequest::respHeaderValue(int ndx) { if (_readyState < readyStateHdrsRecvd) @@ -1828,7 +1770,8 @@ char* AsyncHTTPRequest::respHeaderValue(int ndx) return hdr->value; } -//************************************************************************************************************** +//////////////////////////////////////// + bool AsyncHTTPRequest::respHeaderExists(const char* name) { if (_readyState < readyStateHdrsRecvd) @@ -1842,10 +1785,12 @@ bool AsyncHTTPRequest::respHeaderExists(const char* name) return true; } +//////////////////////////////////////// -#if (ESP32 || ESP8266) +#if ( defined(ESP32) || defined(ESP8266) ) + +//////////////////////////////////////// -//************************************************************************************************************** char* AsyncHTTPRequest::respHeaderValue(const __FlashStringHelper *name) { if (_readyState < readyStateHdrsRecvd) @@ -1862,7 +1807,8 @@ char* AsyncHTTPRequest::respHeaderValue(const __FlashStringHelper *name) return hdr->value; } -//************************************************************************************************************** +//////////////////////////////////////// + bool AsyncHTTPRequest::respHeaderExists(const __FlashStringHelper *name) { if (_readyState < readyStateHdrsRecvd) @@ -1878,10 +1824,12 @@ bool AsyncHTTPRequest::respHeaderExists(const __FlashStringHelper *name) return true; } +//////////////////////////////////////// #endif -//************************************************************************************************************** +//////////////////////////////////////// + String AsyncHTTPRequest::headers() { MUTEX_LOCK(String()) @@ -1905,7 +1853,8 @@ String AsyncHTTPRequest::headers() return _response; } -//************************************************************************************************************** +//////////////////////////////////////// + AsyncHTTPRequest::header* AsyncHTTPRequest::_addHeader(const char* name, const char* value) { MUTEX_LOCK(nullptr) @@ -1965,7 +1914,8 @@ AsyncHTTPRequest::header* AsyncHTTPRequest::_addHeader(const char* name, const return hdr->next; } -//************************************************************************************************************** +//////////////////////////////////////// + AsyncHTTPRequest::header* AsyncHTTPRequest::_getHeader(const char* name) { MUTEX_LOCK(nullptr) @@ -1985,7 +1935,8 @@ AsyncHTTPRequest::header* AsyncHTTPRequest::_getHeader(const char* name) return hdr; } -//************************************************************************************************************** +//////////////////////////////////////// + AsyncHTTPRequest::header* AsyncHTTPRequest::_getHeader(int ndx) { MUTEX_LOCK(nullptr) @@ -2004,10 +1955,12 @@ AsyncHTTPRequest::header* AsyncHTTPRequest::_getHeader(int ndx) return hdr; } +//////////////////////////////////////// -#if (ESP32 || ESP8266) +#if ( defined(ESP32) || defined(ESP8266) ) + +//////////////////////////////////////// -//************************************************************************************************************** char* AsyncHTTPRequest::_charstar(const __FlashStringHelper * str) { if ( ! str) @@ -2023,7 +1976,9 @@ char* AsyncHTTPRequest::_charstar(const __FlashStringHelper * str) // Return good ptr or nullptr return ptr; } +//////////////////////////////////////// #endif +//////////////////////////////////////// #endif // ASYNC_HTTP_REQUEST_GENERIC_IMPL_H