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
// 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@Edge 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@Edge 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@Edge 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;

// 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 BodyHandle = u32;
pub type PendingRequestHandle = u32;
pub type RequestHandle = u32;
pub type ResponseHandle = u32;
pub type DictionaryHandle = u32;
pub type ObjectStoreHandle = 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,
}

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,
        }
    }
}

bitflags::bitflags! {
    /// `Content-Encoding` codings.
    ///
    /// This type must match the definition in `typename.witx`, and will be replaced once we
    /// generate Wasm-side bindings from witx.
    #[derive(Default)]
    #[repr(transparent)]
    pub struct ContentEncodings: u32 {
        const GZIP = 1 << 0;
    }
}

bitflags::bitflags! {
    /// `BackendConfigOptions` codings.
    ///
    /// We are crossing our fingers that witx+wiggle define these in order in the obvious way,
    /// and that our test suite will sort out any incompatibilities. This is not great.
    #[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;
    }

}

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;
    }
}

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::*;

    #[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_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_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;

        #[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_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;

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

        #[link_name = "redirect_to_grip_proxy"]
        pub fn redirect_to_grip_proxy(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;

        #[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_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_wait"]
        pub fn pending_req_wait(
            pending_req_handle: PendingRequestHandle,
            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;

        #[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_object_store {
    use super::*;

    #[link(wasm_import_module = "fastly_object_store")]
    extern "C" {
        #[link_name = "open"]
        pub fn open(
            name_ptr: *const u8,
            name_len: usize,
            object_store_handle_out: *mut ObjectStoreHandle,
        ) -> FastlyStatus;

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

        #[link_name = "insert"]
        pub fn insert(
            object_store_handle: ObjectStoreHandle,
            key_ptr: *const u8,
            key_len: usize,
            body_handle: BodyHandle,
        ) -> 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;
    }
}

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;
    }
}