Documentation in Tiny_httpd_dir.mli + new dir mode IndexAndLists

This commit is contained in:
craff 2021-12-19 19:30:25 -10:00
parent 7e236058f7
commit fe0da80f2f
3 changed files with 36 additions and 13 deletions

View file

@ -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

View file

@ -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 ->

View file

@ -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";