ladybird/Libraries/LibHTTP/HeaderMap.h
Jonne Ransijn d7596a0a61 AK: Don't implicitly convert Optional<T&> to Optional<T>
C++ will jovially select the implicit conversion operator, even if it's
complete bogus, such as for unknown-size types or non-destructible
types. Therefore, all such conversions (which incur a copy) must
(unfortunately) be explicit so that non-copyable types continue to work.

NOTE: We make an exception for trivially copyable types, since they
are, well, trivially copyable.

Co-authored-by: kleines Filmröllchen <filmroellchen@serenityos.org>
2024-12-04 01:58:22 +01:00

75 lines
1.5 KiB
C++

/*
* Copyright (c) 2024, Andreas Kling <andreas@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibHTTP/Header.h>
namespace HTTP {
class HeaderMap {
public:
HeaderMap() = default;
~HeaderMap() = default;
HeaderMap(Vector<Header> headers)
: m_headers(move(headers))
{
for (auto& header : m_headers)
m_map.set(header.name, header.value);
}
HeaderMap(HeaderMap const&) = default;
HeaderMap(HeaderMap&&) = default;
HeaderMap& operator=(HeaderMap const&) = default;
HeaderMap& operator=(HeaderMap&&) = default;
void set(ByteString name, ByteString value)
{
m_map.set(name, value);
m_headers.append({ move(name), move(value) });
}
[[nodiscard]] bool contains(ByteString const& name) const
{
return m_map.contains(name);
}
[[nodiscard]] Optional<ByteString const&> get(ByteString const& name) const
{
return m_map.get(name);
}
[[nodiscard]] Vector<Header> const& headers() const
{
return m_headers;
}
private:
HashMap<ByteString, ByteString, CaseInsensitiveStringTraits> m_map;
Vector<Header> m_headers;
};
}
namespace IPC {
template<>
inline ErrorOr<void> encode(Encoder& encoder, HTTP::HeaderMap const& header_map)
{
TRY(encoder.encode(header_map.headers()));
return {};
}
template<>
inline ErrorOr<HTTP::HeaderMap> decode(Decoder& decoder)
{
auto headers = TRY(decoder.decode<Vector<HTTP::Header>>());
return HTTP::HeaderMap { move(headers) };
}
}