1515) ]
1616
1717use futures:: future:: BoxFuture ;
18- use futures:: io:: { AsyncRead , Cursor } ;
19-
20- use std:: error:: Error ;
21- use std:: fmt:: { self , Debug } ;
22- use std:: io;
23- use std:: pin:: Pin ;
24- use std:: task:: { Context , Poll } ;
2518
2619#[ cfg_attr( feature = "docs" , doc( cfg( curl_client) ) ) ]
2720#[ cfg( all( feature = "curl_client" , not( target_arch = "wasm32" ) ) ) ]
@@ -35,11 +28,15 @@ pub mod wasm;
3528#[ cfg( feature = "native_client" ) ]
3629pub mod native;
3730
31+ #[ cfg_attr( feature = "docs" , doc( cfg( h1_client) ) ) ]
32+ #[ cfg( feature = "h1_client" ) ]
33+ pub mod h1;
34+
3835/// An HTTP Request type with a streaming body.
39- pub type Request = http :: Request < Body > ;
36+ pub type Request = http_types :: Request ;
4037
4138/// An HTTP Response type with a streaming body.
42- pub type Response = http :: Response < Body > ;
39+ pub type Response = http_types :: Response ;
4340
4441/// An abstract HTTP client.
4542///
@@ -55,90 +52,16 @@ pub type Response = http::Response<Body>;
5552///
5653/// How `Clone` is implemented is up to the implementors, but in an ideal scenario combining this
5754/// with the `Client` builder will allow for high connection reuse, improving latency.
58- pub trait HttpClient : Debug + Unpin + Send + Sync + Clone + ' static {
55+ pub trait HttpClient : std :: fmt :: Debug + Unpin + Send + Sync + Clone + ' static {
5956 /// The associated error type.
60- type Error : Error + Send + Sync ;
57+ type Error : Send + Sync + Into < Error > ;
6158
6259 /// Perform a request.
6360 fn send ( & self , req : Request ) -> BoxFuture < ' static , Result < Response , Self :: Error > > ;
6461}
6562
6663/// The raw body of an http request or response.
67- ///
68- /// A body is a stream of `Bytes` values, which are shared handles to byte buffers.
69- /// Both `Body` and `Bytes` values can be easily created from standard owned byte buffer types
70- /// like `Vec<u8>` or `String`, using the `From` trait.
71- pub struct Body {
72- reader : Option < Box < dyn AsyncRead + Unpin + Send + ' static > > ,
73- /// Intentionally use `u64` over `usize` here.
74- /// `usize` won't work if you try to send 10GB file from 32bit host.
75- #[ allow( dead_code) ] // not all backends make use of this
76- len : Option < u64 > ,
77- }
78-
79- impl Body {
80- /// Create a new empty body.
81- pub fn empty ( ) -> Self {
82- Self {
83- reader : None ,
84- len : Some ( 0 ) ,
85- }
86- }
87-
88- /// Create a new instance from a reader.
89- pub fn from_reader ( reader : impl AsyncRead + Unpin + Send + ' static ) -> Self {
90- Self {
91- reader : Some ( Box :: new ( reader) ) ,
92- len : None ,
93- }
94- }
95-
96- /// Validate that the body was created with `Body::empty()`.
97- pub fn is_empty ( & self ) -> bool {
98- self . reader . is_none ( )
99- }
100- }
64+ pub type Body = http_types:: Body ;
10165
102- impl AsyncRead for Body {
103- #[ allow( missing_doc_code_examples) ]
104- fn poll_read (
105- mut self : Pin < & mut Self > ,
106- cx : & mut Context < ' _ > ,
107- buf : & mut [ u8 ] ,
108- ) -> Poll < io:: Result < usize > > {
109- match self . reader . as_mut ( ) {
110- Some ( reader) => Pin :: new ( reader) . poll_read ( cx, buf) ,
111- None => Poll :: Ready ( Ok ( 0 ) ) ,
112- }
113- }
114- }
115-
116- impl fmt:: Debug for Body {
117- #[ allow( missing_doc_code_examples) ]
118- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
119- f. debug_struct ( "Body" ) . field ( "reader" , & "<hidden>" ) . finish ( )
120- }
121- }
122-
123- impl From < Vec < u8 > > for Body {
124- #[ allow( missing_doc_code_examples) ]
125- #[ inline]
126- fn from ( vec : Vec < u8 > ) -> Body {
127- let len = vec. len ( ) as u64 ;
128- Self {
129- reader : Some ( Box :: new ( Cursor :: new ( vec) ) ) ,
130- len : Some ( len) ,
131- }
132- }
133- }
134-
135- impl < R : AsyncRead + Unpin + Send + ' static > From < Box < R > > for Body {
136- /// Converts an `AsyncRead` into a Body.
137- #[ allow( missing_doc_code_examples) ]
138- fn from ( reader : Box < R > ) -> Self {
139- Self {
140- reader : Some ( reader) ,
141- len : None ,
142- }
143- }
144- }
66+ /// Error type.
67+ pub type Error = http_types:: Error ;
0 commit comments