Skip to content

Commit baf0a6e

Browse files
authored
feat: Fanout update to redirect_to_grip_proxy_v2 passing request handle (#1079)
1 parent 27a16a2 commit baf0a6e

File tree

7 files changed

+33
-24
lines changed

7 files changed

+33
-24
lines changed

runtime/fastly/builtins/fastly.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ bool Fastly::createFanoutHandoff(JSContext *cx, unsigned argc, JS::Value *vp) {
212212
JS_ReportErrorUTF8(cx, "createFanoutHandoff: request parameter must be an instance of Request");
213213
return false;
214214
}
215+
auto grip_upgrade_request = &request_value.toObject();
215216

216217
auto response_handle = host_api::HttpResp::make();
217218
if (auto *err = response_handle.to_err()) {
@@ -250,11 +251,10 @@ bool Fastly::createFanoutHandoff(JSContext *cx, unsigned argc, JS::Value *vp) {
250251
}
251252

252253
bool is_upstream = true;
253-
bool is_grip_upgrade = true;
254254

255255
JS::RootedObject response(cx, Response::create(cx, response_instance, response_handle.unwrap(),
256-
body_handle.unwrap(), is_upstream, is_grip_upgrade,
257-
backend_str));
256+
body_handle.unwrap(), is_upstream,
257+
grip_upgrade_request, backend_str));
258258
if (!response) {
259259
return false;
260260
}

runtime/fastly/builtins/fetch-event.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -668,18 +668,18 @@ bool response_promise_then_handler(JSContext *cx, JS::HandleObject event, JS::Ha
668668
return false;
669669
}
670670

671-
bool streaming = false;
672-
if (Response::is_grip_upgrade(response_obj)) {
671+
if (auto grip_upgrade_request = Response::grip_upgrade_request(response_obj)) {
673672
auto backend = Response::backend_str(cx, response_obj);
674673

675-
auto res = host_api::HttpReq::redirect_to_grip_proxy(backend);
674+
auto res = grip_upgrade_request->redirect_to_grip_proxy(backend);
676675
if (auto *err = res.to_err()) {
677676
HANDLE_ERROR(cx, *err);
678677
return false;
679678
}
680679
return true;
681680
}
682681

682+
bool streaming = false;
683683
if (!RequestOrResponse::maybe_stream_body(cx, response_obj, &streaming)) {
684684
return false;
685685
}

runtime/fastly/builtins/fetch/request-response.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,9 @@ bool RequestOrResponse::process_pending_request(JSContext *cx,
211211
}
212212

213213
bool is_upstream = true;
214-
bool is_grip_upgrade = false;
215214
RootedString backend(cx, RequestOrResponse::backend(context));
216215
JS::RootedObject response(cx, Response::create(cx, response_instance, response_handle, body,
217-
is_upstream, is_grip_upgrade, backend));
216+
is_upstream, nullptr, backend));
218217
if (!response) {
219218
return false;
220219
}
@@ -2283,9 +2282,14 @@ bool Response::is_upstream(JSObject *obj) {
22832282
return JS::GetReservedSlot(obj, static_cast<uint32_t>(Slots::IsUpstream)).toBoolean();
22842283
}
22852284

2286-
bool Response::is_grip_upgrade(JSObject *obj) {
2285+
std::optional<host_api::HttpReq> Response::grip_upgrade_request(JSObject *obj) {
22872286
MOZ_ASSERT(is_instance(obj));
2288-
return JS::GetReservedSlot(obj, static_cast<uint32_t>(Slots::IsGripUpgrade)).toBoolean();
2287+
auto grip_upgrade_request =
2288+
JS::GetReservedSlot(obj, static_cast<uint32_t>(Slots::GripUpgradeRequest));
2289+
if (grip_upgrade_request.isUndefined()) {
2290+
return std::nullopt;
2291+
}
2292+
return host_api::HttpReq(grip_upgrade_request.toInt32());
22892293
}
22902294

22912295
host_api::HostString Response::backend_str(JSContext *cx, JSObject *obj) {
@@ -2716,7 +2720,7 @@ bool Response::redirect(JSContext *cx, unsigned argc, JS::Value *vp) {
27162720
return false;
27172721
}
27182722
JS::RootedObject response(
2719-
cx, create(cx, response_instance, response_handle, body, false, false, nullptr));
2723+
cx, create(cx, response_instance, response_handle, body, false, nullptr, nullptr));
27202724
if (!response) {
27212725
return false;
27222726
}
@@ -2860,7 +2864,7 @@ bool Response::json(JSContext *cx, unsigned argc, JS::Value *vp) {
28602864
return false;
28612865
}
28622866
JS::RootedObject response(
2863-
cx, create(cx, response_instance, response_handle, body, false, false, nullptr));
2867+
cx, create(cx, response_instance, response_handle, body, false, nullptr, nullptr));
28642868
if (!response) {
28652869
return false;
28662870
}
@@ -3079,7 +3083,7 @@ bool Response::constructor(JSContext *cx, unsigned argc, JS::Value *vp) {
30793083
auto body = make_res.unwrap();
30803084
JS::RootedObject responseInstance(cx, JS_NewObjectForConstructor(cx, &class_, args));
30813085
JS::RootedObject response(
3082-
cx, create(cx, responseInstance, response_handle, body, false, false, nullptr));
3086+
cx, create(cx, responseInstance, response_handle, body, false, nullptr, nullptr));
30833087
if (!response) {
30843088
return false;
30853089
}
@@ -3184,7 +3188,8 @@ bool Response::init_class(JSContext *cx, JS::HandleObject global) {
31843188

31853189
JSObject *Response::create(JSContext *cx, JS::HandleObject response,
31863190
host_api::HttpResp response_handle, host_api::HttpBody body_handle,
3187-
bool is_upstream, bool is_grip, JS::HandleString backend) {
3191+
bool is_upstream, JSObject *grip_upgrade_request,
3192+
JS::HandleString backend) {
31883193
JS::SetReservedSlot(response, static_cast<uint32_t>(Slots::Response),
31893194
JS::Int32Value(response_handle.handle));
31903195
JS::SetReservedSlot(response, static_cast<uint32_t>(Slots::Headers), JS::NullValue());
@@ -3196,8 +3201,10 @@ JSObject *Response::create(JSContext *cx, JS::HandleObject response,
31963201
JS::SetReservedSlot(response, static_cast<uint32_t>(Slots::Redirected), JS::FalseValue());
31973202
JS::SetReservedSlot(response, static_cast<uint32_t>(Slots::IsUpstream),
31983203
JS::BooleanValue(is_upstream));
3199-
JS::SetReservedSlot(response, static_cast<uint32_t>(Slots::IsGripUpgrade),
3200-
JS::BooleanValue(is_grip));
3204+
if (grip_upgrade_request) {
3205+
JS::SetReservedSlot(response, static_cast<uint32_t>(Slots::GripUpgradeRequest),
3206+
JS::Int32Value(Request::request_handle(grip_upgrade_request).handle));
3207+
}
32013208
if (backend) {
32023209
JS::SetReservedSlot(response, static_cast<uint32_t>(Slots::Backend), JS::StringValue(backend));
32033210
}

runtime/fastly/builtins/fetch/request-response.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ class Response final : public builtins::BuiltinImpl<Response> {
222222
Status,
223223
StatusMessage,
224224
Redirected,
225-
IsGripUpgrade,
225+
GripUpgradeRequest,
226226
Count,
227227
};
228228
static const JSFunctionSpec static_methods[];
@@ -237,7 +237,8 @@ class Response final : public builtins::BuiltinImpl<Response> {
237237

238238
static JSObject *create(JSContext *cx, JS::HandleObject response,
239239
host_api::HttpResp response_handle, host_api::HttpBody body_handle,
240-
bool is_upstream, bool is_grip_upgrade, JS::HandleString backend);
240+
bool is_upstream, JSObject *grip_upgrade_request,
241+
JS::HandleString backend);
241242

242243
/**
243244
* Returns the RequestOrResponse's Headers, reifying it if necessary.
@@ -246,7 +247,7 @@ class Response final : public builtins::BuiltinImpl<Response> {
246247

247248
static host_api::HttpResp response_handle(JSObject *obj);
248249
static bool is_upstream(JSObject *obj);
249-
static bool is_grip_upgrade(JSObject *obj);
250+
static std::optional<host_api::HttpReq> grip_upgrade_request(JSObject *obj);
250251
static host_api::HostString backend_str(JSContext *cx, JSObject *obj);
251252
static uint16_t status(JSObject *obj);
252253
static JSString *status_message(JSObject *obj);

runtime/fastly/host-api/fastly.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,9 @@ int req_register_dynamic_backend(const char *name_prefix, size_t name_prefix_len
302302
WASM_IMPORT("fastly_http_req", "body_downstream_get")
303303
int req_body_downstream_get(uint32_t *req_handle_out, uint32_t *body_handle_out);
304304

305-
WASM_IMPORT("fastly_http_req", "redirect_to_grip_proxy")
306-
int req_redirect_to_grip_proxy(const char *backend_name, size_t backend_name_len);
305+
WASM_IMPORT("fastly_http_req", "redirect_to_grip_proxy_v2")
306+
int req_redirect_to_grip_proxy_v2(uint32_t req_handle, const char *backend_name,
307+
size_t backend_name_len);
307308

308309
/**
309310
* Set the cache override behavior for this request.

runtime/fastly/host-api/host_api.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,8 +1071,8 @@ Result<Void> HttpReq::redirect_to_grip_proxy(std::string_view backend) {
10711071

10721072
fastly::fastly_host_error err;
10731073
fastly::fastly_world_string backend_str = string_view_to_world_string(backend);
1074-
if (!convert_result(fastly::req_redirect_to_grip_proxy(reinterpret_cast<char *>(backend_str.ptr),
1075-
backend_str.len),
1074+
if (!convert_result(fastly::req_redirect_to_grip_proxy_v2(
1075+
this->handle, reinterpret_cast<char *>(backend_str.ptr), backend_str.len),
10761076
&err)) {
10771077
res.emplace_err(err);
10781078
} else {

runtime/fastly/host-api/host_api_fastly.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ class HttpReq final : public HttpBase {
509509

510510
static Result<HttpReq> make();
511511

512-
static Result<Void> redirect_to_grip_proxy(std::string_view backend);
512+
Result<Void> redirect_to_grip_proxy(std::string_view backend);
513513

514514
static Result<Void> register_dynamic_backend(std::string_view name, std::string_view target,
515515
const BackendConfig &config);

0 commit comments

Comments
 (0)