@@ -1543,126 +1543,127 @@ void HTTPClient::clearAllCookies()
1543
1543
1544
1544
void HTTPClient::setCookie (String date, String headerValue)
1545
1545
{
1546
- if (_cookieJar)
1546
+ if (! _cookieJar)
1547
1547
{
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"
1549
1551
1550
- Cookie cookie;
1551
- String value;
1552
- int pos1, pos2;
1552
+ Cookie cookie;
1553
+ String value;
1554
+ int pos1, pos2;
1553
1555
1554
- headerValue.toLowerCase ();
1556
+ headerValue.toLowerCase ();
1555
1557
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);
1559
1561
1560
- pos1 = headerValue.indexOf (' =' );
1561
- pos2 = headerValue.indexOf (' ;' );
1562
+ pos1 = headerValue.indexOf (' =' );
1563
+ pos2 = headerValue.indexOf (' ;' );
1562
1564
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
+ }
1569
1571
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);
1574
1576
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);
1579
1581
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
+ }
1584
1586
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);
1589
1591
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);
1594
1596
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
+ }
1598
1600
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);
1603
1605
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);
1608
1610
1609
- if (value.startsWith (" ." )) value.remove (0 , 1 );
1611
+ if (value.startsWith (" ." )) value.remove (0 , 1 );
1610
1612
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;
1616
1615
} 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
1622
1617
}
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
+ }
1623
1625
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);
1628
1630
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
+ }
1634
1636
1635
- // HttpOnly
1636
- cookie.http_only = (headerValue.indexOf (" httponly" ) >= 0 );
1637
+ // HttpOnly
1638
+ cookie.http_only = (headerValue.indexOf (" httponly" ) >= 0 );
1637
1639
1638
- // secure
1639
- cookie.secure = (headerValue.indexOf (" secure" ) >= 0 );
1640
+ // secure
1641
+ cookie.secure = (headerValue.indexOf (" secure" ) >= 0 );
1640
1642
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));
1644
1646
1645
- bool found = false ;
1647
+ bool found = false ;
1646
1648
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;
1658
1658
}
1659
+ found = true ;
1659
1660
}
1660
-
1661
- // add cookie to jar
1662
- if (!found && !(cookie.max_age .valid && cookie.max_age .duration <= 0 ))
1663
- _cookieJar->push_back (cookie);
1664
1661
}
1665
1662
1663
+ // add cookie to jar
1664
+ if (!found && !(cookie.max_age .valid && cookie.max_age .duration <= 0 ))
1665
+ _cookieJar->push_back (cookie);
1666
+
1666
1667
}
1667
1668
1668
1669
bool HTTPClient::generateCookieString (String *cookieString)
@@ -1673,20 +1674,22 @@ bool HTTPClient::generateCookieString(String *cookieString)
1673
1674
*cookieString = " " ;
1674
1675
bool found = false ;
1675
1676
1676
- if (_cookieJar)
1677
+ if (! _cookieJar)
1677
1678
{
1679
+ return false ;
1680
+ }
1678
1681
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 ;
1689
1691
}
1690
1692
}
1693
+
1691
1694
return found;
1692
1695
}
0 commit comments