#pragma once
#include "ExceptionOr.h"
#include "HTTPHeaderMap.h"
#include <wtf/HashTraits.h>
#include <wtf/Variant.h>
#include <wtf/Vector.h>
namespace WebCore {
class FetchHeaders : public RefCounted<FetchHeaders> {
public:
enum class Guard {
None,
Immutable,
Request,
RequestNoCors,
Response
};
using Init = Variant<Vector<Vector<String>>, Vector<WTF::KeyValuePair<String, String>>>;
static ExceptionOr<Ref<FetchHeaders>> create(Optional<Init>&&);
static Ref<FetchHeaders> create(Guard guard = Guard::None, HTTPHeaderMap&& headers = { }) { return adoptRef(*new FetchHeaders { guard, WTFMove(headers) }); }
static Ref<FetchHeaders> create(const FetchHeaders& headers) { return adoptRef(*new FetchHeaders { headers }); }
ExceptionOr<void> append(const String& name, const String& value);
ExceptionOr<void> remove(const String&);
ExceptionOr<String> get(const String&) const;
ExceptionOr<bool> has(const String&) const;
ExceptionOr<void> set(const String& name, const String& value);
ExceptionOr<void> fill(const Init&);
ExceptionOr<void> fill(const FetchHeaders&);
void filterAndFill(const HTTPHeaderMap&, Guard);
String fastGet(HTTPHeaderName name) const { return m_headers.get(name); }
bool fastHas(HTTPHeaderName name) const { return m_headers.contains(name); }
void fastSet(HTTPHeaderName name, const String& value) { m_headers.set(name, value); }
class Iterator {
public:
explicit Iterator(FetchHeaders&);
Optional<WTF::KeyValuePair<String, String>> next();
private:
Ref<FetchHeaders> m_headers;
size_t m_currentIndex { 0 };
Vector<String> m_keys;
};
Iterator createIterator() { return Iterator { *this }; }
const HTTPHeaderMap& internalHeaders() const { return m_headers; }
void setGuard(Guard);
Guard guard() const { return m_guard; }
private:
FetchHeaders(Guard, HTTPHeaderMap&&);
FetchHeaders(const FetchHeaders&);
Guard m_guard;
HTTPHeaderMap m_headers;
};
inline FetchHeaders::FetchHeaders(Guard guard, HTTPHeaderMap&& headers)
: m_guard(guard)
, m_headers(WTFMove(headers))
{
}
inline FetchHeaders::FetchHeaders(const FetchHeaders& other)
: RefCounted<FetchHeaders>()
, m_guard(other.m_guard)
, m_headers(other.m_headers)
{
}
inline void FetchHeaders::setGuard(Guard guard)
{
ASSERT(!m_headers.size());
m_guard = guard;
}
}
namespace WTF {
template<> struct EnumTraits<WebCore::FetchHeaders::Guard> {
using values = EnumValues<
WebCore::FetchHeaders::Guard,
WebCore::FetchHeaders::Guard::None,
WebCore::FetchHeaders::Guard::Immutable,
WebCore::FetchHeaders::Guard::Request,
WebCore::FetchHeaders::Guard::RequestNoCors,
WebCore::FetchHeaders::Guard::Response
>;
};
}