#include "config.h"
#include "FetchRequest.h"
#include "HTTPParsers.h"
#include "ScriptExecutionContext.h"
#include "SecurityOrigin.h"
namespace WebCore {
static std::optional<Exception> setMethod(ResourceRequest& request, const String& initMethod)
{
if (!isValidHTTPToken(initMethod))
return Exception { TypeError, "Method is not a valid HTTP token."_s };
if (isForbiddenMethod(initMethod))
return Exception { TypeError, "Method is forbidden."_s };
request.setHTTPMethod(normalizeHTTPMethod(initMethod));
return std::nullopt;
}
static ExceptionOr<String> computeReferrer(ScriptExecutionContext& context, const String& referrer)
{
if (referrer.isEmpty())
return String { "no-referrer" };
URL referrerURL = context.completeURL(referrer);
if (!referrerURL.isValid())
return Exception { TypeError, "Referrer is not a valid URL."_s };
if (referrerURL.protocolIs("about") && referrerURL.path() == "client")
return String { "client" };
if (!(context.securityOrigin() && context.securityOrigin()->canRequest(referrerURL)))
return Exception { TypeError, "Referrer is not same-origin."_s };
return String { referrerURL.string() };
}
static std::optional<Exception> buildOptions(FetchOptions& options, ResourceRequest& request, String& referrer, ScriptExecutionContext& context, const FetchRequest::Init& init)
{
if (!init.window.isUndefinedOrNull() && !init.window.isEmpty())
return Exception { TypeError, "Window can only be null."_s };
if (init.hasMembers()) {
if (options.mode == FetchOptions::Mode::Navigate)
options.mode = FetchOptions::Mode::SameOrigin;
referrer = "client"_s;
options.referrerPolicy = { };
}
if (!init.referrer.isNull()) {
auto result = computeReferrer(context, init.referrer);
if (result.hasException())
return result.releaseException();
referrer = result.releaseReturnValue();
}
if (init.referrerPolicy)
options.referrerPolicy = init.referrerPolicy.value();
if (init.mode) {
options.mode = init.mode.value();
if (options.mode == FetchOptions::Mode::Navigate)
return Exception { TypeError, "Request constructor does not accept navigate fetch mode."_s };
}
if (init.credentials)
options.credentials = init.credentials.value();
if (init.cache)
options.cache = init.cache.value();
if (options.cache == FetchOptions::Cache::OnlyIfCached && options.mode != FetchOptions::Mode::SameOrigin)
return Exception { TypeError, "only-if-cached cache option requires fetch mode to be same-origin."_s };
if (init.redirect)
options.redirect = init.redirect.value();
if (!init.integrity.isNull())
options.integrity = init.integrity;
if (init.keepalive && init.keepalive.value())
options.keepAlive = true;
if (!init.method.isNull()) {
if (auto exception = setMethod(request, init.method))
return exception;
}
return std::nullopt;
}
static bool methodCanHaveBody(const ResourceRequest& request)
{
return request.httpMethod() != "GET" && request.httpMethod() != "HEAD";
}
ExceptionOr<void> FetchRequest::initializeOptions(const Init& init)
{
ASSERT(scriptExecutionContext());
auto exception = buildOptions(m_options, m_request, m_referrer, *scriptExecutionContext(), init);
if (exception)
return WTFMove(exception.value());
if (m_options.mode == FetchOptions::Mode::NoCors) {
const String& method = m_request.httpMethod();
if (method != "GET" && method != "POST" && method != "HEAD")
return Exception { TypeError, "Method must be GET, POST or HEAD in no-cors mode."_s };
if (!m_options.integrity.isEmpty())
return Exception { TypeError, "There cannot be an integrity in no-cors mode."_s };
m_headers->setGuard(FetchHeaders::Guard::RequestNoCors);
}
return { };
}
ExceptionOr<void> FetchRequest::initializeWith(const String& url, Init&& init)
{
ASSERT(scriptExecutionContext());
URL requestURL = scriptExecutionContext()->completeURL(url);
if (!requestURL.isValid() || !requestURL.user().isEmpty() || !requestURL.pass().isEmpty())
return Exception { TypeError, "URL is not valid or contains user credentials."_s };
m_options.mode = Mode::Cors;
m_options.credentials = Credentials::SameOrigin;
m_referrer = "client"_s;
m_request.setURL(requestURL);
m_request.setRequester(ResourceRequest::Requester::Fetch);
m_request.setInitiatorIdentifier(scriptExecutionContext()->resourceRequestIdentifier());
auto optionsResult = initializeOptions(init);
if (optionsResult.hasException())
return optionsResult.releaseException();
if (init.headers) {
auto fillResult = m_headers->fill(*init.headers);
if (fillResult.hasException())
return fillResult.releaseException();
}
if (init.body) {
auto setBodyResult = setBody(WTFMove(*init.body));
if (setBodyResult.hasException())
return setBodyResult.releaseException();
}
updateContentType();
return { };
}
ExceptionOr<void> FetchRequest::initializeWith(FetchRequest& input, Init&& init)
{
if (input.isDisturbedOrLocked())
return Exception {TypeError, "Request input is disturbed or locked."_s };
m_request = input.m_request;
m_options = input.m_options;
m_referrer = input.m_referrer;
auto optionsResult = initializeOptions(init);
if (optionsResult.hasException())
return optionsResult.releaseException();
if (init.headers) {
auto fillResult = m_headers->fill(*init.headers);
if (fillResult.hasException())
return fillResult.releaseException();
} else {
auto fillResult = m_headers->fill(input.headers());
if (fillResult.hasException())
return fillResult.releaseException();
}
if (init.body) {
auto setBodyResult = setBody(WTFMove(*init.body));
if (setBodyResult.hasException())
return setBodyResult.releaseException();
} else {
auto setBodyResult = setBody(input);
if (setBodyResult.hasException())
return setBodyResult.releaseException();
}
updateContentType();
return { };
}
ExceptionOr<void> FetchRequest::setBody(FetchBody::Init&& body)
{
if (!methodCanHaveBody(m_request))
return Exception { TypeError, makeString("Request has method '", m_request.httpMethod(), "' and cannot have a body") };
ASSERT(scriptExecutionContext());
extractBody(*scriptExecutionContext(), WTFMove(body));
if (m_options.keepAlive && hasReadableStreamBody())
return Exception { TypeError, "Request cannot have a ReadableStream body and keepalive set to true"_s };
return { };
}
ExceptionOr<void> FetchRequest::setBody(FetchRequest& request)
{
if (!request.isBodyNull()) {
if (!methodCanHaveBody(m_request))
return Exception { TypeError, makeString("Request has method '", m_request.httpMethod(), "' and cannot have a body") };
m_body = WTFMove(request.m_body);
request.setDisturbed();
}
if (m_options.keepAlive && hasReadableStreamBody())
return Exception { TypeError, "Request cannot have a ReadableStream body and keepalive set to true"_s };
return { };
}
ExceptionOr<Ref<FetchRequest>> FetchRequest::create(ScriptExecutionContext& context, Info&& input, Init&& init)
{
auto request = adoptRef(*new FetchRequest(context, std::nullopt, FetchHeaders::create(FetchHeaders::Guard::Request), { }, { }, { }));
if (WTF::holds_alternative<String>(input)) {
auto result = request->initializeWith(WTF::get<String>(input), WTFMove(init));
if (result.hasException())
return result.releaseException();
} else {
auto result = request->initializeWith(*WTF::get<RefPtr<FetchRequest>>(input), WTFMove(init));
if (result.hasException())
return result.releaseException();
}
return WTFMove(request);
}
String FetchRequest::referrer() const
{
if (m_referrer == "no-referrer")
return String();
if (m_referrer == "client")
return "about:client"_s;
return m_referrer;
}
const String& FetchRequest::urlString() const
{
if (m_requestURL.isNull())
m_requestURL = m_request.url();
return m_requestURL;
}
ResourceRequest FetchRequest::resourceRequest() const
{
ASSERT(scriptExecutionContext());
ResourceRequest request = m_request;
request.setHTTPHeaderFields(m_headers->internalHeaders());
if (!isBodyNull())
request.setHTTPBody(body().bodyAsFormData(*scriptExecutionContext()));
return request;
}
ExceptionOr<Ref<FetchRequest>> FetchRequest::clone(ScriptExecutionContext& context)
{
if (isDisturbedOrLocked())
return Exception { TypeError, "Body is disturbed or locked"_s };
auto clone = adoptRef(*new FetchRequest(context, std::nullopt, FetchHeaders::create(m_headers.get()), ResourceRequest { m_request }, FetchOptions { m_options}, String { m_referrer }));
clone->cloneBody(*this);
return WTFMove(clone);
}
const char* FetchRequest::activeDOMObjectName() const
{
return "Request";
}
bool FetchRequest::canSuspendForDocumentSuspension() const
{
return !isActive();
}
}