1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 07:38:10 +00:00
serenity/Userland/Libraries/LibWeb/Cookie/Cookie.cpp
Timothy Flynn 9b483625e6 LibIPC+Everywhere: Change IPC decoders to construct values in-place
Currently, the generated IPC decoders will default-construct the type to
be decoded, then pass that value by reference to the concrete decoder.
This, of course, requires that the type is default-constructible. This
was an issue for decoding Variants, which had to require the first type
in the Variant list is Empty, to ensure it is default constructible.

Further, this made it possible for values to become uninitialized in
user-defined decoders.

This patch makes the decoder interface such that the concrete decoders
themselves contruct the decoded type upon return from the decoder. To do
so, the default decoders in IPC::Decoder had to be moved to the IPC
namespace scope, as these decoders are now specializations instead of
overloaded methods (C++ requires specializations to be in a namespace
scope).
2022-12-26 09:36:16 +01:00

77 lines
2.3 KiB
C++

/*
* Copyright (c) 2022, Tobias Christiansen <tobyase@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "Cookie.h"
#include <LibIPC/Decoder.h>
#include <LibIPC/Encoder.h>
namespace Web::Cookie {
StringView same_site_to_string(SameSite same_site)
{
switch (same_site) {
case SameSite::Default:
return "Default"sv;
case SameSite::None:
return "None"sv;
case SameSite::Lax:
return "Lax"sv;
case SameSite::Strict:
return "Strict"sv;
}
VERIFY_NOT_REACHED();
}
SameSite same_site_from_string(StringView same_site_mode)
{
if (same_site_mode.equals_ignoring_case("None"sv))
return SameSite::None;
if (same_site_mode.equals_ignoring_case("Strict"sv))
return SameSite::Strict;
if (same_site_mode.equals_ignoring_case("Lax"sv))
return SameSite::Lax;
return SameSite::Default;
}
}
template<>
bool IPC::encode(Encoder& encoder, Web::Cookie::Cookie const& cookie)
{
encoder << cookie.name;
encoder << cookie.value;
encoder << cookie.domain;
encoder << cookie.path;
encoder << cookie.creation_time;
encoder << cookie.expiry_time;
encoder << cookie.host_only;
encoder << cookie.http_only;
encoder << cookie.last_access_time;
encoder << cookie.persistent;
encoder << cookie.secure;
encoder << cookie.same_site;
return true;
}
template<>
ErrorOr<Web::Cookie::Cookie> IPC::decode(Decoder& decoder)
{
auto name = TRY(decoder.decode<DeprecatedString>());
auto value = TRY(decoder.decode<DeprecatedString>());
auto domain = TRY(decoder.decode<DeprecatedString>());
auto path = TRY(decoder.decode<DeprecatedString>());
auto creation_time = TRY(decoder.decode<Core::DateTime>());
auto expiry_time = TRY(decoder.decode<Core::DateTime>());
auto host_only = TRY(decoder.decode<bool>());
auto http_only = TRY(decoder.decode<bool>());
auto last_access_time = TRY(decoder.decode<Core::DateTime>());
auto persistent = TRY(decoder.decode<bool>());
auto secure = TRY(decoder.decode<bool>());
auto same_site = TRY(decoder.decode<Web::Cookie::SameSite>());
return Web::Cookie::Cookie { move(name), move(value), same_site, move(creation_time), move(last_access_time), move(expiry_time), move(domain), move(path), secure, http_only, host_only, persistent };
}