1use crate::{Authority, Fields, Method, Path, Protocol, Scheme, Version, body::EmptyBody};
5use std::ops::{Deref, DerefMut};
6
7#[derive(Clone, Debug)]
11pub struct RequestMetadata {
12 pub method: Method,
13 pub scheme: Option<Scheme>,
14 pub authority: Option<Authority>,
15 pub path: Option<Path>,
18 pub version: Option<Version>,
19 pub fields: Fields,
20 pub content_length: Option<u64>,
23 pub persistent: Option<bool>,
26 pub protocol: Option<Protocol>,
27}
28
29impl RequestMetadata {
30 pub fn new(method: Method) -> Self {
31 Self {
32 method,
33 scheme: None,
34 authority: None,
35 path: None,
36 version: None,
37 fields: Fields::new(),
38 content_length: None,
39 persistent: None,
40 protocol: None,
41 }
42 }
43}
44
45#[derive(Clone, Debug)]
48pub struct Request<B = EmptyBody> {
49 pub metadata: RequestMetadata,
50 pub body: B,
51}
52
53impl<B> Request<B> {
54 pub fn new(method: Method, path: impl Into<Path>, body: B) -> Self {
55 let mut metadata = RequestMetadata::new(method);
56 metadata.path = Some(path.into());
57 Self { metadata, body }
58 }
59
60 pub fn from_parts(metadata: RequestMetadata, body: B) -> Self {
61 Self { metadata, body }
62 }
63 pub fn into_parts(self) -> (RequestMetadata, B) {
64 (self.metadata, self.body)
65 }
66 pub fn map_body<T>(self, map: impl FnOnce(B) -> T) -> Request<T> {
68 Request {
69 metadata: self.metadata,
70 body: map(self.body),
71 }
72 }
73 pub fn with_body<T>(self, body: T) -> Request<T> {
76 self.map_body(|_| body)
77 }
78 pub fn is_head(&self) -> bool {
79 self.method == Method::HEAD
80 }
81 pub fn is_connect(&self) -> bool {
82 self.method == Method::CONNECT
83 }
84}
85
86impl Request<EmptyBody> {
87 pub fn get(path: impl Into<Path>) -> Self {
88 Self::new(Method::GET, path, EmptyBody)
89 }
90}
91
92impl<B> Deref for Request<B> {
93 type Target = RequestMetadata;
94 fn deref(&self) -> &Self::Target {
95 &self.metadata
96 }
97}
98impl<B> DerefMut for Request<B> {
99 fn deref_mut(&mut self) -> &mut Self::Target {
100 &mut self.metadata
101 }
102}
103
104impl<B: crate::body::ReadableBody> Request<B> {
105 pub async fn read(
108 &mut self,
109 cancellation: &base_scheduler::Cancellation,
110 ) -> Result<crate::body::BodyFrame<B::Data>, B::Error> {
111 self.body.read(cancellation).await
112 }
113}