mirror of
https://github.com/c-cube/tiny_httpd.git
synced 2025-12-06 19:25:32 -05:00
Documentation in Tiny_httpd_dir.mli + new dir mode IndexAndLists
This commit is contained in:
parent
7e236058f7
commit
fe0da80f2f
3 changed files with 36 additions and 13 deletions
|
|
@ -3,7 +3,7 @@ module U = Tiny_httpd_util
|
||||||
module Pf = Printf
|
module Pf = Printf
|
||||||
|
|
||||||
type dir_behavior =
|
type dir_behavior =
|
||||||
Index | Forbidden | Lists
|
Index | Lists | IndexAndLists | Forbidden
|
||||||
|
|
||||||
type config = {
|
type config = {
|
||||||
mutable download: bool;
|
mutable download: bool;
|
||||||
|
|
@ -183,21 +183,19 @@ 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 ->
|
| Index | IndexAndLists when
|
||||||
if Sys.file_exists (full_path // "index.html") then (
|
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)
|
||||||
else
|
| Lists | IndexAndLists ->
|
||||||
S.Response.fail_raise ~code:403 "Cannot access file"
|
|
||||||
| 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]
|
||||||
(Ok body)
|
(Ok body)
|
||||||
| Forbidden ->
|
| Forbidden | Index ->
|
||||||
S.Response.make_raw ~code:405 "listing dir not allowed"
|
S.Response.make_raw ~code:405 "listing dir not allowed"
|
||||||
) else (
|
) else (
|
||||||
try
|
try
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,16 @@
|
||||||
|
|
||||||
|
(** behavior of static directory *)
|
||||||
type dir_behavior =
|
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 = {
|
type config = {
|
||||||
mutable download: bool;
|
mutable download: bool;
|
||||||
mutable dir_behavior: dir_behavior;
|
mutable dir_behavior: dir_behavior;
|
||||||
|
|
@ -10,8 +19,18 @@ type config = {
|
||||||
mutable max_upload_size: int;
|
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
|
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 ->
|
val add_dir_path : config:config ->
|
||||||
dir:string ->
|
dir:string ->
|
||||||
prefix:string ->
|
prefix:string ->
|
||||||
|
|
|
||||||
|
|
@ -34,10 +34,16 @@ let main () =
|
||||||
"--debug", Unit (fun () -> S._enable_debug true), " debug mode";
|
"--debug", Unit (fun () -> S._enable_debug true), " debug mode";
|
||||||
"--upload", Unit (fun () -> config.upload <- true), " enable file uploading";
|
"--upload", Unit (fun () -> config.upload <- true), " enable file uploading";
|
||||||
"--no-upload", Unit (fun () -> config.upload <- false), " disable 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),
|
"--max-upload", String (fun i -> config.max_upload_size <- parse_size i),
|
||||||
" maximum size of files that can be uploaded";
|
" maximum size of files that can be uploaded";
|
||||||
"--auto-index", Unit (fun () -> config.dir_behavior <- Index),
|
"--auto-index", Unit (fun () -> config.dir_behavior <- Index),
|
||||||
" automatically redirect to index.html if present";
|
" 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";
|
"--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";
|
||||||
"-j", Set_int j, " maximum number of simultaneous connections";
|
"-j", Set_int j, " maximum number of simultaneous connections";
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue