@@ -1155,45 +1155,39 @@ bool Request::apply_cache_override(JSContext *cx, JS::HandleObject self) {
1155
1155
return true ;
1156
1156
}
1157
1157
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;
1162
1159
JS::RootedValue val (cx, builtins::CacheOverride::ttl (override ));
1163
- if (val.isUndefined ()) {
1164
- has_ttl = false ;
1165
- } else {
1160
+ if (!val.isUndefined ()) {
1166
1161
ttl = val.toInt32 ();
1167
1162
}
1168
1163
1169
- bool has_swr = true ;
1170
- uint32_t swr;
1164
+ std::optional<uint32_t > stale_while_revalidate;
1171
1165
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 ();
1176
1168
}
1177
1169
1178
- fastly_world_string_t sk_str;
1170
+ JS::UniqueChars sk_chars;
1171
+ std::optional<std::string_view> surrogate_key;
1179
1172
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) {
1186
1177
return false ;
1187
- sk_str.ptr = sk_chars.release ();
1178
+ }
1179
+
1180
+ surrogate_key.emplace (sk_chars.get (), len);
1188
1181
}
1189
1182
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);
1195
1188
return false ;
1196
1189
}
1190
+
1197
1191
return true ;
1198
1192
}
1199
1193
@@ -1321,10 +1315,9 @@ bool Request::clone(JSContext *cx, unsigned argc, JS::Value *vp) {
1321
1315
return false ;
1322
1316
}
1323
1317
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);
1328
1321
return false ;
1329
1322
}
1330
1323
@@ -1334,6 +1327,7 @@ bool Request::clone(JSContext *cx, unsigned argc, JS::Value *vp) {
1334
1327
return false ;
1335
1328
}
1336
1329
1330
+ auto request_handle = request_handle_res.unwrap ();
1337
1331
auto body_handle = res.unwrap ();
1338
1332
if (!JS::IsReadableStream (&body1_val.toObject ())) {
1339
1333
return false ;
@@ -1347,7 +1341,7 @@ bool Request::clone(JSContext *cx, unsigned argc, JS::Value *vp) {
1347
1341
1348
1342
JS::RootedObject requestInstance (cx, Request::create_instance (cx));
1349
1343
JS::SetReservedSlot (requestInstance, static_cast <uint32_t >(Slots::Request),
1350
- JS::Int32Value (request_handle));
1344
+ JS::Int32Value (request_handle. handle ));
1351
1345
JS::SetReservedSlot (requestInstance, static_cast <uint32_t >(Slots::Body),
1352
1346
JS::Int32Value (body_handle.handle ));
1353
1347
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
1604
1598
1605
1599
// Actually set the URL derived in steps 5 or 6 above.
1606
1600
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);
1609
1603
if (!url) {
1610
1604
return nullptr ;
1611
1605
} 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);
1616
1609
return nullptr ;
1617
1610
}
1618
1611
}
@@ -2357,17 +2350,20 @@ bool Response::redirect(JSContext *cx, unsigned argc, JS::Value *vp) {
2357
2350
}
2358
2351
2359
2352
// 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);
2363
2356
return false ;
2364
2357
}
2365
2358
// To ensure that we really have the same status value as the host,
2366
2359
// 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);
2369
2363
return false ;
2370
2364
}
2365
+ status = get_res.unwrap ();
2366
+
2371
2367
JS::SetReservedSlot (response, static_cast <uint32_t >(Slots::Status), JS::Int32Value (status));
2372
2368
JS::SetReservedSlot (response, static_cast <uint32_t >(Slots::StatusMessage),
2373
2369
JS::StringValue (JS_GetEmptyString (cx)));
@@ -2504,17 +2500,19 @@ bool Response::json(JSContext *cx, unsigned argc, JS::Value *vp) {
2504
2500
}
2505
2501
2506
2502
// 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);
2510
2506
return false ;
2511
2507
}
2512
2508
// To ensure that we really have the same status value as the host,
2513
2509
// 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);
2516
2513
return false ;
2517
2514
}
2515
+ status = get_res.unwrap ();
2518
2516
2519
2517
JS::SetReservedSlot (response, static_cast <uint32_t >(Slots::Status), JS::Int32Value (status));
2520
2518
@@ -2673,17 +2671,19 @@ bool Response::constructor(JSContext *cx, unsigned argc, JS::Value *vp) {
2673
2671
// (implicit)
2674
2672
2675
2673
// 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);
2679
2677
return false ;
2680
2678
}
2681
2679
// To ensure that we really have the same status value as the host,
2682
2680
// 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);
2685
2684
return false ;
2686
2685
}
2686
+ status = get_res.unwrap ();
2687
2687
2688
2688
JS::SetReservedSlot (response, static_cast <uint32_t >(Slots::Status), JS::Int32Value (status));
2689
2689
@@ -2766,13 +2766,13 @@ JSObject *Response::create(JSContext *cx, JS::HandleObject response, HttpResp re
2766
2766
JS::BooleanValue (is_grip));
2767
2767
2768
2768
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);
2773
2772
return nullptr ;
2774
2773
}
2775
2774
2775
+ auto status = res.unwrap ();
2776
2776
JS::SetReservedSlot (response, static_cast <uint32_t >(Slots::Status), JS::Int32Value (status));
2777
2777
set_status_message_from_code (cx, response, status);
2778
2778
0 commit comments