Skip to content

Fix Check for _cookieJar in HTTPClient (#6266) #6280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 16, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions libraries/HTTPClient/src/HTTPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1543,6 +1543,10 @@ void HTTPClient::clearAllCookies()

void HTTPClient::setCookie(String date, String headerValue)
{
if (!_cookieJar)
{
return;
}
#define HTTP_TIME_PATTERN "%a, %d %b %Y %H:%M:%S"

Cookie cookie;
Expand Down Expand Up @@ -1574,7 +1578,7 @@ void HTTPClient::setCookie(String date, String headerValue)
value = headerValue.substring(pos1, pos2);
else
value = headerValue.substring(pos1);

strptime(value.c_str(), HTTP_TIME_PATTERN, &tm);
cookie.expires.date = mktime(&tm);
cookie.expires.valid = true;
Expand All @@ -1589,7 +1593,7 @@ void HTTPClient::setCookie(String date, String headerValue)
value = headerValue.substring(pos1, pos2);
else
value = headerValue.substring(pos1);

cookie.max_age.duration = value.toInt();
cookie.max_age.valid = true;
}
Expand Down Expand Up @@ -1639,10 +1643,10 @@ void HTTPClient::setCookie(String date, String headerValue)
// overwrite or delete cookie in/from cookie jar
time_t now_local = time(NULL);
time_t now_gmt = mktime(gmtime(&now_local));

bool found = false;

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

if (!_cookieJar)
{
return false;
}
for (auto c = _cookieJar->begin(); c != _cookieJar->end(); ++c) {
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)) {
_cookieJar->erase(c);
Expand All @@ -1682,5 +1690,6 @@ bool HTTPClient::generateCookieString(String *cookieString)
found = true;
}
}

return found;
}