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
*****************************************************************************************************************************/
#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");