From cdd7df29ace3c035ca55c46ed177cd18c1e28ba8 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Tue, 1 Mar 2022 16:24:40 -0500 Subject: [PATCH] prevent Tiny_httpd_dir.config from being built by hand; provide builder --- src/Tiny_httpd_dir.ml | 16 +++++++++++++++- src/Tiny_httpd_dir.mli | 21 ++++++++++++++++++++- src/bin/http_of_dir.ml | 5 +++-- 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/Tiny_httpd_dir.ml b/src/Tiny_httpd_dir.ml index 3aadfdc6..a86f0404 100644 --- a/src/Tiny_httpd_dir.ml +++ b/src/Tiny_httpd_dir.ml @@ -5,22 +5,36 @@ module Pf = Printf type dir_behavior = | Index | Lists | Index_or_lists | Forbidden +type hidden = unit type config = { mutable download: bool; mutable dir_behavior: dir_behavior; mutable delete: bool; mutable upload: bool; mutable max_upload_size: int; + _rest: hidden } -let default_config () : config = +let default_config_ : config = { download=true; dir_behavior=Forbidden; delete=false; upload=false; max_upload_size = 10 * 1024 * 1024; + _rest=(); } +let default_config () = default_config_ +let config + ?(download=default_config_.download) + ?(dir_behavior=default_config_.dir_behavior) + ?(delete=default_config_.delete) + ?(upload=default_config_.upload) + ?(max_upload_size=default_config_.max_upload_size) + () : config = + { download; dir_behavior; delete; upload; max_upload_size; + _rest=()} + let contains_dot_dot s = try String.iteri diff --git a/src/Tiny_httpd_dir.mli b/src/Tiny_httpd_dir.mli index fbd457d9..4e5cdf77 100644 --- a/src/Tiny_httpd_dir.mli +++ b/src/Tiny_httpd_dir.mli @@ -23,7 +23,12 @@ type dir_behavior = | Forbidden (** Forbid access to directory. This is suited for serving assets, for example. *) -(** configuration for static file handlers *) +type hidden +(** Type used to prevent users from building a config directly. + Use {!default_config} or {!config} instead. *) + +(** configuration for static file handlers. This might get + more fields over time. *) type config = { mutable download: bool; (** Is downloading files allowed? *) @@ -40,6 +45,9 @@ type config = { mutable max_upload_size: int; (** If {!upload} is true, this is the maximum size in bytes for uploaded files. *) + + _rest: hidden; + (** Just ignore this field. *) } (** default configuration: [ @@ -51,6 +59,17 @@ type config = { }] *) val default_config : unit -> config +val config : + ?download:bool -> + ?dir_behavior:dir_behavior -> + ?delete:bool -> + ?upload:bool -> + ?max_upload_size:int -> + unit -> + config +(** Build a config from {!default_config}. + @since NEXT_RELEASE *) + (** [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]. *) diff --git a/src/bin/http_of_dir.ml b/src/bin/http_of_dir.ml index 395efe26..e532ac19 100644 --- a/src/bin/http_of_dir.ml +++ b/src/bin/http_of_dir.ml @@ -20,8 +20,9 @@ let parse_size s : int = with _ -> raise (Arg.Bad "invalid size (expected [kM]?)") let main () = - let config = D.default_config() in - config.dir_behavior <- Index_or_lists; (* keep old behavior *) + let config = + D.config ~dir_behavior:Index_or_lists () + in let dir_ = ref "." in let addr = ref "127.0.0.1" in let port = ref 8080 in