1- use base64:: engine:: general_purpose;
2- use base64:: Engine ;
31use crate :: error:: AppError ;
42use crate :: fs:: generate_directory_listing;
53use crate :: response:: send_response;
64use crate :: utils:: get_request_path;
5+ use base64:: engine:: general_purpose;
6+ use base64:: Engine ;
77use glob:: Pattern ;
88use log:: { debug, error, info, warn} ;
99use std:: collections:: HashMap ;
@@ -14,6 +14,7 @@ use std::path::{Path, PathBuf};
1414use std:: sync:: { Arc , Mutex } ;
1515use std:: time:: Duration ;
1616
17+ #[ allow( clippy:: too_many_arguments) ]
1718pub fn handle_client (
1819 mut stream : TcpStream ,
1920 file_directory : & Arc < Mutex < PathBuf > > ,
@@ -41,6 +42,7 @@ pub fn handle_client(
4142 }
4243}
4344
45+ #[ allow( clippy:: too_many_arguments) ]
4446fn handle_request (
4547 stream : & mut TcpStream ,
4648 file_directory : & Arc < Mutex < PathBuf > > ,
@@ -64,7 +66,7 @@ fn handle_request(
6466 }
6567 } ;
6668
67- debug ! ( "{} Request line: {}" , log_prefix , request_line ) ;
69+ debug ! ( "{log_prefix } Request line: {request_line}" ) ;
6870
6971 let mut headers_map = HashMap :: new ( ) ;
7072 for line in lines_iter {
@@ -77,14 +79,20 @@ fn handle_request(
7779 }
7880 }
7981
80- if let ( Some ( username) , Some ( password) ) = ( username. as_ref ( ) . as_ref ( ) , password. as_ref ( ) . as_ref ( ) ) {
82+ if let ( Some ( username) , Some ( password) ) =
83+ ( username. as_ref ( ) . as_ref ( ) , password. as_ref ( ) . as_ref ( ) )
84+ {
8185 if !authenticate ( & headers_map, username, password) ? {
8286 return Err ( AppError :: Unauthorized ) ;
8387 }
8488 }
8589
8690 let request_path_str = get_request_path ( & request_line) ;
87- let request_path = PathBuf :: from ( request_path_str. strip_prefix ( '/' ) . unwrap_or ( request_path_str) ) ;
91+ let request_path = PathBuf :: from (
92+ request_path_str
93+ . strip_prefix ( '/' )
94+ . unwrap_or ( request_path_str) ,
95+ ) ;
8896
8997 let full_path = file_directory. lock ( ) . unwrap ( ) . join ( & request_path) ;
9098 let canonical_path = match full_path. canonicalize ( ) {
@@ -108,13 +116,7 @@ fn handle_request(
108116 . iter ( )
109117 . any ( |pattern| pattern. matches_path ( & request_path) )
110118 {
111- serve_file (
112- stream,
113- & canonical_path,
114- headers_map,
115- chunk_size,
116- log_prefix,
117- ) ?;
119+ serve_file ( stream, & canonical_path, headers_map, chunk_size, log_prefix) ?;
118120 } else {
119121 warn ! (
120122 "{} File extension not allowed for path: '{}'" ,
@@ -130,33 +132,42 @@ fn handle_request(
130132fn send_error_response ( stream : & mut TcpStream , err : AppError , log_prefix : & str ) {
131133 let ( status_code, status_text, body) = match err {
132134 AppError :: NotFound => ( 404 , "Not Found" , "The requested resource was not found." ) ,
133- AppError :: Forbidden => ( 403 , "Forbidden" , "You do not have permission to access this resource." ) ,
134- AppError :: BadRequest => ( 400 , "Bad Request" , "The server could not understand the request." ) ,
135+ AppError :: Forbidden => (
136+ 403 ,
137+ "Forbidden" ,
138+ "You do not have permission to access this resource." ,
139+ ) ,
140+ AppError :: BadRequest => (
141+ 400 ,
142+ "Bad Request" ,
143+ "The server could not understand the request." ,
144+ ) ,
135145 AppError :: Unauthorized => ( 401 , "Unauthorized" , "Authentication required." ) ,
136146 AppError :: InternalServerError ( ref msg) => ( 500 , "Internal Server Error" , msg. as_str ( ) ) ,
137147 AppError :: Io ( ref e)
138148 if e. kind ( ) == ErrorKind :: ConnectionReset
139149 || e. kind ( ) == ErrorKind :: BrokenPipe
140150 || e. kind ( ) == ErrorKind :: WouldBlock =>
141151 {
142- warn ! (
143- "{} Connection error when sending response: {}" ,
144- log_prefix, e
145- ) ;
152+ warn ! ( "{log_prefix} Connection error when sending response: {e}" ) ;
146153 return ;
147154 }
148- _ => ( 500 , "Internal Server Error" , "An unexpected error occurred." ) ,
155+ _ => (
156+ 500 ,
157+ "Internal Server Error" ,
158+ "An unexpected error occurred." ,
159+ ) ,
149160 } ;
150161
151- error ! ( "{} Responding with error {}: {}" , log_prefix , status_code , status_text ) ;
162+ error ! ( "{log_prefix } Responding with error {status_code }: {status_text}" ) ;
152163
153164 let mut headers = HashMap :: new ( ) ;
154165 if status_code == 401 {
155166 headers. insert ( "WWW-Authenticate" , "Basic realm=\" Restricted\" " ) ;
156167 }
157168
158169 if let Err ( e) = send_response ( stream, status_code, status_text, body, log_prefix) {
159- error ! ( "{} Failed to send error response: {}" , log_prefix , e ) ;
170+ error ! ( "{log_prefix } Failed to send error response: {e}" ) ;
160171 }
161172}
162173
@@ -195,7 +206,11 @@ fn serve_file(
195206 chunk_size : usize ,
196207 log_prefix : & str ,
197208) -> Result < ( ) , AppError > {
198- info ! ( "{} serve_file started for: '{}'" , log_prefix, path. display( ) ) ;
209+ info ! (
210+ "{} serve_file started for: '{}'" ,
211+ log_prefix,
212+ path. display( )
213+ ) ;
199214 let mut file = match File :: open ( path) {
200215 Ok ( f) => f,
201216 Err ( e) if e. kind ( ) == ErrorKind :: NotFound => return Err ( AppError :: NotFound ) ,
@@ -220,13 +235,11 @@ fn serve_file(
220235
221236 let content_length = end_byte - start_byte + 1 ;
222237 let mut response = format ! (
223- "HTTP/1.1 {} {}\r \n Content-Disposition: attachment; filename=\" {}\" \r \n Content-Length: {}\r \n Content-Type: application/octet-stream\r \n Accept-Ranges: bytes\r \n " ,
224- status_code, status_text, filename, content_length
238+ "HTTP/1.1 {status_code} {status_text}\r \n Content-Disposition: attachment; filename=\" {filename}\" \r \n Content-Length: {content_length}\r \n Content-Type: application/octet-stream\r \n Accept-Ranges: bytes\r \n "
225239 ) ;
226240 if status_code == 206 {
227241 response. push_str ( & format ! (
228- "Content-Range: bytes {}-{}/{}\r \n " ,
229- start_byte, end_byte, file_size
242+ "Content-Range: bytes {start_byte}-{end_byte}/{file_size}\r \n "
230243 ) ) ;
231244 }
232245 response. push_str ( "\r \n " ) ;
@@ -246,20 +259,28 @@ fn serve_file(
246259 bytes_remaining -= bytes_read as u64 ;
247260 }
248261
249- info ! ( "{} serve_file finished for: '{}'" , log_prefix, path. display( ) ) ;
262+ info ! (
263+ "{} serve_file finished for: '{}'" ,
264+ log_prefix,
265+ path. display( )
266+ ) ;
250267 Ok ( ( ) )
251268}
252269
253270/// Serves a directory listing as an HTML page.
254- fn serve_directory (
255- stream : & mut TcpStream ,
256- path : & Path ,
257- log_prefix : & str ,
258- ) -> Result < ( ) , AppError > {
259- info ! ( "{} serve_directory started for: '{}'" , log_prefix , path . display ( ) ) ;
271+ fn serve_directory ( stream : & mut TcpStream , path : & Path , log_prefix : & str ) -> Result < ( ) , AppError > {
272+ info ! (
273+ "{} serve_directory started for: '{}'" ,
274+ log_prefix ,
275+ path . display ( )
276+ ) ;
260277 let html = generate_directory_listing ( path, log_prefix) ?;
261278 send_response ( stream, 200 , "OK" , & html, log_prefix) ?;
262- info ! ( "{} serve_directory finished for: '{}'" , log_prefix, path. display( ) ) ;
279+ info ! (
280+ "{} serve_directory finished for: '{}'" ,
281+ log_prefix,
282+ path. display( )
283+ ) ;
263284 Ok ( ( ) )
264285}
265286
0 commit comments