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
// This router implementation is heavily inspired by the `Endpoint` type in the https://github.com/http-rs/tide project.

use super::conversions::{IntoResponse, TryFromRequest, TryIntoRequest};
use super::{responses, Method, Request, Response};
use async_trait::async_trait;
use routefinder::{Captures, Router as MethodRouter};
use std::future::Future;
use std::{collections::HashMap, fmt::Display};

/// An HTTP request handler.
///  
/// This trait is automatically implemented for `Fn` types, and so is rarely implemented
/// directly by Spin users.
#[async_trait(?Send)]
pub trait Handler {
    /// Invoke the handler.
    async fn handle(&self, req: Request, params: Params) -> Response;
}

#[async_trait(?Send)]
impl Handler for Box<dyn Handler> {
    async fn handle(&self, req: Request, params: Params) -> Response {
        self.as_ref().handle(req, params).await
    }
}

#[async_trait(?Send)]
impl<F, Fut> Handler for F
where
    F: Fn(Request, Params) -> Fut + 'static,
    Fut: Future<Output = Response> + 'static,
{
    async fn handle(&self, req: Request, params: Params) -> Response {
        let fut = (self)(req, params);
        fut.await
    }
}

/// Route parameters extracted from a URI that match a route pattern.
pub type Params = Captures<'static, 'static>;

/// The Spin SDK HTTP router.
pub struct Router {
    methods_map: HashMap<Method, MethodRouter<Box<dyn Handler>>>,
    any_methods: MethodRouter<Box<dyn Handler>>,
}

impl Default for Router {
    fn default() -> Router {
        Router::new()
    }
}

impl Display for Router {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "Registered routes:")?;
        for (method, router) in &self.methods_map {
            for route in router.iter() {
                writeln!(f, "- {}: {}", method, route.0)?;
            }
        }
        Ok(())
    }
}

struct RouteMatch<'a> {
    params: Captures<'static, 'static>,
    handler: &'a dyn Handler,
}

impl Router {
    /// Synchronously dispatches a request to the appropriate handler along with the URI parameters.
    pub fn handle<R>(&self, request: R) -> Response
    where
        R: TryIntoRequest,
        R::Error: IntoResponse,
    {
        crate::http::executor::run(self.handle_async(request))
    }

    /// Asynchronously dispatches a request to the appropriate handler along with the URI parameters.
    pub async fn handle_async<R>(&self, request: R) -> Response
    where
        R: TryIntoRequest,
        R::Error: IntoResponse,
    {
        let request = match R::try_into_request(request) {
            Ok(r) => r,
            Err(e) => return e.into_response(),
        };
        let method = request.method.clone();
        let path = &request.path();
        let RouteMatch { params, handler } = self.find(path, method);
        handler.handle(request, params).await
    }

    fn find(&self, path: &str, method: Method) -> RouteMatch<'_> {
        let best_match = self
            .methods_map
            .get(&method)
            .and_then(|r| r.best_match(path));

        if let Some(m) = best_match {
            let params = m.captures().into_owned();
            let handler = m.handler();
            return RouteMatch { handler, params };
        }

        let best_match = self.any_methods.best_match(path);

        match best_match {
            Some(m) => {
                let params = m.captures().into_owned();
                let handler = m.handler();
                RouteMatch { handler, params }
            }
            None if method == Method::Head => {
                // If it is a HTTP HEAD request then check if there is a callback in the methods map
                // if not then fallback to the behavior of HTTP GET else proceed as usual
                self.find(path, Method::Get)
            }
            None => {
                // Handle the failure case where no match could be resolved.
                self.fail(path, method)
            }
        }
    }

    // Helper function to handle the case where a best match couldn't be resolved.
    fn fail(&self, path: &str, method: Method) -> RouteMatch<'_> {
        // First, filter all routers to determine if the path can match but the provided method is not allowed.
        let is_method_not_allowed = self
            .methods_map
            .iter()
            .filter(|(k, _)| **k != method)
            .any(|(_, r)| r.best_match(path).is_some());

        if is_method_not_allowed {
            // If this `path` can be handled by a callback registered with a different HTTP method
            // should return 405 Method Not Allowed
            RouteMatch {
                handler: &method_not_allowed,
                params: Captures::default(),
            }
        } else {
            // ... Otherwise, nothing matched so 404.
            RouteMatch {
                handler: &not_found,
                params: Captures::default(),
            }
        }
    }

    /// Register a handler at the path for all methods.
    pub fn any<F, Req, Resp>(&mut self, path: &str, handler: F)
    where
        F: Fn(Req, Params) -> Resp + 'static,
        Req: TryFromRequest + 'static,
        Req::Error: IntoResponse + 'static,
        Resp: IntoResponse + 'static,
    {
        let handler = move |req, params| {
            let res = TryFromRequest::try_from_request(req).map(|r| handler(r, params));
            async move {
                match res {
                    Ok(res) => res.into_response(),
                    Err(e) => e.into_response(),
                }
            }
        };

        self.any_async(path, handler)
    }

    /// Register an async handler at the path for all methods.
    pub fn any_async<F, Fut, I, O>(&mut self, path: &str, handler: F)
    where
        F: Fn(I, Params) -> Fut + 'static,
        Fut: Future<Output = O> + 'static,
        I: TryFromRequest + 'static,
        I::Error: IntoResponse + 'static,
        O: IntoResponse + 'static,
    {
        let handler = move |req, params| {
            let res = TryFromRequest::try_from_request(req).map(|r| handler(r, params));
            async move {
                match res {
                    Ok(f) => f.await.into_response(),
                    Err(e) => e.into_response(),
                }
            }
        };

        self.any_methods.add(path, Box::new(handler)).unwrap();
    }

    /// Register a handler at the path for the specified HTTP method.
    pub fn add<F, Req, Resp>(&mut self, path: &str, method: Method, handler: F)
    where
        F: Fn(Req, Params) -> Resp + 'static,
        Req: TryFromRequest + 'static,
        Req::Error: IntoResponse + 'static,
        Resp: IntoResponse + 'static,
    {
        let handler = move |req, params| {
            let res = TryFromRequest::try_from_request(req).map(|r| handler(r, params));
            async move {
                match res {
                    Ok(res) => res.into_response(),
                    Err(e) => e.into_response(),
                }
            }
        };

        self.add_async(path, method, handler)
    }

    /// Register an async handler at the path for the specified HTTP method.
    pub fn add_async<F, Fut, I, O>(&mut self, path: &str, method: Method, handler: F)
    where
        F: Fn(I, Params) -> Fut + 'static,
        Fut: Future<Output = O> + 'static,
        I: TryFromRequest + 'static,
        I::Error: IntoResponse + 'static,
        O: IntoResponse + 'static,
    {
        let handler = move |req, params| {
            let res = TryFromRequest::try_from_request(req).map(|r| handler(r, params));
            async move {
                match res {
                    Ok(f) => f.await.into_response(),
                    Err(e) => e.into_response(),
                }
            }
        };

        self.methods_map
            .entry(method)
            .or_default()
            .add(path, Box::new(handler))
            .unwrap();
    }

    /// Register a handler at the path for the HTTP GET method.
    pub fn get<F, Req, Resp>(&mut self, path: &str, handler: F)
    where
        F: Fn(Req, Params) -> Resp + 'static,
        Req: TryFromRequest + 'static,
        Req::Error: IntoResponse + 'static,
        Resp: IntoResponse + 'static,
    {
        self.add(path, Method::Get, handler)
    }

    /// Register an async handler at the path for the HTTP GET method.
    pub fn get_async<F, Fut, Req, Resp>(&mut self, path: &str, handler: F)
    where
        F: Fn(Req, Params) -> Fut + 'static,
        Fut: Future<Output = Resp> + 'static,
        Req: TryFromRequest + 'static,
        Req::Error: IntoResponse + 'static,
        Resp: IntoResponse + 'static,
    {
        self.add_async(path, Method::Get, handler)
    }

    /// Register a handler at the path for the HTTP HEAD method.
    pub fn head<F, Req, Resp>(&mut self, path: &str, handler: F)
    where
        F: Fn(Req, Params) -> Resp + 'static,
        Req: TryFromRequest + 'static,
        Req::Error: IntoResponse + 'static,
        Resp: IntoResponse + 'static,
    {
        self.add(path, Method::Head, handler)
    }

    /// Register an async handler at the path for the HTTP HEAD method.
    pub fn head_async<F, Fut, Req, Resp>(&mut self, path: &str, handler: F)
    where
        F: Fn(Req, Params) -> Fut + 'static,
        Fut: Future<Output = Resp> + 'static,
        Req: TryFromRequest + 'static,
        Req::Error: IntoResponse + 'static,
        Resp: IntoResponse + 'static,
    {
        self.add_async(path, Method::Head, handler)
    }

    /// Register a handler at the path for the HTTP POST method.
    pub fn post<F, Req, Resp>(&mut self, path: &str, handler: F)
    where
        F: Fn(Req, Params) -> Resp + 'static,
        Req: TryFromRequest + 'static,
        Req::Error: IntoResponse + 'static,
        Resp: IntoResponse + 'static,
    {
        self.add(path, Method::Post, handler)
    }

    /// Register an async handler at the path for the HTTP POST method.
    pub fn post_async<F, Fut, Req, Resp>(&mut self, path: &str, handler: F)
    where
        F: Fn(Req, Params) -> Fut + 'static,
        Fut: Future<Output = Resp> + 'static,
        Req: TryFromRequest + 'static,
        Req::Error: IntoResponse + 'static,
        Resp: IntoResponse + 'static,
    {
        self.add_async(path, Method::Post, handler)
    }

    /// Register a handler at the path for the HTTP DELETE method.
    pub fn delete<F, Req, Resp>(&mut self, path: &str, handler: F)
    where
        F: Fn(Req, Params) -> Resp + 'static,
        Req: TryFromRequest + 'static,
        Req::Error: IntoResponse + 'static,
        Resp: IntoResponse + 'static,
    {
        self.add(path, Method::Delete, handler)
    }

    /// Register an async handler at the path for the HTTP DELETE method.
    pub fn delete_async<F, Fut, Req, Resp>(&mut self, path: &str, handler: F)
    where
        F: Fn(Req, Params) -> Fut + 'static,
        Fut: Future<Output = Resp> + 'static,
        Req: TryFromRequest + 'static,
        Req::Error: IntoResponse + 'static,
        Resp: IntoResponse + 'static,
    {
        self.add_async(path, Method::Delete, handler)
    }

    /// Register a handler at the path for the HTTP PUT method.
    pub fn put<F, Req, Resp>(&mut self, path: &str, handler: F)
    where
        F: Fn(Req, Params) -> Resp + 'static,
        Req: TryFromRequest + 'static,
        Req::Error: IntoResponse + 'static,
        Resp: IntoResponse + 'static,
    {
        self.add(path, Method::Put, handler)
    }

    /// Register an async handler at the path for the HTTP PUT method.
    pub fn put_async<F, Fut, Req, Resp>(&mut self, path: &str, handler: F)
    where
        F: Fn(Req, Params) -> Fut + 'static,
        Fut: Future<Output = Resp> + 'static,
        Req: TryFromRequest + 'static,
        Req::Error: IntoResponse + 'static,
        Resp: IntoResponse + 'static,
    {
        self.add_async(path, Method::Put, handler)
    }

    /// Register a handler at the path for the HTTP PATCH method.
    pub fn patch<F, Req, Resp>(&mut self, path: &str, handler: F)
    where
        F: Fn(Req, Params) -> Resp + 'static,
        Req: TryFromRequest + 'static,
        Req::Error: IntoResponse + 'static,
        Resp: IntoResponse + 'static,
    {
        self.add(path, Method::Patch, handler)
    }

    /// Register an async handler at the path for the HTTP PATCH method.
    pub fn patch_async<F, Fut, Req, Resp>(&mut self, path: &str, handler: F)
    where
        F: Fn(Req, Params) -> Fut + 'static,
        Fut: Future<Output = Resp> + 'static,
        Req: TryFromRequest + 'static,
        Req::Error: IntoResponse + 'static,
        Resp: IntoResponse + 'static,
    {
        self.add_async(path, Method::Patch, handler)
    }

    /// Register a handler at the path for the HTTP OPTIONS method.
    pub fn options<F, Req, Resp>(&mut self, path: &str, handler: F)
    where
        F: Fn(Req, Params) -> Resp + 'static,
        Req: TryFromRequest + 'static,
        Req::Error: IntoResponse + 'static,
        Resp: IntoResponse + 'static,
    {
        self.add(path, Method::Options, handler)
    }

    /// Register an async handler at the path for the HTTP OPTIONS method.
    pub fn options_async<F, Fut, Req, Resp>(&mut self, path: &str, handler: F)
    where
        F: Fn(Req, Params) -> Fut + 'static,
        Fut: Future<Output = Resp> + 'static,
        Req: TryFromRequest + 'static,
        Req::Error: IntoResponse + 'static,
        Resp: IntoResponse + 'static,
    {
        self.add_async(path, Method::Options, handler)
    }

    /// Construct a new Router.
    pub fn new() -> Self {
        Router {
            methods_map: HashMap::default(),
            any_methods: MethodRouter::new(),
        }
    }
}

async fn not_found(_req: Request, _params: Params) -> Response {
    responses::not_found()
}

async fn method_not_allowed(_req: Request, _params: Params) -> Response {
    responses::method_not_allowed()
}

/// A macro to help with constructing a Router from a stream of tokens.
#[macro_export]
macro_rules! http_router {
    ($($method:tt $path:literal => $h:expr),*) => {
        {
            let mut router = $crate::http::Router::new();
            $(
                $crate::http_router!(@build router $method $path => $h);
            )*
            router
        }
    };
    (@build $r:ident HEAD $path:literal => $h:expr) => {
        $r.head($path, $h);
    };
    (@build $r:ident GET $path:literal => $h:expr) => {
        $r.get($path, $h);
    };
    (@build $r:ident PUT $path:literal => $h:expr) => {
        $r.put($path, $h);
    };
    (@build $r:ident POST $path:literal => $h:expr) => {
        $r.post($path, $h);
    };
    (@build $r:ident PATCH $path:literal => $h:expr) => {
        $r.patch($path, $h);
    };
    (@build $r:ident DELETE $path:literal => $h:expr) => {
        $r.delete($path, $h);
    };
    (@build $r:ident OPTIONS $path:literal => $h:expr) => {
        $r.options($path, $h);
    };
    (@build $r:ident _ $path:literal => $h:expr) => {
        $r.any($path, $h);
    };
}

#[cfg(test)]
mod tests {
    use super::*;

    fn make_request(method: Method, path: &str) -> Request {
        Request::new(method, path)
    }

    fn echo_param(_req: Request, params: Params) -> Response {
        match params.get("x") {
            Some(path) => Response::new(200, path),
            None => responses::not_found(),
        }
    }

    #[test]
    fn test_method_not_allowed() {
        let mut router = Router::default();
        router.get("/:x", echo_param);

        let req = make_request(Method::Post, "/foobar");
        let res = router.handle(req);
        assert_eq!(res.status, hyperium::StatusCode::METHOD_NOT_ALLOWED);
    }

    #[test]
    fn test_not_found() {
        fn h1(_req: Request, _params: Params) -> anyhow::Result<Response> {
            Ok(Response::new(200, ()))
        }

        let mut router = Router::default();
        router.get("/h1/:param", h1);

        let req = make_request(Method::Get, "/h1/");
        let res = router.handle(req);
        assert_eq!(res.status, hyperium::StatusCode::NOT_FOUND);
    }

    #[test]
    fn test_multi_param() {
        fn multiply(_req: Request, params: Params) -> anyhow::Result<Response> {
            let x: i64 = params.get("x").unwrap().parse()?;
            let y: i64 = params.get("y").unwrap().parse()?;
            Ok(Response::new(200, format!("{result}", result = x * y)))
        }

        let mut router = Router::default();
        router.get("/multiply/:x/:y", multiply);

        let req = make_request(Method::Get, "/multiply/2/4");
        let res = router.handle(req);

        assert_eq!(res.body, "8".to_owned().into_bytes());
    }

    #[test]
    fn test_param() {
        let mut router = Router::default();
        router.get("/:x", echo_param);

        let req = make_request(Method::Get, "/y");
        let res = router.handle(req);

        assert_eq!(res.body, "y".to_owned().into_bytes());
    }

    #[test]
    fn test_wildcard() {
        fn echo_wildcard(_req: Request, params: Params) -> Response {
            match params.wildcard() {
                Some(path) => Response::new(200, path),
                None => responses::not_found(),
            }
        }

        let mut router = Router::default();
        router.get("/*", echo_wildcard);

        let req = make_request(Method::Get, "/foo/bar");
        let res = router.handle(req);
        assert_eq!(res.status, hyperium::StatusCode::OK);
        assert_eq!(res.body, "foo/bar".to_owned().into_bytes());
    }

    #[test]
    fn test_wildcard_last_segment() {
        let mut router = Router::default();
        router.get("/:x/*", echo_param);

        let req = make_request(Method::Get, "/foo/bar");
        let res = router.handle(req);
        assert_eq!(res.body, "foo".to_owned().into_bytes());
    }

    #[test]
    fn test_router_display() {
        let mut router = Router::default();
        router.get("/:x", echo_param);

        let expected = "Registered routes:\n- GET: /:x\n";
        let actual = format!("{}", router);

        assert_eq!(actual.as_str(), expected);
    }

    #[test]
    fn test_ambiguous_wildcard_vs_star() {
        fn h1(_req: Request, _params: Params) -> anyhow::Result<Response> {
            Ok(Response::new(200, "one/two"))
        }

        fn h2(_req: Request, _params: Params) -> anyhow::Result<Response> {
            Ok(Response::new(200, "posts/*"))
        }

        let mut router = Router::default();
        router.get("/:one/:two", h1);
        router.get("/posts/*", h2);

        let req = make_request(Method::Get, "/posts/2");
        let res = router.handle(req);

        assert_eq!(res.body, "posts/*".to_owned().into_bytes());
    }
}