2021-08-03 21:55:48 +02:00
|
|
|
use super::{FromRequest, RequestParts};
|
|
|
|
|
use async_trait::async_trait;
|
|
|
|
|
use std::convert::Infallible;
|
|
|
|
|
|
|
|
|
|
/// Extractor that extracts the raw query string, without parsing it.
|
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
///
|
|
|
|
|
/// ```rust,no_run
|
2021-08-18 00:04:15 +02:00
|
|
|
/// use axum::{
|
|
|
|
|
/// extract::RawQuery,
|
|
|
|
|
/// handler::get,
|
|
|
|
|
/// route,
|
|
|
|
|
/// };
|
2021-08-03 21:55:48 +02:00
|
|
|
/// use futures::StreamExt;
|
|
|
|
|
///
|
2021-08-18 00:04:15 +02:00
|
|
|
/// async fn handler(RawQuery(query): RawQuery) {
|
2021-08-03 21:55:48 +02:00
|
|
|
/// // ...
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// let app = route("/users", get(handler));
|
|
|
|
|
/// # async {
|
2021-08-04 15:38:51 +02:00
|
|
|
/// # axum::Server::bind(&"".parse().unwrap()).serve(app.into_make_service()).await.unwrap();
|
2021-08-03 21:55:48 +02:00
|
|
|
/// # };
|
|
|
|
|
/// ```
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub struct RawQuery(pub Option<String>);
|
|
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
|
impl<B> FromRequest<B> for RawQuery
|
|
|
|
|
where
|
|
|
|
|
B: Send,
|
|
|
|
|
{
|
|
|
|
|
type Rejection = Infallible;
|
|
|
|
|
|
|
|
|
|
async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
|
2021-08-07 20:24:13 +02:00
|
|
|
let query = req.uri().query().map(|query| query.to_string());
|
2021-08-03 21:55:48 +02:00
|
|
|
Ok(Self(query))
|
|
|
|
|
}
|
|
|
|
|
}
|