@@ -95,7 +95,8 @@ impl fmt::Debug for DebugListRewrite<'_> {
95
95
96
96
pub trait Rewrite : Send + Sync + Any {
97
97
/// Modify RewritablePath as needed.
98
- fn rewrite ( & self , req : & Request < ' _ > , path : FileServerResponse , root : & Path ) -> FileServerResponse ;
98
+ fn rewrite ( & self , req : & Request < ' _ > , path : FileServerResponse , root : & Path )
99
+ -> FileServerResponse ;
99
100
/// Allow multiple of the same rewrite
100
101
fn allow_multiple ( & self ) -> bool { false }
101
102
fn name ( & self ) -> & ' static str { type_name :: < Self > ( ) }
@@ -129,9 +130,12 @@ pub enum FileServerResponse {
129
130
// These might have to remain as basic options (always processed first)
130
131
struct DotFiles ;
131
132
impl Rewrite for DotFiles {
132
- fn rewrite ( & self , _req : & Request < ' _ > , path : FileServerResponse , _root : & Path ) -> FileServerResponse {
133
+ fn rewrite ( & self , _req : & Request < ' _ > , path : FileServerResponse , _root : & Path )
134
+ -> FileServerResponse
135
+ {
133
136
match path {
134
- FileServerResponse :: Hidden { name, reason : HiddenReason :: DotFile } => FileServerResponse :: File { name, modified : None , headers : HeaderMap :: new ( ) } ,
137
+ FileServerResponse :: Hidden { name, reason : HiddenReason :: DotFile } =>
138
+ FileServerResponse :: File { name, modified : None , headers : HeaderMap :: new ( ) } ,
135
139
path => path,
136
140
}
137
141
}
@@ -145,35 +149,37 @@ impl Rewrite for DotFiles {
145
149
146
150
struct Index ( & ' static str ) ;
147
151
impl Rewrite for Index {
148
- fn rewrite ( & self , _req : & Request < ' _ > , path : FileServerResponse , root : & Path ) -> FileServerResponse {
149
- // if path.file_name_path.is_dir() {
150
- // path.file_name_path.push(self.0);
151
- // // TODO: handle file_data_path
152
- // }
152
+ fn rewrite ( & self , _req : & Request < ' _ > , path : FileServerResponse , root : & Path )
153
+ -> FileServerResponse
154
+ {
153
155
match path {
154
- FileServerResponse :: File { name, modified, headers } if root. join ( & name) . is_dir ( ) => FileServerResponse :: File { name : name. join ( self . 0 ) , modified, headers } ,
156
+ FileServerResponse :: File { name, modified, headers } if root. join ( & name) . is_dir ( ) =>
157
+ FileServerResponse :: File { name : name. join ( self . 0 ) , modified, headers } ,
155
158
path => path,
156
159
}
157
160
}
158
161
}
159
- // Actually, curiously, this already just works as-is (the only thing that prevents it is the startup check)
162
+ // Actually, curiously, this already just works as-is (the only thing that prevents
163
+ // it is the startup check)
160
164
struct IndexFile ;
161
165
impl Rewrite for IndexFile {
162
- fn rewrite ( & self , _req : & Request < ' _ > , path : FileServerResponse , _root : & Path ) -> FileServerResponse {
166
+ fn rewrite ( & self , _req : & Request < ' _ > , path : FileServerResponse , _root : & Path )
167
+ -> FileServerResponse
168
+ {
163
169
match path {
164
- // FileServerResponse::File { name, modified, headers } if _root.is_file() && name.iter().count() == 0 => {
165
- // FileServerResponse::File { name, modified, headers }
166
- // }
167
170
path => path,
168
171
}
169
172
}
170
173
}
171
174
172
175
struct NormalizeDirs ;
173
176
impl Rewrite for NormalizeDirs {
174
- fn rewrite ( & self , req : & Request < ' _ > , path : FileServerResponse , root : & Path ) -> FileServerResponse {
177
+ fn rewrite ( & self , req : & Request < ' _ > , path : FileServerResponse , root : & Path )
178
+ -> FileServerResponse
179
+ {
175
180
match path {
176
- FileServerResponse :: File { name, .. } if !req. uri ( ) . path ( ) . ends_with ( '/' ) && root. join ( & name) . is_dir ( ) =>
181
+ FileServerResponse :: File { name, .. } if !req. uri ( ) . path ( ) . ends_with ( '/' ) &&
182
+ root. join ( & name) . is_dir ( ) =>
177
183
FileServerResponse :: PermanentRedirect {
178
184
to : req. uri ( ) . map_path ( |p| format ! ( "{}/" , p) )
179
185
. expect ( "adding a trailing slash to a known good path => valid path" )
@@ -301,10 +307,14 @@ impl FileServer {
301
307
}
302
308
303
309
pub fn rewrite ( mut self , rewrite : impl Rewrite ) -> Self {
304
- if rewrite. allow_multiple ( ) || !self . rewrites . iter ( ) . any ( |f| f. as_ref ( ) . type_id ( ) == rewrite. type_id ( ) ) {
310
+ if rewrite. allow_multiple ( ) ||
311
+ !self . rewrites . iter ( ) . any ( |f| f. as_ref ( ) . type_id ( ) == rewrite. type_id ( ) ) {
305
312
self . rewrites . push ( Arc :: new ( rewrite) ) ;
306
313
} else {
307
- error ! ( "Attempted to insert multiple of the same rewrite `{}` on a FileServer" , rewrite. name( ) ) ;
314
+ error ! (
315
+ "Attempted to insert multiple of the same rewrite `{}` on a FileServer" ,
316
+ rewrite. name( )
317
+ ) ;
308
318
}
309
319
self
310
320
}
@@ -346,8 +356,10 @@ impl Handler for FileServer {
346
356
. and_then ( |segments| segments. to_path_buf_dotfiles ( ) . ok ( ) ) ;
347
357
// .map(|path| self.root.join(path));
348
358
let mut response = match path {
349
- Some ( ( name, false ) ) => FileServerResponse :: File { name, modified : None , headers : HeaderMap :: new ( ) } ,
350
- Some ( ( name, true ) ) => FileServerResponse :: Hidden { name, reason : HiddenReason :: DotFile } ,
359
+ Some ( ( name, false ) ) =>
360
+ FileServerResponse :: File { name, modified : None , headers : HeaderMap :: new ( ) } ,
361
+ Some ( ( name, true ) ) =>
362
+ FileServerResponse :: Hidden { name, reason : HiddenReason :: DotFile } ,
351
363
None => return Outcome :: forward ( data, Status :: NotFound ) ,
352
364
} ;
353
365
println ! ( "initial: {response:?}" ) ;
@@ -372,9 +384,14 @@ impl Handler for FileServer {
372
384
r
373
385
} ) . or_forward ( ( data, Status :: NotFound ) )
374
386
} ,
375
- FileServerResponse :: Hidden { .. } | FileServerResponse :: NotFound { ..} => Outcome :: forward ( data, Status :: NotFound ) ,
376
- FileServerResponse :: PermanentRedirect { to } => Redirect :: permanent ( to) . respond_to ( req) . or_forward ( ( data, Status :: InternalServerError ) ) ,
377
- FileServerResponse :: TemporaryRedirect { to } => Redirect :: temporary ( to) . respond_to ( req) . or_forward ( ( data, Status :: InternalServerError ) ) ,
387
+ FileServerResponse :: Hidden { .. } | FileServerResponse :: NotFound { ..} =>
388
+ Outcome :: forward ( data, Status :: NotFound ) ,
389
+ FileServerResponse :: PermanentRedirect { to } => Redirect :: permanent ( to)
390
+ . respond_to ( req)
391
+ . or_forward ( ( data, Status :: InternalServerError ) ) ,
392
+ FileServerResponse :: TemporaryRedirect { to } => Redirect :: temporary ( to)
393
+ . respond_to ( req)
394
+ . or_forward ( ( data, Status :: InternalServerError ) ) ,
378
395
}
379
396
}
380
397
}
0 commit comments