@@ -438,7 +438,7 @@ impl Server {
438438 ///
439439 /// # Arguments
440440 ///
441- /// - `H: AsRef<str>` - The host address.
441+ /// - `AsRef<str>` - The host address.
442442 /// - `u16` - The port number.
443443 ///
444444 /// # Returns
@@ -577,50 +577,79 @@ impl Server {
577577 } ;
578578 }
579579
580- /// Creates and binds a `TcpListener` based on the server's configuration .
580+ /// Configures socket options for a newly accepted `TcpStream` .
581581 ///
582- /// # Returns
582+ /// This applies settings like `TCP_NODELAY`, and `IP_TTL` from the server's configuration.
583583 ///
584- /// - `Result<TcpListener, ServerError>` - A `Result` containing the bound `TcpListener` on success,
585- /// or a `ServerError` on failure.
586- async fn create_tcp_listener ( & self ) -> Result < TcpListener , ServerError > {
587- Ok ( TcpListener :: bind ( self . get_server_config ( ) . get_address ( ) ) . await ?)
584+ /// # Arguments
585+ ///
586+ /// - `&TcpStream` - A reference to the `TcpStream` to configure.
587+ async fn configure_stream ( & self , stream : & TcpStream ) {
588+ let config: ServerConfig = self . get_server_config ( ) . clone ( ) ;
589+ if let Some ( nodelay) = config. try_get_nodelay ( ) {
590+ let _ = stream. set_nodelay ( * nodelay) ;
591+ }
592+ if let Some ( ttl) = config. try_get_ttl ( ) {
593+ let _ = stream. set_ttl ( * ttl) ;
594+ }
588595 }
589596
590- /// Enters a loop to accept incoming TCP connections and spawn handlers for them .
597+ /// Executes trait-based request middleware in sequence .
591598 ///
592599 /// # Arguments
593600 ///
594- /// - `&TcpListener ` - A reference to the `TcpListener` to accept connections from .
601+ /// - `&mut Context ` - The request context .
595602 ///
596603 /// # Returns
597604 ///
598- /// - `Result<(), ServerError> ` - A `Result` which is typically `Ok(())` unless an unrecoverable
599- /// error occurs.
600- async fn accept_connections ( & self , tcp_listener : & TcpListener ) -> Result < ( ) , ServerError > {
601- while let Ok ( ( stream , _ ) ) = tcp_listener . accept ( ) . await {
602- self . configure_stream ( & stream ) . await ;
603- let stream : ArcRwLockStream = ArcRwLockStream :: from_stream ( stream ) ;
604- self . spawn_connection_handler ( stream ) . await ;
605+ /// - `bool ` - `true` if the lifecycle was aborted, `false` otherwise.
606+ pub ( super ) async fn handle_request_middleware ( & self , ctx : & mut Context ) -> bool {
607+ for hook in self . get_request_middleware ( ) . iter ( ) {
608+ self . task_handler ( ctx , hook , true ) . await ;
609+ if ctx . get_aborted ( ) {
610+ return true ;
611+ }
605612 }
606- Ok ( ( ) )
613+ false
607614 }
608615
609- /// Configures socket options for a newly accepted `TcpStream`.
610- ///
611- /// This applies settings like `TCP_NODELAY`, and `IP_TTL` from the server's configuration.
616+ /// Executes a trait-based route hook if one matches.
612617 ///
613618 /// # Arguments
614619 ///
615- /// - `&TcpStream` - A reference to the `TcpStream` to configure.
616- async fn configure_stream ( & self , stream : & TcpStream ) {
617- let config: ServerConfig = self . get_server_config ( ) . clone ( ) ;
618- if let Some ( nodelay) = config. try_get_nodelay ( ) {
619- let _ = stream. set_nodelay ( * nodelay) ;
620+ /// - `&mut Context` - The request context.
621+ /// - `&str` - The request path to match.
622+ ///
623+ /// # Returns
624+ ///
625+ /// - `bool` - `true` if the lifecycle was aborted, `false` otherwise.
626+ pub ( super ) async fn handle_route_matcher ( & self , ctx : & mut Context , path : & str ) -> bool {
627+ if let Some ( hook) = self . get_route_matcher ( ) . try_resolve_route ( ctx, path) {
628+ self . task_handler ( ctx, & hook, true ) . await ;
629+ if ctx. get_aborted ( ) {
630+ return true ;
631+ }
620632 }
621- if let Some ( ttl) = config. try_get_ttl ( ) {
622- let _ = stream. set_ttl ( * ttl) ;
633+ false
634+ }
635+
636+ /// Executes trait-based response middleware in sequence.
637+ ///
638+ /// # Arguments
639+ ///
640+ /// - `&mut Context` - The request context.
641+ ///
642+ /// # Returns
643+ ///
644+ /// - `bool` - `true` if the lifecycle was aborted, `false` otherwise.
645+ pub ( super ) async fn handle_response_middleware ( & self , ctx : & mut Context ) -> bool {
646+ for hook in self . get_response_middleware ( ) . iter ( ) {
647+ self . task_handler ( ctx, hook, true ) . await ;
648+ if ctx. get_aborted ( ) {
649+ return true ;
650+ }
623651 }
652+ false
624653 }
625654
626655 /// Spawns a new asynchronous task to handle a single client connection.
@@ -655,26 +684,6 @@ impl Server {
655684 ctx. set_aborted ( true ) . set_closed ( true ) ;
656685 }
657686
658- /// Handles a single client connection, determining whether it's an HTTP or WebSocket request.
659- ///
660- /// It reads the initial request from the stream and dispatches it to the appropriate hook.
661- ///
662- /// # Arguments
663- ///
664- /// - `ArcRwLockStream` - The stream for the client connection.
665- async fn handle_connection ( & self , stream : ArcRwLockStream ) {
666- match Request :: http_from_stream ( & stream, self . get_request_config ( ) ) . await {
667- Ok ( request) => {
668- let server_address: usize = self . into ( ) ;
669- let hook: HandlerState = HandlerState :: new ( stream, server_address. into ( ) ) ;
670- self . handle_http_requests ( & hook, & request) . await ;
671- }
672- Err ( error) => {
673- self . handle_request_error ( & mut stream. into ( ) , & error) . await ;
674- }
675- }
676- }
677-
678687 /// The core request handling pipeline.
679688 ///
680689 /// This function orchestrates the execution of request middleware, the route hook,
@@ -732,62 +741,53 @@ impl Server {
732741 }
733742 }
734743
735- /// Executes trait-based request middleware in sequence.
736- ///
737- /// # Arguments
744+ /// Handles a single client connection, determining whether it's an HTTP or WebSocket request.
738745 ///
739- /// - `&mut Context` - The request context .
746+ /// It reads the initial request from the stream and dispatches it to the appropriate hook .
740747 ///
741- /// # Returns
748+ /// # Arguments
742749 ///
743- /// - `bool` - `true` if the lifecycle was aborted, `false` otherwise.
744- pub ( super ) async fn handle_request_middleware ( & self , ctx : & mut Context ) -> bool {
745- for hook in self . get_request_middleware ( ) . iter ( ) {
746- self . task_handler ( ctx, hook, true ) . await ;
747- if ctx. get_aborted ( ) {
748- return true ;
750+ /// - `ArcRwLockStream` - The stream for the client connection.
751+ async fn handle_connection ( & self , stream : ArcRwLockStream ) {
752+ match Request :: http_from_stream ( & stream, self . get_request_config ( ) ) . await {
753+ Ok ( request) => {
754+ let server_address: usize = self . into ( ) ;
755+ let hook: HandlerState = HandlerState :: new ( stream, server_address. into ( ) ) ;
756+ self . handle_http_requests ( & hook, & request) . await ;
757+ }
758+ Err ( error) => {
759+ self . handle_request_error ( & mut stream. into ( ) , & error) . await ;
749760 }
750761 }
751- false
752762 }
753763
754- /// Executes a trait-based route hook if one matches .
764+ /// Enters a loop to accept incoming TCP connections and spawn handlers for them .
755765 ///
756766 /// # Arguments
757767 ///
758- /// - `&mut Context` - The request context.
759- /// - `&str` - The request path to match.
768+ /// - `&TcpListener` - A reference to the `TcpListener` to accept connections from.
760769 ///
761770 /// # Returns
762771 ///
763- /// - `bool ` - `true` if the lifecycle was aborted, `false` otherwise.
764- pub ( super ) async fn handle_route_matcher ( & self , ctx : & mut Context , path : & str ) -> bool {
765- if let Some ( hook ) = self . get_route_matcher ( ) . try_resolve_route ( ctx , path ) {
766- self . task_handler ( ctx , & hook , true ) . await ;
767- if ctx . get_aborted ( ) {
768- return true ;
769- }
772+ /// - `Result<(), ServerError> ` - A `Result` which is typically `Ok(())` unless an unrecoverable
773+ /// error occurs.
774+ async fn accept_connections ( & self , tcp_listener : & TcpListener ) -> Result < ( ) , ServerError > {
775+ while let Ok ( ( stream , _ ) ) = tcp_listener . accept ( ) . await {
776+ self . configure_stream ( & stream ) . await ;
777+ let stream : ArcRwLockStream = ArcRwLockStream :: from_stream ( stream ) ;
778+ self . spawn_connection_handler ( stream ) . await ;
770779 }
771- false
780+ Ok ( ( ) )
772781 }
773782
774- /// Executes trait-based response middleware in sequence.
775- ///
776- /// # Arguments
777- ///
778- /// - `&mut Context` - The request context.
783+ /// Creates and binds a `TcpListener` based on the server's configuration.
779784 ///
780785 /// # Returns
781786 ///
782- /// - `bool` - `true` if the lifecycle was aborted, `false` otherwise.
783- pub ( super ) async fn handle_response_middleware ( & self , ctx : & mut Context ) -> bool {
784- for hook in self . get_response_middleware ( ) . iter ( ) {
785- self . task_handler ( ctx, hook, true ) . await ;
786- if ctx. get_aborted ( ) {
787- return true ;
788- }
789- }
790- false
787+ /// - `Result<TcpListener, ServerError>` - A `Result` containing the bound `TcpListener` on success,
788+ /// or a `ServerError` on failure.
789+ async fn create_tcp_listener ( & self ) -> Result < TcpListener , ServerError > {
790+ Ok ( TcpListener :: bind ( self . get_server_config ( ) . get_address ( ) ) . await ?)
791791 }
792792
793793 /// Starts the server, binds to the configured address, and begins listening for connections.
0 commit comments