@@ -4,6 +4,7 @@ mod config;
4
4
pub mod hteapot;
5
5
mod logger;
6
6
7
+ use std:: fmt:: format;
7
8
use std:: fs;
8
9
use std:: io;
9
10
use std:: path:: Path ;
@@ -44,12 +45,7 @@ fn serve_proxy(proxy_url: String) -> HttpResponse {
44
45
}
45
46
}
46
47
47
- fn get_mime_tipe ( config : & Config , path : String ) -> String {
48
- let mut path = format ! ( "{}{}" , config. root, path) ;
49
- if Path :: new ( path. as_str ( ) ) . is_dir ( ) {
50
- let separator = if path. ends_with ( '/' ) { "" } else { "/" } ;
51
- path = format ! ( "{}{}{}" , path, separator, config. index) ;
52
- }
48
+ fn get_mime_tipe ( path : & String ) -> String {
53
49
let extension = Path :: new ( path. as_str ( ) )
54
50
. extension ( )
55
51
. unwrap ( )
@@ -60,18 +56,14 @@ fn get_mime_tipe(config: &Config, path: String) -> String {
60
56
"json" => "application/json" ,
61
57
"css" => "text/css" ,
62
58
"html" => "text/html" ,
59
+ "ico" => "image/x-icon" ,
63
60
_ => "text/plain" ,
64
61
} ;
65
62
66
63
mimetipe. to_string ( )
67
64
}
68
65
69
- fn serve_file ( config : & Config , path : String ) -> Option < Vec < u8 > > {
70
- let mut path = format ! ( "{}{}" , config. root, path) ;
71
- if Path :: new ( path. as_str ( ) ) . is_dir ( ) {
72
- let separator = if path. ends_with ( '/' ) { "" } else { "/" } ;
73
- path = format ! ( "{}{}{}" , path, separator, config. index) ;
74
- }
66
+ fn serve_file ( path : & String ) -> Option < Vec < u8 > > {
75
67
let r = fs:: read ( path) ;
76
68
if r. is_ok ( ) {
77
69
Some ( r. unwrap ( ) )
@@ -166,22 +158,32 @@ fn main() {
166
158
if proxy_only || is_proxy. is_some ( ) {
167
159
return serve_proxy ( is_proxy. unwrap ( ) ) ;
168
160
}
169
- if !Path :: new ( req. path . clone ( ) . as_str ( ) ) . exists ( ) {
161
+
162
+ let mut full_path = format ! ( "{}{}" , config. root, req. path. clone( ) ) ;
163
+ if Path :: new ( full_path. as_str ( ) ) . is_dir ( ) {
164
+ let separator = if full_path. ends_with ( '/' ) { "" } else { "/" } ;
165
+ full_path = format ! ( "{}{}{}" , full_path, separator, config. index) ;
166
+ }
167
+ if !Path :: new ( full_path. as_str ( ) ) . exists ( ) {
168
+ logger
169
+ . lock ( )
170
+ . expect ( "this doesnt work :C" )
171
+ . msg ( format ! ( "path {} does not exist" , req. path) ) ;
170
172
return HttpResponse :: new ( HttpStatus :: NotFound , "Not found" , None ) ;
171
173
}
172
- let mimetype = get_mime_tipe ( & config , req . path . clone ( ) ) ;
174
+ let mimetype = get_mime_tipe ( & full_path ) ;
173
175
let content: Option < Vec < u8 > > = if config. cache {
174
176
let mut cachee = cache. lock ( ) . expect ( "Error locking cache" ) ;
175
177
let mut r = cachee. get ( req. path . clone ( ) ) ;
176
178
if r. is_none ( ) {
177
- r = serve_file ( & config , req . path . clone ( ) ) ;
179
+ r = serve_file ( & full_path ) ;
178
180
if r. is_some ( ) {
179
181
cachee. set ( req. path . clone ( ) , r. clone ( ) . unwrap ( ) ) ;
180
182
}
181
183
}
182
184
r
183
185
} else {
184
- serve_file ( & config , req . path )
186
+ serve_file ( & full_path )
185
187
} ;
186
188
match content {
187
189
Some ( c) => HttpResponse :: new ( HttpStatus :: OK , c, headers ! ( "Content-Type" => mimetype) ) ,
0 commit comments