@@ -155,6 +155,8 @@ where
155155 conn_id : id,
156156 logger : logger. clone ( ) ,
157157 max_connections : self . cfg . max_connections ,
158+ enable_http : self . cfg . enable_http ,
159+ enable_ws : self . cfg . enable_ws ,
158160 } ;
159161 process_connection ( & self . service_builder , & connection_guard, data, socket, & mut connections) ;
160162 id = id. wrapping_add ( 1 ) ;
@@ -193,6 +195,10 @@ struct Settings {
193195 tokio_runtime : Option < tokio:: runtime:: Handle > ,
194196 /// The interval at which `Ping` frames are submitted.
195197 ping_interval : Duration ,
198+ /// Enable HTTP.
199+ enable_http : bool ,
200+ /// Enable WS.
201+ enable_ws : bool ,
196202}
197203
198204impl Default for Settings {
@@ -207,6 +213,8 @@ impl Default for Settings {
207213 allow_hosts : AllowHosts :: Any ,
208214 tokio_runtime : None ,
209215 ping_interval : Duration :: from_secs ( 60 ) ,
216+ enable_http : true ,
217+ enable_ws : true ,
210218 }
211219 }
212220}
@@ -425,6 +433,26 @@ impl<B, L> Builder<B, L> {
425433 }
426434 }
427435
436+ /// Configure the server to only serve JSON-RPC HTTP requests.
437+ ///
438+ /// Default: both http and ws are enabled.
439+ pub fn http_only ( mut self ) -> Self {
440+ self . settings . enable_http = true ;
441+ self . settings . enable_ws = false ;
442+ self
443+ }
444+
445+ /// Configure the server to only serve JSON-RPC WebSocket requests.
446+ ///
447+ /// That implies that server just denies HTTP requests which isn't a WebSocket upgrade request
448+ ///
449+ /// Default: both http and ws are enabled.
450+ pub fn ws_only ( mut self ) -> Self {
451+ self . settings . enable_http = false ;
452+ self . settings . enable_ws = true ;
453+ self
454+ }
455+
428456 /// Finalize the configuration of the server. Consumes the [`Builder`].
429457 ///
430458 /// ```rust
@@ -547,6 +575,10 @@ pub(crate) struct ServiceData<L: Logger> {
547575 pub ( crate ) logger : L ,
548576 /// Handle to hold a `connection permit`.
549577 pub ( crate ) conn : Arc < OwnedSemaphorePermit > ,
578+ /// Enable HTTP.
579+ pub ( crate ) enable_http : bool ,
580+ /// Enable WS.
581+ pub ( crate ) enable_ws : bool ,
550582}
551583
552584/// JsonRPSee service compatible with `tower`.
@@ -589,7 +621,9 @@ impl<L: Logger> hyper::service::Service<hyper::Request<hyper::Body>> for TowerSe
589621 return async { Ok ( http:: response:: host_not_allowed ( ) ) } . boxed ( ) ;
590622 }
591623
592- if is_upgrade_request ( & request) {
624+ let is_upgrade_request = is_upgrade_request ( & request) ;
625+
626+ if self . inner . enable_ws && is_upgrade_request {
593627 let mut server = soketto:: handshake:: http:: Server :: new ( ) ;
594628
595629 let response = match server. receive_request ( & request) {
@@ -626,7 +660,7 @@ impl<L: Logger> hyper::service::Service<hyper::Request<hyper::Body>> for TowerSe
626660 } ;
627661
628662 async { Ok ( response) } . boxed ( )
629- } else {
663+ } else if self . inner . enable_http && !is_upgrade_request {
630664 // The request wasn't an upgrade request; let's treat it as a standard HTTP request:
631665 let data = http:: HandleRequest {
632666 methods : self . inner . methods . clone ( ) ,
@@ -643,6 +677,8 @@ impl<L: Logger> hyper::service::Service<hyper::Request<hyper::Body>> for TowerSe
643677 self . inner . logger . on_connect ( self . inner . remote_addr , & request, TransportProtocol :: Http ) ;
644678
645679 Box :: pin ( http:: handle_request ( request, data) . map ( Ok ) )
680+ } else {
681+ Box :: pin ( async { http:: response:: denied ( ) } . map ( Ok ) )
646682 }
647683 }
648684}
@@ -730,6 +766,10 @@ struct ProcessConnection<L> {
730766 conn_id : u32 ,
731767 /// Logger.
732768 logger : L ,
769+ /// Allow JSON-RPC HTTP requests.
770+ enable_http : bool ,
771+ /// Allow JSON-RPC WS request and WS upgrade requests.
772+ enable_ws : bool ,
733773}
734774
735775#[ instrument( name = "connection" , skip_all, fields( remote_addr = %cfg. remote_addr, conn_id = %cfg. conn_id) , level = "INFO" ) ]
@@ -787,6 +827,8 @@ fn process_connection<'a, L: Logger, B, U>(
787827 conn_id : cfg. conn_id ,
788828 logger : cfg. logger ,
789829 conn : Arc :: new ( conn) ,
830+ enable_http : cfg. enable_http ,
831+ enable_ws : cfg. enable_ws ,
790832 } ,
791833 } ;
792834
0 commit comments