@@ -286,182 +286,6 @@ impl<State: Send + Sync + 'static> Server<State> {
286
286
}
287
287
}
288
288
289
- /// Asynchronously serve the app at the given address.
290
- #[ cfg( feature = "hyper-server" ) ]
291
- pub async fn listen ( self , addr : impl ToSocketAddrs ) -> std:: io:: Result < ( ) > {
292
- use async_std:: task;
293
- use futures:: prelude:: * ;
294
- use http_service:: Body ;
295
- use std:: convert:: TryInto ;
296
-
297
- /// A type that wraps an `AsyncRead` into a `Stream` of `hyper::Chunk`. Used for writing data to a
298
- /// Hyper response.
299
- struct ChunkStream < R : AsyncRead > {
300
- body : R ,
301
- }
302
-
303
- impl < R : AsyncRead + Unpin > futures:: Stream for ChunkStream < R > {
304
- type Item = Result < Vec < u8 > , Box < dyn std:: error:: Error + Send + Sync + ' static > > ;
305
-
306
- fn poll_next (
307
- mut self : Pin < & mut Self > ,
308
- cx : & mut task:: Context < ' _ > ,
309
- ) -> Poll < Option < Self :: Item > > {
310
- // This is not at all efficient, but that's okay for now.
311
- let mut buf = vec ! [ 0 ; 1024 ] ;
312
- let read = futures:: ready!( Pin :: new( & mut self . body) . poll_read( cx, & mut buf) ) ?;
313
- dbg ! ( read) ;
314
- if read == 0 {
315
- return Poll :: Ready ( None ) ;
316
- } else {
317
- dbg ! ( & buf) ;
318
- Poll :: Ready ( Some ( Ok ( buf. clone ( ) ) ) )
319
- }
320
- }
321
- }
322
-
323
- #[ derive( Copy , Clone ) ]
324
- struct Spawner ;
325
-
326
- impl < F > hyper:: rt:: Executor < F > for Spawner
327
- where
328
- F : Future + Send + ' static ,
329
- F :: Output : Send ,
330
- {
331
- fn execute ( & self , future : F ) {
332
- task:: spawn ( future) ;
333
- }
334
- }
335
-
336
- let http_service = self . into_http_service ( ) ;
337
- let service = Arc :: new ( http_service) ;
338
-
339
- let make_svc = hyper:: service:: make_service_fn ( |_socket : & Stream | {
340
- let service = service. clone ( ) ;
341
- async move {
342
- let connection = service
343
- . connect ( )
344
- . into_future ( )
345
- . await
346
- . map_err ( |_| io:: Error :: from ( io:: ErrorKind :: Other ) ) ?;
347
-
348
- Ok :: < _ , io:: Error > ( hyper:: service:: service_fn (
349
- move |req : http:: Request < hyper:: Body > | {
350
- let service = service. clone ( ) ;
351
-
352
- // Convert Request
353
- let length = req
354
- . headers ( )
355
- . get ( "content-length" )
356
- . and_then ( |v| v. to_str ( ) . ok ( ) )
357
- . and_then ( |v| v. parse ( ) . ok ( ) ) ;
358
-
359
- let req_hyper: http:: Request < Body > = req. map ( |body| {
360
- use futures:: stream:: TryStreamExt ;
361
- let body_stream = body. map ( |chunk| {
362
- chunk. map_err ( |err| {
363
- io:: Error :: new ( io:: ErrorKind :: Other , err. to_string ( ) )
364
- } )
365
- } ) ;
366
- let body_reader = body_stream. into_async_read ( ) ;
367
- Body :: from_reader ( body_reader, length)
368
- } ) ;
369
-
370
- let connection = connection. clone ( ) ;
371
-
372
- // Convert Request
373
- async move {
374
- let req: http_types:: Request =
375
- req_hyper. try_into ( ) . map_err ( |err : url:: ParseError | {
376
- io:: Error :: new ( io:: ErrorKind :: Other , err. to_string ( ) )
377
- } ) ?;
378
-
379
- let res: http_types:: Response = service
380
- . respond ( connection, req)
381
- . into_future ( )
382
- . await
383
- . map_err ( |err| {
384
- io:: Error :: new ( io:: ErrorKind :: Other , err. to_string ( ) )
385
- } ) ?;
386
- let res_hyper = hyper:: Response :: < Body > :: from ( res) ;
387
-
388
- let ( parts, body) = res_hyper. into_parts ( ) ;
389
- let body = hyper:: Body :: wrap_stream ( ChunkStream { body } ) ;
390
-
391
- Ok :: < _ , io:: Error > ( hyper:: Response :: from_parts ( parts, body) )
392
- }
393
- } ,
394
- ) )
395
- }
396
- } ) ;
397
-
398
- let listener = async_std:: net:: TcpListener :: bind ( addr) . await ?;
399
- let addr = format ! ( "http://{}" , listener. local_addr( ) ?) ;
400
- log:: info!( "Server is listening on: {}" , addr) ;
401
-
402
- struct Incoming < ' a > ( async_std:: net:: Incoming < ' a > ) ;
403
- let listener = Incoming ( listener. incoming ( ) ) ;
404
-
405
- struct Stream ( async_std:: net:: TcpStream ) ;
406
-
407
- impl < ' a > hyper:: server:: accept:: Accept for Incoming < ' a > {
408
- type Conn = Stream ;
409
- type Error = async_std:: io:: Error ;
410
-
411
- fn poll_accept (
412
- mut self : Pin < & mut Self > ,
413
- cx : & mut Context < ' _ > ,
414
- ) -> Poll < Option < Result < Self :: Conn , Self :: Error > > > {
415
- use futures_core:: stream:: Stream ;
416
-
417
- match Pin :: new ( & mut self . 0 ) . poll_next ( cx) {
418
- Poll :: Ready ( Some ( s) ) => Poll :: Ready ( Some ( s. map ( Stream ) ) ) ,
419
- Poll :: Ready ( None ) => Poll :: Ready ( None ) ,
420
- Poll :: Pending => Poll :: Pending ,
421
- }
422
- }
423
- }
424
-
425
- impl tokio:: io:: AsyncRead for Stream {
426
- fn poll_read (
427
- mut self : Pin < & mut Self > ,
428
- cx : & mut Context < ' _ > ,
429
- buf : & mut [ u8 ] ,
430
- ) -> Poll < io:: Result < usize > > {
431
- Pin :: new ( & mut self . 0 ) . poll_read ( cx, buf)
432
- }
433
- }
434
-
435
- impl tokio:: io:: AsyncWrite for Stream {
436
- fn poll_write (
437
- mut self : Pin < & mut Self > ,
438
- cx : & mut Context < ' _ > ,
439
- buf : & [ u8 ] ,
440
- ) -> Poll < io:: Result < usize > > {
441
- Pin :: new ( & mut self . 0 ) . poll_write ( cx, buf)
442
- }
443
-
444
- fn poll_flush ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < io:: Result < ( ) > > {
445
- Pin :: new ( & mut self . 0 ) . poll_flush ( cx)
446
- }
447
-
448
- fn poll_shutdown (
449
- mut self : Pin < & mut Self > ,
450
- cx : & mut Context < ' _ > ,
451
- ) -> Poll < io:: Result < ( ) > > {
452
- Pin :: new ( & mut self . 0 ) . poll_close ( cx)
453
- }
454
- }
455
-
456
- let res = hyper:: Server :: builder ( listener)
457
- . executor ( Spawner )
458
- . serve ( make_svc)
459
- . await ;
460
-
461
- res. map_err ( |e| std:: io:: Error :: new ( std:: io:: ErrorKind :: Other , e) ) ?;
462
- Ok ( ( ) )
463
- }
464
-
465
289
/// Asynchronously serve the app at the given address.
466
290
#[ cfg( feature = "h1-server" ) ]
467
291
pub async fn listen ( self , addr : impl ToSocketAddrs ) -> std:: io:: Result < ( ) > {
0 commit comments