@@ -32,8 +32,8 @@ use termcolor::{Color, ColorSpec};
32
32
33
33
use color:: { build_spec, Printer } ;
34
34
use util:: {
35
- enable_string, encode_link_path, error_io2iron, error_resp, now_string,
36
- system_time_to_date_time, StringError , ROOT_LINK ,
35
+ enable_string, encode_link_path, error_io2iron, error_resp, now_string, root_link ,
36
+ system_time_to_date_time, StringError ,
37
37
} ;
38
38
39
39
use middlewares:: { AuthChecker , CompressionHandler , RequestLogger } ;
@@ -209,6 +209,12 @@ fn main() {
209
209
. long ( "open" )
210
210
. short ( "o" )
211
211
. help ( "Open the page in the default browser" ) )
212
+ . arg ( clap:: Arg :: with_name ( "base-url" )
213
+ . short ( "b" )
214
+ . long ( "base-url" )
215
+ . default_value ( "/" )
216
+ . takes_value ( true )
217
+ . help ( "Base URL to prepend in directory indexes. For reverse proxying. This prefix is supposed to be pre-stripped when reaching simple-http-server." ) )
212
218
. get_matches ( ) ;
213
219
214
220
let root = matches
@@ -273,6 +279,7 @@ fn main() {
273
279
}
274
280
275
281
let silent = matches. is_present ( "silent" ) ;
282
+ let base_url: & str = matches. value_of ( "base-url" ) . unwrap ( ) ;
276
283
277
284
let upload: Option < Upload > = if upload_arg {
278
285
let token: String = thread_rng ( )
@@ -353,6 +360,7 @@ fn main() {
353
360
. map ( |exts| exts. iter ( ) . map ( |s| format ! ( ".{}" , s) ) . collect ( ) ) ,
354
361
try_file_404 : try_file_404. map ( PathBuf :: from) ,
355
362
upload_size_limit,
363
+ base_url : base_url. to_string ( ) ,
356
364
} ) ;
357
365
if cors {
358
366
chain. link_around ( CorsMiddleware :: with_allow_any ( ) ) ;
@@ -376,6 +384,7 @@ fn main() {
376
384
if !silent {
377
385
chain. link_after ( RequestLogger {
378
386
printer : Printer :: new ( ) ,
387
+ base_url : base_url. to_string ( ) ,
379
388
} ) ;
380
389
}
381
390
let mut server = Iron :: new ( chain) ;
@@ -433,6 +442,7 @@ struct MainHandler {
433
442
compress : Option < Vec < String > > ,
434
443
try_file_404 : Option < PathBuf > ,
435
444
upload_size_limit : u64 ,
445
+ base_url : String ,
436
446
}
437
447
438
448
impl Handler for MainHandler {
@@ -475,9 +485,19 @@ impl Handler for MainHandler {
475
485
476
486
if self . upload . is_some ( ) && req. method == method:: Post {
477
487
if let Err ( ( s, msg) ) = self . save_files ( req, & fs_path) {
478
- return Ok ( error_resp ( s, & msg) ) ;
479
- } else {
488
+ return Ok ( error_resp ( s, & msg, & self . base_url ) ) ;
489
+ } else if self . base_url == "/" {
480
490
return Ok ( Response :: with ( ( status:: Found , Redirect ( req. url . clone ( ) ) ) ) ) ;
491
+ } else {
492
+ let mut inner_url: iron:: url:: Url = req. url . clone ( ) . into ( ) ;
493
+ let mut path: & str = inner_url. path ( ) ;
494
+ if path. starts_with ( '/' ) {
495
+ path = & path[ 1 ..] ;
496
+ }
497
+ let new_path = format ! ( "{}{}" , self . base_url, path) ;
498
+ inner_url. set_path ( & new_path) ;
499
+ let new_url = iron:: Url :: from_generic_url ( inner_url) . unwrap ( ) ;
500
+ return Ok ( Response :: with ( ( status:: Found , Redirect ( new_url) ) ) ) ;
481
501
}
482
502
}
483
503
@@ -505,7 +525,7 @@ impl Handler for MainHandler {
505
525
. iter ( )
506
526
. map ( |s| s. to_string_lossy ( ) . to_string ( ) )
507
527
. collect ( ) ;
508
- self . list_directory ( req, & fs_path, & path_prefix)
528
+ self . list_directory ( req, & fs_path, & path_prefix, & self . base_url [ .. ] )
509
529
} else {
510
530
self . send_file ( req, & fs_path)
511
531
}
@@ -602,6 +622,7 @@ impl MainHandler {
602
622
req : & mut Request ,
603
623
fs_path : & Path ,
604
624
path_prefix : & [ String ] ,
625
+ base_url : & str ,
605
626
) -> IronResult < Response > {
606
627
struct Entry {
607
628
filename : String ,
@@ -628,16 +649,17 @@ impl MainHandler {
628
649
let mut bread_links: Vec < String > = vec ! [ breadcrumb. pop( ) . unwrap( ) ] ;
629
650
while !breadcrumb. is_empty ( ) {
630
651
bread_links. push ( format ! (
631
- r#"<a href="/ {link}/"><strong>{label}</strong></a>"# ,
652
+ r#"<a href="{base_url} {link}/"><strong>{label}</strong></a>"# ,
632
653
link = encode_link_path( & breadcrumb) ,
633
654
label = encode_minimal( & breadcrumb. pop( ) . unwrap( ) . to_owned( ) ) ,
655
+ base_url = base_url,
634
656
) ) ;
635
657
}
636
- bread_links. push ( ROOT_LINK . to_owned ( ) ) ;
658
+ bread_links. push ( root_link ( base_url ) ) ;
637
659
bread_links. reverse ( ) ;
638
660
bread_links. join ( " / " )
639
661
} else {
640
- ROOT_LINK . to_owned ( )
662
+ root_link ( base_url )
641
663
} ;
642
664
643
665
// Sort links
@@ -709,16 +731,17 @@ impl MainHandler {
709
731
format ! (
710
732
r#"
711
733
<tr>
712
- <th><a href="/ {link}?sort=name&order={name_order}">Name</a></th>
713
- <th><a href="/ {link}?sort=modified&order={modified_order}">Last modified</a></th>
714
- <th><a href="/ {link}?sort=size&order={size_order}">Size</a></th>
734
+ <th><a href="{base_url} {link}?sort=name&order={name_order}">Name</a></th>
735
+ <th><a href="{base_url} {link}?sort=modified&order={modified_order}">Last modified</a></th>
736
+ <th><a href="{base_url} {link}?sort=size&order={size_order}">Size</a></th>
715
737
</tr>
716
738
<tr><td style="border-top:1px dashed #BBB;" colspan="5"></td></tr>
717
739
"# ,
718
740
link = encode_link_path( & current_link) ,
719
741
name_order = order_labels. get( "name" ) . unwrap_or( & DEFAULT_ORDER ) ,
720
742
modified_order = order_labels. get( "modified" ) . unwrap_or( & DEFAULT_ORDER ) ,
721
- size_order = order_labels. get( "size" ) . unwrap_or( & DEFAULT_ORDER )
743
+ size_order = order_labels. get( "size" ) . unwrap_or( & DEFAULT_ORDER ) ,
744
+ base_url = base_url,
722
745
)
723
746
} else {
724
747
"" . to_owned ( )
@@ -734,12 +757,13 @@ impl MainHandler {
734
757
rows. push ( format ! (
735
758
r#"
736
759
<tr>
737
- <td><a href="/ {link}"><strong>[Up]</strong></a></td>
760
+ <td><a href="{base_url} {link}"><strong>[Up]</strong></a></td>
738
761
<td></td>
739
762
<td></td>
740
763
</tr>
741
764
"# ,
742
- link = encode_link_path( & link)
765
+ link = encode_link_path( & link) ,
766
+ base_url = base_url,
743
767
) ) ;
744
768
} else {
745
769
rows. push ( r#"<tr><td> </td></tr>"# . to_owned ( ) ) ;
@@ -789,7 +813,7 @@ impl MainHandler {
789
813
rows. push ( format ! (
790
814
r#"
791
815
<tr>
792
- <td><a {linkstyle} href="/ {link}">{label}</a></td>
816
+ <td><a {linkstyle} href="{base_url} {link}">{label}</a></td>
793
817
<td style="color:#888;">[{modified}]</td>
794
818
<td><bold>{filesize}</bold></td>
795
819
</tr>
@@ -798,22 +822,24 @@ impl MainHandler {
798
822
link = encode_link_path( & link) ,
799
823
label = encode_minimal( & file_name_label) ,
800
824
modified = file_modified,
801
- filesize = file_size
825
+ filesize = file_size,
826
+ base_url = base_url,
802
827
) ) ;
803
828
}
804
829
805
830
// Optional upload form
806
831
let upload_form = if self . upload . is_some ( ) {
807
832
format ! (
808
833
r#"
809
- <form style="margin-top:1em; margin-bottom:1em;" action="/ {path}" method="POST" enctype="multipart/form-data">
834
+ <form style="margin-top:1em; margin-bottom:1em;" action="{base_url} {path}" method="POST" enctype="multipart/form-data">
810
835
<input type="file" name="files" accept="*" multiple />
811
836
<input type="hidden" name="csrf" value="{csrf}"/>
812
837
<input type="submit" value="Upload" />
813
838
</form>
814
839
"# ,
815
840
path = encode_link_path( path_prefix) ,
816
- csrf = self . upload. as_ref( ) . unwrap( ) . csrf_token
841
+ csrf = self . upload. as_ref( ) . unwrap( ) . csrf_token,
842
+ base_url = base_url,
817
843
)
818
844
} else {
819
845
"" . to_owned ( )
0 commit comments