Skip to main content

fastly_sys/
fastly_http_cache.rs

1use crate::{
2    fastly_cache::{CacheDurationNs, CacheHitCount, CacheLookupState, CacheObjectLength},
3    BodyHandle, RequestHandle, ResponseHandle,
4};
5use fastly_shared::FastlyStatus;
6
7pub type HttpCacheHandle = u32;
8pub type IsCacheable = u32;
9pub type IsSensitive = u32;
10
11pub const INVALID_HTTP_CACHE_HANDLE: HttpCacheHandle = HttpCacheHandle::MAX - 1;
12
13#[repr(u32)]
14#[derive(Clone, Copy, Debug, PartialEq, Eq)]
15pub enum HttpStorageAction {
16    Insert,
17    Update,
18    DoNotStore,
19    RecordUncacheable,
20}
21
22#[repr(C)]
23#[derive(Clone, Copy, Debug, PartialEq, Eq)]
24pub struct HttpCacheLookupOptions {
25    pub override_key_ptr: *const u8,
26    pub override_key_len: usize,
27    pub backend_name_ptr: *const u8,
28    pub backend_name_len: usize,
29}
30
31bitflags::bitflags! {
32    #[repr(transparent)]
33    pub struct HttpCacheLookupOptionsMask: u32 {
34        const _RESERVED = 1 << 0;
35        const OVERRIDE_KEY = 1 << 1;
36        const BACKEND_NAME = 1 << 2;
37    }
38}
39
40#[repr(C)]
41#[derive(Clone, Copy, Debug, PartialEq, Eq)]
42pub struct HttpCacheWriteOptions {
43    pub max_age_ns: CacheDurationNs,
44    pub vary_rule_ptr: *const u8,
45    pub vary_rule_len: usize,
46    pub initial_age_ns: CacheDurationNs,
47    pub stale_while_revalidate_ns: CacheDurationNs,
48    pub surrogate_keys_ptr: *const u8,
49    pub surrogate_keys_len: usize,
50    pub length: CacheObjectLength,
51    pub stale_if_error_ns: CacheDurationNs,
52}
53
54bitflags::bitflags! {
55    #[repr(transparent)]
56    pub struct HttpCacheWriteOptionsMask: u32 {
57        const _RESERVED = 1 << 0;
58        const VARY_RULE = 1 << 1;
59        const INITIAL_AGE_NS = 1 << 2;
60        const STALE_WHILE_REVALIDATE_NS = 1 << 3;
61        const SURROGATE_KEYS = 1 << 4;
62        const LENGTH = 1 << 5;
63        const SENSITIVE_DATA = 1 << 6;
64        const STALE_IF_ERROR_NS = 1 << 7;
65    }
66}
67
68#[link(wasm_import_module = "fastly_http_cache")]
69extern "C" {
70    #[link_name = "is_request_cacheable"]
71    pub fn is_request_cacheable(
72        req_handle: RequestHandle,
73        is_cacheable_out: *mut IsCacheable,
74    ) -> FastlyStatus;
75
76    #[link_name = "lookup"]
77    #[deprecated]
78    pub fn lookup(
79        req_handle: RequestHandle,
80        options_mask: HttpCacheLookupOptionsMask,
81        options: *const HttpCacheLookupOptions,
82        cache_handle_out: *mut HttpCacheHandle,
83    ) -> FastlyStatus;
84
85    #[link_name = "transaction_lookup"]
86    pub fn transaction_lookup(
87        req_handle: RequestHandle,
88        options_mask: HttpCacheLookupOptionsMask,
89        options: *const HttpCacheLookupOptions,
90        cache_handle_out: *mut HttpCacheHandle,
91    ) -> FastlyStatus;
92
93    #[link_name = "transaction_insert"]
94    pub fn transaction_insert(
95        handle: HttpCacheHandle,
96        resp_handle: ResponseHandle,
97        options_mask: HttpCacheWriteOptionsMask,
98        options: *const HttpCacheWriteOptions,
99        body_handle_out: *mut BodyHandle,
100    ) -> FastlyStatus;
101
102    #[link_name = "transaction_insert_and_stream_back"]
103    pub fn transaction_insert_and_stream_back(
104        handle: HttpCacheHandle,
105        resp_handle: ResponseHandle,
106        options_mask: HttpCacheWriteOptionsMask,
107        options: *const HttpCacheWriteOptions,
108        body_handle_out: *mut BodyHandle,
109        cache_handle_out: *mut HttpCacheHandle,
110    ) -> FastlyStatus;
111
112    #[link_name = "transaction_update"]
113    pub fn transaction_update(
114        handle: HttpCacheHandle,
115        resp_handle: ResponseHandle,
116        options_mask: HttpCacheWriteOptionsMask,
117        options: *const HttpCacheWriteOptions,
118    ) -> FastlyStatus;
119
120    #[link_name = "transaction_update_and_return_fresh"]
121    pub fn transaction_update_and_return_fresh(
122        handle: HttpCacheHandle,
123        resp_handle: ResponseHandle,
124        options_mask: HttpCacheWriteOptionsMask,
125        options: *const HttpCacheWriteOptions,
126        cache_handle_out: *mut HttpCacheHandle,
127    ) -> FastlyStatus;
128
129    #[link_name = "transaction_choose_stale"]
130    pub fn transaction_choose_stale(handle: HttpCacheHandle) -> FastlyStatus;
131
132    #[link_name = "transaction_record_not_cacheable"]
133    pub fn transaction_record_not_cacheable(
134        handle: HttpCacheHandle,
135        options_mask: HttpCacheWriteOptionsMask,
136        options: *const HttpCacheWriteOptions,
137    ) -> FastlyStatus;
138
139    #[link_name = "transaction_abandon"]
140    pub fn transaction_abandon(handle: HttpCacheHandle) -> FastlyStatus;
141
142    #[link_name = "close"]
143    pub fn close(handle: HttpCacheHandle) -> FastlyStatus;
144
145    #[link_name = "get_suggested_backend_request"]
146    pub fn get_suggested_backend_request(
147        handle: HttpCacheHandle,
148        req_handle_out: *mut RequestHandle,
149    ) -> FastlyStatus;
150
151    #[link_name = "get_suggested_cache_options"]
152    pub fn get_suggested_cache_options(
153        handle: HttpCacheHandle,
154        resp_handle: ResponseHandle,
155        options_mask: HttpCacheWriteOptionsMask,
156        options: *const HttpCacheWriteOptions,
157        options_mask_out: *mut HttpCacheWriteOptionsMask,
158        options_out: *mut HttpCacheWriteOptions,
159    ) -> FastlyStatus;
160
161    #[link_name = "prepare_response_for_storage"]
162    pub fn prepare_response_for_storage(
163        handle: HttpCacheHandle,
164        resp_handle: ResponseHandle,
165        http_storage_action_out: *mut HttpStorageAction,
166        resp_handle_out: *mut ResponseHandle,
167    ) -> FastlyStatus;
168
169    #[link_name = "get_found_response"]
170    pub fn get_found_response(
171        handle: HttpCacheHandle,
172        transform_for_client: u32,
173        resp_handle_out: *mut ResponseHandle,
174        body_handle_out: *mut BodyHandle,
175    ) -> FastlyStatus;
176
177    #[link_name = "get_state"]
178    pub fn get_state(
179        handle: HttpCacheHandle,
180        cache_lookup_state_out: *mut CacheLookupState,
181    ) -> FastlyStatus;
182
183    #[link_name = "get_length"]
184    pub fn get_length(handle: HttpCacheHandle, length_out: *mut CacheObjectLength) -> FastlyStatus;
185
186    #[link_name = "get_max_age_ns"]
187    pub fn get_max_age_ns(
188        handle: HttpCacheHandle,
189        duration_out: *mut CacheDurationNs,
190    ) -> FastlyStatus;
191
192    #[link_name = "get_stale_while_revalidate_ns"]
193    pub fn get_stale_while_revalidate_ns(
194        handle: HttpCacheHandle,
195        duration_out: *mut CacheDurationNs,
196    ) -> FastlyStatus;
197
198    #[link_name = "get_stale_if_error_ns"]
199    pub fn get_stale_if_error_ns(
200        handle: HttpCacheHandle,
201        duration_out: *mut CacheDurationNs,
202    ) -> FastlyStatus;
203
204    #[link_name = "get_age_ns"]
205    pub fn get_age_ns(handle: HttpCacheHandle, duration_out: *mut CacheDurationNs) -> FastlyStatus;
206
207    #[link_name = "get_hits"]
208    pub fn get_hits(handle: HttpCacheHandle, hits_out: *mut CacheHitCount) -> FastlyStatus;
209
210    #[link_name = "get_sensitive_data"]
211    pub fn get_sensitive_data(
212        handle: HttpCacheHandle,
213        sensitive_data_out: *mut IsSensitive,
214    ) -> FastlyStatus;
215
216    #[link_name = "get_surrogate_keys"]
217    pub fn get_surrogate_keys(
218        handle: HttpCacheHandle,
219        surrogate_keys_out_ptr: *mut u8,
220        surrogate_keys_out_len: usize,
221        nwritten_out: *mut usize,
222    ) -> FastlyStatus;
223
224    #[link_name = "get_vary_rule"]
225    pub fn get_vary_rule(
226        handle: HttpCacheHandle,
227        vary_rule_out_ptr: *mut u8,
228        vary_rule_out_len: usize,
229        nwritten_out: *mut usize,
230    ) -> FastlyStatus;
231}