11/* pub mod handler; */
22use builders:: Builder ;
33mod parse;
4+ use core:: fmt;
45use std:: {
56 collections:: HashMap ,
67 env,
78 ffi:: OsStr ,
8- io:: { BufReader , Read , Write } ,
9+ io:: { BufRead , BufReader , Read , Write } ,
910 path:: Path ,
1011} ;
1112
1213use parse:: parse_request;
1314
14- use crate :: { HttpMethod , HttpResponse , HttpStream , Result , StatusCode , encoding:: Chunked } ;
15+ use crate :: {
16+ HttpMethod , HttpResponse , HttpStream , Result , StatusCode ,
17+ encoding:: Chunked ,
18+ stream:: { self , IntoHttpStream } ,
19+ } ;
1520
1621/// HTTP Request
1722///
1823/// Represents an HTTP request
19- #[ derive( Builder , Debug ) ]
24+ #[ derive( Builder ) ]
2025pub struct HttpRequest {
26+ #[ builder( def = { HttpMethod :: GET } ) ]
2127 method : HttpMethod ,
2228 url : Box < str > ,
2329 #[ builder( map = "header" ) ]
@@ -28,35 +34,55 @@ pub struct HttpRequest {
2834 response_headers : HashMap < Box < str > , Box < str > > ,
2935 #[ builder( def = 1.0 ) ]
3036 version : f32 ,
31- #[ builder( disabled = true ) ]
32- #[ builder( def = { BufReader :: new( HttpStream :: dummy( ) ) } ) ]
33- stream : BufReader < HttpStream > ,
37+ #[ builder( def = { BufReader :: new( stream:: dummy( ) ) } ) ]
38+ stream : BufReader < Box < dyn HttpStream > > ,
3439 #[ builder( def = 200u16 ) ]
3540 status : u16 ,
3641 #[ builder( optional = true ) ]
3742 body : Option < Box < [ u8 ] > > ,
3843}
3944
45+ impl fmt:: Debug for HttpRequest {
46+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
47+ f. debug_struct ( "HttpRequest" )
48+ . field ( "method" , & self . method )
49+ . field ( "url" , & self . url )
50+ . field ( "headers" , & self . headers )
51+ . field ( "params" , & self . params )
52+ . field ( "response_headers" , & self . response_headers )
53+ . field ( "version" , & self . version )
54+ . field ( "status" , & self . status )
55+ . field ( "body" , & self . body )
56+ . finish ( )
57+ }
58+ }
59+
4060impl HttpRequest {
4161 /// Read and parse an HTTP request from the given [`HttpStream`]
42- pub fn parse ( stream : impl Into < HttpStream > ) -> Result < Self > {
43- let stream = BufReader :: new ( stream. into ( ) ) ;
44- parse_request ( stream)
62+ pub fn parse < S : IntoHttpStream > ( stream : S ) -> Result < Self > {
63+ let stream: Box < dyn HttpStream > = Box :: new ( stream. into_http_stream ( ) ) ;
64+ parse_request ( BufReader :: new ( stream) )
4565 }
66+
4667 #[ inline]
4768 pub fn keep_alive ( self ) -> Result < Self > {
4869 let mut req = parse_request ( self . stream ) ?;
4970 req. set_header ( "Connection" , "keep-alive" ) ;
5071 Ok ( req)
5172 }
73+
74+ #[ inline]
75+ pub fn stream ( & self ) -> & BufReader < Box < dyn HttpStream > > {
76+ & self . stream
77+ }
78+
5279 #[ inline]
53- #[ must_use]
54- pub fn stream ( & self ) -> & HttpStream {
55- self . stream . get_ref ( )
80+ pub fn stream_mut ( & mut self ) -> & mut BufReader < Box < dyn HttpStream > > {
81+ & mut self . stream
5682 }
83+
5784 /// Url of the request
5885 #[ inline]
59- #[ must_use]
6086 pub fn url ( & self ) -> & str {
6187 & self . url
6288 }
@@ -120,7 +146,8 @@ impl HttpRequest {
120146 ///
121147 /// # Errors
122148 /// If the transfer fails, returns the error
123- pub fn send_to ( & self , mut stream : HttpStream ) -> crate :: Result < HttpResponse > {
149+ pub fn send_to < Out : IntoHttpStream > ( & self , stream : Out ) -> crate :: Result < HttpResponse > {
150+ let mut stream = stream. into_http_stream ( ) ;
124151 self . write_to ( & mut stream) ?;
125152 stream. flush ( ) ?;
126153 HttpResponse :: parse ( stream)
@@ -230,8 +257,8 @@ impl HttpRequest {
230257 /// [`stream`]'s availability
231258 ///
232259 /// [`stream`]: HttpStream
233- pub fn has_body ( & self ) -> Result < bool > {
234- Ok ( self . body . is_some ( ) || self . stream . get_ref ( ) . is_ready ( ) ? )
260+ pub fn has_body ( & mut self ) -> Result < bool > {
261+ Ok ( self . body . is_some ( ) || ! self . stream . fill_buf ( ) ? . is_empty ( ) )
235262 }
236263
237264 /// Reads the request body into [writer](Write)
0 commit comments