1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 12:05:00 +00:00
serenity/Userland/Libraries/LibCore/Proxy.h
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

62 lines
1.4 KiB
C++

/*
* Copyright (c) 2022, Ali Mohammad Pur <mpfard@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Error.h>
#include <AK/IPv4Address.h>
#include <AK/Types.h>
#include <AK/URL.h>
#include <LibIPC/Forward.h>
namespace Core {
// FIXME: Username/password support.
struct ProxyData {
enum Type {
Direct,
SOCKS5,
} type { Type::Direct };
u32 host_ipv4 { 0 };
int port { 0 };
bool operator==(ProxyData const& other) const = default;
static ErrorOr<ProxyData> parse_url(URL const& url)
{
if (!url.is_valid())
return Error::from_string_literal("Invalid proxy URL");
ProxyData proxy_data;
if (url.scheme() != "socks5")
return Error::from_string_literal("Unsupported proxy type");
proxy_data.type = ProxyData::Type::SOCKS5;
auto host_ipv4 = IPv4Address::from_string(url.host());
if (!host_ipv4.has_value())
return Error::from_string_literal("Invalid proxy host, must be an IPv4 address");
proxy_data.host_ipv4 = host_ipv4->to_u32();
auto port = url.port();
if (!port.has_value())
return Error::from_string_literal("Invalid proxy, must have a port");
proxy_data.port = *port;
return proxy_data;
}
};
}
namespace IPC {
template<>
bool encode(Encoder&, Core::ProxyData const&);
template<>
ErrorOr<Core::ProxyData> decode(Decoder&);
}