1
1
use std:: io:: { Read , Write } ;
2
2
use std:: net:: { TcpListener , TcpStream } ;
3
- use std:: path:: { Path , PathBuf } ;
3
+ use std:: path:: PathBuf ;
4
4
use std:: thread;
5
5
6
6
use itertools:: Itertools ;
@@ -88,13 +88,15 @@ fn handle_connection(mut stream: TcpStream, path: Option<PathBuf>) -> Result<(),
88
88
stream. write ( "HTTP/1.1 200 OK\r \n \r \n " . as_bytes ( ) ) . unwrap ( ) ;
89
89
} else if req. path . starts_with ( "/echo/" ) {
90
90
let txt = & req. path [ 6 ..] ;
91
- send_text_content ( & mut stream, txt) . unwrap ( ) ;
91
+ send_text_content ( & mut stream, txt) ? ;
92
92
} else if req. path . starts_with ( "/files/" ) {
93
93
let path = path. unwrap_or_else ( || PathBuf :: from ( "." ) ) ;
94
94
let file_path = path. join ( & req. path [ 7 ..] ) ;
95
95
96
- let file = std:: fs:: read_to_string ( file_path) ?;
97
- send_content ( & mut stream, "application/octet-stream" , file) ;
96
+ match std:: fs:: read_to_string ( file_path) {
97
+ Ok ( file) => send_text_content ( & mut stream, & file) ?,
98
+ Err ( _) => not_found ( & mut stream) ?,
99
+ }
98
100
} else if req. path == "/user-agent" {
99
101
let ua = req
100
102
. headers
@@ -103,7 +105,7 @@ fn handle_connection(mut stream: TcpStream, path: Option<PathBuf>) -> Result<(),
103
105
. map ( |( _, v) | v)
104
106
. unwrap_or ( & "Unknown" ) ;
105
107
106
- send_text_content ( & mut stream, ua) . unwrap ( ) ;
108
+ send_text_content ( & mut stream, ua) ? ;
107
109
} else {
108
110
not_found ( & mut stream) ?;
109
111
}
@@ -116,6 +118,7 @@ fn handle_connection(mut stream: TcpStream, path: Option<PathBuf>) -> Result<(),
116
118
}
117
119
118
120
#[ derive( Debug ) ]
121
+ #[ allow( dead_code) ]
119
122
pub struct Request < ' a > {
120
123
method : & ' a str ,
121
124
path : & ' a str ,
0 commit comments