fastly_shared/
lib.rs

1// Warnings (other than unused variables) in doctests are promoted to errors.
2#![doc(test(attr(deny(warnings))))]
3#![doc(test(attr(allow(dead_code))))]
4#![doc(test(attr(allow(unused_variables))))]
5#![deny(rustdoc::broken_intra_doc_links)]
6#![deny(rustdoc::invalid_codeblock_attributes)]
7
8use std::fmt;
9
10use http::HeaderValue;
11
12/// The maximum number of pending requests that can be passed to `select`.
13///
14/// In practice, a program will be limited first by the number of requests it can create.
15pub const MAX_PENDING_REQS: u32 = 16 * 1024;
16
17// These should always be a very high number that is not `MAX`, to avoid clashing with both
18// legitimate handles, as well as other sentinel values defined by cranelift_entity.
19pub const INVALID_ACL_HANDLE: u32 = u32::MAX - 1;
20pub const INVALID_BODY_HANDLE: u32 = u32::MAX - 1;
21pub const INVALID_CACHE_BUSY_HANDLE: u32 = u32::MAX - 1;
22pub const INVALID_CACHE_HANDLE: u32 = u32::MAX - 1;
23pub const INVALID_CACHE_REPLACE_HANDLE: u32 = u32::MAX - 1;
24pub const INVALID_CONFIG_STORE_HANDLE: u32 = u32::MAX - 1;
25pub const INVALID_DICTIONARY_HANDLE: u32 = u32::MAX - 1;
26pub const INVALID_KV_PENDING_DELETE_HANDLE: u32 = u32::MAX - 1;
27pub const INVALID_KV_PENDING_INSERT_HANDLE: u32 = u32::MAX - 1;
28pub const INVALID_KV_PENDING_LIST_HANDLE: u32 = u32::MAX - 1;
29pub const INVALID_KV_PENDING_LOOKUP_HANDLE: u32 = u32::MAX - 1;
30pub const INVALID_KV_STORE_HANDLE: u32 = u32::MAX - 1;
31pub const INVALID_PENDING_REQUEST_HANDLE: u32 = u32::MAX - 1;
32pub const INVALID_REQUEST_HANDLE: u32 = u32::MAX - 1;
33pub const INVALID_REQUEST_PROMISE_HANDLE: u32 = u32::MAX - 1;
34pub const INVALID_RESPONSE_HANDLE: u32 = u32::MAX - 1;
35pub const INVALID_SECRET_HANDLE: u32 = u32::MAX - 1;
36pub const INVALID_SECRET_STORE_HANDLE: u32 = u32::MAX - 1;
37
38/// Constants for defining minimum/maximum TLS versions for connecting to backends.
39#[allow(non_snake_case)]
40#[derive(Clone, Copy, Debug, PartialEq, Eq)]
41#[repr(u32)]
42pub enum SslVersion {
43    TLS1 = 0,
44    TLS1_1 = 1,
45    TLS1_2 = 2,
46    TLS1_3 = 3,
47}
48
49impl SslVersion {
50    pub fn as_u32(&self) -> u32 {
51        *self as u32
52    }
53}
54
55// TODO KTM 2023-02-08: could use num-derive for this, but I don't think it's worth pulling in a
56// whole new set of dependencies when this will likely be encoded by witx shortly (see HttpVersion)
57impl TryFrom<u32> for SslVersion {
58    type Error = String;
59    fn try_from(x: u32) -> Result<Self, Self::Error> {
60        if x == Self::TLS1 as u32 {
61            Ok(Self::TLS1)
62        } else if x == Self::TLS1_1 as u32 {
63            Ok(Self::TLS1_1)
64        } else if x == Self::TLS1_2 as u32 {
65            Ok(Self::TLS1_2)
66        } else if x == Self::TLS1_3 as u32 {
67            Ok(Self::TLS1_3)
68        } else {
69            Err(format!("unknown ssl version enum value: {}", x))
70        }
71    }
72}
73
74#[derive(Clone, Copy, Eq, PartialEq)]
75#[repr(transparent)]
76pub struct FastlyStatus {
77    pub code: i32,
78}
79
80impl FastlyStatus {
81    /// Success value.
82    ///
83    /// This indicates that a hostcall finished successfully.
84    pub const OK: Self = Self { code: 0 };
85    /// Generic error value.
86    ///
87    /// This means that some unexpected error occurred during a hostcall.
88    pub const ERROR: Self = Self { code: 1 };
89    /// Invalid argument.
90    pub const INVAL: Self = Self { code: 2 };
91    /// Invalid handle.
92    ///
93    /// Returned when a request, response, or body handle is not valid.
94    pub const BADF: Self = Self { code: 3 };
95    /// Buffer length error.
96    ///
97    /// Returned when a buffer is too long.
98    pub const BUFLEN: Self = Self { code: 4 };
99    /// Unsupported operation error.
100    ///
101    /// This error is returned when some operation cannot be performed, because it is not supported.
102    pub const UNSUPPORTED: Self = Self { code: 5 };
103    /// Alignment error.
104    ///
105    /// This is returned when a pointer does not point to a properly aligned slice of memory.
106    pub const BADALIGN: Self = Self { code: 6 };
107    /// Invalid HTTP error.
108    ///
109    /// This can be returned when a method, URI, or header is not valid.
110    pub const HTTPINVALID: Self = Self { code: 7 };
111    /// HTTP user error.
112    ///
113    /// This is returned in cases where user code caused an HTTP error. For example, attempt to send
114    /// a 1xx response code, or a request with a non-absolute URI. This can also be caused by
115    /// an unexpected header: both `content-length` and `transfer-encoding`, for example.
116    pub const HTTPUSER: Self = Self { code: 8 };
117    /// HTTP incomplete message error.
118    ///
119    /// This can be returned when a stream ended unexpectedly.
120    pub const HTTPINCOMPLETE: Self = Self { code: 9 };
121    /// A `None` error.
122    ///
123    /// This status code is used to indicate when an optional value did not exist, as opposed to
124    /// an empty value.
125    pub const NONE: Self = Self { code: 10 };
126    /// HTTP head too large error.
127    ///
128    /// This error will be returned when the message head is too large.
129    pub const HTTPHEADTOOLARGE: Self = Self { code: 11 };
130    /// HTTP invalid status error.
131    ///
132    /// This error will be returned when the HTTP message contains an invalid status code.
133    pub const HTTPINVALIDSTATUS: Self = Self { code: 12 };
134    /// Limit exceeded
135    ///
136    /// This is returned when an attempt to allocate a resource has exceeded the maximum number of
137    /// resources permitted. For example, creating too many response handles.
138    pub const LIMITEXCEEDED: Self = Self { code: 13 };
139    /// Resource temporarily unavailable
140    ///
141    /// This is returned when an attempting to retrieve a resource that is not yet available.
142    /// For example when attempting to read trailers from a Body that has not yet been consumed.
143    pub const AGAIN: Self = Self { code: 14 };
144
145    pub fn is_ok(&self) -> bool {
146        self == &Self::OK
147    }
148
149    pub fn is_err(&self) -> bool {
150        !self.is_ok()
151    }
152
153    /// Convert a `FastlyStatus` value to a `Result<(), FastlyStatus>`.
154    ///
155    /// This will consume a status code, and return `Ok(())` if and only if the value was
156    /// `FastlyStatus::OK`. If the status code was some error, then it will be returned in the
157    /// result's `Err` variant.
158    pub fn result(self) -> Result<(), Self> {
159        if let Self::OK = self {
160            Ok(())
161        } else {
162            Err(self)
163        }
164    }
165}
166
167impl fmt::Debug for FastlyStatus {
168    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
169        write!(f, "FastlyStatus::")?;
170        match *self {
171            Self::OK => write!(f, "OK"),
172            Self::ERROR => write!(f, "ERROR"),
173            Self::INVAL => write!(f, "INVAL"),
174            Self::BADF => write!(f, "BADF"),
175            Self::BUFLEN => write!(f, "BUFLEN"),
176            Self::UNSUPPORTED => write!(f, "UNSUPPORTED"),
177            Self::BADALIGN => write!(f, "BADALIGN"),
178            Self::HTTPINVALID => write!(f, "HTTP_INVALID_ERROR"),
179            Self::HTTPUSER => write!(f, "HTTP_USER_ERROR"),
180            Self::HTTPINCOMPLETE => write!(f, "HTTP_INCOMPLETE_MESSAGE"),
181            Self::NONE => write!(f, "NONE"),
182            Self::HTTPHEADTOOLARGE => write!(f, "HTTP_HEAD_TOO_LARGE"),
183            Self::HTTPINVALIDSTATUS => write!(f, "HTTP_INVALID_STATUS"),
184            Self::LIMITEXCEEDED => write!(f, "LIMIT_EXCEEDED"),
185            Self::AGAIN => write!(f, "AGAIN"),
186            _ => write!(f, "UNKNOWN ({})", self.code),
187        }
188    }
189}
190
191pub const FASTLY_ABI_VERSION: u64 = 1;
192
193// define our own enum rather than using `http`'s, so that we can easily convert it to a scalar
194#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)]
195#[repr(u32)]
196pub enum HttpVersion {
197    Http09 = 0,
198    Http10 = 1,
199    Http11 = 2,
200    H2 = 3,
201    H3 = 4,
202}
203
204impl HttpVersion {
205    pub fn as_u32(&self) -> u32 {
206        *self as u32
207    }
208}
209
210// TODO ACF 2019-12-04: could use num-derive for this, but I don't think it's worth pulling in a
211// whole new set of dependencies when this will likely be encoded by witx shortly
212impl TryFrom<u32> for HttpVersion {
213    type Error = String;
214
215    fn try_from(x: u32) -> Result<Self, Self::Error> {
216        if x == Self::Http09 as u32 {
217            Ok(Self::Http09)
218        } else if x == Self::Http10 as u32 {
219            Ok(Self::Http10)
220        } else if x == Self::Http11 as u32 {
221            Ok(Self::Http11)
222        } else if x == Self::H2 as u32 {
223            Ok(Self::H2)
224        } else if x == Self::H3 as u32 {
225            Ok(Self::H3)
226        } else {
227            Err(format!("unknown http version enum value: {}", x))
228        }
229    }
230}
231
232impl From<http::Version> for HttpVersion {
233    fn from(v: http::Version) -> Self {
234        match v {
235            http::Version::HTTP_09 => Self::Http09,
236            http::Version::HTTP_10 => Self::Http10,
237            http::Version::HTTP_11 => Self::Http11,
238            http::Version::HTTP_2 => Self::H2,
239            http::Version::HTTP_3 => Self::H3,
240            _ => unreachable!(),
241        }
242    }
243}
244
245impl From<HttpVersion> for http::Version {
246    fn from(v: HttpVersion) -> Self {
247        match v {
248            HttpVersion::Http09 => Self::HTTP_09,
249            HttpVersion::Http10 => Self::HTTP_10,
250            HttpVersion::Http11 => Self::HTTP_11,
251            HttpVersion::H2 => Self::HTTP_2,
252            HttpVersion::H3 => Self::HTTP_3,
253        }
254    }
255}
256
257#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
258#[repr(u32)]
259pub enum BodyWriteEnd {
260    Back = 0,
261    Front = 1,
262}
263
264/// Determines how the framing headers (`Content-Length`/`Transfer-Encoding`) are set for a
265/// request or response.
266#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
267#[repr(u32)]
268pub enum FramingHeadersMode {
269    /// Determine the framing headers automatically based on the message body, and discard any framing
270    /// headers already set in the message. This is the default behavior.
271    ///
272    /// In automatic mode, a `Content-Length` is used when the size of the body can be determined
273    /// before it is sent. Requests/responses sent in streaming mode, where headers are sent immediately
274    /// but the content of the body is streamed later, will receive a `Transfer-Encoding: chunked`
275    /// to accommodate the dynamic generation of the body.
276    #[default]
277    Automatic = 0,
278
279    /// Use the exact framing headers set in the message, falling back to [`Automatic`][`Self::Automatic`]
280    /// if invalid.
281    ///
282    /// In "from headers" mode, any `Content-Length` or `Transfer-Encoding` headers will be honored.
283    /// You must ensure that those headers have correct values permitted by the
284    /// [HTTP/1.1 specification][spec]. If the provided headers are not permitted by the spec,
285    /// the headers will revert to automatic mode and a log diagnostic will be issued about what was
286    /// wrong. If a `Content-Length` is permitted by the spec, but the value doesn't match the size of
287    /// the actual body, the body will either be truncated (if it is too long), or the connection will
288    /// be hung up early (if it is too short).
289    ///
290    /// [spec]: https://datatracker.ietf.org/doc/html/rfc7230#section-3.3.1
291    ManuallyFromHeaders = 1,
292}
293
294/// Determines whether the client is encouraged to stop using the current connection and to open a
295/// new one for the next request.
296///
297/// Most applications do not need to change this setting.
298#[doc(hidden)]
299#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
300#[repr(u32)]
301pub enum HttpKeepaliveMode {
302    /// This is the default behavor.
303    #[default]
304    Automatic = 0,
305
306    /// Send `Connection: close` in HTTP/1 and a GOAWAY frame in HTTP/2 and HTTP/3.  This prompts
307    /// the client to close the current connection and to open a new one for the next request.
308    NoKeepalive = 1,
309}
310
311/// Optional override for response caching behavior.
312#[derive(Clone, Debug)]
313pub enum CacheOverride {
314    /// Do not override the behavior specified in the origin response's cache control headers.
315    None,
316    /// Do not cache the response to this request, regardless of the origin response's headers.
317    Pass,
318    /// Override particular cache control settings.
319    ///
320    /// The origin response's cache control headers will be used for ttl and stale_while_revalidate if `None`.
321    Override {
322        ttl: Option<u32>,
323        stale_while_revalidate: Option<u32>,
324        pci: bool,
325        surrogate_key: Option<HeaderValue>,
326    },
327}
328
329impl Default for CacheOverride {
330    fn default() -> Self {
331        Self::default()
332    }
333}
334
335impl CacheOverride {
336    pub const fn none() -> Self {
337        Self::None
338    }
339
340    pub const fn pass() -> Self {
341        Self::Pass
342    }
343
344    pub fn is_pass(&self) -> bool {
345        matches!(self, Self::Pass)
346    }
347
348    pub const fn ttl(ttl: u32) -> Self {
349        Self::Override {
350            ttl: Some(ttl),
351            stale_while_revalidate: None,
352            pci: false,
353            surrogate_key: None,
354        }
355    }
356
357    pub const fn stale_while_revalidate(swr: u32) -> Self {
358        Self::Override {
359            ttl: None,
360            stale_while_revalidate: Some(swr),
361            pci: false,
362            surrogate_key: None,
363        }
364    }
365
366    pub const fn pci(pci: bool) -> Self {
367        Self::Override {
368            ttl: None,
369            stale_while_revalidate: None,
370            pci,
371            surrogate_key: None,
372        }
373    }
374
375    pub const fn surrogate_key(sk: HeaderValue) -> Self {
376        Self::Override {
377            ttl: None,
378            stale_while_revalidate: None,
379            pci: false,
380            surrogate_key: Some(sk),
381        }
382    }
383
384    pub fn set_none(&mut self) {
385        *self = Self::None;
386    }
387
388    pub fn set_pass(&mut self, pass: bool) {
389        if pass {
390            *self = Self::Pass;
391        } else if let Self::Pass = self {
392            *self = Self::None;
393        }
394    }
395
396    pub fn get_ttl(&self) -> Option<u32> {
397        if let Self::Override { ttl, .. } = self {
398            *ttl
399        } else {
400            None
401        }
402    }
403
404    pub fn set_ttl(&mut self, new_ttl: u32) {
405        match self {
406            Self::Override { ttl, .. } => *ttl = Some(new_ttl),
407            _ => *self = Self::ttl(new_ttl),
408        }
409    }
410
411    pub fn get_stale_while_revalidate(&self) -> Option<u32> {
412        if let Self::Override {
413            stale_while_revalidate,
414            ..
415        } = self
416        {
417            *stale_while_revalidate
418        } else {
419            None
420        }
421    }
422
423    pub fn set_stale_while_revalidate(&mut self, new_swr: u32) {
424        match self {
425            Self::Override {
426                stale_while_revalidate,
427                ..
428            } => *stale_while_revalidate = Some(new_swr),
429            _ => *self = Self::stale_while_revalidate(new_swr),
430        }
431    }
432
433    pub fn get_pci(&self) -> Option<bool> {
434        if let Self::Override { pci, .. } = self {
435            Some(*pci)
436        } else {
437            None
438        }
439    }
440
441    pub fn set_pci(&mut self, new_pci: bool) {
442        match self {
443            Self::Override { pci, .. } => *pci = new_pci,
444            _ => *self = Self::pci(new_pci),
445        }
446    }
447
448    pub fn get_surrogate_key(&self) -> Option<&HeaderValue> {
449        if let Self::Override { surrogate_key, .. } = self {
450            surrogate_key.as_ref()
451        } else {
452            None
453        }
454    }
455
456    pub fn set_surrogate_key(&mut self, new_surrogate_key: HeaderValue) {
457        match self {
458            Self::Override { surrogate_key, .. } => *surrogate_key = Some(new_surrogate_key),
459            _ => *self = Self::surrogate_key(new_surrogate_key),
460        }
461    }
462
463    pub const fn default() -> Self {
464        Self::None
465    }
466
467    /// Convert to a representation suitable for passing across the ABI boundary.
468    ///
469    /// The representation contains the `CacheOverrideTag` along with all of the possible fields:
470    /// `(tag, ttl, swr, sk)`.
471    #[doc(hidden)]
472    pub fn to_abi(&self) -> (u32, u32, u32, Option<&[u8]>) {
473        match *self {
474            Self::None => (CacheOverrideTag::empty().bits(), 0, 0, None),
475            Self::Pass => (CacheOverrideTag::PASS.bits(), 0, 0, None),
476            Self::Override {
477                ttl,
478                stale_while_revalidate,
479                pci,
480                ref surrogate_key,
481            } => {
482                let mut tag = CacheOverrideTag::empty();
483                let ttl = if let Some(ttl) = ttl {
484                    tag |= CacheOverrideTag::TTL;
485                    ttl
486                } else {
487                    0
488                };
489                let swr = if let Some(swr) = stale_while_revalidate {
490                    tag |= CacheOverrideTag::STALE_WHILE_REVALIDATE;
491                    swr
492                } else {
493                    0
494                };
495                if pci {
496                    tag |= CacheOverrideTag::PCI;
497                }
498                let sk = surrogate_key.as_ref().map(HeaderValue::as_bytes);
499                (tag.bits(), ttl, swr, sk)
500            }
501        }
502    }
503}
504
505bitflags::bitflags! {
506    /// A bit field used to tell the host which fields are used when setting the cache override.
507    ///
508    /// If the `PASS` bit is set, all other bits are ignored.
509    struct CacheOverrideTag: u32 {
510        const PASS = 1 << 0;
511        const TTL = 1 << 1;
512        const STALE_WHILE_REVALIDATE = 1 << 2;
513        const PCI = 1 << 3;
514    }
515}
516
517#[derive(Debug, Clone, Copy, Eq, PartialEq)]
518pub enum ClientCertVerifyResult {
519    /// Success value.
520    ///
521    /// This indicates that client certificate verified successfully.
522    Ok,
523    /// Bad certificate error.
524    ///
525    /// This error means the certificate is corrupt
526    /// (e.g., the certificate signatures do not verify correctly).
527    BadCertificate,
528    /// Certificate revoked error.
529    ///
530    /// This error means the client certificate is revoked by its signer.
531    CertificateRevoked,
532    /// Certificate expired error.
533    ///
534    /// This error means the client certificate has expired or is not currently valid.
535    CertificateExpired,
536    /// Unknown CA error.
537    ///
538    /// This error means the valid certificate chain or partial chain was received, but the
539    /// certificate was not accepted because the CA certificate could not be located or could not
540    /// be matched with a known trust anchor.
541    UnknownCa,
542    /// Certificate missing error.
543    ///
544    /// This error means the client did not provide a certificate during the handshake.
545    CertificateMissing,
546    /// Certificate unknown error.
547    ///
548    /// This error means the client certificate was received, but some other (unspecified) issue
549    /// arose in processing the certificate, rendering it unacceptable.
550    CertificateUnknown,
551}
552
553impl ClientCertVerifyResult {
554    pub fn from_u32(value: u32) -> ClientCertVerifyResult {
555        match value {
556            0 => ClientCertVerifyResult::Ok,
557            1 => ClientCertVerifyResult::BadCertificate,
558            2 => ClientCertVerifyResult::CertificateRevoked,
559            3 => ClientCertVerifyResult::CertificateExpired,
560            4 => ClientCertVerifyResult::UnknownCa,
561            5 => ClientCertVerifyResult::CertificateMissing,
562            _ => ClientCertVerifyResult::CertificateUnknown,
563        }
564    }
565}