From 7f6881231243602a98756e253cf9f8e4d366a009 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Sun, 17 Nov 2019 17:15:52 -0600 Subject: [PATCH] feat: allow `delete` and use it in http_of_dir --- src/Tiny_httpd.ml | 7 +++++- src/Tiny_httpd.mli | 1 + src/bin/http_of_dir.ml | 49 +++++++++++++++++++++++++++++------------- 3 files changed, 41 insertions(+), 16 deletions(-) diff --git a/src/Tiny_httpd.ml b/src/Tiny_httpd.ml index f1a94628..e2123f51 100644 --- a/src/Tiny_httpd.ml +++ b/src/Tiny_httpd.ml @@ -46,6 +46,7 @@ module Meth = struct | `PUT | `POST | `HEAD + | `DELETE ] let to_string = function @@ -53,6 +54,7 @@ module Meth = struct | `PUT -> "PUT" | `HEAD -> "HEAD" | `POST -> "POST" + | `DELETE -> "DELETE" let pp out s = Format.pp_print_string out (to_string s) let of_string = function @@ -60,6 +62,7 @@ module Meth = struct | "PUT" -> `PUT | "POST" -> `POST | "HEAD" -> `HEAD + | "DELETE" -> `DELETE | s -> bad_reqf 400 "unknown method %S" s end @@ -388,7 +391,9 @@ let handle_client_ (self:t) (client_sock:Unix.file_descr) : unit = (fun resp cb -> match cb req resp with None -> resp | Some r' -> r') resp self.cb_encode_resp with - | Bad_req _ as e -> raise e + | Bad_req (code,s) -> + continue := false; + Response.make_raw ~code s | e -> Response.fail ~code:500 "server error: %s" (Printexc.to_string e) in diff --git a/src/Tiny_httpd.mli b/src/Tiny_httpd.mli index 574d6c9d..1a00f5b0 100644 --- a/src/Tiny_httpd.mli +++ b/src/Tiny_httpd.mli @@ -5,6 +5,7 @@ module Meth : sig | `PUT | `POST | `HEAD + | `DELETE ] val pp : Format.formatter -> t -> unit diff --git a/src/bin/http_of_dir.ml b/src/bin/http_of_dir.ml index 92a1e75e..800c67f7 100644 --- a/src/bin/http_of_dir.ml +++ b/src/bin/http_of_dir.ml @@ -7,11 +7,13 @@ type config = { mutable port: int; mutable upload: bool; mutable max_upload_size: int; + mutable delete: bool; } let default_config () : config = { addr="127.0.0.1"; port=8080; + delete=false; upload=true; max_upload_size = 10 * 1024 * 1024; } @@ -26,9 +28,10 @@ let contains_dot_dot s = with Exit -> true let header_html = "Content-Type", "text/html" +let (//) = Filename.concat -let html_list_dir ~parent d : string = - let entries = Sys.readdir d in +let html_list_dir ~top ~parent d : string = + let entries = Sys.readdir @@ (top // d) in let body = Buffer.create 256 in Printf.bprintf body "