@@ -14,6 +14,7 @@ use std::{
14
14
} ;
15
15
16
16
use futures:: { channel:: oneshot, future, prelude:: * } ;
17
+ use hyper:: { Body , Error , Request , Response , Server , StatusCode , Uri } ;
17
18
use tokio:: {
18
19
net:: { TcpListener , TcpStream } ,
19
20
runtime:: current_thread:: Runtime ,
@@ -114,7 +115,7 @@ pub fn proxy() -> (String, Bomb) {
114
115
} ;
115
116
116
117
let record = Arc :: new ( Mutex :: new ( record) ) ;
117
- let srv = hyper :: Server :: builder ( listener. incoming ( ) )
118
+ let srv = Server :: builder ( listener. incoming ( ) )
118
119
. serve ( Proxy {
119
120
sink : sink2,
120
121
record : Arc :: clone ( & record) ,
@@ -153,17 +154,16 @@ struct Proxy {
153
154
client : Option < Client > ,
154
155
}
155
156
156
- impl tower_service:: Service < hyper:: Request < hyper:: Body > > for Proxy {
157
- type Response = hyper:: Response < hyper:: Body > ;
158
- type Error = hyper:: Error ;
159
- type Future =
160
- Pin < Box < dyn Future < Output = Result < hyper:: Response < hyper:: Body > , hyper:: Error > > + Send > > ;
157
+ impl tower_service:: Service < Request < Body > > for Proxy {
158
+ type Response = Response < Body > ;
159
+ type Error = Error ;
160
+ type Future = Pin < Box < dyn Future < Output = Result < Response < Body > , Error > > + Send > > ;
161
161
162
162
fn poll_ready ( & mut self , _: & mut Context < ' _ > ) -> Poll < Result < ( ) , Self :: Error > > {
163
163
Poll :: Ready ( Ok ( ( ) ) )
164
164
}
165
165
166
- fn call ( & mut self , req : hyper :: Request < hyper :: Body > ) -> Self :: Future {
166
+ fn call ( & mut self , req : Request < Body > ) -> Self :: Future {
167
167
match * self . record . lock ( ) . unwrap ( ) {
168
168
Record :: Capture ( _, _) => {
169
169
let client = self . client . as_ref ( ) . unwrap ( ) . clone ( ) ;
@@ -185,8 +185,8 @@ impl tower_service::Service<hyper::Request<hyper::Body>> for Proxy {
185
185
186
186
impl < ' a > tower_service:: Service < & ' a TcpStream > for Proxy {
187
187
type Response = Proxy ;
188
- type Error = hyper :: Error ;
189
- type Future = Pin < Box < dyn Future < Output = Result < Proxy , hyper :: Error > > + Send + ' static > > ;
188
+ type Error = Error ;
189
+ type Future = Pin < Box < dyn Future < Output = Result < Proxy , Error > > + Send + ' static > > ;
190
190
191
191
fn poll_ready ( & mut self , _: & mut Context < ' _ > ) -> Poll < Result < ( ) , Self :: Error > > {
192
192
Poll :: Ready ( Ok ( ( ) ) )
@@ -199,32 +199,29 @@ impl<'a> tower_service::Service<&'a TcpStream> for Proxy {
199
199
200
200
#[ derive( Serialize , Deserialize ) ]
201
201
struct Exchange {
202
- request : Request ,
203
- response : Response ,
202
+ request : RecordedRequest ,
203
+ response : RecordedResponse ,
204
204
}
205
205
206
206
#[ derive( Serialize , Deserialize ) ]
207
- struct Request {
207
+ struct RecordedRequest {
208
208
uri : String ,
209
209
method : String ,
210
210
headers : HashSet < ( String , String ) > ,
211
211
body : String ,
212
212
}
213
213
214
214
#[ derive( Serialize , Deserialize ) ]
215
- struct Response {
215
+ struct RecordedResponse {
216
216
status : u16 ,
217
217
headers : HashSet < ( String , String ) > ,
218
218
body : String ,
219
219
}
220
220
221
221
type Client = hyper:: Client < hyper_tls:: HttpsConnector < hyper:: client:: HttpConnector > > ;
222
- type ResponseAndExchange = ( hyper :: Response < hyper :: Body > , Exchange ) ;
222
+ type ResponseAndExchange = ( Response < Body > , Exchange ) ;
223
223
224
- async fn record_http (
225
- req : hyper:: Request < hyper:: Body > ,
226
- client : Client ,
227
- ) -> Result < ResponseAndExchange , hyper:: Error > {
224
+ async fn record_http ( req : Request < Body > , client : Client ) -> Result < ResponseAndExchange , Error > {
228
225
// Deconstruct the incoming request and await for the full body
229
226
let ( header_parts, body) = req. into_parts ( ) ;
230
227
let method = header_parts. method ;
@@ -233,7 +230,7 @@ async fn record_http(
233
230
let body = body. try_concat ( ) . await ?;
234
231
235
232
// Save info on the incoming request for the exchange log
236
- let request = Request {
233
+ let request = RecordedRequest {
237
234
uri : uri. to_string ( ) ,
238
235
method : method. to_string ( ) ,
239
236
headers : headers
@@ -245,8 +242,8 @@ async fn record_http(
245
242
246
243
// Construct an outgoing request
247
244
let uri = uri. to_string ( ) . replace ( "http://" , "https://" ) ;
248
- let uri = uri. parse :: < hyper :: Uri > ( ) . unwrap ( ) ;
249
- let mut req = hyper :: Request :: builder ( )
245
+ let uri = uri. parse :: < Uri > ( ) . unwrap ( ) ;
246
+ let mut req = Request :: builder ( )
250
247
. method ( method. clone ( ) )
251
248
. uri ( uri)
252
249
. body ( body. into ( ) )
@@ -260,7 +257,7 @@ async fn record_http(
260
257
let body = hyper_response. into_body ( ) . try_concat ( ) . await ?;
261
258
262
259
// Save the response for the exchange log
263
- let response = Response {
260
+ let response = RecordedResponse {
264
261
status : status. as_u16 ( ) ,
265
262
headers : headers
266
263
. iter ( )
@@ -270,7 +267,7 @@ async fn record_http(
270
267
} ;
271
268
272
269
// Construct an outgoing response
273
- let mut hyper_response = hyper :: Response :: builder ( )
270
+ let mut hyper_response = Response :: builder ( )
274
271
. status ( status)
275
272
. body ( body. into ( ) )
276
273
. unwrap ( ) ;
@@ -280,10 +277,10 @@ async fn record_http(
280
277
}
281
278
282
279
fn replay_http (
283
- req : hyper :: Request < hyper :: Body > ,
280
+ req : Request < Body > ,
284
281
mut exchange : Exchange ,
285
282
stdout : & mut dyn Write ,
286
- ) -> impl Future < Output = Result < hyper :: Response < hyper :: Body > , hyper :: Error > > + Send {
283
+ ) -> impl Future < Output = Result < Response < Body > , Error > > + Send {
287
284
static IGNORED_HEADERS : & [ & str ] = & [ "authorization" , "date" , "user-agent" ] ;
288
285
289
286
assert_eq ! ( req. uri( ) . to_string( ) , exchange. request. uri) ;
@@ -319,8 +316,8 @@ fn replay_http(
319
316
base64:: decode( & exchange. request. body) . unwrap( )
320
317
) ;
321
318
322
- let mut builder = hyper :: Response :: builder ( ) ;
323
- builder. status ( hyper :: StatusCode :: from_u16 ( exchange. response . status ) . unwrap ( ) ) ;
319
+ let mut builder = Response :: builder ( ) ;
320
+ builder. status ( StatusCode :: from_u16 ( exchange. response . status ) . unwrap ( ) ) ;
324
321
for ( key, value) in exchange. response . headers {
325
322
builder. header ( key. as_str ( ) , value. as_str ( ) ) ;
326
323
}
0 commit comments