1
+ use std:: convert:: Infallible ;
1
2
use std:: error:: Error ;
2
3
use std:: net:: { IpAddr , Ipv4Addr , SocketAddr } ;
4
+ use std:: str:: FromStr ;
5
+
3
6
use bytes:: { Bytes , BytesMut } ;
7
+ use http_body_util:: Full ;
4
8
use hyper:: { Request , Response } ;
5
9
use hyper:: server:: conn:: http1;
6
10
use hyper:: service:: service_fn;
7
11
use hyper_util:: rt:: { TokioExecutor , TokioIo } ;
8
12
use hyper_util:: server:: conn:: auto;
9
13
use log:: error;
10
14
use tokio:: net:: { TcpListener , TcpStream } ;
11
- use crate :: core :: stream :: Stream ;
15
+
12
16
use crate :: core:: feature:: Feature ;
13
- use std:: convert:: Infallible ;
14
- use std:: str:: FromStr ;
15
- use http_body_util:: Full ;
17
+ use crate :: core:: stream:: Stream ;
16
18
17
19
pub enum HttpVersion {
18
20
V1 ,
@@ -77,23 +79,23 @@ impl Stream for HttpStream {
77
79
}
78
80
} ) ;
79
81
}
80
- Ok ( ( ) )
81
82
}
82
83
83
84
fn close ( & self ) -> Result < ( ) , Box < dyn Error > > {
84
85
Ok ( ( ) )
85
86
}
86
87
87
- fn feature ( & self ) -> Box < dyn Feature > {
88
- Box :: new ( Self {
89
- config : HttpConfig {
90
- addr : "" . to_string ( ) ,
91
- port : 0 ,
92
- interface : "" . to_string ( ) ,
93
- version : HttpVersion :: V1 ,
94
- }
95
- } )
96
- }
88
+ // fn feature(&self) -> impl Feature {
89
+ // // Box::new(Self {
90
+ // // config: HttpConfig {
91
+ // // addr: "".to_string(),
92
+ // // port: 0,
93
+ // // interface: "".to_string(),
94
+ // // version: HttpVersion::V1,
95
+ // // }
96
+ // // })
97
+ // *self
98
+ // }
97
99
98
100
fn handle ( bs : Vec < u8 > ) -> Result < ( ) , Box < dyn Error > > {
99
101
BytesMut :: with_capacity ( 10 ) ;
@@ -105,6 +107,12 @@ impl Feature for HttpStream {}
105
107
106
108
#[ cfg( test) ]
107
109
mod test {
110
+ use std:: time;
111
+
112
+ use http_body_util:: BodyStream ;
113
+ use hyper_util:: rt:: TokioIo ;
114
+ use tokio:: net:: TcpStream ;
115
+
108
116
use crate :: core:: stream:: Stream ;
109
117
use crate :: proxy:: http:: { HttpConfig , HttpStream , HttpVersion } ;
110
118
@@ -118,4 +126,34 @@ mod test {
118
126
} ) ;
119
127
server. start ( ) . await . unwrap ( ) ;
120
128
}
129
+
130
+ #[ tokio:: test]
131
+ async fn test_http_client ( ) {
132
+ // Parse our URL...
133
+ let url = "http://127.0.0.1:80" . parse :: < hyper:: Uri > ( ) . unwrap ( ) ;
134
+
135
+ // Get the host and the port
136
+ let host = url. host ( ) . expect ( "uri has no host" ) ;
137
+ let port = url. port_u16 ( ) . unwrap_or ( 80 ) ;
138
+
139
+ let address = format ! ( "{}:{}" , host, port) ;
140
+
141
+ // Open a TCP connection to the remote host
142
+ let stream = TcpStream :: connect ( address) . await . unwrap ( ) ;
143
+
144
+ // Use an adapter to access something implementing `tokio::io` traits as if they implement
145
+ // `hyper::rt` IO traits.
146
+ let io = TokioIo :: new ( stream) ;
147
+
148
+ // Create the Hyper client
149
+ let ( mut sender, conn) = hyper:: client:: conn:: http1:: handshake :: < TokioIo < TcpStream > , BodyStream < String > > ( io) . await . unwrap ( ) ;
150
+
151
+ // Spawn a task to poll the connection, driving the HTTP state
152
+ tokio:: task:: spawn ( async move {
153
+ if let Err ( err) = conn. await {
154
+ println ! ( "Connection failed: {:?}" , err) ;
155
+ }
156
+ } ) ;
157
+ tokio:: time:: sleep ( time:: Duration :: from_secs ( 1000 ) ) . await ;
158
+ }
121
159
}
0 commit comments