Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: headers WPT conformance #108

Merged
merged 5 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions builtins/web/fetch/fetch-api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#include <memory>

using builtins::web::fetch::Headers;

namespace builtins::web::fetch {

static api::Engine *ENGINE;
Expand All @@ -31,7 +33,8 @@ bool fetch(JSContext *cx, unsigned argc, Value *vp) {
return ReturnPromiseRejectedWithPendingError(cx, args);
}

if (!Request::initialize(cx, request_obj, args[0], args.get(1))) {
if (!Request::initialize(cx, request_obj, args[0], args.get(1),
Headers::HeadersGuard::Immutable)) {
return ReturnPromiseRejectedWithPendingError(cx, args);
}

Expand All @@ -47,14 +50,13 @@ bool fetch(JSContext *cx, unsigned argc, Value *vp) {
return ReturnPromiseRejectedWithPendingError(cx, args);
}

unique_ptr<host_api::HttpHeaders> headers = RequestOrResponse::headers_handle_clone(cx,
request_obj, host_api::HttpHeadersGuard::Request);
unique_ptr<host_api::HttpHeaders> headers =
RequestOrResponse::headers_handle_clone(cx, request_obj);
if (!headers) {
return ReturnPromiseRejectedWithPendingError(cx, args);
}

auto request = host_api::HttpOutgoingRequest::make(method, std::move(url),
std::move(headers));
auto request = host_api::HttpOutgoingRequest::make(method, std::move(url), std::move(headers));
MOZ_RELEASE_ASSERT(request);
JS_SetReservedSlot(request_obj, static_cast<uint32_t>(Request::Slots::Request),
PrivateValue(request));
Expand Down
1 change: 1 addition & 0 deletions builtins/web/fetch/fetch-errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ DEF_ERR(EmptyHeaderName, JSEXN_TYPEERR, "{0}: Header name can't be empty", 1)
DEF_ERR(InvalidHeaderName, JSEXN_TYPEERR, "{0}: Invalid header name \"{1}\"", 2)
DEF_ERR(InvalidHeaderValue, JSEXN_TYPEERR, "{0}: Invalid header value \"{1}\"", 2)
DEF_ERR(HeadersCloningFailed, JSEXN_ERR, "Failed to clone headers", 0)
DEF_ERR(HeadersImmutable, JSEXN_TYPEERR, "{0}: Headers are immutable", 1)
}; // namespace FetchErrors

#endif // FETCH_ERRORS_H
30 changes: 11 additions & 19 deletions builtins/web/fetch/fetch_event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,7 @@ bool send_response(host_api::HttpOutgoingResponse *response, JS::HandleObject se

bool start_response(JSContext *cx, JS::HandleObject response_obj) {
auto status = Response::status(response_obj);
auto headers = RequestOrResponse::headers_handle_clone(cx, response_obj,
host_api::HttpHeadersGuard::Response);
auto headers = RequestOrResponse::headers_handle_clone(cx, response_obj);
if (!headers) {
return false;
}
Expand Down Expand Up @@ -252,16 +251,15 @@ bool FetchEvent::respondWith(JSContext *cx, unsigned argc, JS::Value *vp) {

// Step 2
if (!is_dispatching(self)) {
return dom_exception::DOMException::raise(cx,
"FetchEvent#respondWith must be called synchronously from within a FetchEvent handler",
"InvalidStateError");
return dom_exception::DOMException::raise(
cx, "FetchEvent#respondWith must be called synchronously from within a FetchEvent handler",
"InvalidStateError");
}

// Step 3
if (state(self) != State::unhandled) {
return dom_exception::DOMException::raise(cx,
"FetchEvent#respondWith can't be called twice on the same event",
"InvalidStateError");
return dom_exception::DOMException::raise(
cx, "FetchEvent#respondWith can't be called twice on the same event", "InvalidStateError");
}

// Step 4
Expand Down Expand Up @@ -293,7 +291,7 @@ bool FetchEvent::respondWith(JSContext *cx, unsigned argc, JS::Value *vp) {
bool FetchEvent::respondWithError(JSContext *cx, JS::HandleObject self) {
MOZ_RELEASE_ASSERT(state(self) == State::unhandled || state(self) == State::waitToRespond);

auto headers = std::make_unique<host_api::HttpHeaders>(host_api::HttpHeadersGuard::Response);
auto headers = std::make_unique<host_api::HttpHeaders>();
auto *response = host_api::HttpOutgoingResponse::make(500, std::move(headers));

auto body_res = response->body();
Expand Down Expand Up @@ -331,9 +329,7 @@ bool FetchEvent::waitUntil(JSContext *cx, unsigned argc, JS::Value *vp) {
// Step 2
if (!is_active(self)) {
return dom_exception::DOMException::raise(
cx,
"waitUntil called on a FetchEvent that isn't active anymore",
"InvalidStateError");
cx, "waitUntil called on a FetchEvent that isn't active anymore", "InvalidStateError");
}

// Steps 3-4
Expand All @@ -345,13 +341,9 @@ bool FetchEvent::waitUntil(JSContext *cx, unsigned argc, JS::Value *vp) {
return true;
}

void FetchEvent::increase_interest() {
inc_pending_promise_count(INSTANCE);
}
void FetchEvent::increase_interest() { inc_pending_promise_count(INSTANCE); }

void FetchEvent::decrease_interest() {
dec_pending_promise_count(INSTANCE);
}
void FetchEvent::decrease_interest() { dec_pending_promise_count(INSTANCE); }

const JSFunctionSpec FetchEvent::static_methods[] = {
JS_FS_END,
Expand Down Expand Up @@ -571,7 +563,7 @@ bool eval_request_body(host_api::HttpIncomingRequest *request) {
return true;
}

bool handle_incoming_request(host_api::HttpIncomingRequest * request) {
bool handle_incoming_request(host_api::HttpIncomingRequest *request) {
#ifdef DEBUG
fprintf(stderr, "Warning: Using a DEBUG build. Expect things to be SLOW.\n");
#endif
Expand Down
Loading
Loading