prevent Tiny_httpd_dir.config from being built by hand; provide builder

This commit is contained in:
Simon Cruanes 2022-03-01 16:24:40 -05:00
parent cafa2a5420
commit cdd7df29ac
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
3 changed files with 38 additions and 4 deletions

View file

@ -5,22 +5,36 @@ module Pf = Printf
type dir_behavior = type dir_behavior =
| Index | Lists | Index_or_lists | Forbidden | Index | Lists | Index_or_lists | Forbidden
type hidden = unit
type config = { type config = {
mutable download: bool; mutable download: bool;
mutable dir_behavior: dir_behavior; mutable dir_behavior: dir_behavior;
mutable delete: bool; mutable delete: bool;
mutable upload: bool; mutable upload: bool;
mutable max_upload_size: int; mutable max_upload_size: int;
_rest: hidden
} }
let default_config () : config = let default_config_ : config =
{ download=true; { download=true;
dir_behavior=Forbidden; dir_behavior=Forbidden;
delete=false; delete=false;
upload=false; upload=false;
max_upload_size = 10 * 1024 * 1024; 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 = let contains_dot_dot s =
try try
String.iteri String.iteri

View file

@ -23,7 +23,12 @@ type dir_behavior =
| Forbidden | Forbidden
(** Forbid access to directory. This is suited for serving assets, for example. *) (** 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 = { type config = {
mutable download: bool; mutable download: bool;
(** Is downloading files allowed? *) (** Is downloading files allowed? *)
@ -40,6 +45,9 @@ type config = {
mutable max_upload_size: int; mutable max_upload_size: int;
(** If {!upload} is true, this is the maximum size in bytes for (** If {!upload} is true, this is the maximum size in bytes for
uploaded files. *) uploaded files. *)
_rest: hidden;
(** Just ignore this field. *)
} }
(** default configuration: [ (** default configuration: [
@ -51,6 +59,17 @@ type config = {
}] *) }] *)
val default_config : unit -> 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 (** [add_dirpath ~config ~dir ~prefix server] adds route handle to the
[server] to serve static files in [dir] when url starts with [prefix], [server] to serve static files in [dir] when url starts with [prefix],
using the given configuration [config]. *) using the given configuration [config]. *)

View file

@ -20,8 +20,9 @@ let parse_size s : int =
with _ -> raise (Arg.Bad "invalid size (expected <int>[kM]?)") with _ -> raise (Arg.Bad "invalid size (expected <int>[kM]?)")
let main () = let main () =
let config = D.default_config() in let config =
config.dir_behavior <- Index_or_lists; (* keep old behavior *) D.config ~dir_behavior:Index_or_lists ()
in
let dir_ = ref "." in let dir_ = ref "." in
let addr = ref "127.0.0.1" in let addr = ref "127.0.0.1" in
let port = ref 8080 in let port = ref 8080 in