fastly 0.2.0-alpha4

Fastly Compute@Edge API
Documentation
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
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
pub use fastly_shared::{CacheOverride, FastlyRequestMetadata};

use crate::abi;
use crate::backend::validate_backend;
use crate::body::{Body, BodyHandle};
use crate::error::{anyhow, ensure, Error};
use crate::response::{handles_to_response, ResponseHandle};
use bytes::{BufMut, BytesMut};
use http::header::{HeaderName, HeaderValue};
use http::{Extensions, Method, Request, Response, Uri, Version};
use lazy_static::lazy_static;
use std::convert::{TryFrom, TryInto};
use std::io::Write;
use std::sync::Mutex;

#[derive(Debug, Eq, Hash, PartialEq)]
#[repr(transparent)]
pub struct RequestHandle {
    handle: u32,
}

lazy_static! {
    pub(crate) static ref GOT_DOWNSTREAM: Mutex<bool> = Mutex::new(false);
}

impl RequestHandle {
    pub const INVALID: Self = RequestHandle {
        handle: fastly_shared::INVALID_REQUEST_HANDLE,
    };

    pub fn is_invalid(&self) -> bool {
        self == &Self::INVALID
    }

    /// Get an owned `RequestHandle` from a borrowed one.
    ///
    /// This should only be used when calling the raw ABI directly.
    pub(crate) unsafe fn handle(&self) -> Self {
        Self {
            handle: self.handle,
        }
    }

    pub fn downstream() -> Result<Self, Error> {
        let mut got = GOT_DOWNSTREAM.lock().unwrap();
        if *got {
            return Err(Error::msg(
                "cannot get more than one handle to the downstream request per execution",
            ));
        }

        let mut handle = RequestHandle::INVALID;
        let status = unsafe { abi::xqd_req_body_downstream_get(&mut handle, std::ptr::null_mut()) };
        if status.is_err() || handle.is_invalid() {
            Err(Error::msg("xqd_req_body_downstream_get failed"))
        } else {
            *got = true;
            Ok(handle)
        }
    }

    pub fn new() -> Result<Self, Error> {
        let mut handle = RequestHandle::INVALID;
        let status = unsafe { abi::xqd_req_new(&mut handle) };
        if status.is_err() || handle.is_invalid() {
            Err(Error::msg("xqd_req_new failed"))
        } else {
            Ok(handle)
        }
    }

    /// Read a request's header names via a buffer of the provided size.
    ///
    /// If there is a header name that is longer than the provided buffer, this will return an error;
    /// you can retry with a larger buffer size if necessary.
    pub fn get_header_names<'a>(
        &'a self,
        buf_size: usize,
    ) -> impl Iterator<Item = Result<HeaderName, Error>> + 'a {
        abi::MultiValueHostcall::new(
            b'\0',
            buf_size,
            move |buf, buf_size, cursor, ending_cursor, nwritten| unsafe {
                abi::xqd_req_header_names_get(
                    self.handle(),
                    buf,
                    buf_size,
                    cursor,
                    ending_cursor,
                    nwritten,
                )
            },
        )
        .map(|res| {
            res.and_then(|name_bytes| {
                HeaderName::from_bytes(&name_bytes)
                    .map_err(|e| anyhow!("invalid header name: {}", e))
            })
        })
    }

    pub fn get_header_values<'a>(
        &'a self,
        name: &'a HeaderName,
        buf_size: usize,
    ) -> impl Iterator<Item = Result<HeaderValue, Error>> + 'a {
        abi::MultiValueHostcall::new(
            b'\0',
            buf_size,
            move |buf, buf_size, cursor, ending_cursor, nwritten| unsafe {
                let name: &[u8] = name.as_ref();
                abi::xqd_req_header_values_get(
                    self.handle(),
                    name.as_ptr(),
                    name.len(),
                    buf,
                    buf_size,
                    cursor,
                    ending_cursor,
                    nwritten,
                )
            },
        )
        .map(|res| {
            res.map(|value_bytes| unsafe {
                // we trust that the hostcall is giving us valid header bytes
                HeaderValue::from_maybe_shared_unchecked(value_bytes)
            })
        })
    }

    pub fn set_header_values<'a, I>(&mut self, name: &HeaderName, values: I) -> Result<(), Error>
    where
        I: IntoIterator<Item = &'a HeaderValue>,
    {
        // build a buffer of all the values, each terminated by a nul byte
        let mut buf = vec![];
        for value in values {
            buf.put(value.as_bytes());
            buf.put_u8(b'\0');
        }

        let name: &[u8] = name.as_ref();
        let status = unsafe {
            abi::xqd_req_header_values_set(
                self.handle(),
                name.as_ptr(),
                name.len(),
                buf.as_ptr(),
                buf.len(),
            )
        };

        if status.is_err() {
            Err(Error::msg("xqd_req_header_values_set failed"))
        } else {
            Ok(())
        }
    }

    pub fn insert_header(&mut self, name: &HeaderName, value: &HeaderValue) -> Result<(), Error> {
        let name_bytes: &[u8] = name.as_ref();
        let value_bytes: &[u8] = value.as_ref();
        let status = unsafe {
            abi::xqd_req_header_insert(
                self.handle(),
                name_bytes.as_ptr(),
                name_bytes.len(),
                value_bytes.as_ptr(),
                value_bytes.len(),
            )
        };
        if status.is_err() {
            Err(Error::msg("xqd_req_header_insert returned error"))
        } else {
            Ok(())
        }
    }

    pub fn append_header(&mut self, name: &HeaderName, value: &HeaderValue) -> Result<(), Error> {
        let name_bytes: &[u8] = name.as_ref();
        let value_bytes: &[u8] = value.as_ref();
        let status = unsafe {
            abi::xqd_req_header_append(
                self.handle(),
                name_bytes.as_ptr(),
                name_bytes.len(),
                value_bytes.as_ptr(),
                value_bytes.len(),
            )
        };
        if status.is_err() {
            Err(Error::msg("xqd_req_header_append returned error"))
        } else {
            Ok(())
        }
    }

    pub fn get_version(&self) -> Result<Version, Error> {
        let mut version = 0;
        let status = unsafe { abi::xqd_req_version_get(self.handle(), &mut version) };
        if status.is_err() {
            Err(Error::msg("xqd_req_version_get failed"))
        } else {
            abi::HttpVersion::try_from(version)
                .map(Into::into)
                .map_err(Error::msg)
        }
    }

    pub fn set_version(&mut self, v: Version) -> Result<(), Error> {
        let status =
            unsafe { abi::xqd_req_version_set(self.handle(), abi::HttpVersion::from(v) as u32) };
        if status.is_err() {
            Err(Error::msg("xqd_req_version_get failed"))
        } else {
            Ok(())
        }
    }

    pub fn get_method(&self, max_length: usize) -> Result<Method, Error> {
        let mut method_bytes = Vec::with_capacity(max_length);
        let mut nwritten = 0;
        let status = unsafe {
            abi::xqd_req_method_get(
                self.handle(),
                method_bytes.as_mut_ptr(),
                method_bytes.capacity(),
                &mut nwritten,
            )
        };
        if status.is_err() {
            return Err(Error::msg("xqd_req_method_get failed"));
        }
        assert!(
            nwritten <= method_bytes.capacity(),
            "xqd_req_method_get wrote too many bytes"
        );
        unsafe {
            method_bytes.set_len(nwritten);
        }
        Method::from_bytes(&method_bytes)
            .map_err(|_| anyhow!("invalid method: {}", String::from_utf8_lossy(&method_bytes)))
    }

    pub fn set_method(&self, method: &Method) -> Result<(), Error> {
        let method_bytes = method.as_str().as_bytes();
        let status = unsafe {
            abi::xqd_req_method_set(self.handle(), method_bytes.as_ptr(), method_bytes.len())
        };
        if status.is_err() {
            Err(Error::msg("xqd_req_method_set failed"))
        } else {
            Ok(())
        }
    }

    pub fn get_uri(&self, max_length: usize) -> Result<Uri, Error> {
        let mut uri_bytes = BytesMut::with_capacity(max_length);
        let mut nwritten = 0;
        let status = unsafe {
            abi::xqd_req_uri_get(
                self.handle(),
                uri_bytes.as_mut_ptr(),
                uri_bytes.capacity(),
                &mut nwritten,
            )
        };
        if status.is_err() {
            return Err(Error::msg("xqd_req_uri_get failed"));
        }
        assert!(
            nwritten <= uri_bytes.capacity(),
            "xqd_req_uri_get wrote too many bytes"
        );
        unsafe {
            uri_bytes.set_len(nwritten);
        }
        Uri::from_maybe_shared(uri_bytes.freeze()).map_err(|e| anyhow!("invalid URI: {}", e))
    }

    pub fn set_uri(&mut self, uri: &Uri) -> Result<(), Error> {
        let uri_bytes = uri.to_string().into_bytes();
        let status =
            unsafe { abi::xqd_req_uri_set(self.handle(), uri_bytes.as_ptr(), uri_bytes.len()) };
        if status.is_err() {
            Err(Error::msg("xqd_req_uri_set failed"))
        } else {
            Ok(())
        }
    }

    pub fn send(
        self,
        body: BodyHandle,
        backend: &str,
    ) -> Result<(ResponseHandle, BodyHandle), Error> {
        let mut resp_handle = ResponseHandle::INVALID;
        let mut resp_body_handle = BodyHandle::INVALID;
        let status = unsafe {
            abi::xqd_req_send(
                self.handle(),
                body.handle(),
                backend.as_ptr(),
                backend.len(),
                &mut resp_handle,
                &mut resp_body_handle,
            )
        };
        if status.is_err() || resp_handle.is_invalid() || resp_body_handle.is_invalid() {
            Err(Error::msg("xqd_req_send failed"))
        } else {
            Ok((resp_handle, resp_body_handle))
        }
    }

    /// Send a request asynchronously via the given backend, returning as soon as the request has
    /// begun sending.
    ///
    /// The resulting [`PendingRequestHandle`](struct.PendingRequestHandle.html) can be evaluated
    /// using [`PendingRequestHandle::poll()`](struct.PendingRequestHandle.html#method.poll),
    /// [`PendingRequestHandle::wait()`](struct.PendingRequestHandle.html#method.wait), or
    /// [`select_handles`](fn.select_handles.html). It can also be discarded if the request was sent
    /// for effects it might have, and the response is unimportant.
    pub fn send_async(
        self,
        body: BodyHandle,
        backend: &str,
    ) -> Result<PendingRequestHandle, Error> {
        let mut pending_req_handle = PendingRequestHandle::INVALID;
        let status = unsafe {
            abi::xqd_req_send_async(
                self.handle(),
                body.handle(),
                backend.as_ptr(),
                backend.len(),
                &mut pending_req_handle,
            )
        };
        if status.is_err() || pending_req_handle.is_invalid() {
            Err(Error::msg("xqd_req_send_async failed"))
        } else {
            Ok(pending_req_handle)
        }
    }

    /// Set the cache override behavior for this request.
    ///
    /// This setting will override any cache directive headers returned in response to this request.
    pub fn set_cache_override(&mut self, cache_override: CacheOverride) {
        let (tag, ttl, swr) = cache_override.to_abi();
        let status = unsafe { abi::xqd_req_cache_override_set(self.handle(), tag, ttl, swr) };
        if status.is_err() {
            panic!("xqd_req_cache_override_set failed");
        }
    }
}

/// Get handles to the downstream request parts and body at the same time.
///
/// This will return an error if either the parts of the body have already been given.
//
// TODO ACF 2020-01-15: try to think up a better name for this
pub fn downstream_request_and_body_handles() -> Result<(RequestHandle, BodyHandle), Error> {
    let mut got_req = crate::request::GOT_DOWNSTREAM.lock().unwrap();
    let mut got_body = crate::body::GOT_DOWNSTREAM.lock().unwrap();
    if *got_req || *got_body {
        return Err(Error::msg(
            "cannot get more than one handle to the downstream request per execution",
        ));
    }

    let mut req_handle = RequestHandle::INVALID;
    let mut body_handle = BodyHandle::INVALID;
    let status = unsafe { abi::xqd_req_body_downstream_get(&mut req_handle, &mut body_handle) };
    if status.is_err() || req_handle.is_invalid() || body_handle.is_invalid() {
        Err(Error::msg("xqd_req_body_downstream_get failed"))
    } else {
        *got_req = true;
        *got_body = true;
        Ok((req_handle, body_handle))
    }
}

/// Get the downstream request.
///
/// This may be called exactly once per execution, otherwise an error is returned.
///
/// Note, this currently copies the entire body of the request into the guest. Future revisions
/// of this API will allow streaming through this interface.
//
// ACF 2019-11-27: this is a standalone function rather than a method on `RequestExt` because it has
// no receiver to help resolve `T: AsRef<[u8]>`
pub fn downstream_request() -> Result<Request<Body>, Error> {
    let (req_handle, body_handle) = downstream_request_and_body_handles()?;

    let mut req = Request::builder()
        .version(req_handle.get_version()?)
        .method(req_handle.get_method(crate::METHOD_MAX_LEN)?)
        .uri(req_handle.get_uri(crate::URI_MAX_LEN)?);

    for name in req_handle.get_header_names(crate::HEADER_NAME_MAX_LEN) {
        let name = name?;
        for value in req_handle.get_header_values(&name, crate::HEADER_VALUE_MAX_LEN) {
            req = req.header(&name, value?);
        }
    }

    Ok(req.body(body_handle.into())?)
}

/// Get the cipher suite used to secure the downstream client TLS connection.
///
/// The value returned will be consistent with the [OpenSSL name][openssl-mapping] for the cipher
/// suite, for example:
///
/// ```ignore
/// assert_eq!(downstream_tls_cipher_openssl_name(), "ECDHE-RSA-AES128-GCM-SHA256");
/// ```
///
/// [openssl-mapping]: https://testssl.sh/openssl-iana.mapping.html
pub fn downstream_tls_cipher_openssl_name() -> &'static str {
    lazy_static! {
        static ref OPENSSL_NAME: String = {
            // 3x bigger than any of the cipher suite names in OpenSSL today
            let mut buf = vec![0; 128];
            let mut nwritten = 0;
            let status = unsafe {
                abi::xqd_req_downstream_tls_cipher_openssl_name(
                    buf.as_mut_ptr(),
                    buf.len(),
                    &mut nwritten,
                )
            };
            if status.is_err() {
                panic!("couldn't get the downstream TLS cipher's OpenSSL name");
            }
            buf.truncate(nwritten);
            String::from_utf8(buf).expect("TLS cipher OpenSSL name must be valid UTF-8")
        };
    }

    OPENSSL_NAME.as_str()
}

/// Get the TLS protocol version used to secure the downstream client TLS connection.
///
/// ```ignore
/// assert_eq!(downstream_tls_protocol(), "TLSv1.2");
/// ```
pub fn downstream_tls_protocol() -> &'static str {
    lazy_static! {
        static ref PROTOCOL: String = {
            let mut buf = vec![0; 32];
            let mut nwritten = 0;
            let status = unsafe {
                abi::xqd_req_downstream_tls_protocol(buf.as_mut_ptr(), buf.len(), &mut nwritten)
            };
            if status.is_err() {
                panic!("couldn't get the downstream TLS cipher protocol");
            }
            buf.truncate(nwritten);
            String::from_utf8(buf).expect("TLS protocol must be valid UTF-8")
        };
    }

    PROTOCOL.as_str()
}

pub trait RequestExt: Sized {
    /// Send a request via the given backend, returning as soon as the headers are available.
    fn send(self, backend: &str) -> Result<Response<Body>, Error> {
        self.inner_to_body()?.send(backend)
    }

    /// Send a request asynchronously via the given backend, returning as soon as the request has
    /// begun sending.
    ///
    /// The resulting [`PendingRequest`](struct.PendingRequest.html) can be evaluated using
    /// [`PendingRequest::poll()`](struct.PendingRequest.html#method.poll),
    /// [`PendingRequest::wait()`](struct.PendingRequest.html#method.wait), or
    /// [`select`](fn.select.html). It can also be discarded if the request was sent for effects it
    /// might have, and the response is unimportant.
    fn send_async(self, backend: &str) -> Result<PendingRequest, Error> {
        self.inner_to_body()?.send_async(backend)
    }

    /// Replace the body of a request with a `Body` with the same contents.
    //
    // TODO ACF 2020-01-15: better name?
    fn inner_to_body(self) -> Result<Request<Body>, Error>;

    /// Replace the body of a request with the remaining contents of its body.
    ///
    /// Note that this will involve copying and buffering the body, and so should only be used for
    /// convenience on small request bodies.
    fn inner_to_bytes(self) -> Result<Request<Vec<u8>>, Error>;

    /// Get a reference to the Fastly-specific metadata associated with this request.
    fn fastly_metadata(&self) -> &FastlyRequestMetadata;

    /// Get a mutable reference to the Fastly-specific metadata associated with this request.
    fn fastly_metadata_mut(&mut self) -> &mut FastlyRequestMetadata;

    /// Set the cache override behavior for this request.
    fn set_cache_override(&mut self, cache_override: CacheOverride) {
        self.fastly_metadata_mut().cache_override = cache_override;
    }

    /// Get the cache override behavior for this request.
    fn get_cache_override(&self) -> CacheOverride {
        self.fastly_metadata().cache_override
    }

    /// Set the cache override behavior for this request to Pass.
    fn set_pass(&mut self) {
        self.fastly_metadata_mut().cache_override.set_pass();
    }

    /// Set the cache override behavior for this request to use given TTL.
    fn set_ttl(&mut self, ttl: u32) {
        self.fastly_metadata_mut().cache_override.set_ttl(ttl);
    }

    /// Set the cache override behavior for this request to use given `stale-while-revalidate` time.
    fn set_stale_while_revalidate(&mut self, swr: u32) {
        self.fastly_metadata_mut()
            .cache_override
            .set_stale_while_revalidate(swr);
    }
}

fn request_to_handles(req: Request<Body>) -> Result<(RequestHandle, BodyHandle), Error> {
    let mut req_handle = RequestHandle::new()?;

    req_handle.set_version(req.version())?;
    req_handle.set_method(req.method())?;
    req_handle.set_uri(req.uri())?;

    for name in req.headers().keys() {
        req_handle.set_header_values(name, req.headers().get_all(name))?;
    }

    let md = req.fastly_metadata();
    req_handle.set_cache_override(md.cache_override);

    Ok((req_handle, req.into_body().into_handle()?))
}

/// Check whether a request looks suitable for sending to a backend.
///
/// Note that this is *not* meant to be a filter for things that could cause security issues, it is
/// only meant to catch errors before the hostcalls do in order to yield friendlier error messages.
fn validate_request(req: &Request<Body>, backend: &str) -> Result<(), Error> {
    validate_backend(backend)?;
    ensure!(
        req.uri().scheme().is_some() && req.uri().authority().is_some(),
        "request URIs must have a scheme (http/https) and an authority (host)"
    );
    Ok(())
}

fn get_or_default_metadata(exts: &Extensions) -> &FastlyRequestMetadata {
    const DEFAULT: FastlyRequestMetadata = FastlyRequestMetadata::default();

    if let Some(md) = exts.get::<FastlyRequestMetadata>() {
        md
    } else {
        &DEFAULT
    }
}

fn get_or_insert_metadata(exts: &mut Extensions) -> &mut FastlyRequestMetadata {
    if exts.get::<FastlyRequestMetadata>().is_none() {
        exts.insert(FastlyRequestMetadata::default());
    }
    exts.get_mut::<FastlyRequestMetadata>().unwrap()
}

/// Fastly-specific extensions to [`http::Request`][Request].
///
/// [Request]: https://docs.rs/http/latest/http/request/struct.Request.html
impl RequestExt for Request<Body> {
    fn send(self, backend: &str) -> Result<Response<Body>, Error> {
        validate_request(&self, backend)?;

        let (req_handle, req_body_handle) = request_to_handles(self)?;

        let (resp_handle, resp_body_handle) = req_handle.send(req_body_handle, backend)?;

        handles_to_response(resp_handle, resp_body_handle)
    }

    fn send_async(self, backend: &str) -> Result<PendingRequest, Error> {
        validate_request(&self, backend)?;

        let (req_handle, req_body_handle) = request_to_handles(self)?;

        let pending_req_handle = req_handle.send_async(req_body_handle, backend)?;

        Ok(pending_req_handle.into())
    }

    fn inner_to_body(self) -> Result<Request<Body>, Error> {
        Ok(self)
    }

    fn inner_to_bytes(self) -> Result<Request<Vec<u8>>, Error> {
        let (parts, body) = self.into_parts();
        Ok(Request::from_parts(parts, body.into_bytes()?))
    }

    fn fastly_metadata(&self) -> &FastlyRequestMetadata {
        get_or_default_metadata(self.extensions())
    }

    fn fastly_metadata_mut(&mut self) -> &mut FastlyRequestMetadata {
        get_or_insert_metadata(self.extensions_mut())
    }
}

impl RequestExt for Request<&[u8]> {
    fn inner_to_body(self) -> Result<Request<Body>, Error> {
        let mut body = Body::new()?;
        body.write_all(self.body())?;
        Ok(self.map(|_| body))
    }

    fn inner_to_bytes(self) -> Result<Request<Vec<u8>>, Error> {
        Ok(self.map(|b| b.to_vec()))
    }

    fn fastly_metadata(&self) -> &FastlyRequestMetadata {
        get_or_default_metadata(self.extensions())
    }

    fn fastly_metadata_mut(&mut self) -> &mut FastlyRequestMetadata {
        get_or_insert_metadata(self.extensions_mut())
    }
}

impl RequestExt for Request<Vec<u8>> {
    fn inner_to_body(self) -> Result<Request<Body>, Error> {
        let mut body = Body::new()?;
        body.write_all(self.body())?;
        Ok(self.map(|_| body))
    }

    fn inner_to_bytes(self) -> Result<Request<Vec<u8>>, Error> {
        Ok(self)
    }

    fn fastly_metadata(&self) -> &FastlyRequestMetadata {
        get_or_default_metadata(self.extensions())
    }

    fn fastly_metadata_mut(&mut self) -> &mut FastlyRequestMetadata {
        get_or_insert_metadata(self.extensions_mut())
    }
}

impl RequestExt for Request<&str> {
    fn inner_to_body(self) -> Result<Request<Body>, Error> {
        let mut body = Body::new()?;
        body.write_all(self.body().as_bytes())?;
        Ok(self.map(|_| body))
    }

    fn inner_to_bytes(self) -> Result<Request<Vec<u8>>, Error> {
        Ok(self.map(|b| b.as_bytes().to_vec()))
    }

    fn fastly_metadata(&self) -> &FastlyRequestMetadata {
        get_or_default_metadata(self.extensions())
    }

    fn fastly_metadata_mut(&mut self) -> &mut FastlyRequestMetadata {
        get_or_insert_metadata(self.extensions_mut())
    }
}

impl RequestExt for Request<String> {
    fn inner_to_body(self) -> Result<Request<Body>, Error> {
        let mut body = Body::new()?;
        body.write_all(self.body().as_bytes())?;
        Ok(self.map(|_| body))
    }

    fn inner_to_bytes(self) -> Result<Request<Vec<u8>>, Error> {
        Ok(self.map(|b| b.into_bytes()))
    }

    fn fastly_metadata(&self) -> &FastlyRequestMetadata {
        get_or_default_metadata(self.extensions())
    }

    fn fastly_metadata_mut(&mut self) -> &mut FastlyRequestMetadata {
        get_or_insert_metadata(self.extensions_mut())
    }
}

impl RequestExt for Request<()> {
    fn inner_to_body(self) -> Result<Request<Body>, Error> {
        let body = Body::new()?;
        Ok(self.map(|_| body))
    }

    fn inner_to_bytes(self) -> Result<Request<Vec<u8>>, Error> {
        Ok(self.map(|_| vec![]))
    }

    fn fastly_metadata(&self) -> &FastlyRequestMetadata {
        get_or_default_metadata(self.extensions())
    }

    fn fastly_metadata_mut(&mut self) -> &mut FastlyRequestMetadata {
        get_or_insert_metadata(self.extensions_mut())
    }
}

/// Fastly-specific extensions to [`http::request::Builder`][Builder].
///
/// [Builder]: https://docs.rs/http/latest/http/request/struct.Builder.html
pub trait RequestBuilderExt: Sized {
    /// Get a reference to the Fastly-specific metadata associated with this request.
    ///
    /// Returns `None` if this builder has an error.
    fn fastly_metadata_ref(&self) -> Option<&FastlyRequestMetadata>;

    /// Get a mutable reference to the Fastly-specific metadata associated with this request.
    ///
    /// Returns `None` if this builder has an error.
    fn fastly_metadata_mut(&mut self) -> Option<&mut FastlyRequestMetadata>;

    /// Set the cache override behavior for this request.
    fn cache_override(mut self, cache_override: CacheOverride) -> Self {
        self.fastly_metadata_mut()
            .map(|md| md.cache_override = cache_override);
        self
    }

    /// Get a reference to the cache override behavior for this request.
    ///
    /// Returns `None` if this builder has an error.
    fn cache_override_ref(&self) -> Option<&CacheOverride> {
        self.fastly_metadata_ref().map(|md| &md.cache_override)
    }

    /// Get a mutable reference to the cache override behavior for this request.
    ///
    /// Returns `None` if this builder has an error.
    fn cache_override_mut(&mut self) -> Option<&mut CacheOverride> {
        self.fastly_metadata_mut().map(|md| &mut md.cache_override)
    }

    /// Set the cache override behavior for this request to Pass.
    fn pass(mut self) -> Self {
        self.cache_override_mut().map(|co| co.set_pass());
        self
    }

    /// Set the cache override behavior for this request to use given TTL.
    fn ttl(mut self, ttl: u32) -> Self {
        self.cache_override_mut().map(|co| co.set_ttl(ttl));
        self
    }

    /// Set the cache override behavior for this request to use given `stale-while-revalidate` time.
    fn stale_while_revalidate(mut self, swr: u32) -> Self {
        self.cache_override_mut()
            .map(|co| co.set_stale_while_revalidate(swr));
        self
    }
}

impl RequestBuilderExt for http::request::Builder {
    fn fastly_metadata_ref(&self) -> Option<&FastlyRequestMetadata> {
        self.extensions_ref().map(get_or_default_metadata)
    }

    fn fastly_metadata_mut(&mut self) -> Option<&mut FastlyRequestMetadata> {
        self.extensions_mut().map(get_or_insert_metadata)
    }
}

/// A handle to a pending asynchronous request returned by
/// [`RequestHandle::send_async()`](struct.RequestHandle.html#method.send_async).
///
/// A handle can be evaluated using
/// [`PendingRequestHandle::poll()`](struct.PendingRequestHandle.html#method.poll),
/// [`PendingRequestHandle::wait()`](struct.PendingRequestHandle.html#method.wait), or
/// [`select_handles`](fn.select_handles.html). It can also be discarded if the request was sent for
/// effects it might have, and the response is unimportant.
#[derive(Debug, Eq, Hash, PartialEq)]
#[repr(transparent)]
pub struct PendingRequestHandle {
    handle: u32,
}

impl From<PendingRequest> for PendingRequestHandle {
    fn from(pr: PendingRequest) -> Self {
        pr.handle
    }
}

impl PendingRequestHandle {
    pub(crate) const INVALID: Self = Self {
        handle: fastly_shared::INVALID_PENDING_REQUEST_HANDLE,
    };

    pub(crate) fn is_invalid(&self) -> bool {
        self == &Self::INVALID
    }

    /// Get an owned `PendingRequestHandle` from a borrowed one.
    ///
    /// This should only be used when calling the raw ABI directly.
    pub(crate) unsafe fn handle(&self) -> Self {
        Self {
            handle: self.handle,
        }
    }

    /// Try to get the result of a pending request without blocking.
    ///
    /// This function returns immediately with a [`PollHandleResult`](enum.PollHandleResult.html);
    /// if you want to block until a result is ready, use
    /// [`PendingRequestHandle::wait()`](struct.PendingRequestHandle.html#method.wait).
    pub fn poll(self) -> PollHandleResult {
        let mut is_done = -1;
        let mut resp_handle = ResponseHandle::INVALID;
        let mut body_handle = BodyHandle::INVALID;
        let status = unsafe {
            abi::xqd_pending_req_poll(
                self.handle(),
                &mut is_done,
                &mut resp_handle,
                &mut body_handle,
            )
        };
        if status.is_err() || is_done < 0 || is_done > 1 {
            // since we are providing the out-pointers, and an owned `PendingRequestHandle` in the
            // guest can only exist if it's present in the host, any error returns from the hostcall
            // would indicate an internal (host) bug
            panic!("xqd_pending_req_poll internal error");
        }
        let is_done = if is_done == 0 { false } else { true };
        if !is_done {
            return PollHandleResult::Pending(self);
        }
        if is_done && (resp_handle.is_invalid() || body_handle.is_invalid()) {
            PollHandleResult::Done(Err(Error::msg("asynchronous request failed")))
        } else {
            PollHandleResult::Done(Ok((resp_handle, body_handle)))
        }
    }

    /// Block until the result of a pending request is ready.
    ///
    /// If you want check whether the result is ready without blocking, use
    /// [`PendingRequestHandle::poll()`](struct.PendingRequestHandle.html#method.poll).
    pub fn wait(self) -> Result<(ResponseHandle, BodyHandle), Error> {
        let mut resp_handle = ResponseHandle::INVALID;
        let mut body_handle = BodyHandle::INVALID;
        let status =
            unsafe { abi::xqd_pending_req_wait(self.handle(), &mut resp_handle, &mut body_handle) };
        if status.is_err() || resp_handle.is_invalid() || body_handle.is_invalid() {
            // TODO ACF 2020-01-24: differentiate between propagating a failed request error, and an
            // internal error?
            return Err(Error::msg("xqd_pending_req_poll failed"));
        }
        Ok((resp_handle, body_handle))
    }
}

/// The result of a call to
/// [`PendingRequestHandle::poll()`](struct.PendingRequestHandle.html#method.poll).
pub enum PollHandleResult {
    /// The request is still in progress, and can be polled again using the given handle.
    Pending(PendingRequestHandle),
    /// The request has either completed or errored.
    Done(Result<(ResponseHandle, BodyHandle), Error>),
}

/// Given a collection of [`PendingRequestHandle`](struct.PendingRequestHandle.html)s, block until
/// the result of one of the handles is ready.
///
/// This function accepts any type which can become an iterator that yields handles; a common choice
/// is `Vec<PendingRequestHandle>`.
///
/// Returns a tuple `(result, index, remaining)`, where:
///
/// - `result` is the result of the handle that became ready.
///
/// - `index` is the index of the handle in the argument collection (e.g., the index of the handle
/// in a vector) that became ready.
///   
/// - `remaining` is a vector containing all of the handles that did not become ready. The order of
/// the handles in this vector is not guaranteed to match the order of the handles in the argument
/// collection.
///
/// ### Panics
///
/// Panics if the argument collection is empty, or contains more than
/// `fastly_shared::MAX_PENDING_REQS` handles.
pub fn select_handles<I>(
    pending_reqs: I,
) -> (
    Result<(ResponseHandle, BodyHandle), Error>,
    usize,
    Vec<PendingRequestHandle>,
)
where
    I: IntoIterator<Item = PendingRequestHandle>,
{
    let mut prs = pending_reqs
        .into_iter()
        .collect::<Vec<PendingRequestHandle>>();
    if prs.is_empty() || prs.len() > fastly_shared::MAX_PENDING_REQS as usize {
        panic!(
            "the number of selected handles must be at least 1, and less than {}",
            fastly_shared::MAX_PENDING_REQS
        );
    }
    let mut done_index = -1;
    let mut resp_handle = ResponseHandle::INVALID;
    let mut body_handle = BodyHandle::INVALID;

    let status = unsafe {
        abi::xqd_pending_req_select(
            prs.as_ptr(),
            prs.len(),
            &mut done_index,
            &mut resp_handle,
            &mut body_handle,
        )
    };

    if status.is_err() || done_index < 0 {
        // since we are providing the out-pointers, and an owned `PendingRequestHandle` in the guest
        // can only exist if it's present in the host, any error returns from the hostcall would
        // indicate an internal (host) bug
        panic!("xqd_pending_req_select internal error");
    }

    let done_index = done_index
        .try_into()
        .expect("xqd_pending_req_select returned an invalid index");

    // quickly remove the completed handle from the set to return
    prs.swap_remove(done_index);

    let res = if resp_handle.is_invalid() || body_handle.is_invalid() {
        Err(Error::msg("selected request failed"))
    } else {
        Ok((resp_handle, body_handle))
    };

    (res, done_index, prs)
}

/// A handle to a pending asynchronous request returned by
/// [`RequestExt::send_async()`](trait.RequestExt.html#tymethod.send_async).
///
/// A handle can be evaluated using
/// [`PendingRequest::poll()`](struct.PendingRequest.html#method.poll),
/// [`PendingRequest::wait()`](struct.PendingRequest.html#method.wait), or
/// [`select`](fn.select.html). It can also be discarded if the request was sent for effects it
/// might have, and the response is unimportant.
pub struct PendingRequest {
    handle: PendingRequestHandle,
}

impl From<PendingRequestHandle> for PendingRequest {
    fn from(handle: PendingRequestHandle) -> Self {
        Self { handle }
    }
}

impl PendingRequest {
    /// Try to get the result of a pending request without blocking.
    ///
    /// This function returns immediately with a [`PollResult`](enum.PollResult.html); if you want
    /// to block until a result is ready, use
    /// [`PendingRequest::wait()`](struct.PendingRequest.html#method.wait).
    pub fn poll(self) -> PollResult {
        match self.handle.poll() {
            PollHandleResult::Pending(prh) => PollResult::Pending(prh.into()),
            PollHandleResult::Done(Ok((resp_handle, resp_body_handle))) => {
                PollResult::Done(handles_to_response(resp_handle, resp_body_handle))
            }
            PollHandleResult::Done(Err(e)) => PollResult::Done(Err(e)),
        }
    }

    /// Block until the result of a pending request is ready.
    ///
    /// If you want check whether the result is ready without blocking, use
    /// [`PendingRequest::poll()`](struct.PendingRequest.html#method.poll).
    pub fn wait(self) -> Result<Response<Body>, Error> {
        let (resp_handle, resp_body_handle) = self.handle.wait()?;
        handles_to_response(resp_handle, resp_body_handle)
    }
}

/// The result of a call to [`PendingRequest::poll()`](struct.PendingRequest.html#method.poll).
pub enum PollResult {
    /// The request is still in progress, and can be polled again.
    Pending(PendingRequest),
    /// The request has either completed or errored.
    Done(Result<Response<Body>, Error>),
}

/// Given a collection of [`PendingRequest`](struct.PendingRequest.html)s, block until the result of
/// one of the requests is ready.
///
/// This function accepts any type which can become an iterator that yields requests; a common
/// choice is `Vec<PendingRequest>`.
///
/// Returns a tuple `(result, index, remaining)`, where:
///
/// - `result` is the result of the request that became ready.
///
/// - `index` is the index of the request in the argument collection (e.g., the index of the request
/// in a vector) that became ready.
///   
/// - `remaining` is a vector containing all of the requests that did not become ready. The order of
/// the requests in this vector is not guaranteed to match the order of the requests in the argument
/// collection.
///
/// ### Panics
///
/// Panics if the argument collection is empty, or contains more than
/// `fastly_shared::MAX_PENDING_REQS` requests.
pub fn select<I>(pending_reqs: I) -> (Result<Response<Body>, Error>, usize, Vec<PendingRequest>)
where
    I: IntoIterator<Item = PendingRequest>,
{
    let (res, done_index, rest) = select_handles(pending_reqs.into_iter().map(Into::into));
    let res = match res {
        Ok((resp_handle, resp_body_handle)) => handles_to_response(resp_handle, resp_body_handle),
        Err(e) => Err(e),
    };
    let rest = rest.into_iter().map(Into::into).collect();
    (res, done_index, rest)
}