mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-27 17:52:56 +01:00
Implement FromRequest
for request parts (#2263)
Co-authored-by: Rob Ede <robjtede@icloud.com>
This commit is contained in:
parent
12f7720309
commit
b1148fd735
@ -1,12 +1,17 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
## Unreleased - 2021-xx-xx
|
## Unreleased - 2021-xx-xx
|
||||||
|
### Added
|
||||||
|
* Add extractors for `Uri` and `Method`. [#2263]
|
||||||
|
* Add extractor for `ConnectionInfo` and `PeerAddr`. [#2263]
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
* Change compression algorithm features flags. [#2250]
|
* Change compression algorithm features flags. [#2250]
|
||||||
* Deprecate `App::data` and `App::data_factory`. [#2271]
|
* Deprecate `App::data` and `App::data_factory`. [#2271]
|
||||||
|
|
||||||
[#2250]: https://github.com/actix/actix-web/pull/2250
|
[#2250]: https://github.com/actix/actix-web/pull/2250
|
||||||
[#2271]: https://github.com/actix/actix-web/pull/2271
|
[#2271]: https://github.com/actix/actix-web/pull/2271
|
||||||
|
[#2263]: https://github.com/actix/actix-web/pull/2263
|
||||||
|
|
||||||
|
|
||||||
## 4.0.0-beta.7 - 2021-06-17
|
## 4.0.0-beta.7 - 2021-06-17
|
||||||
|
@ -1,12 +1,14 @@
|
|||||||
//! Request extractors
|
//! Request extractors
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
|
convert::Infallible,
|
||||||
future::Future,
|
future::Future,
|
||||||
pin::Pin,
|
pin::Pin,
|
||||||
task::{Context, Poll},
|
task::{Context, Poll},
|
||||||
};
|
};
|
||||||
|
|
||||||
use actix_utils::future::{ready, Ready};
|
use actix_http::http::{Method, Uri};
|
||||||
|
use actix_utils::future::{ok, Ready};
|
||||||
use futures_core::ready;
|
use futures_core::ready;
|
||||||
|
|
||||||
use crate::{dev::Payload, Error, HttpRequest};
|
use crate::{dev::Payload, Error, HttpRequest};
|
||||||
@ -216,14 +218,58 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Extract the request's URI.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
/// ```
|
||||||
|
/// use actix_web::{http::Uri, web, App, Responder};
|
||||||
|
///
|
||||||
|
/// async fn handler(uri: Uri) -> impl Responder {
|
||||||
|
/// format!("Requested path: {}", uri.path())
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// let app = App::new().default_service(web::to(handler));
|
||||||
|
/// ```
|
||||||
|
impl FromRequest for Uri {
|
||||||
|
type Error = Infallible;
|
||||||
|
type Future = Ready<Result<Self, Self::Error>>;
|
||||||
|
type Config = ();
|
||||||
|
|
||||||
|
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
||||||
|
ok(req.uri().clone())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Extract the request's method.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
/// ```
|
||||||
|
/// use actix_web::{http::Method, web, App, Responder};
|
||||||
|
///
|
||||||
|
/// async fn handler(method: Method) -> impl Responder {
|
||||||
|
/// format!("Request method: {}", method)
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// let app = App::new().default_service(web::to(handler));
|
||||||
|
/// ```
|
||||||
|
impl FromRequest for Method {
|
||||||
|
type Error = Infallible;
|
||||||
|
type Future = Ready<Result<Self, Self::Error>>;
|
||||||
|
type Config = ();
|
||||||
|
|
||||||
|
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
||||||
|
ok(req.method().clone())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
impl FromRequest for () {
|
impl FromRequest for () {
|
||||||
type Error = Error;
|
type Error = Infallible;
|
||||||
type Future = Ready<Result<(), Error>>;
|
type Future = Ready<Result<(), Infallible>>;
|
||||||
type Config = ();
|
type Config = ();
|
||||||
|
|
||||||
fn from_request(_: &HttpRequest, _: &mut Payload) -> Self::Future {
|
fn from_request(_: &HttpRequest, _: &mut Payload) -> Self::Future {
|
||||||
ready(Ok(()))
|
ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -411,4 +457,18 @@ mod tests {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
assert!(r.is_err());
|
assert!(r.is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_uri() {
|
||||||
|
let req = TestRequest::default().uri("/foo/bar").to_http_request();
|
||||||
|
let uri = Uri::extract(&req).await.unwrap();
|
||||||
|
assert_eq!(uri.path(), "/foo/bar");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_method() {
|
||||||
|
let req = TestRequest::default().method(Method::GET).to_http_request();
|
||||||
|
let method = Method::extract(&req).await.unwrap();
|
||||||
|
assert_eq!(method, Method::GET);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
111
src/info.rs
111
src/info.rs
@ -1,13 +1,36 @@
|
|||||||
use std::cell::Ref;
|
use std::{cell::Ref, convert::Infallible, net::SocketAddr};
|
||||||
|
|
||||||
use crate::dev::{AppConfig, RequestHead};
|
use actix_utils::future::{err, ok, Ready};
|
||||||
use crate::http::header::{self, HeaderName};
|
use derive_more::{Display, Error};
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
dev::{AppConfig, Payload, RequestHead},
|
||||||
|
http::header::{self, HeaderName},
|
||||||
|
FromRequest, HttpRequest, ResponseError,
|
||||||
|
};
|
||||||
|
|
||||||
const X_FORWARDED_FOR: &[u8] = b"x-forwarded-for";
|
const X_FORWARDED_FOR: &[u8] = b"x-forwarded-for";
|
||||||
const X_FORWARDED_HOST: &[u8] = b"x-forwarded-host";
|
const X_FORWARDED_HOST: &[u8] = b"x-forwarded-host";
|
||||||
const X_FORWARDED_PROTO: &[u8] = b"x-forwarded-proto";
|
const X_FORWARDED_PROTO: &[u8] = b"x-forwarded-proto";
|
||||||
|
|
||||||
/// `HttpRequest` connection information
|
/// HTTP connection information.
|
||||||
|
///
|
||||||
|
/// `ConnectionInfo` implements `FromRequest` and can be extracted in handlers.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
/// ```
|
||||||
|
/// # use actix_web::{HttpResponse, Responder};
|
||||||
|
/// use actix_web::dev::ConnectionInfo;
|
||||||
|
///
|
||||||
|
/// async fn handler(conn: ConnectionInfo) -> impl Responder {
|
||||||
|
/// match conn.host() {
|
||||||
|
/// "actix.rs" => HttpResponse::Ok().body("Welcome!"),
|
||||||
|
/// "admin.actix.rs" => HttpResponse::Ok().body("Admin portal."),
|
||||||
|
/// _ => HttpResponse::NotFound().finish()
|
||||||
|
/// }
|
||||||
|
/// }
|
||||||
|
/// # let _svc = actix_web::web::to(handler);
|
||||||
|
/// ```
|
||||||
#[derive(Debug, Clone, Default)]
|
#[derive(Debug, Clone, Default)]
|
||||||
pub struct ConnectionInfo {
|
pub struct ConnectionInfo {
|
||||||
scheme: String,
|
scheme: String,
|
||||||
@ -187,6 +210,65 @@ impl ConnectionInfo {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl FromRequest for ConnectionInfo {
|
||||||
|
type Error = Infallible;
|
||||||
|
type Future = Ready<Result<Self, Self::Error>>;
|
||||||
|
type Config = ();
|
||||||
|
|
||||||
|
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
||||||
|
ok(req.connection_info().clone())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Extractor for peer's socket address.
|
||||||
|
///
|
||||||
|
/// Also see [`HttpRequest::peer_addr`].
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
/// ```
|
||||||
|
/// # use actix_web::Responder;
|
||||||
|
/// use actix_web::dev::PeerAddr;
|
||||||
|
///
|
||||||
|
/// async fn handler(peer_addr: PeerAddr) -> impl Responder {
|
||||||
|
/// let socket_addr = peer_addr.0;
|
||||||
|
/// socket_addr.to_string()
|
||||||
|
/// }
|
||||||
|
/// # let _svc = actix_web::web::to(handler);
|
||||||
|
/// ```
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Display)]
|
||||||
|
#[display(fmt = "{}", _0)]
|
||||||
|
pub struct PeerAddr(pub SocketAddr);
|
||||||
|
|
||||||
|
impl PeerAddr {
|
||||||
|
/// Unwrap into inner `SocketAddr` value.
|
||||||
|
pub fn into_inner(self) -> SocketAddr {
|
||||||
|
self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Display, Error)]
|
||||||
|
#[non_exhaustive]
|
||||||
|
#[display(fmt = "Missing peer address")]
|
||||||
|
pub struct MissingPeerAddr;
|
||||||
|
|
||||||
|
impl ResponseError for MissingPeerAddr {}
|
||||||
|
|
||||||
|
impl FromRequest for PeerAddr {
|
||||||
|
type Error = MissingPeerAddr;
|
||||||
|
type Future = Ready<Result<Self, Self::Error>>;
|
||||||
|
type Config = ();
|
||||||
|
|
||||||
|
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
||||||
|
match req.peer_addr() {
|
||||||
|
Some(addr) => ok(PeerAddr(addr)),
|
||||||
|
None => {
|
||||||
|
log::error!("Missing peer address.");
|
||||||
|
err(MissingPeerAddr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
@ -239,4 +321,25 @@ mod tests {
|
|||||||
let info = req.connection_info();
|
let info = req.connection_info();
|
||||||
assert_eq!(info.scheme(), "https");
|
assert_eq!(info.scheme(), "https");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_conn_info() {
|
||||||
|
let req = TestRequest::default()
|
||||||
|
.uri("http://actix.rs/")
|
||||||
|
.to_http_request();
|
||||||
|
let conn_info = ConnectionInfo::extract(&req).await.unwrap();
|
||||||
|
assert_eq!(conn_info.scheme(), "http");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_peer_addr() {
|
||||||
|
let addr = "127.0.0.1:8080".parse().unwrap();
|
||||||
|
let req = TestRequest::default().peer_addr(addr).to_http_request();
|
||||||
|
let peer_addr = PeerAddr::extract(&req).await.unwrap();
|
||||||
|
assert_eq!(peer_addr, PeerAddr(addr));
|
||||||
|
|
||||||
|
let req = TestRequest::default().to_http_request();
|
||||||
|
let res = PeerAddr::extract(&req).await;
|
||||||
|
assert!(res.is_err());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -131,7 +131,7 @@ pub mod dev {
|
|||||||
pub use crate::config::{AppConfig, AppService};
|
pub use crate::config::{AppConfig, AppService};
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub use crate::handler::Handler;
|
pub use crate::handler::Handler;
|
||||||
pub use crate::info::ConnectionInfo;
|
pub use crate::info::{ConnectionInfo, PeerAddr};
|
||||||
pub use crate::rmap::ResourceMap;
|
pub use crate::rmap::ResourceMap;
|
||||||
pub use crate::service::{HttpServiceFactory, ServiceRequest, ServiceResponse, WebService};
|
pub use crate::service::{HttpServiceFactory, ServiceRequest, ServiceResponse, WebService};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user