mirror of
https://github.com/c-cube/tiny_httpd.git
synced 2025-12-06 11:15:35 -05:00
code cleanup
This commit is contained in:
parent
bfee36572c
commit
bf2bf6832d
2 changed files with 73 additions and 82 deletions
|
|
@ -3,7 +3,7 @@ module U = Tiny_httpd_util
|
||||||
module Pf = Printf
|
module Pf = Printf
|
||||||
|
|
||||||
type dir_behavior =
|
type dir_behavior =
|
||||||
Index | Lists | IndexAndLists | Forbidden
|
| Index | Lists | Index_or_lists | Forbidden
|
||||||
|
|
||||||
type config = {
|
type config = {
|
||||||
mutable download: bool;
|
mutable download: bool;
|
||||||
|
|
@ -100,15 +100,6 @@ let finally_ ~h x f =
|
||||||
h x;
|
h x;
|
||||||
raise e
|
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 =
|
let add_dir_path ~config ~dir ~prefix server =
|
||||||
|
|
||||||
if config.delete then (
|
if config.delete then (
|
||||||
|
|
@ -183,14 +174,14 @@ let add_dir_path ~config ~dir ~prefix server =
|
||||||
let parent = Filename.(dirname path) in
|
let parent = Filename.(dirname path) in
|
||||||
let parent = if parent <> path then Some parent else None in
|
let parent = if parent <> path then Some parent else None in
|
||||||
match config.dir_behavior with
|
match config.dir_behavior with
|
||||||
| Index | IndexAndLists when
|
| Index | Index_or_lists when
|
||||||
Sys.file_exists (full_path // "index.html") ->
|
Sys.file_exists (full_path // "index.html") ->
|
||||||
(* redirect using path, not full path *)
|
(* redirect using path, not full path *)
|
||||||
let new_path = "/" // path // "index.html" in
|
let new_path = "/" // path // "index.html" in
|
||||||
S._debug (fun k->k "redirect to `%s`" new_path);
|
S._debug (fun k->k "redirect to `%s`" new_path);
|
||||||
S.Response.make_raw ~code:301 ""
|
S.Response.make_raw ~code:301 ""
|
||||||
~headers:S.Headers.(empty |> set "location" new_path)
|
~headers:S.Headers.(empty |> set "location" new_path)
|
||||||
| Lists | IndexAndLists ->
|
| Lists | Index_or_lists ->
|
||||||
let body = html_list_dir ~top:dir path ~parent in
|
let body = html_list_dir ~top:dir path ~parent in
|
||||||
S.Response.make_string
|
S.Response.make_string
|
||||||
~headers:[header_html; "ETag", Lazy.force mtime]
|
~headers:[header_html; "ETag", Lazy.force mtime]
|
||||||
|
|
|
||||||
|
|
@ -42,7 +42,7 @@ let main () =
|
||||||
" automatically redirect to index.html if present";
|
" automatically redirect to index.html if present";
|
||||||
"--list-dir", Unit (fun () -> config.dir_behavior <- Lists),
|
"--list-dir", Unit (fun () -> config.dir_behavior <- Lists),
|
||||||
" automatically lists directory";
|
" 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";
|
" automatically redirect to index.html or lists directory";
|
||||||
"--delete", Unit (fun () -> config.delete <- true), " enable `delete` on files";
|
"--delete", Unit (fun () -> config.delete <- true), " enable `delete` on files";
|
||||||
"--no-delete", Unit (fun () -> config.delete <- false), " disable `delete` on files";
|
"--no-delete", Unit (fun () -> config.delete <- false), " disable `delete` on files";
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue