micro_web/extract/extract_header.rs
1//! HTTP header extraction implementations
2//!
3//! This module provides extractors for HTTP header-related information from requests,
4//! including HTTP methods, header maps and raw request headers. These extractors allow
5//! handlers to directly access header information in a type-safe way.
6//!
7//! The extractors support both owned and borrowed access to the header data:
8//! - Owned extractors like `Method` and `HeaderMap` take ownership of the data
9//! - Borrowed extractors like `&Method` and `&HeaderMap` provide reference access
10//!
11//! # Examples
12//!
13//! ```no_run
14//! use http::{HeaderMap, Method};
15//!
16//! // Access HTTP method
17//! async fn handle_method(method: Method) {
18//! match method {
19//! Method::GET => println!("Handling GET request"),
20//! Method::POST => println!("Handling POST request"),
21//! _ => println!("Handling other request method")
22//! }
23//! }
24//!
25//! // Access headers
26//! async fn handle_headers(headers: &HeaderMap) {
27//! if let Some(content_type) = headers.get("content-type") {
28//! println!("Content-Type: {:?}", content_type);
29//! }
30//! }
31//! ```
32
33use crate::RequestContext;
34use crate::body::OptionReqBody;
35use crate::extract::from_request::FromRequest;
36use http::{HeaderMap, Method};
37use micro_http::protocol::{ParseError, RequestHeader};
38
39/// Extracts the HTTP method by value
40///
41/// This extractor takes ownership of the request method.
42impl FromRequest for Method {
43 type Output<'any> = Method;
44 type Error = ParseError;
45
46 async fn from_request(req: &RequestContext<'_, '_>, _body: OptionReqBody) -> Result<Self::Output<'static>, Self::Error> {
47 Ok(req.method().clone())
48 }
49}
50
51/// Extracts a reference to the HTTP method
52///
53/// This extractor borrows the request method, avoiding cloning.
54impl FromRequest for &Method {
55 type Output<'r> = &'r Method;
56 type Error = ParseError;
57
58 async fn from_request<'r>(req: &'r RequestContext<'_, '_>, _body: OptionReqBody) -> Result<Self::Output<'r>, Self::Error> {
59 Ok(req.method())
60 }
61}
62
63/// Extracts a reference to the raw request header
64///
65/// Provides access to the underlying HTTP request header structure.
66impl FromRequest for &RequestHeader {
67 type Output<'r> = &'r RequestHeader;
68 type Error = ParseError;
69
70 async fn from_request<'r>(req: &'r RequestContext<'_, '_>, _body: OptionReqBody) -> Result<Self::Output<'r>, Self::Error> {
71 Ok(req.request_header())
72 }
73}
74
75/// Extracts a reference to the header map
76///
77/// This extractor provides borrowed access to all HTTP headers.
78impl FromRequest for &HeaderMap {
79 type Output<'r> = &'r HeaderMap;
80 type Error = ParseError;
81
82 async fn from_request<'r>(req: &'r RequestContext<'_, '_>, _body: OptionReqBody) -> Result<Self::Output<'r>, Self::Error> {
83 Ok(req.headers())
84 }
85}
86
87/// Extracts the header map by value
88///
89/// This extractor clones and takes ownership of all HTTP headers.
90impl FromRequest for HeaderMap {
91 type Output<'any> = HeaderMap;
92 type Error = ParseError;
93
94 async fn from_request(req: &RequestContext<'_, '_>, _body: OptionReqBody) -> Result<Self::Output<'static>, Self::Error> {
95 Ok(req.headers().clone())
96 }
97}