1
1
#include < algorithm>
2
+ #include < type_traits>
2
3
3
4
#include " core/allocator.h"
4
5
#include " fastly-world/fastly_world.h"
@@ -13,8 +14,33 @@ fastly_world_string_t string_view_to_world_string(std::string_view str) {
13
14
};
14
15
}
15
16
17
+ HostString make_host_string (fastly_world_string_t str) {
18
+ return HostString{JS::UniqueChars{str.ptr }, str.len };
19
+ }
20
+
21
+ HostBytes make_host_bytes (fastly_world_list_u8_t str) {
22
+ return HostBytes{std::unique_ptr<uint8_t []>{str.ptr }, str.len };
23
+ }
24
+
25
+ Response make_response (fastly_compute_at_edge_fastly_response_t &resp) {
26
+ return Response{HttpResp{resp.f0 }, HttpBody{resp.f1 }};
27
+ }
28
+
16
29
} // namespace
17
30
31
+ // Ensure that the handle types stay in sync with fastly-world.h
32
+ static_assert (std::is_same_v<AsyncHandle::Handle, fastly_compute_at_edge_fastly_async_handle_t >);
33
+ static_assert (std::is_same_v<HttpBody::Handle, fastly_compute_at_edge_fastly_body_handle_t >);
34
+ static_assert (
35
+ std::is_same_v<HttpPendingReq::Handle, fastly_compute_at_edge_fastly_pending_request_handle_t >);
36
+ static_assert (std::is_same_v<HttpReq::Handle, fastly_compute_at_edge_fastly_request_handle_t >);
37
+ static_assert (std::is_same_v<HttpResp::Handle, fastly_compute_at_edge_fastly_response_handle_t >);
38
+ static_assert (
39
+ std::is_same_v<LogEndpoint::Handle, fastly_compute_at_edge_fastly_log_endpoint_handle_t >);
40
+ static_assert (std::is_same_v<Dict::Handle, fastly_compute_at_edge_fastly_dictionary_handle_t >);
41
+ static_assert (
42
+ std::is_same_v<ObjectStore::Handle, fastly_compute_at_edge_fastly_object_store_handle_t >);
43
+
18
44
Result<bool > AsyncHandle::is_ready () const {
19
45
Result<bool > res;
20
46
@@ -155,7 +181,7 @@ Result<std::vector<HostString>> generic_get_header_names(auto handle) {
155
181
std::vector<HostString> names;
156
182
157
183
for (int i = 0 ; i < ret.len ; i++) {
158
- names.emplace_back (HostString{ ret.ptr [i]} );
184
+ names.emplace_back (make_host_string ( ret.ptr [i]) );
159
185
}
160
186
161
187
// Free the vector of string pointers, but leave the individual strings alone.
@@ -183,7 +209,7 @@ Result<std::optional<std::vector<HostString>>> generic_get_header_values(auto ha
183
209
std::vector<HostString> names;
184
210
185
211
for (int i = 0 ; i < ret.val .len ; i++) {
186
- names.emplace_back (HostString{ ret.val .ptr [i]} );
212
+ names.emplace_back (make_host_string ( ret.val .ptr [i]) );
187
213
}
188
214
189
215
// Free the vector of string pointers, but leave the individual strings alone.
@@ -235,7 +261,7 @@ Result<std::optional<Response>> HttpPendingReq::poll() {
235
261
if (!fastly_compute_at_edge_fastly_http_req_pending_req_poll (this ->handle , &ret, &err)) {
236
262
res.emplace_err (err);
237
263
} else if (ret.is_some ) {
238
- res.emplace (ret.val );
264
+ res.emplace (make_response ( ret.val ) );
239
265
} else {
240
266
res.emplace (std::nullopt);
241
267
}
@@ -251,7 +277,7 @@ Result<Response> HttpPendingReq::wait() {
251
277
if (!fastly_compute_at_edge_fastly_http_req_pending_req_wait (this ->handle , &ret, &err)) {
252
278
res.emplace_err (err);
253
279
} else {
254
- res.emplace (ret);
280
+ res.emplace (make_response ( ret) );
255
281
}
256
282
257
283
return res;
@@ -377,7 +403,7 @@ Result<Response> HttpReq::send(HttpBody body, std::string_view backend) {
377
403
&err)) {
378
404
res.emplace_err (err);
379
405
} else {
380
- res.emplace (ret);
406
+ res.emplace (make_response ( ret) );
381
407
}
382
408
383
409
return res;
@@ -435,7 +461,7 @@ Result<HostString> HttpReq::get_method() const {
435
461
if (!fastly_compute_at_edge_fastly_http_req_method_get (this ->handle , &ret, &err)) {
436
462
res.emplace_err (err);
437
463
} else {
438
- res.emplace (ret);
464
+ res.emplace (make_host_string ( ret) );
439
465
}
440
466
441
467
return res;
@@ -463,7 +489,7 @@ Result<HostString> HttpReq::get_uri() const {
463
489
if (!fastly_compute_at_edge_fastly_http_req_uri_get (this ->handle , &uri, &err)) {
464
490
res.emplace_err (err);
465
491
} else {
466
- res.emplace (uri);
492
+ res.emplace (make_host_string ( uri) );
467
493
}
468
494
469
495
return res;
@@ -509,7 +535,7 @@ Result<HostBytes> HttpReq::downstream_client_ip_addr() {
509
535
if (!fastly_compute_at_edge_fastly_http_req_downstream_client_ip_addr (&octets, &err)) {
510
536
res.emplace_err (err);
511
537
} else {
512
- res.emplace (octets);
538
+ res.emplace (make_host_bytes ( octets) );
513
539
}
514
540
515
541
return res;
@@ -524,7 +550,7 @@ Result<HostString> HttpReq::http_req_downstream_tls_cipher_openssl_name() {
524
550
if (!fastly_compute_at_edge_fastly_http_req_downstream_tls_cipher_openssl_name (&ret, &err)) {
525
551
res.emplace_err (err);
526
552
} else {
527
- res.emplace (ret);
553
+ res.emplace (make_host_string ( ret) );
528
554
}
529
555
530
556
return res;
@@ -539,7 +565,7 @@ Result<HostString> HttpReq::http_req_downstream_tls_protocol() {
539
565
if (!fastly_compute_at_edge_fastly_http_req_downstream_tls_protocol (&ret, &err)) {
540
566
res.emplace_err (err);
541
567
} else {
542
- res.emplace (ret);
568
+ res.emplace (make_host_string ( ret) );
543
569
}
544
570
545
571
return res;
@@ -554,7 +580,7 @@ Result<HostBytes> HttpReq::http_req_downstream_tls_client_hello() {
554
580
if (!fastly_compute_at_edge_fastly_http_req_downstream_tls_client_hello (&ret, &err)) {
555
581
res.emplace_err (err);
556
582
} else {
557
- res.emplace (ret);
583
+ res.emplace (make_host_bytes ( ret) );
558
584
}
559
585
560
586
return res;
@@ -569,7 +595,7 @@ Result<HostBytes> HttpReq::http_req_downstream_tls_raw_client_certificate() {
569
595
if (!fastly_compute_at_edge_fastly_http_req_downstream_tls_raw_client_certificate (&ret, &err)) {
570
596
res.emplace_err (err);
571
597
} else {
572
- res.emplace (ret);
598
+ res.emplace (make_host_bytes ( ret) );
573
599
}
574
600
575
601
return res;
@@ -584,7 +610,7 @@ Result<HostBytes> HttpReq::http_req_downstream_tls_ja3_md5() {
584
610
if (!fastly_compute_at_edge_fastly_http_req_downstream_tls_ja3_md5 (&ret, &err)) {
585
611
res.emplace_err (err);
586
612
} else {
587
- res.emplace (ret);
613
+ res.emplace (make_host_bytes ( ret) );
588
614
}
589
615
590
616
return res;
@@ -736,7 +762,7 @@ Result<HostString> GeoIp::lookup(std::span<uint8_t> bytes) {
736
762
if (!fastly_compute_at_edge_fastly_geo_lookup (&octets_list, &ret, &err)) {
737
763
res.emplace_err (err);
738
764
} else {
739
- res.emplace (ret);
765
+ res.emplace (make_host_string ( ret) );
740
766
}
741
767
742
768
return res;
@@ -795,7 +821,7 @@ Result<std::optional<HostString>> Dict::get(std::string_view name) {
795
821
if (!fastly_compute_at_edge_fastly_dictionary_get (this ->handle , &name_str, &ret, &err)) {
796
822
res.emplace_err (err);
797
823
} else if (ret.is_some ) {
798
- res.emplace (ret.val );
824
+ res.emplace (make_host_string ( ret.val ) );
799
825
} else {
800
826
res.emplace (std::nullopt);
801
827
}
@@ -852,6 +878,10 @@ Result<Void> ObjectStore::insert(std::string_view name, HttpBody body) {
852
878
853
879
namespace host_api {
854
880
881
+ static_assert (std::is_same_v<Secret::Handle, fastly_compute_at_edge_fastly_secret_handle_t >);
882
+ static_assert (
883
+ std::is_same_v<SecretStore::Handle, fastly_compute_at_edge_fastly_secret_store_handle_t >);
884
+
855
885
Result<std::optional<HostString>> Secret::plaintext () const {
856
886
Result<std::optional<HostString>> res;
857
887
@@ -860,7 +890,7 @@ Result<std::optional<HostString>> Secret::plaintext() const {
860
890
if (!fastly_compute_at_edge_fastly_secret_store_plaintext (this ->handle , &ret, &err)) {
861
891
res.emplace_err (err);
862
892
} else if (ret.is_some ) {
863
- res.emplace (ret.val );
893
+ res.emplace (make_host_string ( ret.val ) );
864
894
} else {
865
895
res.emplace (std::nullopt);
866
896
}
0 commit comments