Files
axum/axum/src/extract/mod.rs
T
David Pedersen 184ea656c0 Change HeaderMap extractor to clone the headers (#698)
* Change `HeaderMap` extractor to clone the headers

* fix docs

* changelog

* inline variable

* also add changelog item to axum

* don't list types from axum in axum-core's changelog

* document that `HeaderMap::from_request` clones the headers

* fix typo

* a few more typos
2022-01-23 18:01:52 +01:00

116 lines
2.7 KiB
Rust

#![doc = include_str!("../docs/extract.md")]
use http::header;
use rejection::*;
pub mod connect_info;
pub mod extractor_middleware;
pub mod path;
pub mod rejection;
#[cfg(feature = "ws")]
#[cfg_attr(docsrs, doc(cfg(feature = "ws")))]
pub mod ws;
mod content_length_limit;
mod extension;
mod form;
mod query;
mod raw_query;
mod request_parts;
#[doc(inline)]
pub use axum_core::extract::{FromRequest, RequestParts};
#[doc(inline)]
#[allow(deprecated)]
pub use self::{
connect_info::ConnectInfo,
content_length_limit::ContentLengthLimit,
extension::Extension,
extractor_middleware::extractor_middleware,
form::Form,
path::Path,
query::Query,
raw_query::RawQuery,
request_parts::{BodyStream, RawBody},
};
#[doc(no_inline)]
#[cfg(feature = "json")]
pub use crate::Json;
#[cfg(feature = "matched-path")]
mod matched_path;
#[cfg(feature = "matched-path")]
#[cfg_attr(docsrs, doc(cfg(feature = "matched-path")))]
#[doc(inline)]
pub use self::matched_path::MatchedPath;
#[cfg(feature = "multipart")]
#[cfg_attr(docsrs, doc(cfg(feature = "multipart")))]
pub mod multipart;
#[cfg(feature = "multipart")]
#[cfg_attr(docsrs, doc(cfg(feature = "multipart")))]
#[doc(inline)]
pub use self::multipart::Multipart;
#[cfg(feature = "original-uri")]
#[cfg_attr(docsrs, doc(cfg(feature = "original-uri")))]
#[doc(inline)]
pub use self::request_parts::OriginalUri;
#[cfg(feature = "ws")]
#[cfg_attr(docsrs, doc(cfg(feature = "ws")))]
#[doc(inline)]
pub use self::ws::WebSocketUpgrade;
#[cfg(feature = "headers")]
#[cfg_attr(docsrs, doc(cfg(feature = "headers")))]
mod typed_header;
#[cfg(feature = "headers")]
#[cfg_attr(docsrs, doc(cfg(feature = "headers")))]
#[doc(inline)]
pub use self::typed_header::TypedHeader;
pub(crate) fn has_content_type<B>(
req: &RequestParts<B>,
expected_content_type: &mime::Mime,
) -> bool {
let content_type = if let Some(content_type) = req.headers().get(header::CONTENT_TYPE) {
content_type
} else {
return false;
};
let content_type = if let Ok(content_type) = content_type.to_str() {
content_type
} else {
return false;
};
content_type.starts_with(expected_content_type.as_ref())
}
pub(crate) fn take_body<B>(req: &mut RequestParts<B>) -> Result<B, BodyAlreadyExtracted> {
req.take_body().ok_or_else(BodyAlreadyExtracted::default)
}
#[cfg(test)]
mod tests {
use crate::{routing::get, test_helpers::*, Router};
#[tokio::test]
async fn consume_body() {
let app = Router::new().route("/", get(|body: String| async { body }));
let client = TestClient::new(app);
let res = client.get("/").body("foo").send().await;
let body = res.text().await;
assert_eq!(body, "foo");
}
}