Skip to content

Commit

Permalink
Browser: Process Max-Age cookie attribute
Browse files Browse the repository at this point in the history
Note: the default expiry time should be the "the latest representable
date". However, DateTime::from_timestamp(NumericLimits<time_t>::max())
isn't feasible due to the for-loops in LibC's time_to_tm. So instead,
this just sets the date to the maxium year.
  • Loading branch information
trflynn89 authored and awesomekling committed Apr 12, 2021
1 parent d610aeb commit a554676
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
24 changes: 23 additions & 1 deletion Userland/Applications/Browser/CookieJar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
*/

#include "CookieJar.h"
#include <AK/NumericLimits.h>
#include <AK/URL.h>
#include <ctype.h>

namespace Browser {

Expand Down Expand Up @@ -128,6 +130,8 @@ Optional<Cookie> CookieJar::parse_cookie(const String& cookie_string)
// 6. The cookie-name is the name string, and the cookie-value is the value string.
Cookie cookie { name, value };

cookie.expiry_time = Core::DateTime::create(AK::NumericLimits<unsigned>::max());

parse_attributes(cookie, unparsed_attributes);
return cookie;
}
Expand Down Expand Up @@ -204,9 +208,27 @@ void CookieJar::on_expires_attribute([[maybe_unused]] Cookie& cookie, [[maybe_un
// https://tools.ietf.org/html/rfc6265#section-5.2.1
}

void CookieJar::on_max_age_attribute([[maybe_unused]] Cookie& cookie, [[maybe_unused]] StringView attribute_value)
void CookieJar::on_max_age_attribute(Cookie& cookie, StringView attribute_value)
{
// https://tools.ietf.org/html/rfc6265#section-5.2.2

// If the first character of the attribute-value is not a DIGIT or a "-" character, ignore the cookie-av.
if (attribute_value.is_empty() || (!isdigit(attribute_value[0]) && (attribute_value[0] != '-')))
return;

// Let delta-seconds be the attribute-value converted to an integer.
if (auto delta_seconds = attribute_value.to_int(); delta_seconds.has_value()) {
Core::DateTime expiry_time;

if (*delta_seconds <= 0) {
// If delta-seconds is less than or equal to zero (0), let expiry-time be the earliest representable date and time.
cookie.expiry_time = Core::DateTime::from_timestamp(0);
} else {
// Otherwise, let the expiry-time be the current date and time plus delta-seconds seconds.
time_t now = Core::DateTime::now().timestamp();
cookie.expiry_time = Core::DateTime::from_timestamp(now + *delta_seconds);
}
}
}

void CookieJar::on_domain_attribute([[maybe_unused]] Cookie& cookie, [[maybe_unused]] StringView attribute_value)
Expand Down
2 changes: 2 additions & 0 deletions Userland/Applications/Browser/CookieJar.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@
#include <AK/Optional.h>
#include <AK/String.h>
#include <AK/Vector.h>
#include <LibCore/DateTime.h>

namespace Browser {

struct Cookie {
String name;
String value;
Core::DateTime expiry_time {};
};

class CookieJar {
Expand Down

0 comments on commit a554676

Please sign in to comment.