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

mod legacy;
pub use legacy::*;

/// An alias for `u32` to denote which ABI arguments represent `BodyHandle`s.
pub type BodyHandle = u32;
/// An alias for `u32` to denote which ABI arguments represent `PendingRequestHandle`s.
pub type PendingRequestHandle = u32;
/// An alias for `u32` to denote which ABI arguments represent `RequestHandle`s.
pub type RequestHandle = u32;
/// An alias for `u32` to denote which ABI arguments represent `ResponseHandle`s.
pub type ResponseHandle = u32;
/// An alias for `u32` to denote which ABI arguments represent `DictionaryHandle`s.
pub type DictionaryHandle = u32;

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 = "downstream_client_ip_addr"]
        pub fn downstream_client_ip_addr(
            addr_octets_out: *mut u8,
            nwritten_out: *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 = "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 = "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;
    }
}

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

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