-
Notifications
You must be signed in to change notification settings - Fork 192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Collect extractors into request
module
#2008
Merged
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a0b7e3c
Collect extractors into `extract` module
c974a47
Merge branch 'main' into harryb/extractors
hlbarber 24ae36b
Add license info
05ce487
Add the extract module
272f3b4
Re-purpose request module
18fdd08
A module documentation
5c90261
Update connect_info documentation
d7f75f1
Fix connect_info paths
88295c5
Merge branch 'main' into harryb/extractors
hlbarber 372af5d
Update pokemon-service-connect-info
a85ca1c
Merge branch 'main' into harryb/extractors
hlbarber File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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 |
---|---|---|
|
@@ -32,6 +32,8 @@ | |
* DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
//! Extractor for getting connection information from a client. | ||
|
||
use std::{ | ||
convert::Infallible, | ||
fmt, | ||
|
@@ -48,12 +50,11 @@ use tower_http::add_extension::{AddExtension, AddExtensionLayer}; | |
|
||
use crate::{request::FromParts, Extension}; | ||
|
||
/// A [`MakeService`] created from a router. | ||
/// A [`MakeService`] used to insert [`ConnectInfo<T>`] into [`http::Request`]s. | ||
/// | ||
/// See [`Router::into_make_service_with_connect_info`] for more details. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This intradoc link would be possible with #2014 |
||
/// The `T` must be derivable from the underlying IO resource using the [`Connected`] trait. | ||
/// | ||
/// [`MakeService`]: tower::make::MakeService | ||
/// [`Router::into_make_service_with_connect_info`]: crate::routing::Router::into_make_service_with_connect_info | ||
pub struct IntoMakeServiceWithConnectInfo<S, C> { | ||
inner: S, | ||
_connect_info: PhantomData<fn() -> C>, | ||
|
@@ -96,10 +97,6 @@ where | |
/// | ||
/// The goal for this trait is to allow users to implement custom IO types that | ||
/// can still provide the same connection metadata. | ||
/// | ||
/// See [`Router::into_make_service_with_connect_info`] for more details. | ||
/// | ||
/// [`Router::into_make_service_with_connect_info`]: crate::routing::Router::into_make_service_with_connect_info | ||
pub trait Connected<T>: Clone { | ||
/// Create type holding information about the connection. | ||
fn connect_info(target: T) -> Self; | ||
|
@@ -140,13 +137,9 @@ opaque_future! { | |
|
||
/// Extractor for getting connection information produced by a `Connected`. | ||
/// | ||
/// Note this extractor requires you to use | ||
/// [`Router::into_make_service_with_connect_info`] to run your app | ||
/// otherwise it will fail at runtime. | ||
/// | ||
/// See [`Router::into_make_service_with_connect_info`] for more details. | ||
/// | ||
/// [`Router::into_make_service_with_connect_info`]: crate::routing::Router::into_make_service_with_connect_info | ||
/// Note this extractor requires the existence of [`Extension<ConnectInfo<T>>`] in the [`http::Extensions`]. This is | ||
/// automatically inserted by the [`IntoMakeServiceWithConnectInfo`] middleware, which can be applied using the | ||
/// `into_make_service_with_connect_info` method on your generated service. | ||
#[derive(Clone, Debug)] | ||
pub struct ConnectInfo<T>(pub T); | ||
|
||
|
103 changes: 103 additions & 0 deletions
103
rust-runtime/aws-smithy-http-server/src/request/extension.rs
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,103 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
// This code was copied and then modified from Tokio's Axum. | ||
|
||
/* Copyright (c) 2021 Tower Contributors | ||
* | ||
* Permission is hereby granted, free of charge, to any | ||
* person obtaining a copy of this software and associated | ||
* documentation files (the "Software"), to deal in the | ||
* Software without restriction, including without | ||
* limitation the rights to use, copy, modify, merge, | ||
* publish, distribute, sublicense, and/or sell copies of | ||
* the Software, and to permit persons to whom the Software | ||
* is furnished to do so, subject to the following | ||
* conditions: | ||
* | ||
* The above copyright notice and this permission notice | ||
* shall be included in all copies or substantial portions | ||
* of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF | ||
* ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED | ||
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A | ||
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT | ||
* SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | ||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR | ||
* IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
* DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
//! Extension types. | ||
//! | ||
//! Extension types are types that are stored in and extracted from _both_ requests and | ||
//! responses. | ||
//! | ||
//! There is only one _generic_ extension type _for requests_, [`Extension`]. | ||
//! | ||
//! On the other hand, the server SDK uses multiple concrete extension types for responses in order | ||
//! to store a variety of information, like the operation that was executed, the operation error | ||
//! that got returned, or the runtime error that happened, among others. The information stored in | ||
//! these types may be useful to [`tower::Layer`]s that post-process the response: for instance, a | ||
//! particular metrics layer implementation might want to emit metrics about the number of times an | ||
//! an operation got executed. | ||
//! | ||
//! [extensions]: https://docs.rs/http/latest/http/struct.Extensions.html | ||
|
||
use std::ops::Deref; | ||
|
||
use http::StatusCode; | ||
use thiserror::Error; | ||
|
||
use crate::{ | ||
body::{empty, BoxBody}, | ||
request::FromParts, | ||
response::IntoResponse, | ||
}; | ||
|
||
/// Generic extension type stored in and extracted from [request extensions]. | ||
/// | ||
/// This is commonly used to share state across handlers. | ||
/// | ||
/// If the extension is missing it will reject the request with a `500 Internal | ||
/// Server Error` response. | ||
/// | ||
/// [request extensions]: https://docs.rs/http/latest/http/struct.Extensions.html | ||
#[derive(Debug, Clone)] | ||
pub struct Extension<T>(pub T); | ||
|
||
impl<T> Deref for Extension<T> { | ||
type Target = T; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
/// The extension has not been added to the [`Request`](http::Request) or has been previously removed. | ||
#[derive(Debug, Error)] | ||
#[error("the `Extension` is not present in the `http::Request`")] | ||
pub struct MissingExtension; | ||
|
||
impl<Protocol> IntoResponse<Protocol> for MissingExtension { | ||
fn into_response(self) -> http::Response<BoxBody> { | ||
let mut response = http::Response::new(empty()); | ||
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR; | ||
response | ||
} | ||
} | ||
|
||
impl<Protocol, T> FromParts<Protocol> for Extension<T> | ||
where | ||
T: Send + Sync + 'static, | ||
{ | ||
type Rejection = MissingExtension; | ||
|
||
fn from_parts(parts: &mut http::request::Parts) -> Result<Self, Self::Rejection> { | ||
parts.extensions.remove::<T>().map(Extension).ok_or(MissingExtension) | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removing these two re-exports would be a breaking change.