@@ -8,35 +8,28 @@ use crate::util::timeout_in;
88use crate :: router:: r#final:: Router ;
99use crate :: { Request , Response } ;
1010
11- #[ cfg( feature="ws" ) ]
12- use crate :: __rt__:: TcpStream ;
13-
14- pub ( crate ) struct Session < S > {
11+ pub ( crate ) struct Session < C > {
1512 router : Arc < Router > ,
16- connection : S ,
13+ connection : C ,
1714 ip : std:: net:: IpAddr ,
1815}
1916
20- # [ cfg ( feature= "ws" ) ]
21- pub ( crate ) trait WebSocketUpgradeable {
22- fn into_websocket_stream ( self ) -> Result < TcpStream , & ' static str > ;
17+ pub ( crate ) trait Connection : AsyncRead + AsyncWrite + Unpin {
18+ # [ cfg ( feature= "ws" ) ]
19+ fn into_websocket_stream ( self ) -> Result < crate :: __rt__ :: TcpStream , & ' static str > ;
2320}
2421
25- # [ cfg ( feature= "ws" ) ]
26- impl WebSocketUpgradeable for TcpStream {
27- fn into_websocket_stream ( self ) -> Result < TcpStream , & ' static str > {
22+ impl Connection for crate :: __rt__ :: TcpStream {
23+ # [ cfg ( feature= "ws" ) ]
24+ fn into_websocket_stream ( self ) -> Result < crate :: __rt__ :: TcpStream , & ' static str > {
2825 Ok ( self )
2926 }
3027}
3128
32- #[ cfg( feature="ws" ) ]
33- impl < S > Session < S >
34- where
35- S : AsyncRead + AsyncWrite + Unpin + WebSocketUpgradeable ,
36- {
29+ impl < C : Connection > Session < C > {
3730 pub ( crate ) fn new (
3831 router : Arc < Router > ,
39- connection : S ,
32+ connection : C ,
4033 ip : std:: net:: IpAddr
4134 ) -> Self {
4235 Self {
@@ -85,14 +78,15 @@ where
8578 }
8679 }
8780 } ) . await {
88- None => crate :: WARNING !( "[WARNING] \
81+ None => crate :: WARNING !( "\
8982 Session timeouted. In Ohkami, Keep-Alive timeout \
9083 is set to 42 seconds by default and is configurable \
9184 by `OHKAMI_KEEPALIVE_TIMEOUT` environment variable.\
9285 ") ,
9386
9487 Some ( Upgrade :: None ) => crate :: DEBUG !( "about to shutdown connection" ) ,
9588
89+ #[ cfg( feature="ws" ) ]
9690 Some ( Upgrade :: WebSocket ( ws) ) => {
9791 match self . connection . into_websocket_stream ( ) {
9892 Ok ( tcp_stream) => {
10397 tcp_stream
10498 ) . await ;
10599 if aborted {
106- crate :: WARNING !( "[WARNING] \
100+ crate :: WARNING !( "\
107101 WebSocket session aborted by timeout. In Ohkami, \
108102 WebSocket timeout is set to 3600 seconds (1 hour) \
109103 by default and is configurable by `OHKAMI_WEBSOCKET_TIMEOUT` \
@@ -114,78 +108,10 @@ where
114108 crate :: DEBUG !( "WebSocket session finished" ) ;
115109 }
116110 Err ( msg) => {
117- crate :: WARNING !( "[WARNING] {}" , msg ) ;
111+ crate :: WARNING !( "{msg}" ) ;
118112 }
119113 }
120114 }
121115 }
122116 }
123117}
124-
125- // There has to be some cleaner implementation to apply the conditional trait bounds in this...
126- #[ cfg( not( feature="ws" ) ) ]
127- impl < S > Session < S >
128- where
129- S : AsyncRead + AsyncWrite + Unpin ,
130- {
131- pub ( crate ) fn new (
132- router : Arc < Router > ,
133- connection : S ,
134- ip : std:: net:: IpAddr
135- ) -> Self {
136- Self {
137- router,
138- connection,
139- ip
140- }
141- }
142-
143- pub ( crate ) async fn manage ( mut self ) {
144- #[ cold] #[ inline( never) ]
145- fn panicking ( panic : Box < dyn Any + Send > ) -> Response {
146- if let Some ( msg) = panic. downcast_ref :: < String > ( ) {
147- crate :: WARNING !( "[Panicked]: {msg}" ) ;
148- } else if let Some ( msg) = panic. downcast_ref :: < & str > ( ) {
149- crate :: WARNING !( "[Panicked]: {msg}" ) ;
150- } else {
151- crate :: WARNING !( "[Panicked]" ) ;
152- }
153- crate :: Response :: InternalServerError ( )
154- }
155-
156- match timeout_in ( Duration :: from_secs ( crate :: CONFIG . keepalive_timeout ( ) ) , async {
157- let mut req = Request :: init ( self . ip ) ;
158- let mut req = unsafe { Pin :: new_unchecked ( & mut req) } ;
159- loop {
160- req. clear ( ) ;
161- match req. as_mut ( ) . read ( & mut self . connection ) . await {
162- Ok ( Some ( ( ) ) ) => {
163- let close = matches ! ( req. headers. Connection ( ) , Some ( "close" | "Close" ) ) ;
164-
165- let res = match catch_unwind ( AssertUnwindSafe ( {
166- let req = req. as_mut ( ) ;
167- || self . router . handle ( req. get_mut ( ) )
168- } ) ) {
169- Ok ( future) => future. await ,
170- Err ( panic) => panicking ( panic) ,
171- } ;
172- let upgrade = res. send ( & mut self . connection ) . await ;
173-
174- if !upgrade. is_none ( ) { break upgrade}
175- if close { break Upgrade :: None }
176- }
177- Ok ( None ) => break Upgrade :: None ,
178- Err ( res) => { res. send ( & mut self . connection ) . await ; } ,
179- }
180- }
181- } ) . await {
182- None => crate :: WARNING !( "[WARNING] \
183- Session timeouted. In Ohkami, Keep-Alive timeout \
184- is set to 42 seconds by default and is configurable \
185- by `OHKAMI_KEEPALIVE_TIMEOUT` environment variable.\
186- ") ,
187-
188- Some ( Upgrade :: None ) => crate :: DEBUG !( "about to shutdown connection" ) ,
189- }
190- }
191- }
0 commit comments