@@ -37,41 +37,74 @@ pub mod hyper;
3737pub struct WsUpgrade < S >
3838 where S : Stream
3939{
40- stream : S ,
41- request : Request ,
40+ pub headers : Headers ,
41+ pub stream : S ,
42+ pub request : Request ,
4243}
4344
4445impl < S > WsUpgrade < S >
4546 where S : Stream
4647{
47- pub fn accept ( self ) -> IoResult < Client < S > > {
48+ pub fn use_protocol < P > ( mut self , protocol : P ) -> Self
49+ where P : Into < String >
50+ {
51+ upsert_header ! ( self . headers; WebSocketProtocol ; {
52+ Some ( protos) => protos. 0 . push( protocol. into( ) ) ,
53+ None => WebSocketProtocol ( vec![ protocol. into( ) ] )
54+ } ) ;
55+ self
56+ }
57+
58+ pub fn use_extension ( mut self , extension : Extension ) -> Self {
59+ upsert_header ! ( self . headers; WebSocketExtensions ; {
60+ Some ( protos) => protos. 0 . push( extension) ,
61+ None => WebSocketExtensions ( vec![ extension] )
62+ } ) ;
63+ self
64+ }
65+
66+ pub fn use_extensions < I > ( mut self , extensions : I ) -> Self
67+ where I : IntoIterator < Item = Extension >
68+ {
69+ let mut extensions: Vec < Extension > =
70+ extensions. into_iter ( ) . collect ( ) ;
71+ upsert_header ! ( self . headers; WebSocketExtensions ; {
72+ Some ( protos) => protos. 0 . append( & mut extensions) ,
73+ None => WebSocketExtensions ( extensions)
74+ } ) ;
75+ self
76+ }
77+
78+ pub fn accept ( self ) -> Result < Client < S > , ( S , IoError ) > {
4879 self . accept_with ( & Headers :: new ( ) )
4980 }
5081
51- pub fn accept_with ( mut self , custom_headers : & Headers ) -> IoResult < Client < S > > {
52- let mut headers = Headers :: new ( ) ;
53- headers. extend ( custom_headers. iter ( ) ) ;
54- headers. set ( WebSocketAccept :: new (
55- // NOTE: we know there is a key because this is a valid request
56- // i.e. to construct this you must go through the validate function
57- self . request . headers . get :: < WebSocketKey > ( ) . unwrap ( )
58- ) ) ;
59- headers. set ( Connection ( vec ! [
82+ pub fn accept_with ( mut self , custom_headers : & Headers ) -> Result < Client < S > , ( S , IoError ) > {
83+ self . headers . extend ( custom_headers. iter ( ) ) ;
84+ self . headers
85+ . set ( WebSocketAccept :: new ( // NOTE: we know there is a key because this is a valid request
86+ // i.e. to construct this you must go through the validate function
87+ self . request . headers . get :: < WebSocketKey > ( ) . unwrap ( ) ) ) ;
88+ self . headers
89+ . set ( Connection ( vec ! [
6090 ConnectionOption :: ConnectionHeader ( UniCase ( "Upgrade" . to_string( ) ) )
6191 ] ) ) ;
62- headers. set ( Upgrade ( vec ! [ Protocol :: new( ProtocolName :: WebSocket , None ) ] ) ) ;
92+ self . headers . set ( Upgrade ( vec ! [ Protocol :: new( ProtocolName :: WebSocket , None ) ] ) ) ;
6393
64- try!( self . send ( StatusCode :: SwitchingProtocols , & headers) ) ;
94+ if let Err ( e) = self . send ( StatusCode :: SwitchingProtocols ) {
95+ return Err ( ( self . stream , e) ) ;
96+ }
6597
66- Ok ( Client :: unchecked ( self . stream ) )
98+ Ok ( Client :: unchecked ( self . stream , self . headers ) )
6799 }
68100
69101 pub fn reject ( self ) -> Result < S , ( S , IoError ) > {
70102 self . reject_with ( & Headers :: new ( ) )
71103 }
72104
73105 pub fn reject_with ( mut self , headers : & Headers ) -> Result < S , ( S , IoError ) > {
74- match self . send ( StatusCode :: BadRequest , headers) {
106+ self . headers . extend ( headers. iter ( ) ) ;
107+ match self . send ( StatusCode :: BadRequest ) {
75108 Ok ( ( ) ) => Ok ( self . stream ) ,
76109 Err ( e) => Err ( ( self . stream , e) ) ,
77110 }
@@ -113,12 +146,12 @@ impl<S> WsUpgrade<S>
113146 self . stream
114147 }
115148
116- fn send ( & mut self , status : StatusCode , headers : & Headers ) -> IoResult < ( ) > {
149+ fn send ( & mut self , status : StatusCode ) -> IoResult < ( ) > {
117150 try!( write ! ( self . stream. writer( ) ,
118151 "{} {}\r \n " ,
119152 self . request. version,
120153 status) ) ;
121- try!( write ! ( self . stream. writer( ) , "{}\r \n " , headers) ) ;
154+ try!( write ! ( self . stream. writer( ) , "{}\r \n " , self . headers) ) ;
122155 Ok ( ( ) )
123156 }
124157}
@@ -173,6 +206,7 @@ impl<S> IntoWs for S
173206 match validate ( & request. subject . 0 , & request. version , & request. headers ) {
174207 Ok ( _) => {
175208 Ok ( WsUpgrade {
209+ headers : Headers :: new ( ) ,
176210 stream : self ,
177211 request : request,
178212 } )
@@ -192,6 +226,7 @@ impl<S> IntoWs for RequestStreamPair<S>
192226 match validate ( & self . 1 . subject . 0 , & self . 1 . version , & self . 1 . headers ) {
193227 Ok ( _) => {
194228 Ok ( WsUpgrade {
229+ headers : Headers :: new ( ) ,
195230 stream : self . 0 ,
196231 request : self . 1 ,
197232 } )
0 commit comments