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
use std::convert::TryFrom;

use crate::{
    cf::Cf, error::Error, headers::Headers, http::Method, ByteStream, FormData, RequestInit, Result,
};

use serde::de::DeserializeOwned;
use std::borrow::Cow;
use url::{form_urlencoded::Parse, Url};
use wasm_bindgen::JsCast;
use wasm_bindgen_futures::JsFuture;
use worker_sys::ext::RequestExt;

/// A [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) representation for
/// handling incoming and creating outbound HTTP requests.
#[derive(Debug)]
pub struct Request {
    method: Method,
    path: String,
    headers: Headers,
    cf: Option<Cf>,
    edge_request: web_sys::Request,
    body_used: bool,
    immutable: bool,
}

unsafe impl Send for Request {}
unsafe impl Sync for Request {}

#[cfg(feature = "http")]
impl<B: http_body::Body<Data = bytes::Bytes> + 'static> TryFrom<http::Request<B>> for Request {
    type Error = crate::Error;
    fn try_from(req: http::Request<B>) -> Result<Self> {
        let web_request: web_sys::Request = crate::http::request::to_wasm(req)?;
        Ok(Request::from(web_request))
    }
}

#[cfg(feature = "http")]
impl TryFrom<Request> for crate::HttpRequest {
    type Error = crate::Error;
    fn try_from(req: Request) -> Result<Self> {
        crate::http::request::from_wasm(req.edge_request)
    }
}

impl From<web_sys::Request> for Request {
    fn from(req: web_sys::Request) -> Self {
        Self {
            method: req.method().into(),
            path: Url::parse(&req.url())
                .map(|u| u.path().into())
                .unwrap_or_else(|_| {
                    let u = req.url();
                    if !u.starts_with('/') {
                        return "/".to_string() + &u;
                    }
                    u
                }),
            headers: Headers(req.headers()),
            cf: req.cf().map(Into::into),
            edge_request: req,
            body_used: false,
            immutable: true,
        }
    }
}

impl TryFrom<Request> for web_sys::Request {
    type Error = Error;
    fn try_from(req: Request) -> Result<Self> {
        req.inner().clone().map_err(Error::from)
    }
}

impl TryFrom<&Request> for web_sys::Request {
    type Error = Error;
    fn try_from(req: &Request) -> Result<Self> {
        req.inner().clone().map_err(Error::from)
    }
}

impl Request {
    /// Construct a new `Request` with an HTTP Method.
    pub fn new(uri: &str, method: Method) -> Result<Self> {
        web_sys::Request::new_with_str_and_init(
            uri,
            web_sys::RequestInit::new().method(method.as_ref()),
        )
        .map(|req| {
            let mut req: Request = req.into();
            req.immutable = false;
            req
        })
        .map_err(|e| {
            Error::JsError(
                e.as_string()
                    .unwrap_or_else(|| "invalid URL or method for Request".to_string()),
            )
        })
    }

    /// Construct a new `Request` with a `RequestInit` configuration.
    pub fn new_with_init(uri: &str, init: &RequestInit) -> Result<Self> {
        web_sys::Request::new_with_str_and_init(uri, &init.into())
            .map(|req| {
                let mut req: Request = req.into();
                req.immutable = false;
                req
            })
            .map_err(|e| {
                Error::JsError(
                    e.as_string()
                        .unwrap_or_else(|| "invalid URL or options for Request".to_string()),
                )
            })
    }

    /// Access this request's body encoded as JSON.
    pub async fn json<B: DeserializeOwned>(&mut self) -> Result<B> {
        if !self.body_used {
            self.body_used = true;
            return JsFuture::from(self.edge_request.json()?)
                .await
                .map_err(|e| {
                    Error::JsError(
                        e.as_string()
                            .unwrap_or_else(|| "failed to get JSON for body value".into()),
                    )
                })
                .and_then(|val| serde_wasm_bindgen::from_value(val).map_err(Error::from));
        }

        Err(Error::BodyUsed)
    }

    /// Access this request's body as plaintext.
    pub async fn text(&mut self) -> Result<String> {
        if !self.body_used {
            self.body_used = true;
            return JsFuture::from(self.edge_request.text()?)
                .await
                .map(|val| val.as_string().unwrap())
                .map_err(|e| {
                    Error::JsError(
                        e.as_string()
                            .unwrap_or_else(|| "failed to get text for body value".into()),
                    )
                });
        }

        Err(Error::BodyUsed)
    }

    /// Access this request's body as raw bytes.
    pub async fn bytes(&mut self) -> Result<Vec<u8>> {
        if !self.body_used {
            self.body_used = true;
            return JsFuture::from(self.edge_request.array_buffer()?)
                .await
                .map(|val| js_sys::Uint8Array::new(&val).to_vec())
                .map_err(|e| {
                    Error::JsError(
                        e.as_string()
                            .unwrap_or_else(|| "failed to read array buffer from request".into()),
                    )
                });
        }

        Err(Error::BodyUsed)
    }

    /// Access this request's body as a form-encoded payload and pull out fields and files.
    pub async fn form_data(&mut self) -> Result<FormData> {
        if !self.body_used {
            self.body_used = true;
            return JsFuture::from(self.edge_request.form_data()?)
                .await
                .map(|val| val.into())
                .map_err(|e| {
                    Error::JsError(
                        e.as_string()
                            .unwrap_or_else(|| "failed to get form data from request".into()),
                    )
                });
        }

        Err(Error::BodyUsed)
    }

    /// Access this request's body as a [`Stream`](futures::stream::Stream) of bytes.
    pub fn stream(&mut self) -> Result<ByteStream> {
        if self.body_used {
            return Err(Error::BodyUsed);
        }

        self.body_used = true;

        let stream = self
            .edge_request
            .body()
            .ok_or_else(|| Error::RustError("no body for request".into()))?;

        let stream = wasm_streams::ReadableStream::from_raw(stream.dyn_into().unwrap());
        Ok(ByteStream {
            inner: stream.into_stream(),
        })
    }

    /// Get the `Headers` for this request.
    pub fn headers(&self) -> &Headers {
        &self.headers
    }

    /// Get a mutable reference to this request's `Headers`.
    /// **Note:** they can only be modified if the request was created from scratch or cloned.
    pub fn headers_mut(&mut self) -> Result<&mut Headers> {
        if self.immutable {
            return Err(Error::JsError(
                "Cannot get a mutable reference to an immutable headers object.".into(),
            ));
        }
        Ok(&mut self.headers)
    }

    /// Access this request's Cloudflare-specific properties.
    ///
    /// # Note
    ///
    /// Request objects constructed by the user and not the runtime will not have a [Cf] associated.
    ///
    /// See [workerd#825](https://github.com/cloudflare/workerd/issues/825)
    pub fn cf(&self) -> Option<&Cf> {
        self.cf.as_ref()
    }

    /// The HTTP Method associated with this `Request`.
    pub fn method(&self) -> Method {
        self.method.clone()
    }

    /// The URL Path of this `Request`.
    pub fn path(&self) -> String {
        self.path.clone()
    }

    /// Get a mutable reference to this request's path.
    /// **Note:** they can only be modified if the request was created from scratch or cloned.
    pub fn path_mut(&mut self) -> Result<&mut String> {
        if self.immutable {
            return Err(Error::JsError(
                "Cannot get a mutable reference to an immutable path.".into(),
            ));
        }
        Ok(&mut self.path)
    }

    /// The parsed [`url::Url`] of this `Request`.
    pub fn url(&self) -> Result<Url> {
        let url = self.edge_request.url();
        url.parse()
            .map_err(|e| Error::RustError(format!("failed to parse Url from {e}: {url}")))
    }

    /// Deserialize the url query
    pub fn query<Q: DeserializeOwned>(&self) -> Result<Q> {
        let url = self.url()?;
        let pairs = url.query_pairs();
        let deserializer = serde_urlencoded::Deserializer::new(pairs);

        Q::deserialize(deserializer).map_err(Error::from)
    }

    #[allow(clippy::should_implement_trait)]
    pub fn clone(&self) -> Result<Self> {
        self.edge_request
            .clone()
            .map(|req| req.into())
            .map_err(Error::from)
    }

    pub fn clone_mut(&self) -> Result<Self> {
        let mut req: Request = web_sys::Request::new_with_request(&self.edge_request)?.into();
        req.immutable = false;
        Ok(req)
    }

    pub fn inner(&self) -> &web_sys::Request {
        &self.edge_request
    }
}

/// Used to add additional helper functions to url::Url
pub trait UrlExt {
    /// Given a query parameter, returns the value of the first occurrence of that parameter if it
    /// exists
    fn param<'a>(&'a self, key: &'a str) -> Option<Cow<'a, str>> {
        self.param_iter(key).next()
    }
    /// Given a query parameter, returns an Iterator of values for that parameter in the url's
    /// query string
    fn param_iter<'a>(&'a self, key: &'a str) -> ParamIter<'a>;
}

impl UrlExt for Url {
    fn param_iter<'a>(&'a self, key: &'a str) -> ParamIter<'a> {
        ParamIter {
            inner: self.query_pairs(),
            key,
        }
    }
}

pub struct ParamIter<'a> {
    inner: Parse<'a>,
    key: &'a str,
}

impl<'a> Iterator for ParamIter<'a> {
    type Item = Cow<'a, str>;

    fn next(&mut self) -> Option<Self::Item> {
        let key = self.key;
        Some(self.inner.find(|(k, _)| k == key)?.1)
    }
}

#[test]
fn url_param_works() {
    let url = Url::parse("https://example.com/foo.html?a=foo&b=bar&a=baz").unwrap();
    assert_eq!(url.param("a").as_deref(), Some("foo"));
    assert_eq!(url.param("b").as_deref(), Some("bar"));
    assert_eq!(url.param("c").as_deref(), None);
    let mut a_values = url.param_iter("a");
    assert_eq!(a_values.next().as_deref(), Some("foo"));
    assert_eq!(a_values.next().as_deref(), Some("baz"));
    assert_eq!(a_values.next(), None);
}

#[test]
fn clone_mut_works() {
    let req = Request::new(
        "https://example.com/foo.html?a=foo&b=bar&a=baz",
        crate::Method::Get,
    )
    .unwrap();
    assert!(!req.immutable);
    let mut_req = req.clone_mut().unwrap();
    assert!(mut_req.immutable);
}

/// A trait used to represent any viable Request type that can be used in the Worker.
/// The only requirement is that it be convertible from a web_sys::Request.
pub trait FromRequest: std::marker::Sized {
    fn from_raw(
        request: web_sys::Request,
    ) -> std::result::Result<Self, impl Into<Box<dyn std::error::Error>>>;
}

impl FromRequest for web_sys::Request {
    fn from_raw(
        request: web_sys::Request,
    ) -> std::result::Result<Self, impl Into<Box<dyn std::error::Error>>> {
        Ok::<web_sys::Request, Error>(request)
    }
}

impl FromRequest for Request {
    fn from_raw(
        request: web_sys::Request,
    ) -> std::result::Result<Self, impl Into<Box<dyn std::error::Error>>> {
        Ok::<Request, Error>(request.into())
    }
}

#[cfg(feature = "http")]
impl FromRequest for crate::HttpRequest {
    fn from_raw(
        request: web_sys::Request,
    ) -> std::result::Result<Self, impl Into<Box<dyn std::error::Error>>> {
        crate::http::request::from_wasm(request)
    }
}