Skip to content

Commit 371e617

Browse files
authored
Return as soon as possible w/o _cookieJar
1 parent e628569 commit 371e617

File tree

1 file changed

+105
-102
lines changed

1 file changed

+105
-102
lines changed

libraries/HTTPClient/src/HTTPClient.cpp

Lines changed: 105 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,126 +1543,127 @@ void HTTPClient::clearAllCookies()
15431543

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

1550-
Cookie cookie;
1551-
String value;
1552-
int pos1, pos2;
1552+
Cookie cookie;
1553+
String value;
1554+
int pos1, pos2;
15531555

1554-
headerValue.toLowerCase();
1556+
headerValue.toLowerCase();
15551557

1556-
struct tm tm;
1557-
strptime(date.c_str(), HTTP_TIME_PATTERN, &tm);
1558-
cookie.date = mktime(&tm);
1558+
struct tm tm;
1559+
strptime(date.c_str(), HTTP_TIME_PATTERN, &tm);
1560+
cookie.date = mktime(&tm);
15591561

1560-
pos1 = headerValue.indexOf('=');
1561-
pos2 = headerValue.indexOf(';');
1562+
pos1 = headerValue.indexOf('=');
1563+
pos2 = headerValue.indexOf(';');
15621564

1563-
if (pos1 >= 0 && pos2 > pos1){
1564-
cookie.name = headerValue.substring(0, pos1);
1565-
cookie.value = headerValue.substring(pos1 + 1, pos2);
1566-
} else {
1567-
return; // invalid cookie header
1568-
}
1565+
if (pos1 >= 0 && pos2 > pos1){
1566+
cookie.name = headerValue.substring(0, pos1);
1567+
cookie.value = headerValue.substring(pos1 + 1, pos2);
1568+
} else {
1569+
return; // invalid cookie header
1570+
}
15691571

1570-
// expires
1571-
if (headerValue.indexOf("expires=") >= 0){
1572-
pos1 = headerValue.indexOf("expires=") + strlen("expires=");
1573-
pos2 = headerValue.indexOf(';', pos1);
1572+
// expires
1573+
if (headerValue.indexOf("expires=") >= 0){
1574+
pos1 = headerValue.indexOf("expires=") + strlen("expires=");
1575+
pos2 = headerValue.indexOf(';', pos1);
15741576

1575-
if (pos2 > pos1)
1576-
value = headerValue.substring(pos1, pos2);
1577-
else
1578-
value = headerValue.substring(pos1);
1577+
if (pos2 > pos1)
1578+
value = headerValue.substring(pos1, pos2);
1579+
else
1580+
value = headerValue.substring(pos1);
15791581

1580-
strptime(value.c_str(), HTTP_TIME_PATTERN, &tm);
1581-
cookie.expires.date = mktime(&tm);
1582-
cookie.expires.valid = true;
1583-
}
1582+
strptime(value.c_str(), HTTP_TIME_PATTERN, &tm);
1583+
cookie.expires.date = mktime(&tm);
1584+
cookie.expires.valid = true;
1585+
}
15841586

1585-
// max-age
1586-
if (headerValue.indexOf("max-age=") >= 0){
1587-
pos1 = headerValue.indexOf("max-age=") + strlen("max-age=");
1588-
pos2 = headerValue.indexOf(';', pos1);
1587+
// max-age
1588+
if (headerValue.indexOf("max-age=") >= 0){
1589+
pos1 = headerValue.indexOf("max-age=") + strlen("max-age=");
1590+
pos2 = headerValue.indexOf(';', pos1);
15891591

1590-
if (pos2 > pos1)
1591-
value = headerValue.substring(pos1, pos2);
1592-
else
1593-
value = headerValue.substring(pos1);
1592+
if (pos2 > pos1)
1593+
value = headerValue.substring(pos1, pos2);
1594+
else
1595+
value = headerValue.substring(pos1);
15941596

1595-
cookie.max_age.duration = value.toInt();
1596-
cookie.max_age.valid = true;
1597-
}
1597+
cookie.max_age.duration = value.toInt();
1598+
cookie.max_age.valid = true;
1599+
}
15981600

1599-
// domain
1600-
if (headerValue.indexOf("domain=") >= 0){
1601-
pos1 = headerValue.indexOf("domain=") + strlen("domain=");
1602-
pos2 = headerValue.indexOf(';', pos1);
1601+
// domain
1602+
if (headerValue.indexOf("domain=") >= 0){
1603+
pos1 = headerValue.indexOf("domain=") + strlen("domain=");
1604+
pos2 = headerValue.indexOf(';', pos1);
16031605

1604-
if (pos2 > pos1)
1605-
value = headerValue.substring(pos1, pos2);
1606-
else
1607-
value = headerValue.substring(pos1);
1606+
if (pos2 > pos1)
1607+
value = headerValue.substring(pos1, pos2);
1608+
else
1609+
value = headerValue.substring(pos1);
16081610

1609-
if (value.startsWith(".")) value.remove(0, 1);
1611+
if (value.startsWith(".")) value.remove(0, 1);
16101612

1611-
if (_host.indexOf(value) >= 0) {
1612-
cookie.domain = value;
1613-
} else {
1614-
return; // server tries to set a cookie on a different domain; ignore it
1615-
}
1613+
if (_host.indexOf(value) >= 0) {
1614+
cookie.domain = value;
16161615
} else {
1617-
pos1 = _host.lastIndexOf('.', _host.lastIndexOf('.') - 1);
1618-
if (pos1 >= 0)
1619-
cookie.domain = _host.substring(pos1 + 1);
1620-
else
1621-
cookie.domain = _host;
1616+
return; // server tries to set a cookie on a different domain; ignore it
16221617
}
1618+
} else {
1619+
pos1 = _host.lastIndexOf('.', _host.lastIndexOf('.') - 1);
1620+
if (pos1 >= 0)
1621+
cookie.domain = _host.substring(pos1 + 1);
1622+
else
1623+
cookie.domain = _host;
1624+
}
16231625

1624-
// path
1625-
if (headerValue.indexOf("path=") >= 0){
1626-
pos1 = headerValue.indexOf("path=") + strlen("path=");
1627-
pos2 = headerValue.indexOf(';', pos1);
1626+
// path
1627+
if (headerValue.indexOf("path=") >= 0){
1628+
pos1 = headerValue.indexOf("path=") + strlen("path=");
1629+
pos2 = headerValue.indexOf(';', pos1);
16281630

1629-
if (pos2 > pos1)
1630-
cookie.path = headerValue.substring(pos1, pos2);
1631-
else
1632-
cookie.path = headerValue.substring(pos1);
1633-
}
1631+
if (pos2 > pos1)
1632+
cookie.path = headerValue.substring(pos1, pos2);
1633+
else
1634+
cookie.path = headerValue.substring(pos1);
1635+
}
16341636

1635-
// HttpOnly
1636-
cookie.http_only = (headerValue.indexOf("httponly") >= 0);
1637+
// HttpOnly
1638+
cookie.http_only = (headerValue.indexOf("httponly") >= 0);
16371639

1638-
// secure
1639-
cookie.secure = (headerValue.indexOf("secure") >= 0);
1640+
// secure
1641+
cookie.secure = (headerValue.indexOf("secure") >= 0);
16401642

1641-
// overwrite or delete cookie in/from cookie jar
1642-
time_t now_local = time(NULL);
1643-
time_t now_gmt = mktime(gmtime(&now_local));
1643+
// overwrite or delete cookie in/from cookie jar
1644+
time_t now_local = time(NULL);
1645+
time_t now_gmt = mktime(gmtime(&now_local));
16441646

1645-
bool found = false;
1647+
bool found = false;
16461648

1647-
for (auto c = _cookieJar->begin(); c != _cookieJar->end(); ++c) {
1648-
if (c->domain == cookie.domain && c->name == cookie.name) {
1649-
// when evaluating, max-age takes precedence over expires if both are defined
1650-
if ((cookie.max_age.valid && ((cookie.date + cookie.max_age.duration) < now_gmt)) || cookie.max_age.duration <= 0
1651-
|| (!cookie.max_age.valid && cookie.expires.valid && cookie.expires.date < now_gmt)) {
1652-
_cookieJar->erase(c);
1653-
c--;
1654-
} else {
1655-
*c = cookie;
1656-
}
1657-
found = true;
1649+
for (auto c = _cookieJar->begin(); c != _cookieJar->end(); ++c) {
1650+
if (c->domain == cookie.domain && c->name == cookie.name) {
1651+
// when evaluating, max-age takes precedence over expires if both are defined
1652+
if ((cookie.max_age.valid && ((cookie.date + cookie.max_age.duration) < now_gmt)) || cookie.max_age.duration <= 0
1653+
|| (!cookie.max_age.valid && cookie.expires.valid && cookie.expires.date < now_gmt)) {
1654+
_cookieJar->erase(c);
1655+
c--;
1656+
} else {
1657+
*c = cookie;
16581658
}
1659+
found = true;
16591660
}
1660-
1661-
// add cookie to jar
1662-
if (!found && !(cookie.max_age.valid && cookie.max_age.duration <= 0))
1663-
_cookieJar->push_back(cookie);
16641661
}
16651662

1663+
// add cookie to jar
1664+
if (!found && !(cookie.max_age.valid && cookie.max_age.duration <= 0))
1665+
_cookieJar->push_back(cookie);
1666+
16661667
}
16671668

16681669
bool HTTPClient::generateCookieString(String *cookieString)
@@ -1673,20 +1674,22 @@ bool HTTPClient::generateCookieString(String *cookieString)
16731674
*cookieString = "";
16741675
bool found = false;
16751676

1676-
if (_cookieJar)
1677+
if (!_cookieJar)
16771678
{
1679+
return false;
1680+
}
16781681
for (auto c = _cookieJar->begin(); c != _cookieJar->end(); ++c) {
1679-
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)) {
1680-
_cookieJar->erase(c);
1681-
c--;
1682-
} else if (_host.indexOf(c->domain) >= 0 && (!c->secure || _secure) ) {
1683-
if (*cookieString == "")
1684-
*cookieString = c->name + "=" + c->value;
1685-
else
1686-
*cookieString += " ;" + c->name + "=" + c->value;
1687-
found = true;
1688-
}
1682+
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)) {
1683+
_cookieJar->erase(c);
1684+
c--;
1685+
} else if (_host.indexOf(c->domain) >= 0 && (!c->secure || _secure) ) {
1686+
if (*cookieString == "")
1687+
*cookieString = c->name + "=" + c->value;
1688+
else
1689+
*cookieString += " ;" + c->name + "=" + c->value;
1690+
found = true;
16891691
}
16901692
}
1693+
16911694
return found;
16921695
}

0 commit comments

Comments
 (0)