code cleanup

This commit is contained in:
Simon Cruanes 2021-12-20 09:53:19 -05:00
parent bfee36572c
commit bf2bf6832d
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
2 changed files with 73 additions and 82 deletions

View file

@ -3,7 +3,7 @@ module U = Tiny_httpd_util
module Pf = Printf
type dir_behavior =
Index | Lists | IndexAndLists | Forbidden
| Index | Lists | Index_or_lists | Forbidden
type config = {
mutable download: bool;
@ -100,15 +100,6 @@ let finally_ ~h x f =
h x;
raise e
(* TODO
let wdays = [|"Sun"; "Mon"; "Tue"; "Wed"; "Thu"; "Fri"; "Sat"|]
let date_of_time (f:float) : string =
let open Unix in
let t = Unix.gmtime f in
Printf.sprintf "%s, %02d %d %d %d:%d:%d GMT"
wdays.(t.tm_yday) t.tm_mday t.tm_mon t.tm_year t.tm_hour t.tm_min t.tm_sec
*)
let add_dir_path ~config ~dir ~prefix server =
if config.delete then (
@ -183,14 +174,14 @@ let add_dir_path ~config ~dir ~prefix server =
let parent = Filename.(dirname path) in
let parent = if parent <> path then Some parent else None in
match config.dir_behavior with
| Index | IndexAndLists when
| Index | Index_or_lists when
Sys.file_exists (full_path // "index.html") ->
(* redirect using path, not full path *)
let new_path = "/" // path // "index.html" in
S._debug (fun k->k "redirect to `%s`" new_path);
S.Response.make_raw ~code:301 ""
~headers:S.Headers.(empty |> set "location" new_path)
| Lists | IndexAndLists ->
| Lists | Index_or_lists ->
let body = html_list_dir ~top:dir path ~parent in
S.Response.make_string
~headers:[header_html; "ETag", Lazy.force mtime]

View file

@ -42,7 +42,7 @@ let main () =
" automatically redirect to index.html if present";
"--list-dir", Unit (fun () -> config.dir_behavior <- Lists),
" automatically lists directory";
"--index-and-list", Unit (fun () -> config.dir_behavior <- IndexAndLists),
"--index-and-list", Unit (fun () -> config.dir_behavior <- Index_or_lists),
" automatically redirect to index.html or lists directory";
"--delete", Unit (fun () -> config.delete <- true), " enable `delete` on files";
"--no-delete", Unit (fun () -> config.delete <- false), " disable `delete` on files";