CrossOriginPreflightResultCache.h [plain text]
#pragma once
#include "StoredCredentialsPolicy.h"
#include <wtf/Expected.h>
#include <wtf/HashMap.h>
#include <wtf/HashSet.h>
#include <wtf/MonotonicTime.h>
#include <wtf/URLHash.h>
#include <wtf/UniqueRef.h>
namespace WebCore {
class HTTPHeaderMap;
class ResourceResponse;
class CrossOriginPreflightResultCacheItem {
WTF_MAKE_NONCOPYABLE(CrossOriginPreflightResultCacheItem); WTF_MAKE_FAST_ALLOCATED;
public:
static Expected<UniqueRef<CrossOriginPreflightResultCacheItem>, String> create(StoredCredentialsPolicy, const ResourceResponse&);
CrossOriginPreflightResultCacheItem(MonotonicTime, StoredCredentialsPolicy, HashSet<String>&&, HashSet<String, ASCIICaseInsensitiveHash>&&);
Optional<String> validateMethodAndHeaders(const String& method, const HTTPHeaderMap&) const;
bool allowsRequest(StoredCredentialsPolicy, const String& method, const HTTPHeaderMap&) const;
private:
bool allowsCrossOriginMethod(const String&, StoredCredentialsPolicy) const;
Optional<String> validateCrossOriginHeaders(const HTTPHeaderMap&, StoredCredentialsPolicy) const;
MonotonicTime m_absoluteExpiryTime;
StoredCredentialsPolicy m_storedCredentialsPolicy;
HashSet<String> m_methods;
HashSet<String, ASCIICaseInsensitiveHash> m_headers;
};
class CrossOriginPreflightResultCache {
WTF_MAKE_NONCOPYABLE(CrossOriginPreflightResultCache); WTF_MAKE_FAST_ALLOCATED;
public:
WEBCORE_EXPORT static CrossOriginPreflightResultCache& singleton();
WEBCORE_EXPORT void appendEntry(const String& origin, const URL&, std::unique_ptr<CrossOriginPreflightResultCacheItem>);
WEBCORE_EXPORT bool canSkipPreflight(const String& origin, const URL&, StoredCredentialsPolicy, const String& method, const HTTPHeaderMap& requestHeaders);
WEBCORE_EXPORT void clear();
private:
friend NeverDestroyed<CrossOriginPreflightResultCache>;
CrossOriginPreflightResultCache();
HashMap<std::pair<String, URL>, std::unique_ptr<CrossOriginPreflightResultCacheItem>> m_preflightHashMap;
};
inline CrossOriginPreflightResultCacheItem::CrossOriginPreflightResultCacheItem(MonotonicTime absoluteExpiryTime, StoredCredentialsPolicy storedCredentialsPolicy, HashSet<String>&& methods, HashSet<String, ASCIICaseInsensitiveHash>&& headers)
: m_absoluteExpiryTime(absoluteExpiryTime)
, m_storedCredentialsPolicy(storedCredentialsPolicy)
, m_methods(WTFMove(methods))
, m_headers(WTFMove(headers))
{
}
}