Skip to content

Commit 5a56613

Browse files
authored
Fully migrate request-response and fetch-request to the host_api (#538)
1 parent d2fe21b commit 5a56613

File tree

4 files changed

+217
-83
lines changed

4 files changed

+217
-83
lines changed

runtime/js-compute-runtime/builtins/fetch-event.cpp

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,6 @@ bool FetchEvent::init_downstream_request(JSContext *cx, JS::HandleObject request
8383
HttpBody body) {
8484
MOZ_ASSERT(!Request::request_handle(request).is_valid());
8585

86-
fastly_error_t err;
87-
8886
JS::SetReservedSlot(request, static_cast<uint32_t>(Request::Slots::Request),
8987
JS::Int32Value(req.handle));
9088
JS::SetReservedSlot(request, static_cast<uint32_t>(Request::Slots::Body),
@@ -119,15 +117,15 @@ bool FetchEvent::init_downstream_request(JSContext *cx, JS::HandleObject request
119117
JS::SetReservedSlot(request, static_cast<uint32_t>(Request::Slots::HasBody), JS::TrueValue());
120118
}
121119

122-
fastly_world_string_t uri_str;
123-
if (!fastly_http_req_uri_get(req.handle, &uri_str, &err)) {
124-
HANDLE_ERROR(cx, err);
120+
auto uri_res = req.get_uri();
121+
if (auto *err = uri_res.to_err()) {
122+
HANDLE_ERROR(cx, *err);
125123
return false;
126124
}
127125

128-
JS::RootedString url(cx, JS_NewStringCopyN(cx, uri_str.ptr, uri_str.len));
126+
auto uri_str = std::move(uri_res.unwrap());
127+
JS::RootedString url(cx, JS_NewStringCopyN(cx, uri_str.ptr.get(), uri_str.len));
129128
if (!url) {
130-
JS_free(cx, uri_str.ptr);
131129
return false;
132130
}
133131
JS::SetReservedSlot(request, static_cast<uint32_t>(Request::Slots::URL), JS::StringValue(url));
@@ -136,13 +134,11 @@ bool FetchEvent::init_downstream_request(JSContext *cx, JS::HandleObject request
136134
JS::RootedObject url_instance(
137135
cx, JS_NewObjectWithGivenProto(cx, &builtins::URL::class_, builtins::URL::proto_obj));
138136
if (!url_instance) {
139-
JS_free(cx, uri_str.ptr);
140137
return false;
141138
}
142139

143-
jsurl::SpecString spec(reinterpret_cast<uint8_t *>(uri_str.ptr), uri_str.len, uri_str.len);
140+
jsurl::SpecString spec(reinterpret_cast<uint8_t *>(uri_str.ptr.get()), uri_str.len, uri_str.len);
144141
builtins::WorkerLocation::url = builtins::URL::create(cx, url_instance, spec);
145-
JS_free(cx, uri_str.ptr);
146142
if (!builtins::WorkerLocation::url) {
147143
return false;
148144
}
@@ -178,9 +174,9 @@ bool start_response(JSContext *cx, JS::HandleObject response_obj, bool streaming
178174
auto response = Response::response_handle(response_obj);
179175
auto body = RequestOrResponse::body_handle(response_obj);
180176

181-
fastly_error_t err;
182-
if (!fastly_http_resp_send_downstream(response.handle, body.handle, streaming, &err)) {
183-
HANDLE_ERROR(cx, err);
177+
auto res = response.send_downstream(body, streaming);
178+
if (auto *err = res.to_err()) {
179+
HANDLE_ERROR(cx, *err);
184180
return false;
185181
}
186182
return true;
@@ -224,13 +220,10 @@ bool response_promise_then_handler(JSContext *cx, JS::HandleObject event, JS::Ha
224220
bool streaming = false;
225221
if (Response::is_grip_upgrade(response_obj)) {
226222
std::string backend(Response::grip_backend(response_obj));
227-
fastly_world_string_t backend_str;
228-
backend_str.len = backend.length();
229-
backend_str.ptr = backend.data();
230223

231-
fastly_error_t err;
232-
if (!fastly_http_req_redirect_to_grip_proxy(&backend_str, &err)) {
233-
HANDLE_ERROR(cx, err);
224+
auto res = HttpReq::redirect_to_grip_proxy(backend);
225+
if (auto *err = res.to_err()) {
226+
HANDLE_ERROR(cx, *err);
234227
return false;
235228
}
236229
return true;
@@ -313,11 +306,10 @@ bool FetchEvent::respondWith(JSContext *cx, unsigned argc, JS::Value *vp) {
313306
bool FetchEvent::respondWithError(JSContext *cx, JS::HandleObject self) {
314307
MOZ_RELEASE_ASSERT(state(self) == State::unhandled || state(self) == State::waitToRespond);
315308
set_state(self, State::responsedWithError);
316-
fastly_response_handle_t response = INVALID_HANDLE;
317-
fastly_error_t err;
318309

319-
if (!fastly_http_resp_new(&response, &err)) {
320-
HANDLE_ERROR(cx, err);
310+
auto response_res = HttpResp::make();
311+
if (auto *err = response_res.to_err()) {
312+
HANDLE_ERROR(cx, *err);
321313
return false;
322314
}
323315

@@ -327,12 +319,19 @@ bool FetchEvent::respondWithError(JSContext *cx, JS::HandleObject self) {
327319
return false;
328320
}
329321

330-
auto body = make_res.unwrap();
331-
if (!fastly_http_resp_status_set(response, 500, &err) ||
332-
!fastly_http_resp_send_downstream(response, body.handle, false, &err)) {
333-
HANDLE_ERROR(cx, err);
322+
auto response = response_res.unwrap();
323+
auto status_res = response.set_status(500);
324+
if (auto *err = status_res.to_err()) {
325+
HANDLE_ERROR(cx, *err);
334326
return false;
335327
}
328+
329+
auto send_res = response.send_downstream(make_res.unwrap(), false);
330+
if (auto *err = send_res.to_err()) {
331+
HANDLE_ERROR(cx, *err);
332+
return false;
333+
}
334+
336335
return true;
337336
}
338337

runtime/js-compute-runtime/builtins/request-response.cpp

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,45 +1155,39 @@ bool Request::apply_cache_override(JSContext *cx, JS::HandleObject self) {
11551155
return true;
11561156
}
11571157

1158-
fastly_http_cache_override_tag_t tag = builtins::CacheOverride::abi_tag(override);
1159-
1160-
bool has_ttl = true;
1161-
uint32_t ttl;
1158+
std::optional<uint32_t> ttl;
11621159
JS::RootedValue val(cx, builtins::CacheOverride::ttl(override));
1163-
if (val.isUndefined()) {
1164-
has_ttl = false;
1165-
} else {
1160+
if (!val.isUndefined()) {
11661161
ttl = val.toInt32();
11671162
}
11681163

1169-
bool has_swr = true;
1170-
uint32_t swr;
1164+
std::optional<uint32_t> stale_while_revalidate;
11711165
val = builtins::CacheOverride::swr(override);
1172-
if (val.isUndefined()) {
1173-
has_swr = false;
1174-
} else {
1175-
swr = val.toInt32();
1166+
if (!val.isUndefined()) {
1167+
stale_while_revalidate = val.toInt32();
11761168
}
11771169

1178-
fastly_world_string_t sk_str;
1170+
JS::UniqueChars sk_chars;
1171+
std::optional<std::string_view> surrogate_key;
11791172
val = builtins::CacheOverride::surrogate_key(override);
1180-
if (val.isUndefined()) {
1181-
sk_str.len = 0;
1182-
} else {
1183-
JS::UniqueChars sk_chars;
1184-
sk_chars = encode(cx, val, &sk_str.len);
1185-
if (!sk_chars)
1173+
if (!val.isUndefined()) {
1174+
size_t len;
1175+
sk_chars = encode(cx, val, &len);
1176+
if (!sk_chars) {
11861177
return false;
1187-
sk_str.ptr = sk_chars.release();
1178+
}
1179+
1180+
surrogate_key.emplace(sk_chars.get(), len);
11881181
}
11891182

1190-
fastly_error_t err;
1191-
if (!fastly_http_req_cache_override_set(Request::request_handle(self).handle, tag,
1192-
has_ttl ? &ttl : NULL, has_swr ? &swr : NULL,
1193-
sk_str.len ? &sk_str : NULL, &err)) {
1194-
HANDLE_ERROR(cx, err);
1183+
auto tag = builtins::CacheOverride::abi_tag(override);
1184+
auto res =
1185+
Request::request_handle(self).cache_override(tag, ttl, stale_while_revalidate, surrogate_key);
1186+
if (auto *err = res.to_err()) {
1187+
HANDLE_ERROR(cx, *err);
11951188
return false;
11961189
}
1190+
11971191
return true;
11981192
}
11991193

@@ -1321,10 +1315,9 @@ bool Request::clone(JSContext *cx, unsigned argc, JS::Value *vp) {
13211315
return false;
13221316
}
13231317

1324-
fastly_error_t err;
1325-
fastly_request_handle_t request_handle = INVALID_HANDLE;
1326-
if (!fastly_http_req_new(&request_handle, &err)) {
1327-
HANDLE_ERROR(cx, err);
1318+
auto request_handle_res = HttpReq::make();
1319+
if (auto *err = request_handle_res.to_err()) {
1320+
HANDLE_ERROR(cx, *err);
13281321
return false;
13291322
}
13301323

@@ -1334,6 +1327,7 @@ bool Request::clone(JSContext *cx, unsigned argc, JS::Value *vp) {
13341327
return false;
13351328
}
13361329

1330+
auto request_handle = request_handle_res.unwrap();
13371331
auto body_handle = res.unwrap();
13381332
if (!JS::IsReadableStream(&body1_val.toObject())) {
13391333
return false;
@@ -1347,7 +1341,7 @@ bool Request::clone(JSContext *cx, unsigned argc, JS::Value *vp) {
13471341

13481342
JS::RootedObject requestInstance(cx, Request::create_instance(cx));
13491343
JS::SetReservedSlot(requestInstance, static_cast<uint32_t>(Slots::Request),
1350-
JS::Int32Value(request_handle));
1344+
JS::Int32Value(request_handle.handle));
13511345
JS::SetReservedSlot(requestInstance, static_cast<uint32_t>(Slots::Body),
13521346
JS::Int32Value(body_handle.handle));
13531347
JS::SetReservedSlot(requestInstance, static_cast<uint32_t>(Slots::BodyStream), body1_val);
@@ -1604,15 +1598,14 @@ JSObject *Request::create(JSContext *cx, JS::HandleObject requestInstance, JS::H
16041598

16051599
// Actually set the URL derived in steps 5 or 6 above.
16061600
RequestOrResponse::set_url(request, StringValue(url_str));
1607-
fastly_world_string_t url_fastly_str;
1608-
JS::UniqueChars url = encode(cx, url_str, &url_fastly_str.len);
1601+
size_t len;
1602+
auto url = encode(cx, url_str, &len);
16091603
if (!url) {
16101604
return nullptr;
16111605
} else {
1612-
url_fastly_str.ptr = url.get();
1613-
fastly_error_t err;
1614-
if (!fastly_http_req_uri_set(request_handle.handle, &url_fastly_str, &err)) {
1615-
HANDLE_ERROR(cx, err);
1606+
auto res = request_handle.set_uri(std::string_view{url.get(), len});
1607+
if (auto *err = res.to_err()) {
1608+
HANDLE_ERROR(cx, *err);
16161609
return nullptr;
16171610
}
16181611
}
@@ -2357,17 +2350,20 @@ bool Response::redirect(JSContext *cx, unsigned argc, JS::Value *vp) {
23572350
}
23582351

23592352
// 5. Set responseObject’s response’s status to status.
2360-
fastly_error_t err;
2361-
if (!fastly_http_resp_status_set(response_handle.handle, status, &err)) {
2362-
HANDLE_ERROR(cx, err);
2353+
auto set_res = response_handle.set_status(status);
2354+
if (auto *err = set_res.to_err()) {
2355+
HANDLE_ERROR(cx, *err);
23632356
return false;
23642357
}
23652358
// To ensure that we really have the same status value as the host,
23662359
// we always read it back here.
2367-
if (!fastly_http_resp_status_get(response_handle.handle, &status, &err)) {
2368-
HANDLE_ERROR(cx, err);
2360+
auto get_res = response_handle.get_status();
2361+
if (auto *err = get_res.to_err()) {
2362+
HANDLE_ERROR(cx, *err);
23692363
return false;
23702364
}
2365+
status = get_res.unwrap();
2366+
23712367
JS::SetReservedSlot(response, static_cast<uint32_t>(Slots::Status), JS::Int32Value(status));
23722368
JS::SetReservedSlot(response, static_cast<uint32_t>(Slots::StatusMessage),
23732369
JS::StringValue(JS_GetEmptyString(cx)));
@@ -2504,17 +2500,19 @@ bool Response::json(JSContext *cx, unsigned argc, JS::Value *vp) {
25042500
}
25052501

25062502
// Set `this`’s `response`’s `status` to `init`["status"].
2507-
fastly_error_t err;
2508-
if (!fastly_http_resp_status_set(response_handle.handle, status, &err)) {
2509-
HANDLE_ERROR(cx, err);
2503+
auto set_res = response_handle.set_status(status);
2504+
if (auto *err = set_res.to_err()) {
2505+
HANDLE_ERROR(cx, *err);
25102506
return false;
25112507
}
25122508
// To ensure that we really have the same status value as the host,
25132509
// we always read it back here.
2514-
if (!fastly_http_resp_status_get(response_handle.handle, &status, &err)) {
2515-
HANDLE_ERROR(cx, err);
2510+
auto get_res = response_handle.get_status();
2511+
if (auto *err = get_res.to_err()) {
2512+
HANDLE_ERROR(cx, *err);
25162513
return false;
25172514
}
2515+
status = get_res.unwrap();
25182516

25192517
JS::SetReservedSlot(response, static_cast<uint32_t>(Slots::Status), JS::Int32Value(status));
25202518

@@ -2673,17 +2671,19 @@ bool Response::constructor(JSContext *cx, unsigned argc, JS::Value *vp) {
26732671
// (implicit)
26742672

26752673
// 5. Set `this`’s `response`’s `status` to `init`["status"].
2676-
fastly_error_t err;
2677-
if (!fastly_http_resp_status_set(response_handle.handle, status, &err)) {
2678-
HANDLE_ERROR(cx, err);
2674+
auto set_res = response_handle.set_status(status);
2675+
if (auto *err = set_res.to_err()) {
2676+
HANDLE_ERROR(cx, *err);
26792677
return false;
26802678
}
26812679
// To ensure that we really have the same status value as the host,
26822680
// we always read it back here.
2683-
if (!fastly_http_resp_status_get(response_handle.handle, &status, &err)) {
2684-
HANDLE_ERROR(cx, err);
2681+
auto get_res = response_handle.get_status();
2682+
if (auto *err = get_res.to_err()) {
2683+
HANDLE_ERROR(cx, *err);
26852684
return false;
26862685
}
2686+
status = get_res.unwrap();
26872687

26882688
JS::SetReservedSlot(response, static_cast<uint32_t>(Slots::Status), JS::Int32Value(status));
26892689

@@ -2766,13 +2766,13 @@ JSObject *Response::create(JSContext *cx, JS::HandleObject response, HttpResp re
27662766
JS::BooleanValue(is_grip));
27672767

27682768
if (is_upstream) {
2769-
uint16_t status = 0;
2770-
fastly_error_t err;
2771-
if (!fastly_http_resp_status_get(response_handle.handle, &status, &err)) {
2772-
HANDLE_ERROR(cx, err);
2769+
auto res = response_handle.get_status();
2770+
if (auto *err = res.to_err()) {
2771+
HANDLE_ERROR(cx, *err);
27732772
return nullptr;
27742773
}
27752774

2775+
auto status = res.unwrap();
27762776
JS::SetReservedSlot(response, static_cast<uint32_t>(Slots::Status), JS::Int32Value(status));
27772777
set_status_message_from_code(cx, response, status);
27782778

0 commit comments

Comments
 (0)