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
// Warnings (other than unused variables) in doctests are promoted to errors.
#![doc(test(attr(deny(warnings))))]
#![doc(test(attr(allow(dead_code))))]
#![doc(test(attr(allow(unused_variables))))]
#![deny(broken_intra_doc_links)]
#![deny(invalid_codeblock_attributes)]

use std::convert::TryFrom;
use std::fmt;

use http::HeaderValue;

/// The maximum number of pending requests that can be passed to `select`.
///
/// In practice, a program will be limited first by the number of requests it can create.
pub const MAX_PENDING_REQS: u32 = 16 * 1024;

// These should always be a very high number that is not `MAX`, to avoid clashing with both
// legitimate handles, as well as other sentinel values defined by cranelift_entity.
pub const INVALID_REQUEST_HANDLE: u32 = std::u32::MAX - 1;
pub const INVALID_PENDING_REQUEST_HANDLE: u32 = std::u32::MAX - 1;
pub const INVALID_RESPONSE_HANDLE: u32 = std::u32::MAX - 1;
pub const INVALID_BODY_HANDLE: u32 = std::u32::MAX - 1;
pub const INVALID_DICTIONARY_HANDLE: u32 = std::u32::MAX - 1;

#[deprecated(since = "0.5.0", note = "renamed to FastlyStatus")]
pub type XqdStatus = FastlyStatus;

#[derive(Clone, Copy, Eq, PartialEq)]
#[repr(transparent)]
pub struct FastlyStatus {
    pub code: i32,
}

impl FastlyStatus {
    /// Success value.
    ///
    /// This indicates that a hostcall finished successfully.
    pub const OK: Self = Self { code: 0 };
    /// Generic error value.
    ///
    /// This means that some unexpected error occured during a hostcall.
    pub const ERROR: Self = Self { code: 1 };
    /// Invalid argument.
    pub const INVAL: Self = Self { code: 2 };
    /// Invalid handle.
    ///
    /// Thrown when a request, response, or body handle is not valid.
    pub const BADF: Self = Self { code: 3 };
    /// Buffer length error.
    ///
    /// Thrown when a buffer is too long.
    pub const BUFLEN: Self = Self { code: 4 };
    /// Unsupported operation error.
    ///
    /// This error is thrown when some operation cannot be performed, because it is not supported.
    pub const UNSUPPORTED: Self = Self { code: 5 };
    /// Alignment error.
    ///
    /// This is thrown when a pointer does not point to a properly aligned slice of memory.
    pub const BADALIGN: Self = Self { code: 6 };
    /// HTTP parse error.
    ///
    /// This can be thrown when a method, URI, header, or status is not valid. This can also
    /// be thrown if a message head is too large.
    ///
    #[deprecated(
        since = "0.6.1",
        note = "Please use the equivalent `HTTPINVALID` constant instead"
    )]
    pub const HTTPPARSE: Self = Self { code: 7 };
    /// Invalid HTTP error.
    ///
    /// This can be thrown when a method, URI, header, or status is not valid. This can also
    /// be thrown if a message head is too large.
    pub const HTTPINVALID: Self = Self { code: 7 };
    /// HTTP user error.
    ///
    /// This is thrown in cases where user code caused an HTTP error. For example, attempt to send
    /// a 1xx response code, or a request with a non-absolute URI. This can also be caused by
    /// an unexpected header: both `content-length` and `transfer-encoding`, for example.
    pub const HTTPUSER: Self = Self { code: 8 };
    /// HTTP incomplete message error.
    ///
    /// This can be thrown when a stream ended unexpectedly.
    pub const HTTPINCOMPLETE: Self = Self { code: 9 };
    /// A `None` error.
    ///
    /// This status code is used to indicate when an optional value did not exist, as opposed to
    /// an empty value.
    pub const NONE: Self = Self { code: 10 };

    pub fn is_ok(&self) -> bool {
        self == &Self::OK
    }

    pub fn is_err(&self) -> bool {
        !self.is_ok()
    }

    /// Convert a `FastlyStatus` value to a `Result<(), FastlyStatus>`.
    ///
    /// This will consume a status code, and return `Ok(())` if and only if the value was
    /// `FastlyStatus::OK`. If the status code was some error, then it will be returned in the
    /// result's `Err` variant.
    pub fn result(self) -> Result<(), Self> {
        if let Self::OK = self {
            Ok(())
        } else {
            Err(self)
        }
    }
}

impl fmt::Debug for FastlyStatus {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match *self {
            Self::OK => "OK",
            Self::ERROR => "ERROR",
            Self::INVAL => "INVAL",
            Self::BADF => "BADF",
            Self::BUFLEN => "BUFLEN",
            Self::BADALIGN => "BADALIGN",
            Self::HTTPINVALID => "HTTP_INVALID_ERROR",
            Self::HTTPUSER => "HTTP_USER_ERROR",
            Self::HTTPINCOMPLETE => "HTTP_INCOMPLETE_MESSAGE",
            Self::NONE => "NONE",
            _ => "UNKNOWN",
        })
    }
}

#[deprecated(since = "0.5.0", note = "renamed to FASTLY_ABI_VERSION")]
pub const XQD_ABI_VERSION: u64 = FASTLY_ABI_VERSION;

pub const FASTLY_ABI_VERSION: u64 = 1;

// define our own enum rather than using `http`'s, so that we can easily convert it to a scalar
#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[repr(u32)]
pub enum HttpVersion {
    Http09 = 0,
    Http10 = 1,
    Http11 = 2,
    H2 = 3,
    H3 = 4,
}

impl HttpVersion {
    pub fn as_u32(&self) -> u32 {
        *self as u32
    }
}

// TODO ACF 2019-12-04: could use num-derive for this, but I don't think it's worth pulling in a
// whole new set of dependencies when this will likely be encoded by witx shortly
impl TryFrom<u32> for HttpVersion {
    type Error = String;

    fn try_from(x: u32) -> Result<Self, Self::Error> {
        if x == Self::Http09 as u32 {
            Ok(Self::Http09)
        } else if x == Self::Http10 as u32 {
            Ok(Self::Http10)
        } else if x == Self::Http11 as u32 {
            Ok(Self::Http11)
        } else if x == Self::H2 as u32 {
            Ok(Self::H2)
        } else if x == Self::H3 as u32 {
            Ok(Self::H3)
        } else {
            Err(format!("unknown http version enum value: {}", x))
        }
    }
}

impl From<http::Version> for HttpVersion {
    fn from(v: http::Version) -> Self {
        match v {
            http::Version::HTTP_09 => Self::Http09,
            http::Version::HTTP_10 => Self::Http10,
            http::Version::HTTP_11 => Self::Http11,
            http::Version::HTTP_2 => Self::H2,
            http::Version::HTTP_3 => Self::H3,
            _ => unreachable!(),
        }
    }
}

impl From<HttpVersion> for http::Version {
    fn from(v: HttpVersion) -> Self {
        match v {
            HttpVersion::Http09 => Self::HTTP_09,
            HttpVersion::Http10 => Self::HTTP_10,
            HttpVersion::Http11 => Self::HTTP_11,
            HttpVersion::H2 => Self::HTTP_2,
            HttpVersion::H3 => Self::HTTP_3,
        }
    }
}

#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[repr(u32)]
pub enum BodyWriteEnd {
    Back = 0,
    Front = 1,
}

/// Optional override for response caching behavior.
#[derive(Clone, Debug)]
pub enum CacheOverride {
    /// Do not override the behavior specified in the origin response's cache control headers.
    None,
    /// Do not cache the response to this request, regardless of the origin response's headers.
    Pass,
    /// Override particular cache control settings.
    ///
    /// The origin response's cache control headers will be used for ttl and stale_while_revalidate if `None`.
    Override {
        ttl: Option<u32>,
        stale_while_revalidate: Option<u32>,
        pci: bool,
        surrogate_key: Option<HeaderValue>,
    },
}

impl CacheOverride {
    pub const fn none() -> Self {
        Self::None
    }

    pub const fn pass() -> Self {
        Self::Pass
    }

    pub fn is_pass(&self) -> bool {
        if let Self::Pass = self {
            true
        } else {
            false
        }
    }

    pub const fn ttl(ttl: u32) -> Self {
        Self::Override {
            ttl: Some(ttl),
            stale_while_revalidate: None,
            pci: false,
            surrogate_key: None,
        }
    }

    pub const fn stale_while_revalidate(swr: u32) -> Self {
        Self::Override {
            ttl: None,
            stale_while_revalidate: Some(swr),
            pci: false,
            surrogate_key: None,
        }
    }

    pub const fn pci(pci: bool) -> Self {
        Self::Override {
            ttl: None,
            stale_while_revalidate: None,
            pci,
            surrogate_key: None,
        }
    }

    pub const fn surrogate_key(sk: HeaderValue) -> Self {
        Self::Override {
            ttl: None,
            stale_while_revalidate: None,
            pci: false,
            surrogate_key: Some(sk),
        }
    }

    pub fn set_none(&mut self) {
        *self = Self::None;
    }

    pub fn set_pass(&mut self, pass: bool) {
        if pass {
            *self = Self::Pass;
        } else if let Self::Pass = self {
            *self = Self::None;
        }
    }

    pub fn set_ttl(&mut self, new_ttl: u32) {
        match self {
            Self::Override { ttl, .. } => *ttl = Some(new_ttl),
            _ => *self = Self::ttl(new_ttl),
        }
    }

    pub fn set_stale_while_revalidate(&mut self, new_swr: u32) {
        match self {
            Self::Override {
                stale_while_revalidate,
                ..
            } => *stale_while_revalidate = Some(new_swr),
            _ => *self = Self::stale_while_revalidate(new_swr),
        }
    }

    pub fn set_pci(&mut self, new_pci: bool) {
        match self {
            Self::Override { pci, .. } => *pci = new_pci,
            _ => *self = Self::pci(new_pci),
        }
    }

    pub fn set_surrogate_key(&mut self, new_surrogate_key: HeaderValue) {
        match self {
            Self::Override { surrogate_key, .. } => *surrogate_key = Some(new_surrogate_key),
            _ => *self = Self::surrogate_key(new_surrogate_key),
        }
    }

    pub const fn default() -> Self {
        Self::None
    }

    /// Convert to a representation suitable for passing across the ABI boundary.
    ///
    /// The representation contains the `CacheOverrideTag` along with all of the possible fields:
    /// `(tag, ttl, swr, sk)`.
    #[doc(hidden)]
    pub fn to_abi(&self) -> (u32, u32, u32, Option<&[u8]>) {
        match *self {
            Self::None => (CacheOverrideTag::empty().bits(), 0, 0, None),
            Self::Pass => (CacheOverrideTag::PASS.bits(), 0, 0, None),
            Self::Override {
                ttl,
                stale_while_revalidate,
                pci,
                ref surrogate_key,
            } => {
                let mut tag = CacheOverrideTag::empty();
                let ttl = if let Some(ttl) = ttl {
                    tag |= CacheOverrideTag::TTL;
                    ttl
                } else {
                    0
                };
                let swr = if let Some(swr) = stale_while_revalidate {
                    tag |= CacheOverrideTag::STALE_WHILE_REVALIDATE;
                    swr
                } else {
                    0
                };
                if pci {
                    tag |= CacheOverrideTag::PCI;
                }
                let sk = surrogate_key.as_ref().map(HeaderValue::as_bytes);
                (tag.bits(), ttl, swr, sk)
            }
        }
    }

    /// Convert from the representation suitable for passing across the ABI boundary.
    ///
    /// Returns `None` if the tag is not recognized. Depending on the tag, some of the values may be
    /// ignored.
    #[doc(hidden)]
    pub fn from_abi(
        tag: u32,
        ttl: u32,
        swr: u32,
        surrogate_key: Option<HeaderValue>,
    ) -> Option<Self> {
        CacheOverrideTag::from_bits(tag).map(|tag| {
            if tag.contains(CacheOverrideTag::PASS) {
                return CacheOverride::Pass;
            }
            if tag.is_empty() && surrogate_key.is_none() {
                return CacheOverride::None;
            }
            let ttl = if tag.contains(CacheOverrideTag::TTL) {
                Some(ttl)
            } else {
                None
            };
            let stale_while_revalidate = if tag.contains(CacheOverrideTag::STALE_WHILE_REVALIDATE) {
                Some(swr)
            } else {
                None
            };
            let pci = tag.contains(CacheOverrideTag::PCI);
            CacheOverride::Override {
                ttl,
                stale_while_revalidate,
                pci,
                surrogate_key,
            }
        })
    }
}

bitflags::bitflags! {
    /// A bit field used to tell the host which fields are used when setting the cache override.
    ///
    /// If the `PASS` bit is set, all other bits are ignored.
    struct CacheOverrideTag: u32 {
        const PASS = 1 << 0;
        const TTL = 1 << 1;
        const STALE_WHILE_REVALIDATE = 1 << 2;
        const PCI = 1 << 3;
    }
}