Releases v1.1.3

### Releases v1.1.3

1. Fix non-persistent Connection header bug. Check [**'Connection' header expects 'disconnect' instead 'close' ? #13**](https://github.com/khoih-prog/AsyncHTTPRequest_Generic/issues/13)
2. Add ESP32-S2 support
3. Tested with [**Latest ESP32 Core 1.0.5**](https://github.com/espressif/arduino-esp32) for ESP32-based boards.
This commit is contained in:
Khoi Hoang
2021-02-25 16:51:32 -05:00
committed by GitHub
parent 5cb843c636
commit c9be0bdaab
41 changed files with 1469 additions and 172 deletions

View File

@@ -17,7 +17,7 @@
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
Version: 1.1.2
Version: 1.1.3
Version Modified By Date Comments
------- ----------- ---------- -----------
@@ -27,6 +27,7 @@
1.1.0 K Hoang 23/12/2020 Add HTTP PUT, PATCH, DELETE and HEAD methods
1.1.1 K Hoang 24/12/2020 Prevent crash if request and/or method not correct.
1.1.2 K Hoang 11/02/2021 Rename _lock and _unlock to avoid conflict with AsyncWebServer library
1.1.3 K Hoang 25/02/2021 Fix non-persistent Connection header bug
*****************************************************************************************************************************/
#pragma once
@@ -51,29 +52,49 @@
#define _ASYNC_HTTP_LOGLEVEL_ 0
#endif
#define AHTTP_LOGERROR(x) if(_ASYNC_HTTP_LOGLEVEL_>0) { A_DBG_PORT.print("[AHTTP] "); A_DBG_PORT.println(x); }
#define AHTTP_LOGERROR0(x) if(_ASYNC_HTTP_LOGLEVEL_>0) { A_DBG_PORT.print(x); }
#define AHTTP_LOGERROR1(x,y) if(_ASYNC_HTTP_LOGLEVEL_>0) { A_DBG_PORT.print("[AHTTP] "); A_DBG_PORT.print(x); A_DBG_PORT.print(" "); A_DBG_PORT.println(y); }
#define AHTTP_LOGERROR2(x,y,z) if(_ASYNC_HTTP_LOGLEVEL_>0) { A_DBG_PORT.print("[AHTTP] "); A_DBG_PORT.print(x); A_DBG_PORT.print(" "); A_DBG_PORT.print(y); A_DBG_PORT.print(" "); A_DBG_PORT.println(z); }
#define AHTTP_LOGERROR3(x,y,z,w) if(_ASYNC_HTTP_LOGLEVEL_>0) { A_DBG_PORT.print("[AHTTP] "); A_DBG_PORT.print(x); A_DBG_PORT.print(" "); A_DBG_PORT.print(y); A_DBG_PORT.print(" "); A_DBG_PORT.print(z); A_DBG_PORT.print(" "); A_DBG_PORT.println(w); }
/////////////////////////////////////////////////////////
#define AHTTP_LOGWARN(x) if(_ASYNC_HTTP_LOGLEVEL_>1) { A_DBG_PORT.print("[AHTTP] "); A_DBG_PORT.println(x); }
#define AHTTP_LOGWARN0(x) if(_ASYNC_HTTP_LOGLEVEL_>1) { A_DBG_PORT.print(x); }
#define AHTTP_LOGWARN1(x,y) if(_ASYNC_HTTP_LOGLEVEL_>1) { A_DBG_PORT.print("[AHTTP] "); A_DBG_PORT.print(x); A_DBG_PORT.print(" "); A_DBG_PORT.println(y); }
#define AHTTP_LOGWARN2(x,y,z) if(_ASYNC_HTTP_LOGLEVEL_>1) { A_DBG_PORT.print("[AHTTP] "); A_DBG_PORT.print(x); A_DBG_PORT.print(" "); A_DBG_PORT.print(y); A_DBG_PORT.print(" "); A_DBG_PORT.println(z); }
#define AHTTP_LOGWARN3(x,y,z,w) if(_ASYNC_HTTP_LOGLEVEL_>1) { A_DBG_PORT.print("[AHTTP] "); A_DBG_PORT.print(x); A_DBG_PORT.print(" "); A_DBG_PORT.print(y); A_DBG_PORT.print(" "); A_DBG_PORT.print(z); A_DBG_PORT.print(" "); A_DBG_PORT.println(w); }
const char AHTTP_MARK[] = "[AHTTP] ";
#define AHTTP_LOGINFO(x) if(_ASYNC_HTTP_LOGLEVEL_>2) { A_DBG_PORT.print("[AHTTP] "); A_DBG_PORT.println(x); }
#define AHTTP_LOGINFO0(x) if(_ASYNC_HTTP_LOGLEVEL_>2) { A_DBG_PORT.print(x); }
#define AHTTP_LOGINFO1(x,y) if(_ASYNC_HTTP_LOGLEVEL_>2) { A_DBG_PORT.print("[AHTTP] "); A_DBG_PORT.print(x); A_DBG_PORT.print(" "); A_DBG_PORT.println(y); }
#define AHTTP_LOGINFO2(x,y,z) if(_ASYNC_HTTP_LOGLEVEL_>2) { A_DBG_PORT.print("[AHTTP] "); A_DBG_PORT.print(x); A_DBG_PORT.print(" "); A_DBG_PORT.print(y); A_DBG_PORT.print(" "); A_DBG_PORT.println(z); }
#define AHTTP_LOGINFO3(x,y,z,w) if(_ASYNC_HTTP_LOGLEVEL_>2) { A_DBG_PORT.print("[AHTTP] "); A_DBG_PORT.print(x); A_DBG_PORT.print(" "); A_DBG_PORT.print(y); A_DBG_PORT.print(" "); A_DBG_PORT.print(z); A_DBG_PORT.print(" "); A_DBG_PORT.println(w); }
#define AHTTP_PRINT_MARK AHTTP_PRINT(AHTTP_MARK)
#define AHTTP_PRINT_SP A_DBG_PORT.print(" ")
#define AHTTP_LOGDEBUG(x) if(_ASYNC_HTTP_LOGLEVEL_>3) { A_DBG_PORT.print("[AHTTP] "); A_DBG_PORT.println(x); }
#define AHTTP_LOGDEBUG0(x) if(_ASYNC_HTTP_LOGLEVEL_>3) { A_DBG_PORT.print(x); }
#define AHTTP_LOGDEBUG1(x,y) if(_ASYNC_HTTP_LOGLEVEL_>3) { A_DBG_PORT.print("[AHTTP] "); A_DBG_PORT.print(x); A_DBG_PORT.print(" "); A_DBG_PORT.println(y); }
#define AHTTP_LOGDEBUG2(x,y,z) if(_ASYNC_HTTP_LOGLEVEL_>3) { A_DBG_PORT.print("[AHTTP] "); A_DBG_PORT.print(x); A_DBG_PORT.print(" "); A_DBG_PORT.print(y); A_DBG_PORT.print(" "); A_DBG_PORT.println(z); }
#define AHTTP_LOGDEBUG3(x,y,z,w) if(_ASYNC_HTTP_LOGLEVEL_>3) { A_DBG_PORT.print("[AHTTP] "); A_DBG_PORT.print(x); A_DBG_PORT.print(" "); A_DBG_PORT.print(y); A_DBG_PORT.print(" "); A_DBG_PORT.print(z); A_DBG_PORT.print(" "); A_DBG_PORT.println(w); }
#define AHTTP_PRINT A_DBG_PORT.print
#define AHTTP_PRINTLN A_DBG_PORT.println
/////////////////////////////////////////////////////////
#define AHTTP_LOGERROR(x) if(_ASYNC_HTTP_LOGLEVEL_>0) { AHTTP_PRINT_MARK; AHTTP_PRINTLN(x); }
#define AHTTP_LOGERROR0(x) if(_ASYNC_HTTP_LOGLEVEL_>0) { AHTTP_PRINT(x); }
#define AHTTP_LOGERROR1(x,y) if(_ASYNC_HTTP_LOGLEVEL_>0) { AHTTP_PRINT_MARK; AHTTP_PRINT(x); AHTTP_PRINT_SP; AHTTP_PRINTLN(y); }
#define AHTTP_LOGERROR2(x,y,z) if(_ASYNC_HTTP_LOGLEVEL_>0) { AHTTP_PRINT_MARK; AHTTP_PRINT(x); AHTTP_PRINT_SP; AHTTP_PRINT(y); AHTTP_PRINT_SP; AHTTP_PRINTLN(z); }
#define AHTTP_LOGERROR3(x,y,z,w) if(_ASYNC_HTTP_LOGLEVEL_>0) { AHTTP_PRINT_MARK; AHTTP_PRINT(x); AHTTP_PRINT_SP; AHTTP_PRINT(y); AHTTP_PRINT_SP; AHTTP_PRINT(z); AHTTP_PRINT_SP; AHTTP_PRINTLN(w); }
/////////////////////////////////////////////////////////
#define AHTTP_LOGWARN(x) if(_ASYNC_HTTP_LOGLEVEL_>1) { AHTTP_PRINT_MARK; AHTTP_PRINTLN(x); }
#define AHTTP_LOGWARN0(x) if(_ASYNC_HTTP_LOGLEVEL_>1) { AHTTP_PRINT(x); }
#define AHTTP_LOGWARN1(x,y) if(_ASYNC_HTTP_LOGLEVEL_>1) { AHTTP_PRINT_MARK; AHTTP_PRINT(x); AHTTP_PRINT_SP; AHTTP_PRINTLN(y); }
#define AHTTP_LOGWARN2(x,y,z) if(_ASYNC_HTTP_LOGLEVEL_>1) { AHTTP_PRINT_MARK; AHTTP_PRINT(x); AHTTP_PRINT_SP; AHTTP_PRINT(y); AHTTP_PRINT_SP; AHTTP_PRINTLN(z); }
#define AHTTP_LOGWARN3(x,y,z,w) if(_ASYNC_HTTP_LOGLEVEL_>1) { AHTTP_PRINT_MARK; AHTTP_PRINT(x); AHTTP_PRINT_SP; AHTTP_PRINT(y); AHTTP_PRINT_SP; AHTTP_PRINT(z); AHTTP_PRINT_SP; AHTTP_PRINTLN(w); }
/////////////////////////////////////////////////////////
#define AHTTP_LOGINFO(x) if(_ASYNC_HTTP_LOGLEVEL_>2) { AHTTP_PRINT_MARK; AHTTP_PRINTLN(x); }
#define AHTTP_LOGINFO0(x) if(_ASYNC_HTTP_LOGLEVEL_>2) { AHTTP_PRINT(x); }
#define AHTTP_LOGINFO1(x,y) if(_ASYNC_HTTP_LOGLEVEL_>2) { AHTTP_PRINT_MARK; AHTTP_PRINT(x); AHTTP_PRINT_SP; AHTTP_PRINTLN(y); }
#define AHTTP_LOGINFO2(x,y,z) if(_ASYNC_HTTP_LOGLEVEL_>2) { AHTTP_PRINT_MARK; AHTTP_PRINT(x); AHTTP_PRINT_SP; AHTTP_PRINT(y); AHTTP_PRINT_SP; AHTTP_PRINTLN(z); }
#define AHTTP_LOGINFO3(x,y,z,w) if(_ASYNC_HTTP_LOGLEVEL_>2) { AHTTP_PRINT_MARK; AHTTP_PRINT(x); AHTTP_PRINT_SP; AHTTP_PRINT(y); AHTTP_PRINT_SP; AHTTP_PRINT(z); AHTTP_PRINT_SP; AHTTP_PRINTLN(w); }
/////////////////////////////////////////////////////////
#define AHTTP_LOGDEBUG(x) if(_ASYNC_HTTP_LOGLEVEL_>3) { AHTTP_PRINT_MARK; AHTTP_PRINTLN(x); }
#define AHTTP_LOGDEBUG0(x) if(_ASYNC_HTTP_LOGLEVEL_>3) { AHTTP_PRINT(x); }
#define AHTTP_LOGDEBUG1(x,y) if(_ASYNC_HTTP_LOGLEVEL_>3) { AHTTP_PRINT_MARK; AHTTP_PRINT(x); AHTTP_PRINT_SP; AHTTP_PRINTLN(y); }
#define AHTTP_LOGDEBUG2(x,y,z) if(_ASYNC_HTTP_LOGLEVEL_>3) { AHTTP_PRINT_MARK; AHTTP_PRINT(x); AHTTP_PRINT_SP; AHTTP_PRINT(y); AHTTP_PRINT_SP; AHTTP_PRINTLN(z); }
#define AHTTP_LOGDEBUG3(x,y,z,w) if(_ASYNC_HTTP_LOGLEVEL_>3) { AHTTP_PRINT_MARK; AHTTP_PRINT(x); AHTTP_PRINT_SP; AHTTP_PRINT(y); AHTTP_PRINT_SP; AHTTP_PRINT(z); AHTTP_PRINT_SP; AHTTP_PRINTLN(w); }
/////////////////////////////////////////////////////////
#endif // ASYNC_HTTP_REQUEST_DEBUG_GENERIC_H

View File

@@ -17,7 +17,7 @@
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
Version: 1.1.2
Version: 1.1.3
Version Modified By Date Comments
------- ----------- ---------- -----------
@@ -27,6 +27,7 @@
1.1.0 K Hoang 23/12/2020 Add HTTP PUT, PATCH, DELETE and HEAD methods
1.1.1 K Hoang 24/12/2020 Prevent crash if request and/or method not correct.
1.1.2 K Hoang 11/02/2021 Rename _lock and _unlock to avoid conflict with AsyncWebServer library
1.1.3 K Hoang 25/02/2021 Fix non-persistent Connection header bug
*****************************************************************************************************************************/
#include "AsyncHTTPRequest_Debug_Generic.h"
@@ -96,6 +97,8 @@ bool AsyncHTTPRequest::open(const char* method, const char* URL)
if (_readyState != readyStateUnsent && _readyState != readyStateDone)
{
AHTTP_LOGDEBUG("open: not ready");
return false;
}
@@ -148,17 +151,23 @@ bool AsyncHTTPRequest::open(const char* method, const char* URL)
//////
else
{
AHTTP_LOGDEBUG("open: Bad method");
return false;
}
if (!_parseURL(URL))
{
AHTTP_LOGDEBUG("open: error parsing URL");
return false;
}
if ( _client && _client->connected() && (strcmp(_URL->host, _connectedHost) != 0 || _URL->port != _connectedPort))
{
AHTTP_LOGDEBUG("open: not connected");
return false;
}
@@ -176,11 +185,17 @@ bool AsyncHTTPRequest::open(const char* method, const char* URL)
// New in v1.1.1
_requestReadyToSend = true;
//////
AHTTP_LOGDEBUG1("open: conneting to hostname =", hostName);
return _connect();
}
else
{
AHTTP_LOGDEBUG("open: error alloc");
return false;
}
}
//**************************************************************************************************************
void AsyncHTTPRequest::onReadyStateChange(readyStateChangeCB cb, void* arg)
@@ -422,13 +437,61 @@ String AsyncHTTPRequest::responseText()
localString = _response->readString(avail);
_contentRead += localString.length();
AHTTP_LOGDEBUG3("responseText(char)", localString.substring(0, 16).c_str(), ", avail =", avail);
//AHTTP_LOGDEBUG3("responseText(char)", localString.substring(0, 16).c_str(), ", avail =", avail);
AHTTP_LOGDEBUG3("responseText(char)", localString, ", avail =", avail);
_AHTTP_unlock;
return localString;
}
//**************************************************************************************************************
#if 1
#if (ESP32)
#define GLOBAL_STR_LEN (32 * 1024)
#elif (ESP8266)
#define GLOBAL_STR_LEN (16 * 1024)
#else
#define GLOBAL_STR_LEN (4 * 1024)
#endif
char globalLongString[GLOBAL_STR_LEN + 1];
char* AsyncHTTPRequest::responseLongText()
{
AHTTP_LOGDEBUG("responseLongText()");
MUTEX_LOCK(NULL)
if ( ! _response || _readyState < readyStateLoading || ! available())
{
AHTTP_LOGDEBUG("responseText() no data");
_AHTTP_unlock;
//return String();
return NULL;
}
// String localString;
size_t avail = available();
size_t lenToCopy = (avail <= GLOBAL_STR_LEN) ? avail : GLOBAL_STR_LEN;
strncpy(globalLongString, _response->readString(avail).c_str(), lenToCopy );
globalLongString[ lenToCopy + 1 ] = 0;
_contentRead += _response->readString(avail).length();
AHTTP_LOGDEBUG3("responseLongText(char)", globalLongString, ", avail =", avail);
_AHTTP_unlock;
return globalLongString;
}
#endif
//**************************************************************************************************************
size_t AsyncHTTPRequest::responseRead(uint8_t* buf, size_t len)
{
@@ -810,7 +873,7 @@ void AsyncHTTPRequest::_processChunks()
{
char* connectionHdr = respHeaderValue("connection");
if (connectionHdr && (strcasecmp_P(connectionHdr, PSTR("disconnect")) == 0))
if (connectionHdr && (strcasecmp_P(connectionHdr, PSTR("close")) == 0))
{
AHTTP_LOGDEBUG("*all chunks received - closing TCP");
@@ -850,12 +913,19 @@ void AsyncHTTPRequest::_onConnect(AsyncClient* client)
_client = client;
_setReadyState(readyStateOpened);
// KH test
_response = new xbuf;
//_response = new xbuf(256);
//////
if (!_response)
{
_AHTTP_unlock;
// KH, to remove
AHTTP_LOGDEBUG("_onConnect: Can't new _responser");
///////
return;
}
@@ -865,11 +935,17 @@ void AsyncHTTPRequest::_onConnect(AsyncClient* client)
_client->onAck([](void* obj, AsyncClient * client, size_t len, uint32_t time)
{
(void) client;
(void) len;
(void) time;
((AsyncHTTPRequest*)(obj))->_send();
}, this);
_client->onData([](void* obj, AsyncClient * client, void* data, size_t len)
{
(void) client;
((AsyncHTTPRequest*)(obj))->_onData(data, len);
}, this);
@@ -886,6 +962,8 @@ void AsyncHTTPRequest::_onConnect(AsyncClient* client)
//**************************************************************************************************************
void AsyncHTTPRequest::_onPoll(AsyncClient* client)
{
(void) client;
MUTEX_LOCK_NR
if (_timeout && (millis() - _lastActivity) > (_timeout * 1000))
@@ -907,6 +985,8 @@ void AsyncHTTPRequest::_onPoll(AsyncClient* client)
//**************************************************************************************************************
void AsyncHTTPRequest::_onError(AsyncClient* client, int8_t error)
{
(void) client;
AHTTP_LOGDEBUG1("_onError handler error =", error);
_HTTPcode = error;
@@ -915,6 +995,8 @@ void AsyncHTTPRequest::_onError(AsyncClient* client, int8_t error)
//**************************************************************************************************************
void AsyncHTTPRequest::_onDisconnect(AsyncClient* client)
{
(void) client;
AHTTP_LOGDEBUG("\n_onDisconnect handler");
MUTEX_LOCK_NR
@@ -958,6 +1040,11 @@ void AsyncHTTPRequest::_onData(void* Vbuf, size_t len)
if (_chunks)
{
_chunks->write((uint8_t*)Vbuf, len);
// KH, to remove
AHTTP_LOGDEBUG("_onData: _processChunks");
///////
_processChunks();
}
else
@@ -972,6 +1059,10 @@ void AsyncHTTPRequest::_onData(void* Vbuf, size_t len)
{
_AHTTP_unlock;
// KH, to remove
AHTTP_LOGDEBUG("_onData: headers not complete");
///////
return;
}
}
@@ -987,7 +1078,7 @@ void AsyncHTTPRequest::_onData(void* Vbuf, size_t len)
{
char* connectionHdr = respHeaderValue("connection");
if (connectionHdr && (strcasecmp_P(connectionHdr, PSTR("disconnect")) == 0))
if (connectionHdr && (strcasecmp_P(connectionHdr, PSTR("close")) == 0))
{
AHTTP_LOGDEBUG("*all data received - closing TCP");

View File

@@ -17,7 +17,7 @@
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
Version: 1.1.2
Version: 1.1.3
Version Modified By Date Comments
------- ----------- ---------- -----------
@@ -27,6 +27,7 @@
1.1.0 K Hoang 23/12/2020 Add HTTP PUT, PATCH, DELETE and HEAD methods
1.1.1 K Hoang 24/12/2020 Prevent crash if request and/or method not correct.
1.1.2 K Hoang 11/02/2021 Rename _lock and _unlock to avoid conflict with AsyncWebServer library
1.1.3 K Hoang 25/02/2021 Fix non-persistent Connection header bug
*****************************************************************************************************************************/
#pragma once
@@ -34,7 +35,7 @@
#ifndef ASYNC_HTTP_REQUEST_GENERIC_H
#define ASYNC_HTTP_REQUEST_GENERIC_H
#define ASYNC_HTTP_REQUEST_GENERIC_VERSION "AsyncHTTPRequest_Generic v1.1.2"
#define ASYNC_HTTP_REQUEST_GENERIC_VERSION "AsyncHTTPRequest_Generic v1.1.3"
#include <Arduino.h>
@@ -224,8 +225,11 @@ class AsyncHTTPRequest
size_t responseLength(); // indicated response length or sum of chunks to date
int responseHTTPcode(); // HTTP response code or (negative) error code
String responseText(); // response (whole* or partial* as string)
char* responseLongText(); // response long (whole* or partial* as string)
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)
uint32_t elapsedTime(); // Elapsed time of in progress transaction or last completed (ms)
String version(); // Version of AsyncHTTPRequest
//___________________________________________________________________________________________________________________________________

View File

@@ -17,7 +17,7 @@
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
Version: 1.1.2
Version: 1.1.3
Version Modified By Date Comments
------- ----------- ---------- -----------
@@ -27,6 +27,7 @@
1.1.0 K Hoang 23/12/2020 Add HTTP PUT, PATCH, DELETE and HEAD methods
1.1.1 K Hoang 24/12/2020 Prevent crash if request and/or method not correct.
1.1.2 K Hoang 11/02/2021 Rename _lock and _unlock to avoid conflict with AsyncWebServer library
1.1.3 K Hoang 25/02/2021 Fix non-persistent Connection header bug
*****************************************************************************************************************************/
#include "utility/xbuf.h"

View File

@@ -17,7 +17,7 @@
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
Version: 1.1.2
Version: 1.1.3
Version Modified By Date Comments
------- ----------- ---------- -----------
@@ -27,6 +27,7 @@
1.1.0 K Hoang 23/12/2020 Add HTTP PUT, PATCH, DELETE and HEAD methods
1.1.1 K Hoang 24/12/2020 Prevent crash if request and/or method not correct.
1.1.2 K Hoang 11/02/2021 Rename _lock and _unlock to avoid conflict with AsyncWebServer library
1.1.3 K Hoang 25/02/2021 Fix non-persistent Connection header bug
*****************************************************************************************************************************/
/********************************************************************************************