-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add middleware & update design docs (#175)
* Add middleware & update design docs * Add missing design folder * Improve MapRequest middleware example * Vendor pin_utils::pin_mut * Remove Debug body restrictions * Modify error bound * Remove redundant return
- Loading branch information
Showing
8 changed files
with
288 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# Summary | ||
|
||
- [Http Operations](./operation.md) | ||
- [HTTP middleware](./middleware.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# HTTP middleware | ||
|
||
Signing, endpoint specification, and logging are all handled as middleware. The Rust SDK takes a minimalist approach to middleware: | ||
|
||
Middleware is defined as minimally as possible, then adapted into the middleware system used by the IO layer. Tower is the de facto standard for HTTP middleware in Rust—we will probably use it. But we also want to make our middleware usable for users who aren't using Tower (or if we decide to not use Tower in the long run). | ||
|
||
Because of this, rather than implementing all our middleware as "Tower Middleware", we implement it narrowly (eg. as a function that operates on `operation::Request`), then define optional adapters to make our middleware tower compatible. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0. | ||
*/ | ||
|
||
//! This modules defines the core, framework agnostic, HTTP middleware interface | ||
//! used by the SDK | ||
//! | ||
//! smithy-middleware-tower provides Tower-specific middleware utilities (todo) | ||
use crate::operation; | ||
use crate::pin_mut; | ||
use crate::response::ParseHttpResponse; | ||
use crate::result::{SdkError, SdkSuccess}; | ||
use bytes::{Buf, Bytes}; | ||
use http_body::Body; | ||
use std::error::Error; | ||
|
||
type BoxError = Box<dyn Error + Send + Sync>; | ||
|
||
/// [`MapRequest`] defines a synchronous middleware that transforms an [`operation::Request`]. | ||
/// | ||
/// Typically, these middleware will read configuration from the `PropertyBag` and use it to | ||
/// augment the request. Most fundamental middleware is expressed as `MapRequest`, including | ||
/// signing & endpoint resolution. | ||
/// | ||
/// ```rust | ||
/// # use smithy_http::middleware::MapRequest; | ||
/// # use std::convert::Infallible; | ||
/// # use smithy_http::operation; | ||
/// use http::header::{HeaderName, HeaderValue}; | ||
/// struct AddHeader(HeaderName, HeaderValue); | ||
/// /// Signaling struct added to the request property bag if a header should be added | ||
/// struct NeedsHeader; | ||
/// impl MapRequest for AddHeader { | ||
/// type Error = Infallible; | ||
/// fn apply(&self, request: operation::Request) -> Result<operation::Request, Self::Error> { | ||
/// request.augment(|mut request, properties| { | ||
/// if properties.get::<NeedsHeader>().is_some() { | ||
/// request.headers_mut().append( | ||
/// self.0.clone(), | ||
/// self.1.clone(), | ||
/// ); | ||
/// } | ||
/// Ok(request) | ||
/// }) | ||
/// } | ||
/// } | ||
/// ``` | ||
pub trait MapRequest { | ||
/// The Error type returned by this operation. | ||
/// | ||
/// If this middleware never fails use [std::convert::Infallible] or similar. | ||
type Error: Into<BoxError>; | ||
|
||
/// Apply this middleware to a request. | ||
/// | ||
/// Typically, implementations will use [`request.augment`](crate::operation::Request::augment) | ||
/// to be able to transform an owned `http::Request`. | ||
fn apply(&self, request: operation::Request) -> Result<operation::Request, Self::Error>; | ||
} | ||
|
||
/// Load a response using `handler` to parse the results. | ||
/// | ||
/// This function is intended to be used on the response side of a middleware chain. | ||
/// | ||
/// Success and failure will be split and mapped into `SdkSuccess` and `SdkError`. | ||
/// Generic Parameters: | ||
/// - `B`: The Response Body | ||
/// - `O`: The Http response handler that returns `Result<T, E>` | ||
/// - `T`/`E`: `Result<T, E>` returned by `handler`. | ||
pub async fn load_response<B, T, E, O>( | ||
mut response: http::Response<B>, | ||
handler: &O, | ||
) -> Result<SdkSuccess<T, B>, SdkError<E, B>> | ||
where | ||
B: http_body::Body + Unpin, | ||
B: From<Bytes> + 'static, | ||
B::Error: Into<BoxError>, | ||
O: ParseHttpResponse<B, Output = Result<T, E>>, | ||
{ | ||
if let Some(parsed_response) = handler.parse_unloaded(&mut response) { | ||
return sdk_result(parsed_response, response); | ||
} | ||
|
||
let body = match read_body(response.body_mut()).await { | ||
Ok(body) => body, | ||
Err(e) => { | ||
return Err(SdkError::ResponseError { | ||
raw: response, | ||
err: e.into(), | ||
}); | ||
} | ||
}; | ||
|
||
let response = response.map(|_| Bytes::from(body)); | ||
let parsed = handler.parse_loaded(&response); | ||
sdk_result(parsed, response.map(B::from)) | ||
} | ||
|
||
async fn read_body<B: http_body::Body>(body: B) -> Result<Vec<u8>, B::Error> { | ||
let mut output = Vec::new(); | ||
pin_mut!(body); | ||
while let Some(buf) = body.data().await { | ||
let mut buf = buf?; | ||
while buf.has_remaining() { | ||
output.extend_from_slice(buf.chunk()); | ||
buf.advance(buf.chunk().len()) | ||
} | ||
} | ||
Ok(output) | ||
} | ||
|
||
/// Convert a `Result<T, E>` into an `SdkResult` that includes the raw HTTP response | ||
fn sdk_result<T, E, B>( | ||
parsed: Result<T, E>, | ||
raw: http::Response<B>, | ||
) -> Result<SdkSuccess<T, B>, SdkError<E, B>> { | ||
match parsed { | ||
Ok(parsed) => Ok(SdkSuccess { raw, parsed }), | ||
Err(err) => Err(SdkError::ServiceError { raw, err }), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0. | ||
*/ | ||
|
||
/// Pins a value on the stack. | ||
/// | ||
/// # Example | ||
/// | ||
/// ```rust | ||
/// # use core::pin::Pin; | ||
/// # struct Foo {} | ||
/// # use smithy_http::pin_mut; | ||
/// let foo = Foo { /* ... */ }; | ||
/// pin_mut!(foo); | ||
/// let _: Pin<&mut Foo> = foo; | ||
/// ``` | ||
#[macro_export] | ||
macro_rules! pin_mut { | ||
($($x:ident),* $(,)?) => { $( | ||
// Move the value to ensure that it is owned | ||
let mut $x = $x; | ||
// Shadow the original binding so that it can't be directly accessed | ||
// ever again. | ||
#[allow(unused_mut)] | ||
let mut $x = unsafe { | ||
core::pin::Pin::new_unchecked(&mut $x) | ||
}; | ||
)* } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0. | ||
*/ | ||
|
||
use std::error::Error; | ||
use std::fmt::Debug; | ||
|
||
type BoxError = Box<dyn Error + Send + Sync>; | ||
|
||
/// Body type when a response is returned. Currently, the only use case is introspecting errors | ||
/// so it is simply `Debug`. This is an area of potential design iteration. | ||
// pub type Body = Pin<Box<dyn http_body::Body<Data = Bytes, Error=Box<dyn Error>> + Send + Sync>>; | ||
|
||
/// Successful Sdk Result | ||
/// | ||
/// Typically, transport implementations will type alias (or entirely wrap / transform) this type | ||
/// plugging in a concrete body implementation, eg: | ||
/// ```rust | ||
/// # mod hyper { | ||
/// # pub struct Body; | ||
/// # } | ||
/// type SdkSuccess<O> = smithy_http::result::SdkSuccess<O, hyper::Body>; | ||
/// ``` | ||
#[derive(Debug)] | ||
pub struct SdkSuccess<O, B> { | ||
pub raw: http::Response<B>, | ||
pub parsed: O, | ||
} | ||
|
||
/// Failing Sdk Result | ||
/// | ||
/// Typically, transport implementations will type alias (or entirely wrap / transform) this type | ||
/// by specifying a concrete body implementation: | ||
/// ```rust | ||
/// # mod hyper { | ||
/// # pub struct Body; | ||
/// # } | ||
/// type SdkError<E> = smithy_http::result::SdkError<E, hyper::Body>; | ||
/// ``` | ||
#[derive(Debug)] | ||
pub enum SdkError<E, B> { | ||
/// The request failed during construction. It was not dispatched over the network. | ||
ConstructionFailure(BoxError), | ||
|
||
/// The request failed during dispatch. An HTTP response was not received. The request MAY | ||
/// have been sent. | ||
DispatchFailure(BoxError), | ||
|
||
/// A response was received but it was not parseable according the the protocol (for example | ||
/// the server hung up while the body was being read) | ||
ResponseError { | ||
raw: http::Response<B>, | ||
err: BoxError, | ||
}, | ||
|
||
/// An error response was received from the service | ||
ServiceError { raw: http::Response<B>, err: E }, | ||
} |