-
Notifications
You must be signed in to change notification settings - Fork 206
/
Copy pathrouter.rs
155 lines (132 loc) · 4.78 KB
/
router.rs
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
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
use std::convert::Infallible;
use tower::Layer;
use tower::Service;
use crate::body::BoxBody;
use crate::routing::tiny_map::TinyMap;
use crate::routing::Route;
use crate::routing::Router;
use http::header::ToStrError;
use thiserror::Error;
/// An AWS JSON routing error.
#[derive(Debug, Error)]
pub enum Error {
/// Relative URI was not "/".
#[error("relative URI is not \"/\"")]
NotRootUrl,
/// Method was not `POST`.
#[error("method not POST")]
MethodNotAllowed,
/// Missing the `x-amz-target` header.
#[error("missing the \"x-amz-target\" header")]
MissingHeader,
/// Unable to parse header into UTF-8.
#[error("failed to parse header: {0}")]
InvalidHeader(ToStrError),
/// Operation not found.
#[error("operation not found")]
NotFound,
}
// This constant determines when the `TinyMap` implementation switches from being a `Vec` to a
// `HashMap`. This is chosen to be 15 as a result of the discussion around
// https://github.com/smithy-lang/smithy-rs/pull/1429#issuecomment-1147516546
const ROUTE_CUTOFF: usize = 15;
/// A [`Router`] supporting [`AWS JSON 1.0`] and [`AWS JSON 1.1`] protocols.
///
/// [AWS JSON 1.0]: https://smithy.io/2.0/aws/protocols/aws-json-1_0-protocol.html
/// [AWS JSON 1.1]: https://smithy.io/2.0/aws/protocols/aws-json-1_1-protocol.html
#[derive(Debug, Clone)]
pub struct AwsJsonRouter<S> {
routes: TinyMap<String, S, ROUTE_CUTOFF>,
}
impl<S> AwsJsonRouter<S> {
/// Applies a [`Layer`] uniformly to all routes.
pub fn layer<L>(self, layer: L) -> AwsJsonRouter<L::Service>
where
L: Layer<S>,
{
AwsJsonRouter {
routes: self
.routes
.into_iter()
.map(|(key, route)| (key, layer.layer(route)))
.collect(),
}
}
/// Applies type erasure to the inner route using [`Route::new`].
pub fn boxed<B>(self) -> AwsJsonRouter<Route<B>>
where
S: Service<http::Request<B>, Response = http::Response<BoxBody>, Error = Infallible>,
S: Send + Clone + 'static,
S::Future: Send + 'static,
{
AwsJsonRouter {
routes: self.routes.into_iter().map(|(key, s)| (key, Route::new(s))).collect(),
}
}
}
impl<B, S> Router<B> for AwsJsonRouter<S>
where
S: Clone,
{
type Service = S;
type Error = Error;
fn match_route(&self, request: &http::Request<B>) -> Result<S, Self::Error> {
// The URI path must be root,
if request.uri().path() != "/" {
return Err(Error::NotRootUrl);
}
// Only `Method::POST` is allowed.
if request.method() != http::Method::POST {
return Err(Error::MethodNotAllowed);
}
// Find the `x-amz-target` header.
let target = request.headers().get("x-amz-target").ok_or(Error::MissingHeader)?;
let target = target.to_str().map_err(Error::InvalidHeader)?;
// Lookup in the `TinyMap` for a route for the target.
let route = self.routes.get(target).ok_or(Error::NotFound)?;
Ok(route.clone())
}
}
impl<S> FromIterator<(String, S)> for AwsJsonRouter<S> {
#[inline]
fn from_iter<T: IntoIterator<Item = (String, S)>>(iter: T) -> Self {
Self {
routes: iter.into_iter().collect(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{protocol::test_helpers::req, routing::Router};
use http::{HeaderMap, HeaderValue, Method};
use pretty_assertions::assert_eq;
#[tokio::test]
async fn simple_routing() {
let routes = vec![("Service.Operation")];
let router: AwsJsonRouter<_> = routes
.clone()
.into_iter()
.map(|operation| (operation.to_string(), ()))
.collect();
let mut headers = HeaderMap::new();
headers.insert("x-amz-target", HeaderValue::from_static("Service.Operation"));
// Valid request, should match.
router
.match_route(&req(&Method::POST, "/", Some(headers.clone())))
.unwrap();
// No headers, should return `MissingHeader`.
let res = router.match_route(&req(&Method::POST, "/", None));
assert_eq!(res.unwrap_err().to_string(), Error::MissingHeader.to_string());
// Wrong HTTP method, should return `MethodNotAllowed`.
let res = router.match_route(&req(&Method::GET, "/", Some(headers.clone())));
assert_eq!(res.unwrap_err().to_string(), Error::MethodNotAllowed.to_string());
// Wrong URI, should return `NotRootUrl`.
let res = router.match_route(&req(&Method::POST, "/something", Some(headers)));
assert_eq!(res.unwrap_err().to_string(), Error::NotRootUrl.to_string());
}
}