1- use super :: { Buffer , HyperIntoWsError , WsUpgrade , Request , validate} ;
1+ use super :: { HyperIntoWsError , WsUpgrade , Request , validate} ;
22use std:: io:: { self , ErrorKind } ;
3- use tokio_io:: codec:: FramedParts ;
3+ use tokio_io:: codec:: { Framed , FramedParts } ;
44use hyper:: header:: Headers ;
5+ use hyper:: http:: h1:: Incoming ;
6+ use hyper:: status:: StatusCode ;
57use stream:: AsyncStream ;
6- use futures:: { Stream , Future } ;
8+ use futures:: { Stream , Sink , Future } ;
9+ use futures:: sink:: Send ;
710use codec:: http:: HttpServerCodec ;
11+ use codec:: ws:: { MessageCodec , Context } ;
812use bytes:: BytesMut ;
913use client:: async:: ClientNew ;
1014
11- impl < S > WsUpgrade < S >
12- where S : AsyncStream
15+ pub type AsyncWsUpgrade < S > = WsUpgrade < S , BytesMut > ;
16+
17+ impl < S > AsyncWsUpgrade < S >
18+ where S : AsyncStream + ' static
1319{
14- pub fn async_accept ( self ) -> Result < ClientNew < S > , ( S , io:: Error ) > {
15- unimplemented ! ( ) ;
20+ pub fn async_accept ( self ) -> ClientNew < S > {
21+ self . internal_async_accept ( None )
22+ }
23+
24+ pub fn async_accept_with ( self , custom_headers : & Headers ) -> ClientNew < S > {
25+ self . internal_async_accept ( Some ( custom_headers) )
26+ }
27+
28+ fn internal_async_accept ( mut self , custom_headers : Option < & Headers > ) -> ClientNew < S > {
29+ let status = self . prepare_headers ( custom_headers) ;
30+ let WsUpgrade { headers, stream, request, buffer } = self ;
31+
32+ let duplex = Framed :: from_parts ( FramedParts {
33+ inner : stream,
34+ readbuf : buffer,
35+ writebuf : BytesMut :: with_capacity ( 0 ) ,
36+ } ,
37+ HttpServerCodec ) ;
38+
39+ let future = duplex. send ( Incoming {
40+ version : request. version ,
41+ subject : status,
42+ headers : headers. clone ( ) ,
43+ } )
44+ . map ( move |s| {
45+ let codec = MessageCodec :: default ( Context :: Client ) ;
46+ let client = Framed :: from_parts ( s. into_parts ( ) , codec) ;
47+ ( client, headers)
48+ } )
49+ . map_err ( |e| e. into ( ) ) ;
50+ Box :: new ( future)
1651 }
1752
18- pub fn async_accept_with (
19- self ,
20- custom_headers : & Headers ,
21- ) -> Result < ClientNew < S > , ( S , io:: Error ) > {
22- unimplemented ! ( ) ;
53+ pub fn async_reject ( self ) -> Send < Framed < S , HttpServerCodec > > {
54+ self . internal_async_reject ( None )
2355 }
2456
25- pub fn async_reject ( self ) -> Result < S , ( S , io :: Error ) > {
26- unimplemented ! ( ) ;
57+ pub fn async_reject_with ( self , headers : & Headers ) -> Send < Framed < S , HttpServerCodec > > {
58+ self . internal_async_reject ( Some ( headers ) )
2759 }
2860
29- pub fn async_reject_with ( self , headers : & Headers ) -> Result < S , ( S , io:: Error ) > {
30- unimplemented ! ( ) ;
61+ fn internal_async_reject (
62+ mut self ,
63+ headers : Option < & Headers > ,
64+ ) -> Send < Framed < S , HttpServerCodec > > {
65+ if let Some ( custom) = headers {
66+ self . headers . extend ( custom. iter ( ) ) ;
67+ }
68+ let duplex = Framed :: from_parts ( FramedParts {
69+ inner : self . stream ,
70+ readbuf : self . buffer ,
71+ writebuf : BytesMut :: with_capacity ( 0 ) ,
72+ } ,
73+ HttpServerCodec ) ;
74+ duplex. send ( Incoming {
75+ version : self . request . version ,
76+ subject : StatusCode :: BadRequest ,
77+ headers : self . headers ,
78+ } )
3179 }
3280}
3381
@@ -44,46 +92,42 @@ pub trait AsyncIntoWs {
4492 ///
4593 /// Note: this is the asynchronous version, meaning it will not block when
4694 /// trying to read a request.
47- fn into_ws ( self ) -> Box < Future < Item = WsUpgrade < Self :: Stream > , Error = Self :: Error > > ;
95+ fn into_ws ( self ) -> Box < Future < Item = AsyncWsUpgrade < Self :: Stream > , Error = Self :: Error > > ;
4896}
4997
5098impl < S > AsyncIntoWs for S
5199 where S : AsyncStream + ' static
52100{
53101 type Stream = S ;
54- type Error = ( S , Option < Request > , Option < BytesMut > , HyperIntoWsError ) ;
102+ type Error = ( S , Option < Request > , BytesMut , HyperIntoWsError ) ;
55103
56- fn into_ws ( self ) -> Box < Future < Item = WsUpgrade < Self :: Stream > , Error = Self :: Error > > {
104+ fn into_ws ( self ) -> Box < Future < Item = AsyncWsUpgrade < Self :: Stream > , Error = Self :: Error > > {
57105 let future = self . framed ( HttpServerCodec )
58106 . into_future ( )
59107 . map_err ( |( e, s) | {
60108 let FramedParts { inner, readbuf, .. } = s. into_parts ( ) ;
61- ( inner, None , Some ( readbuf) , e. into ( ) )
109+ ( inner, None , readbuf, e. into ( ) )
62110 } )
63111 . and_then ( |( m, s) | {
64112 let FramedParts { inner, readbuf, .. } = s. into_parts ( ) ;
65113 if let Some ( msg) = m {
66114 match validate ( & msg. subject . 0 , & msg. version , & msg. headers ) {
67115 Ok ( ( ) ) => Ok ( ( msg, inner, readbuf) ) ,
68- Err ( e) => Err ( ( inner, None , Some ( readbuf) , e) ) ,
116+ Err ( e) => Err ( ( inner, None , readbuf, e) ) ,
69117 }
70118 } else {
71119 let err = HyperIntoWsError :: Io ( io:: Error :: new (
72120 ErrorKind :: ConnectionReset ,
73121 "Connection dropped before handshake could be read" ) ) ;
74- Err ( ( inner, None , Some ( readbuf) , err) )
122+ Err ( ( inner, None , readbuf, err) )
75123 }
76124 } )
77125 . map ( |( m, stream, buffer) | {
78126 WsUpgrade {
79127 headers : Headers :: new ( ) ,
80128 stream : stream,
81129 request : m,
82- buffer : Some ( Buffer {
83- buf : unimplemented ! ( ) ,
84- pos : 0 ,
85- cap : buffer. capacity ( ) ,
86- } ) ,
130+ buffer : buffer,
87131 }
88132 } ) ;
89133 Box :: new ( future)
0 commit comments