Skip to content

Commit c99f594

Browse files
authored
Fix Check for _cookieJar in HTTPClient (espressif#6266) (espressif#6280)
* Check for cookieJar before setting cookies * Return as soon as possible w/o _cookieJar
1 parent 05d8cdd commit c99f594

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

libraries/HTTPClient/src/HTTPClient.cpp

+13-4
Original file line numberDiff line numberDiff line change
@@ -1543,6 +1543,10 @@ void HTTPClient::clearAllCookies()
15431543

15441544
void HTTPClient::setCookie(String date, String headerValue)
15451545
{
1546+
if (!_cookieJar)
1547+
{
1548+
return;
1549+
}
15461550
#define HTTP_TIME_PATTERN "%a, %d %b %Y %H:%M:%S"
15471551

15481552
Cookie cookie;
@@ -1574,7 +1578,7 @@ void HTTPClient::setCookie(String date, String headerValue)
15741578
value = headerValue.substring(pos1, pos2);
15751579
else
15761580
value = headerValue.substring(pos1);
1577-
1581+
15781582
strptime(value.c_str(), HTTP_TIME_PATTERN, &tm);
15791583
cookie.expires.date = mktime(&tm);
15801584
cookie.expires.valid = true;
@@ -1589,7 +1593,7 @@ void HTTPClient::setCookie(String date, String headerValue)
15891593
value = headerValue.substring(pos1, pos2);
15901594
else
15911595
value = headerValue.substring(pos1);
1592-
1596+
15931597
cookie.max_age.duration = value.toInt();
15941598
cookie.max_age.valid = true;
15951599
}
@@ -1639,10 +1643,10 @@ void HTTPClient::setCookie(String date, String headerValue)
16391643
// overwrite or delete cookie in/from cookie jar
16401644
time_t now_local = time(NULL);
16411645
time_t now_gmt = mktime(gmtime(&now_local));
1642-
1646+
16431647
bool found = false;
16441648

1645-
for (auto c = _cookieJar->begin(); c != _cookieJar->end(); ++c) {
1649+
for (auto c = _cookieJar->begin(); c != _cookieJar->end(); ++c) {
16461650
if (c->domain == cookie.domain && c->name == cookie.name) {
16471651
// when evaluating, max-age takes precedence over expires if both are defined
16481652
if ((cookie.max_age.valid && ((cookie.date + cookie.max_age.duration) < now_gmt)) || cookie.max_age.duration <= 0
@@ -1670,6 +1674,10 @@ bool HTTPClient::generateCookieString(String *cookieString)
16701674
*cookieString = "";
16711675
bool found = false;
16721676

1677+
if (!_cookieJar)
1678+
{
1679+
return false;
1680+
}
16731681
for (auto c = _cookieJar->begin(); c != _cookieJar->end(); ++c) {
16741682
if ((c->max_age.valid && ((c->date + c->max_age.duration) < now_gmt)) || (!c->max_age.valid && c->expires.valid && c->expires.date < now_gmt)) {
16751683
_cookieJar->erase(c);
@@ -1682,5 +1690,6 @@ bool HTTPClient::generateCookieString(String *cookieString)
16821690
found = true;
16831691
}
16841692
}
1693+
16851694
return found;
16861695
}

0 commit comments

Comments
 (0)