1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
// TODO ACF 2020-12-01: remove once this is fixed: https://github.com/rust-lang/rust/issues/79581
#![allow(clashing_extern_declarations)]

//! FFI bindings to the Fastly Compute ABI.
//!
//! This is a low-level package; the [`fastly`](https://docs.rs/fastly) crate wraps these functions
//! in a much friendlier, Rust-like interface. You should not have to depend on this crate
//! explicitly in your `Cargo.toml`.
//!
//! # Versioning and compatibility
//!
//! The Cargo version of this package was previously set according to compatibility with the
//! Compute platform. Since the [`v0.25.0` release of the Fastly
//! CLI](https://github.com/fastly/cli/releases/tag/v0.25.0), the CLI is configured with the range
//! of `fastly-sys` versions that are currently compatible with the Compute platform. The Cargo
//! version of this package since `0.4.0` instead follows the [Cargo SemVer compatibility
//! guidelines](https://doc.rust-lang.org/cargo/reference/semver.html).
use fastly_shared::FastlyStatus;

pub mod fastly_cache;
pub mod fastly_config_store;

// The following type aliases are used for readability of definitions in this module. They should
// not be confused with types of similar names in the `fastly` crate which are used to provide safe
// wrappers around these definitions.

pub type PendingObjectStoreLookupHandle = u32;
pub type PendingObjectStoreInsertHandle = u32;
pub type PendingObjectStoreDeleteHandle = u32;
pub type BodyHandle = u32;
pub type PendingRequestHandle = u32;
pub type RequestHandle = u32;
pub type ResponseHandle = u32;
pub type DictionaryHandle = u32;
#[deprecated(since = "0.9.3", note = "renamed to KV Store")]
pub type ObjectStoreHandle = u32;
pub type KVStoreHandle = u32;
pub type SecretStoreHandle = u32;
pub type SecretHandle = u32;
pub type AsyncItemHandle = u32;

#[repr(C)]
pub struct DynamicBackendConfig {
    pub host_override: *const u8,
    pub host_override_len: u32,
    pub connect_timeout_ms: u32,
    pub first_byte_timeout_ms: u32,
    pub between_bytes_timeout_ms: u32,
    pub ssl_min_version: u32,
    pub ssl_max_version: u32,
    pub cert_hostname: *const u8,
    pub cert_hostname_len: u32,
    pub ca_cert: *const u8,
    pub ca_cert_len: u32,
    pub ciphers: *const u8,
    pub ciphers_len: u32,
    pub sni_hostname: *const u8,
    pub sni_hostname_len: u32,
    pub client_certificate: *const u8,
    pub client_certificate_len: u32,
    pub client_key: SecretHandle,
}

impl Default for DynamicBackendConfig {
    fn default() -> Self {
        DynamicBackendConfig {
            host_override: std::ptr::null(),
            host_override_len: 0,
            connect_timeout_ms: 0,
            first_byte_timeout_ms: 0,
            between_bytes_timeout_ms: 0,
            ssl_min_version: 0,
            ssl_max_version: 0,
            cert_hostname: std::ptr::null(),
            cert_hostname_len: 0,
            ca_cert: std::ptr::null(),
            ca_cert_len: 0,
            ciphers: std::ptr::null(),
            ciphers_len: 0,
            sni_hostname: std::ptr::null(),
            sni_hostname_len: 0,
            client_certificate: std::ptr::null(),
            client_certificate_len: 0,
            client_key: 0,
        }
    }
}

bitflags::bitflags! {
    /// `Content-Encoding` codings.
    #[derive(Default)]
    #[repr(transparent)]
    pub struct ContentEncodings: u32 {
        const GZIP = 1 << 0;
    }
}

bitflags::bitflags! {
    /// `BackendConfigOptions` codings.
    #[derive(Default)]
    #[repr(transparent)]
    pub struct BackendConfigOptions: u32 {
        const RESERVED = 1 << 0;
        const HOST_OVERRIDE = 1 << 1;
        const CONNECT_TIMEOUT = 1 << 2;
        const FIRST_BYTE_TIMEOUT = 1 << 3;
        const BETWEEN_BYTES_TIMEOUT = 1 << 4;
        const USE_SSL = 1 << 5;
        const SSL_MIN_VERSION = 1 << 6;
        const SSL_MAX_VERSION = 1 << 7;
        const CERT_HOSTNAME = 1 << 8;
        const CA_CERT = 1 << 9;
        const CIPHERS = 1 << 10;
        const SNI_HOSTNAME = 1 << 11;
        const DONT_POOL = 1 << 12;
        const CLIENT_CERT = 1 << 13;
        const GRPC = 1 << 14;
    }
}

pub mod fastly_abi {
    use super::*;

    #[link(wasm_import_module = "fastly_abi")]
    extern "C" {
        #[link_name = "init"]
        /// Tell the runtime what ABI version this program is using (FASTLY_ABI_VERSION)
        pub fn init(abi_version: u64) -> FastlyStatus;
    }
}

pub mod fastly_uap {
    use super::*;

    #[link(wasm_import_module = "fastly_uap")]
    extern "C" {
        #[link_name = "parse"]
        pub fn parse(
            user_agent: *const u8,
            user_agent_max_len: usize,
            family: *mut u8,
            family_max_len: usize,
            family_written: *mut usize,
            major: *mut u8,
            major_max_len: usize,
            major_written: *mut usize,
            minor: *mut u8,
            minor_max_len: usize,
            minor_written: *mut usize,
            patch: *mut u8,
            patch_max_len: usize,
            patch_written: *mut usize,
        ) -> FastlyStatus;
    }
}

pub mod fastly_http_body {
    use super::*;

    #[link(wasm_import_module = "fastly_http_body")]
    extern "C" {
        #[link_name = "append"]
        pub fn append(dst_handle: BodyHandle, src_handle: BodyHandle) -> FastlyStatus;

        #[link_name = "new"]
        pub fn new(handle_out: *mut BodyHandle) -> FastlyStatus;

        #[link_name = "read"]
        pub fn read(
            body_handle: BodyHandle,
            buf: *mut u8,
            buf_len: usize,
            nread_out: *mut usize,
        ) -> FastlyStatus;

        // overeager warning for extern declarations is a rustc bug: https://github.com/rust-lang/rust/issues/79581
        #[allow(clashing_extern_declarations)]
        #[link_name = "write"]
        pub fn write(
            body_handle: BodyHandle,
            buf: *const u8,
            buf_len: usize,
            end: fastly_shared::BodyWriteEnd,
            nwritten_out: *mut usize,
        ) -> FastlyStatus;

        /// Close a body, freeing its resources and causing any sends to finish.
        #[link_name = "close"]
        pub fn close(body_handle: BodyHandle) -> FastlyStatus;

        #[link_name = "trailer_append"]
        pub fn trailer_append(
            body_handle: BodyHandle,
            name: *const u8,
            name_len: usize,
            value: *const u8,
            value_len: usize,
        ) -> FastlyStatus;

        #[link_name = "trailer_names_get"]
        pub fn trailer_names_get(
            body_handle: BodyHandle,
            buf: *mut u8,
            buf_len: usize,
            cursor: u32,
            ending_cursor: *mut i64,
            nwritten: *mut usize,
        ) -> FastlyStatus;

        #[link_name = "trailer_value_get"]
        pub fn trailer_value_get(
            body_handle: BodyHandle,
            name: *const u8,
            name_len: usize,
            value: *mut u8,
            value_max_len: usize,
            nwritten: *mut usize,
        ) -> FastlyStatus;

        #[link_name = "trailer_values_get"]
        pub fn trailer_values_get(
            body_handle: BodyHandle,
            name: *const u8,
            name_len: usize,
            buf: *mut u8,
            buf_len: usize,
            cursor: u32,
            ending_cursor: *mut i64,
            nwritten: *mut usize,
        ) -> FastlyStatus;

        #[link_name = "known_length"]
        pub fn known_length(body_handle: BodyHandle, length_out: *mut u64) -> FastlyStatus;
    }
}

pub mod fastly_log {
    use super::*;

    #[link(wasm_import_module = "fastly_log")]
    extern "C" {
        #[link_name = "endpoint_get"]
        pub fn endpoint_get(
            name: *const u8,
            name_len: usize,
            endpoint_handle_out: *mut u32,
        ) -> FastlyStatus;

        // overeager warning for extern declarations is a rustc bug: https://github.com/rust-lang/rust/issues/79581
        #[allow(clashing_extern_declarations)]
        #[link_name = "write"]
        pub fn write(
            endpoint_handle: u32,
            msg: *const u8,
            msg_len: usize,
            nwritten_out: *mut usize,
        ) -> FastlyStatus;

    }
}

pub mod fastly_http_req {
    use super::*;

    bitflags::bitflags! {
        #[derive(Default)]
        #[repr(transparent)]
        pub struct SendErrorDetailMask: u32 {
            const RESERVED = 1 << 0;
            const DNS_ERROR_RCODE = 1 << 1;
            const DNS_ERROR_INFO_CODE = 1 << 2;
            const TLS_ALERT_ID = 1 << 3;
        }
    }

    #[repr(u32)]
    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
    pub enum SendErrorDetailTag {
        Uninitialized,
        Ok,
        DnsTimeout,
        DnsError,
        DestinationNotFound,
        DestinationUnavailable,
        DestinationIpUnroutable,
        ConnectionRefused,
        ConnectionTerminated,
        ConnectionTimeout,
        ConnectionLimitReached,
        TlsCertificateError,
        TlsConfigurationError,
        HttpIncompleteResponse,
        HttpResponseHeaderSectionTooLarge,
        HttpResponseBodyTooLarge,
        HttpResponseTimeout,
        HttpResponseStatusInvalid,
        HttpUpgradeFailed,
        HttpProtocolError,
        HttpRequestCacheKeyInvalid,
        HttpRequestUriInvalid,
        InternalError,
        TlsAlertReceived,
        TlsProtocolError,
    }

    #[repr(C)]
    #[derive(Clone, Debug, PartialEq, Eq)]
    pub struct SendErrorDetail {
        pub tag: SendErrorDetailTag,
        pub mask: SendErrorDetailMask,
        pub dns_error_rcode: u16,
        pub dns_error_info_code: u16,
        pub tls_alert_id: u8,
    }

    impl SendErrorDetail {
        pub fn uninitialized_all() -> Self {
            Self {
                tag: SendErrorDetailTag::Uninitialized,
                mask: SendErrorDetailMask::all(),
                dns_error_rcode: Default::default(),
                dns_error_info_code: Default::default(),
                tls_alert_id: Default::default(),
            }
        }
    }

    #[link(wasm_import_module = "fastly_http_req")]
    extern "C" {
        #[link_name = "body_downstream_get"]
        pub fn body_downstream_get(
            req_handle_out: *mut RequestHandle,
            body_handle_out: *mut BodyHandle,
        ) -> FastlyStatus;

        #[link_name = "cache_override_set"]
        pub fn cache_override_set(
            req_handle: RequestHandle,
            tag: u32,
            ttl: u32,
            swr: u32,
        ) -> FastlyStatus;

        #[link_name = "cache_override_v2_set"]
        pub fn cache_override_v2_set(
            req_handle: RequestHandle,
            tag: u32,
            ttl: u32,
            swr: u32,
            sk: *const u8,
            sk_len: usize,
        ) -> FastlyStatus;

        #[link_name = "framing_headers_mode_set"]
        pub fn framing_headers_mode_set(
            req_handle: RequestHandle,
            mode: fastly_shared::FramingHeadersMode,
        ) -> FastlyStatus;

        #[link_name = "downstream_client_ip_addr"]
        pub fn downstream_client_ip_addr(
            addr_octets_out: *mut u8,
            nwritten_out: *mut usize,
        ) -> FastlyStatus;

        #[link_name = "downstream_client_h2_fingerprint"]
        pub fn downstream_client_h2_fingerprint(
            h2fp_out: *mut u8,
            h2fp_max_len: usize,
            nwritten: *mut usize,
        ) -> FastlyStatus;

        #[link_name = "downstream_client_request_id"]
        pub fn downstream_client_request_id(
            reqid_out: *mut u8,
            reqid_max_len: usize,
            nwritten: *mut usize,
        ) -> FastlyStatus;

        #[link_name = "downstream_client_oh_fingerprint"]
        pub fn downstream_client_oh_fingerprint(
            ohfp_out: *mut u8,
            ohfp_max_len: usize,
            nwritten: *mut usize,
        ) -> FastlyStatus;

        #[link_name = "downstream_tls_cipher_openssl_name"]
        pub fn downstream_tls_cipher_openssl_name(
            cipher_out: *mut u8,
            cipher_max_len: usize,
            nwritten: *mut usize,
        ) -> FastlyStatus;

        #[link_name = "downstream_tls_protocol"]
        pub fn downstream_tls_protocol(
            protocol_out: *mut u8,
            protocol_max_len: usize,
            nwritten: *mut usize,
        ) -> FastlyStatus;

        #[link_name = "downstream_tls_client_hello"]
        pub fn downstream_tls_client_hello(
            client_hello_out: *mut u8,
            client_hello_max_len: usize,
            nwritten: *mut usize,
        ) -> FastlyStatus;

        #[link_name = "downstream_tls_ja3_md5"]
        pub fn downstream_tls_ja3_md5(
            ja3_md5_out: *mut u8,
            nwritten_out: *mut usize,
        ) -> FastlyStatus;

        #[link_name = "downstream_tls_ja4"]
        pub fn downstream_tls_ja4(
            ja4_out: *mut u8,
            ja4_max_len: usize,
            nwritten: *mut usize,
        ) -> FastlyStatus;

        #[link_name = "downstream_tls_raw_client_certificate"]
        pub fn downstream_tls_raw_client_certificate(
            client_hello_out: *mut u8,
            client_hello_max_len: usize,
            nwritten: *mut usize,
        ) -> FastlyStatus;

        #[link_name = "downstream_tls_client_cert_verify_result"]
        pub fn downstream_tls_client_cert_verify_result(
            verify_result_out: *mut u32,
        ) -> FastlyStatus;

        #[link_name = "header_append"]
        pub fn header_append(
            req_handle: RequestHandle,
            name: *const u8,
            name_len: usize,
            value: *const u8,
            value_len: usize,
        ) -> FastlyStatus;

        #[link_name = "header_insert"]
        pub fn header_insert(
            req_handle: RequestHandle,
            name: *const u8,
            name_len: usize,
            value: *const u8,
            value_len: usize,
        ) -> FastlyStatus;

        #[link_name = "original_header_names_get"]
        pub fn original_header_names_get(
            buf: *mut u8,
            buf_len: usize,
            cursor: u32,
            ending_cursor: *mut i64,
            nwritten: *mut usize,
        ) -> FastlyStatus;

        #[link_name = "original_header_count"]
        pub fn original_header_count(count_out: *mut u32) -> FastlyStatus;

        #[link_name = "header_names_get"]
        pub fn header_names_get(
            req_handle: RequestHandle,
            buf: *mut u8,
            buf_len: usize,
            cursor: u32,
            ending_cursor: *mut i64,
            nwritten: *mut usize,
        ) -> FastlyStatus;

        #[link_name = "header_values_get"]
        pub fn header_values_get(
            req_handle: RequestHandle,
            name: *const u8,
            name_len: usize,
            buf: *mut u8,
            buf_len: usize,
            cursor: u32,
            ending_cursor: *mut i64,
            nwritten: *mut usize,
        ) -> FastlyStatus;

        #[link_name = "header_values_set"]
        pub fn header_values_set(
            req_handle: RequestHandle,
            name: *const u8,
            name_len: usize,
            values: *const u8,
            values_len: usize,
        ) -> FastlyStatus;

        #[link_name = "header_value_get"]
        pub fn header_value_get(
            req_handle: RequestHandle,
            name: *const u8,
            name_len: usize,
            value: *mut u8,
            value_max_len: usize,
            nwritten: *mut usize,
        ) -> FastlyStatus;

        #[link_name = "header_remove"]
        pub fn header_remove(
            req_handle: RequestHandle,
            name: *const u8,
            name_len: usize,
        ) -> FastlyStatus;

        #[link_name = "method_get"]
        pub fn method_get(
            req_handle: RequestHandle,
            method: *mut u8,
            method_max_len: usize,
            nwritten: *mut usize,
        ) -> FastlyStatus;

        #[link_name = "method_set"]
        pub fn method_set(
            req_handle: RequestHandle,
            method: *const u8,
            method_len: usize,
        ) -> FastlyStatus;

        #[link_name = "new"]
        pub fn new(req_handle_out: *mut RequestHandle) -> FastlyStatus;

        #[deprecated(since = "0.9.8", note = "superseded by send_v2")]
        #[link_name = "send"]
        pub fn send(
            req_handle: RequestHandle,
            body_handle: BodyHandle,
            backend: *const u8,
            backend_len: usize,
            resp_handle_out: *mut ResponseHandle,
            resp_body_handle_out: *mut BodyHandle,
        ) -> FastlyStatus;

        #[link_name = "send_v2"]
        pub fn send_v2(
            req_handle: RequestHandle,
            body_handle: BodyHandle,
            backend: *const u8,
            backend_len: usize,
            error_detail: *mut SendErrorDetail,
            resp_handle_out: *mut ResponseHandle,
            resp_body_handle_out: *mut BodyHandle,
        ) -> FastlyStatus;

        #[link_name = "send_async"]
        pub fn send_async(
            req_handle: RequestHandle,
            body_handle: BodyHandle,
            backend: *const u8,
            backend_len: usize,
            pending_req_handle_out: *mut PendingRequestHandle,
        ) -> FastlyStatus;

        #[link_name = "send_async_streaming"]
        pub fn send_async_streaming(
            req_handle: RequestHandle,
            body_handle: BodyHandle,
            backend: *const u8,
            backend_len: usize,
            pending_req_handle_out: *mut PendingRequestHandle,
        ) -> FastlyStatus;

        #[link_name = "upgrade_websocket"]
        pub fn upgrade_websocket(backend: *const u8, backend_len: usize) -> FastlyStatus;

        #[deprecated(note = "kept for backward compatibility")]
        #[link_name = "redirect_to_websocket_proxy"]
        pub fn redirect_to_websocket_proxy(backend: *const u8, backend_len: usize) -> FastlyStatus;

        #[deprecated(note = "kept for backward compatibility")]
        #[link_name = "redirect_to_grip_proxy"]
        pub fn redirect_to_grip_proxy(backend: *const u8, backend_len: usize) -> FastlyStatus;

        #[link_name = "redirect_to_websocket_proxy_v2"]
        pub fn redirect_to_websocket_proxy_v2(
            req: RequestHandle,
            backend: *const u8,
            backend_len: usize,
        ) -> FastlyStatus;

        #[link_name = "redirect_to_grip_proxy_v2"]
        pub fn redirect_to_grip_proxy_v2(
            req: RequestHandle,
            backend: *const u8,
            backend_len: usize,
        ) -> FastlyStatus;

        #[link_name = "register_dynamic_backend"]
        pub fn register_dynamic_backend(
            name_prefix: *const u8,
            name_prefix_len: usize,
            target: *const u8,
            target_len: usize,
            config_mask: BackendConfigOptions,
            config: *const DynamicBackendConfig,
        ) -> FastlyStatus;

        #[link_name = "uri_get"]
        pub fn uri_get(
            req_handle: RequestHandle,
            uri: *mut u8,
            uri_max_len: usize,
            nwritten: *mut usize,
        ) -> FastlyStatus;

        #[link_name = "uri_set"]
        pub fn uri_set(req_handle: RequestHandle, uri: *const u8, uri_len: usize) -> FastlyStatus;

        #[link_name = "version_get"]
        pub fn version_get(req_handle: RequestHandle, version: *mut u32) -> FastlyStatus;

        #[link_name = "version_set"]
        pub fn version_set(req_handle: RequestHandle, version: u32) -> FastlyStatus;

        #[deprecated(since = "0.9.8", note = "superseded by pending_req_poll_v2")]
        #[link_name = "pending_req_poll"]
        pub fn pending_req_poll(
            pending_req_handle: PendingRequestHandle,
            is_done_out: *mut i32,
            resp_handle_out: *mut ResponseHandle,
            resp_body_handle_out: *mut BodyHandle,
        ) -> FastlyStatus;

        #[link_name = "pending_req_poll_v2"]
        pub fn pending_req_poll_v2(
            pending_req_handle: PendingRequestHandle,
            error_detail: *mut SendErrorDetail,
            is_done_out: *mut i32,
            resp_handle_out: *mut ResponseHandle,
            resp_body_handle_out: *mut BodyHandle,
        ) -> FastlyStatus;

        #[deprecated(since = "0.9.8", note = "superseded by pending_req_select_v2")]
        #[link_name = "pending_req_select"]
        pub fn pending_req_select(
            pending_req_handles: *const PendingRequestHandle,
            pending_req_handles_len: usize,
            done_index_out: *mut i32,
            resp_handle_out: *mut ResponseHandle,
            resp_body_handle_out: *mut BodyHandle,
        ) -> FastlyStatus;

        #[link_name = "pending_req_select_v2"]
        pub fn pending_req_select_v2(
            pending_req_handles: *const PendingRequestHandle,
            pending_req_handles_len: usize,
            error_detail: *mut SendErrorDetail,
            done_index_out: *mut i32,
            resp_handle_out: *mut ResponseHandle,
            resp_body_handle_out: *mut BodyHandle,
        ) -> FastlyStatus;

        #[deprecated(since = "0.9.8", note = "superseded by pending_req_wait_v2")]
        #[link_name = "pending_req_wait"]
        pub fn pending_req_wait(
            pending_req_handle: PendingRequestHandle,
            resp_handle_out: *mut ResponseHandle,
            resp_body_handle_out: *mut BodyHandle,
        ) -> FastlyStatus;

        #[link_name = "pending_req_wait_v2"]
        pub fn pending_req_wait_v2(
            pending_req_handle: PendingRequestHandle,
            error_detail: *mut SendErrorDetail,
            resp_handle_out: *mut ResponseHandle,
            resp_body_handle_out: *mut BodyHandle,
        ) -> FastlyStatus;

        #[link_name = "fastly_key_is_valid"]
        pub fn fastly_key_is_valid(is_valid_out: *mut u32) -> FastlyStatus;

        #[link_name = "close"]
        pub fn close(req_handle: RequestHandle) -> FastlyStatus;

        #[link_name = "auto_decompress_response_set"]
        pub fn auto_decompress_response_set(
            req_handle: RequestHandle,
            encodings: ContentEncodings,
        ) -> FastlyStatus;
    }
}

pub mod fastly_http_resp {
    use super::*;

    #[link(wasm_import_module = "fastly_http_resp")]
    extern "C" {
        #[link_name = "header_append"]
        pub fn header_append(
            resp_handle: ResponseHandle,
            name: *const u8,
            name_len: usize,
            value: *const u8,
            value_len: usize,
        ) -> FastlyStatus;

        #[link_name = "header_insert"]
        pub fn header_insert(
            resp_handle: ResponseHandle,
            name: *const u8,
            name_len: usize,
            value: *const u8,
            value_len: usize,
        ) -> FastlyStatus;

        #[link_name = "header_names_get"]
        pub fn header_names_get(
            resp_handle: ResponseHandle,
            buf: *mut u8,
            buf_len: usize,
            cursor: u32,
            ending_cursor: *mut i64,
            nwritten: *mut usize,
        ) -> FastlyStatus;

        #[link_name = "header_value_get"]
        pub fn header_value_get(
            resp_handle: ResponseHandle,
            name: *const u8,
            name_len: usize,
            value: *mut u8,
            value_max_len: usize,
            nwritten: *mut usize,
        ) -> FastlyStatus;

        #[link_name = "header_values_get"]
        pub fn header_values_get(
            resp_handle: ResponseHandle,
            name: *const u8,
            name_len: usize,
            buf: *mut u8,
            buf_len: usize,
            cursor: u32,
            ending_cursor: *mut i64,
            nwritten: *mut usize,
        ) -> FastlyStatus;

        #[link_name = "header_values_set"]
        pub fn header_values_set(
            resp_handle: ResponseHandle,
            name: *const u8,
            name_len: usize,
            values: *const u8,
            values_len: usize,
        ) -> FastlyStatus;

        #[link_name = "header_remove"]
        pub fn header_remove(
            resp_handle: ResponseHandle,
            name: *const u8,
            name_len: usize,
        ) -> FastlyStatus;

        #[link_name = "new"]
        pub fn new(resp_handle_out: *mut ResponseHandle) -> FastlyStatus;

        #[link_name = "send_downstream"]
        pub fn send_downstream(
            resp_handle: ResponseHandle,
            body_handle: BodyHandle,
            streaming: u32,
        ) -> FastlyStatus;

        #[link_name = "status_get"]
        pub fn status_get(resp_handle: ResponseHandle, status: *mut u16) -> FastlyStatus;

        #[link_name = "status_set"]
        pub fn status_set(resp_handle: ResponseHandle, status: u16) -> FastlyStatus;

        #[link_name = "version_get"]
        pub fn version_get(resp_handle: ResponseHandle, version: *mut u32) -> FastlyStatus;

        #[link_name = "version_set"]
        pub fn version_set(resp_handle: ResponseHandle, version: u32) -> FastlyStatus;

        #[link_name = "framing_headers_mode_set"]
        pub fn framing_headers_mode_set(
            resp_handle: ResponseHandle,
            mode: fastly_shared::FramingHeadersMode,
        ) -> FastlyStatus;

        #[doc(hidden)]
        #[link_name = "http_keepalive_mode_set"]
        pub fn http_keepalive_mode_set(
            resp_handle: ResponseHandle,
            mode: fastly_shared::HttpKeepaliveMode,
        ) -> FastlyStatus;

        #[link_name = "close"]
        pub fn close(resp_handle: ResponseHandle) -> FastlyStatus;
    }
}

pub mod fastly_dictionary {
    use super::*;

    #[link(wasm_import_module = "fastly_dictionary")]
    extern "C" {
        #[link_name = "open"]
        pub fn open(
            name: *const u8,
            name_len: usize,
            dict_handle_out: *mut DictionaryHandle,
        ) -> FastlyStatus;

        #[link_name = "get"]
        pub fn get(
            dict_handle: DictionaryHandle,
            key: *const u8,
            key_len: usize,
            value: *mut u8,
            value_max_len: usize,
            nwritten: *mut usize,
        ) -> FastlyStatus;
    }
}

pub mod fastly_geo {
    use super::*;

    #[link(wasm_import_module = "fastly_geo")]
    extern "C" {
        #[link_name = "lookup"]
        pub fn lookup(
            addr_octets: *const u8,
            addr_len: usize,
            buf: *mut u8,
            buf_len: usize,
            nwritten_out: *mut usize,
        ) -> FastlyStatus;
    }
}

pub mod fastly_device_detection {
    use super::*;

    #[link(wasm_import_module = "fastly_device_detection")]
    extern "C" {
        #[link_name = "lookup"]
        pub fn lookup(
            user_agent: *const u8,
            user_agent_max_len: usize,
            buf: *mut u8,
            buf_len: usize,
            nwritten_out: *mut usize,
        ) -> FastlyStatus;
    }
}

pub mod fastly_erl {
    use super::*;

    #[link(wasm_import_module = "fastly_erl")]
    extern "C" {
        #[link_name = "check_rate"]
        pub fn check_rate(
            rc: *const u8,
            rc_max_len: usize,
            entry: *const u8,
            entry_max_len: usize,
            delta: u32,
            window: u32,
            limit: u32,
            pb: *const u8,
            pb_max_len: usize,
            ttl: u32,
            value: *mut u32,
        ) -> FastlyStatus;

        #[link_name = "ratecounter_increment"]
        pub fn ratecounter_increment(
            rc: *const u8,
            rc_max_len: usize,
            entry: *const u8,
            entry_max_len: usize,
            delta: u32,
        ) -> FastlyStatus;

        #[link_name = "ratecounter_lookup_rate"]
        pub fn ratecounter_lookup_rate(
            rc: *const u8,
            rc_max_len: usize,
            entry: *const u8,
            entry_max_len: usize,
            window: u32,
            value: *mut u32,
        ) -> FastlyStatus;

        #[link_name = "ratecounter_lookup_count"]
        pub fn ratecounter_lookup_count(
            rc: *const u8,
            rc_max_len: usize,
            entry: *const u8,
            entry_max_len: usize,
            duration: u32,
            value: *mut u32,
        ) -> FastlyStatus;

        #[link_name = "penaltybox_add"]
        pub fn penaltybox_add(
            pb: *const u8,
            pb_max_len: usize,
            entry: *const u8,
            entry_max_len: usize,
            ttl: u32,
        ) -> FastlyStatus;

        #[link_name = "penaltybox_has"]
        pub fn penaltybox_has(
            pb: *const u8,
            pb_max_len: usize,
            entry: *const u8,
            entry_max_len: usize,
            value: *mut u32,
        ) -> FastlyStatus;
    }
}

#[deprecated(since = "0.9.3", note = "renamed to KV Store")]
pub use fastly_kv_store as fastly_object_store;

pub mod fastly_kv_store {
    use super::*;

    // TODO ACF 2023-04-11: keep the object store name here until the ABI is updated
    #[link(wasm_import_module = "fastly_object_store")]
    extern "C" {
        #[link_name = "open"]
        pub fn open(
            name_ptr: *const u8,
            name_len: usize,
            kv_store_handle_out: *mut KVStoreHandle,
        ) -> FastlyStatus;

        #[link_name = "lookup"]
        pub fn lookup(
            kv_store_handle: KVStoreHandle,
            key_ptr: *const u8,
            key_len: usize,
            body_handle_out: *mut BodyHandle,
        ) -> FastlyStatus;

        #[link_name = "lookup_async"]
        pub fn lookup_async(
            kv_store_handle: KVStoreHandle,
            key_ptr: *const u8,
            key_len: usize,
            pending_body_handle_out: *mut PendingObjectStoreLookupHandle,
        ) -> FastlyStatus;

        #[link_name = "pending_lookup_wait"]
        pub fn pending_lookup_wait(
            pending_body_handle: PendingObjectStoreLookupHandle,
            body_handle_out: *mut BodyHandle,
        ) -> FastlyStatus;

        #[link_name = "insert"]
        pub fn insert(
            kv_store_handle: KVStoreHandle,
            key_ptr: *const u8,
            key_len: usize,
            body_handle: BodyHandle,
        ) -> FastlyStatus;

        #[link_name = "insert_async"]
        pub fn insert_async(
            kv_store_handle: KVStoreHandle,
            key_ptr: *const u8,
            key_len: usize,
            body_handle: BodyHandle,
            pending_body_handle_out: *mut PendingObjectStoreInsertHandle,
        ) -> FastlyStatus;

        #[link_name = "pending_insert_wait"]
        pub fn pending_insert_wait(
            pending_body_handle: PendingObjectStoreInsertHandle,
        ) -> FastlyStatus;

        #[link_name = "delete_async"]
        pub fn delete_async(
            kv_store_handle: KVStoreHandle,
            key_ptr: *const u8,
            key_len: usize,
            pending_body_handle_out: *mut PendingObjectStoreDeleteHandle,
        ) -> FastlyStatus;

        #[link_name = "pending_delete_wait"]
        pub fn pending_delete_wait(
            pending_body_handle: PendingObjectStoreDeleteHandle,
        ) -> FastlyStatus;
    }
}

pub mod fastly_secret_store {
    use super::*;

    #[link(wasm_import_module = "fastly_secret_store")]
    extern "C" {
        #[link_name = "open"]
        pub fn open(
            secret_store_name_ptr: *const u8,
            secret_store_name_len: usize,
            secret_store_handle_out: *mut SecretStoreHandle,
        ) -> FastlyStatus;

        #[link_name = "get"]
        pub fn get(
            secret_store_handle: SecretStoreHandle,
            secret_name_ptr: *const u8,
            secret_name_len: usize,
            secret_handle_out: *mut SecretHandle,
        ) -> FastlyStatus;

        #[link_name = "plaintext"]
        pub fn plaintext(
            secret_handle: SecretHandle,
            plaintext_buf: *mut u8,
            plaintext_max_len: usize,
            nwritten_out: *mut usize,
        ) -> FastlyStatus;

        #[link_name = "from_bytes"]
        pub fn from_bytes(
            plaintext_buf: *const u8,
            plaintext_len: usize,
            secret_handle_out: *mut SecretHandle,
        ) -> FastlyStatus;
    }
}

pub mod fastly_backend {
    use super::*;

    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
    #[repr(u32)]
    pub enum BackendHealth {
        Unknown,
        Healthy,
        Unhealthy,
    }

    #[link(wasm_import_module = "fastly_backend")]
    extern "C" {
        #[link_name = "exists"]
        pub fn exists(
            backend_ptr: *const u8,
            backend_len: usize,
            backend_exists_out: *mut u32,
        ) -> FastlyStatus;

        #[link_name = "is_healthy"]
        pub fn is_healthy(
            backend_ptr: *const u8,
            backend_len: usize,
            backend_health_out: *mut BackendHealth,
        ) -> FastlyStatus;

        #[link_name = "is_dynamic"]
        pub fn is_dynamic(
            backend_ptr: *const u8,
            backend_len: usize,
            value: *mut u32,
        ) -> FastlyStatus;

        #[link_name = "get_host"]
        pub fn get_host(
            backend_ptr: *const u8,
            backend_len: usize,
            value: *mut u8,
            value_max_len: usize,
            nwritten: *mut usize,
        ) -> FastlyStatus;

        #[link_name = "get_override_host"]
        pub fn get_override_host(
            backend_ptr: *const u8,
            backend_len: usize,
            value: *mut u8,
            value_max_len: usize,
            nwritten: *mut usize,
        ) -> FastlyStatus;

        #[link_name = "get_port"]
        pub fn get_port(
            backend_ptr: *const u8,
            backend_len: usize,
            value: *mut u16,
        ) -> FastlyStatus;

        #[link_name = "get_connect_timeout_ms"]
        pub fn get_connect_timeout_ms(
            backend_ptr: *const u8,
            backend_len: usize,
            value: *mut u32,
        ) -> FastlyStatus;

        #[link_name = "get_first_byte_timeout_ms"]
        pub fn get_first_byte_timeout_ms(
            backend_ptr: *const u8,
            backend_len: usize,
            value: *mut u32,
        ) -> FastlyStatus;

        #[link_name = "get_between_bytes_timeout_ms"]
        pub fn get_between_bytes_timeout_ms(
            backend_ptr: *const u8,
            backend_len: usize,
            value: *mut u32,
        ) -> FastlyStatus;

        #[link_name = "is_ssl"]
        pub fn is_ssl(backend_ptr: *const u8, backend_len: usize, value: *mut u32) -> FastlyStatus;

        #[link_name = "get_ssl_min_version"]
        pub fn get_ssl_min_version(
            backend_ptr: *const u8,
            backend_len: usize,
            value: *mut u32,
        ) -> FastlyStatus;

        #[link_name = "get_ssl_max_version"]
        pub fn get_ssl_max_version(
            backend_ptr: *const u8,
            backend_len: usize,
            value: *mut u32,
        ) -> FastlyStatus;
    }
}

pub mod fastly_async_io {
    use super::*;

    #[link(wasm_import_module = "fastly_async_io")]
    extern "C" {
        #[link_name = "select"]
        pub fn select(
            async_item_handles: *const AsyncItemHandle,
            async_item_handles_len: usize,
            timeout_ms: u32,
            done_index_out: *mut u32,
        ) -> FastlyStatus;

        #[link_name = "is_ready"]
        pub fn is_ready(async_item_handle: AsyncItemHandle, ready_out: *mut u32) -> FastlyStatus;
    }
}

pub mod fastly_purge {
    use super::*;

    bitflags::bitflags! {
        #[derive(Default)]
        #[repr(transparent)]
        pub struct PurgeOptionsMask: u32 {
            const SOFT_PURGE = 1 << 0;
            const RET_BUF = 1 << 1;
        }
    }

    #[derive(Debug)]
    #[repr(C)]
    pub struct PurgeOptions {
        pub ret_buf_ptr: *mut u8,
        pub ret_buf_len: usize,
        pub ret_buf_nwritten_out: *mut usize,
    }

    #[link(wasm_import_module = "fastly_purge")]
    extern "C" {
        #[link_name = "purge_surrogate_key"]
        pub fn purge_surrogate_key(
            surrogate_key_ptr: *const u8,
            surrogate_key_len: usize,
            options_mask: PurgeOptionsMask,
            options: *mut PurgeOptions,
        ) -> FastlyStatus;
    }
}