From fe0da80f2fc678e9fd1eaec94dfd60d8608f98db Mon Sep 17 00:00:00 2001 From: craff Date: Sun, 19 Dec 2021 19:30:25 -1000 Subject: [PATCH] Documentation in Tiny_httpd_dir.mli + new dir mode IndexAndLists --- src/Tiny_httpd_dir.ml | 22 ++++++++++------------ src/Tiny_httpd_dir.mli | 21 ++++++++++++++++++++- src/bin/http_of_dir.ml | 6 ++++++ 3 files changed, 36 insertions(+), 13 deletions(-) diff --git a/src/Tiny_httpd_dir.ml b/src/Tiny_httpd_dir.ml index f659bbaa..d7edcae9 100644 --- a/src/Tiny_httpd_dir.ml +++ b/src/Tiny_httpd_dir.ml @@ -3,7 +3,7 @@ module U = Tiny_httpd_util module Pf = Printf type dir_behavior = - Index | Forbidden | Lists + Index | Lists | IndexAndLists | Forbidden type config = { mutable download: bool; @@ -183,21 +183,19 @@ 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 -> - if Sys.file_exists (full_path // "index.html") then ( - (* 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)) - else - S.Response.fail_raise ~code:403 "Cannot access file" - | Lists -> + | Index | IndexAndLists 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 -> let body = html_list_dir ~top:dir path ~parent in S.Response.make_string ~headers:[header_html; "ETag", Lazy.force mtime] (Ok body) - | Forbidden -> + | Forbidden | Index -> S.Response.make_raw ~code:405 "listing dir not allowed" ) else ( try diff --git a/src/Tiny_httpd_dir.mli b/src/Tiny_httpd_dir.mli index a5cbf213..877b7d77 100644 --- a/src/Tiny_httpd_dir.mli +++ b/src/Tiny_httpd_dir.mli @@ -1,7 +1,16 @@ +(** behavior of static directory *) type dir_behavior = - Index | Forbidden | Lists + | Index + (** Redirect to index.html if present *) + | Lists + (** Lists content of directory *) + | IndexAndLists + (** Redirect to index.html if present and Lists content otherwise *) + | Forbidden + (** Forbid access to directory *) +(** configuration for static file handlers *) type config = { mutable download: bool; mutable dir_behavior: dir_behavior; @@ -10,8 +19,18 @@ type config = { mutable max_upload_size: int; } +(** default configuration: [ + { download=true + ; dir_behavior=Forbidden + ; delete=false + ; upload=false + ; max_upload_size = 10 * 1024 * 1024 + }] *) val default_config : unit -> config +(** [add_dirpath ~config ~dir ~prefix server] adds route handle to the + [server] to serve static files in [dir] when url starts with [prefix], + using the given configuration [config]. *) val add_dir_path : config:config -> dir:string -> prefix:string -> diff --git a/src/bin/http_of_dir.ml b/src/bin/http_of_dir.ml index 252cfd10..17c44f64 100644 --- a/src/bin/http_of_dir.ml +++ b/src/bin/http_of_dir.ml @@ -34,10 +34,16 @@ let main () = "--debug", Unit (fun () -> S._enable_debug true), " debug mode"; "--upload", Unit (fun () -> config.upload <- true), " enable file uploading"; "--no-upload", Unit (fun () -> config.upload <- false), " disable file uploading"; + "--download", Unit (fun () -> config.download <- true), " enable file downloading"; + "--no-download", Unit (fun () -> config.download <- false), " disable file downloading"; "--max-upload", String (fun i -> config.max_upload_size <- parse_size i), " maximum size of files that can be uploaded"; "--auto-index", Unit (fun () -> config.dir_behavior <- Index), " 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), + " 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"; "-j", Set_int j, " maximum number of simultaneous connections";