LibWeb/Fetch: Update "HTTP-redirect fetch" algorithm to latest spec

The spec and implementation's comments had diverged a little, this
brings them in line :)
This commit is contained in:
Jamie Mansfield 2024-06-05 19:36:18 +01:00 committed by Andreas Kling
parent 8f2cb6755b
commit 8542a8b458
Notes: sideshowbarker 2024-07-17 14:33:07 +09:00

View File

@ -1120,14 +1120,14 @@ WebIDL::ExceptionOr<JS::GCPtr<PendingResponse>> http_redirect_fetch(JS::Realm& r
// 1. Let request be fetchParamss request.
auto request = fetch_params.request();
// 2. Let actualResponse be response, if response is not a filtered response, and responses internal response
// otherwise.
auto actual_response = !is<Infrastructure::FilteredResponse>(response)
// 2. Let internalResponse be response, if response is not a filtered response; otherwise responses internal
// response.
auto internal_response = !is<Infrastructure::FilteredResponse>(response)
? JS::NonnullGCPtr { response }
: static_cast<Infrastructure::FilteredResponse const&>(response).internal_response();
// 3. Let locationURL be actualResponses location URL given requests current URLs fragment.
auto location_url_or_error = actual_response->location_url(request->current_url().fragment());
// 3. Let locationURL be internalResponses location URL given requests current URLs fragment.
auto location_url_or_error = internal_response->location_url(request->current_url().fragment());
// 4. If locationURL is null, then return response.
if (!location_url_or_error.is_error() && !location_url_or_error.value().has_value())
@ -1150,7 +1150,7 @@ WebIDL::ExceptionOr<JS::GCPtr<PendingResponse>> http_redirect_fetch(JS::Realm& r
// 8. Increase requests redirect count by 1.
request->set_redirect_count(request->redirect_count() + 1);
// 8. If requests mode is "cors", locationURL includes credentials, and requests origin is not same origin with
// 9. If requests mode is "cors", locationURL includes credentials, and requests origin is not same origin with
// locationURLs origin, then return a network error.
if (request->mode() == Infrastructure::Request::Mode::CORS
&& location_url.includes_credentials()
@ -1164,9 +1164,9 @@ WebIDL::ExceptionOr<JS::GCPtr<PendingResponse>> http_redirect_fetch(JS::Realm& r
if (request->response_tainting() == Infrastructure::Request::ResponseTainting::CORS && location_url.includes_credentials())
return PendingResponse::create(vm, request, Infrastructure::Response::network_error(vm, "Request with 'cors' response tainting must not include credentials in redirect URL"sv));
// 11. If actualResponses status is not 303, requests body is non-null, and requests bodys source is null, then
// 11. If internalResponses status is not 303, requests body is non-null, and requests bodys source is null, then
// return a network error.
if (actual_response->status() != 303
if (internal_response->status() != 303
&& !request->body().has<Empty>()
&& request->body().get<JS::NonnullGCPtr<Infrastructure::Body>>()->source().has<Empty>()) {
return PendingResponse::create(vm, request, Infrastructure::Response::network_error(vm, "Request has body but no body source"sv));
@ -1174,10 +1174,10 @@ WebIDL::ExceptionOr<JS::GCPtr<PendingResponse>> http_redirect_fetch(JS::Realm& r
// 12. If one of the following is true
if (
// - actualResponses status is 301 or 302 and requests method is `POST`
((actual_response->status() == 301 || actual_response->status() == 302) && request->method() == "POST"sv.bytes())
// - actualResponses status is 303 and requests method is not `GET` or `HEAD`
|| (actual_response->status() == 303 && !(request->method() == "GET"sv.bytes() || request->method() == "HEAD"sv.bytes()))
// - internalResponses status is 301 or 302 and requests method is `POST`
((internal_response->status() == 301 || internal_response->status() == 302) && request->method() == "POST"sv.bytes())
// - internalResponses status is 303 and requests method is not `GET` or `HEAD`
|| (internal_response->status() == 303 && !(request->method() == "GET"sv.bytes() || request->method() == "HEAD"sv.bytes()))
// then:
) {
// 1. Set requests method to `GET` and requests body to null.
@ -1236,7 +1236,7 @@ WebIDL::ExceptionOr<JS::GCPtr<PendingResponse>> http_redirect_fetch(JS::Realm& r
// 18. Append locationURL to requests URL list.
request->url_list().append(location_url);
// FIXME: 19. Invoke set requests referrer policy on redirect on request and actualResponse.
// FIXME: 19. Invoke set requests referrer policy on redirect on request and internalResponse.
// 20. Let recursive be true.
auto recursive = Recursive::Yes;