mirror of
https://github.com/c-cube/tiny_httpd.git
synced 2025-12-05 19:00:32 -05:00
add a form to echo.ml for manual testing
This commit is contained in:
parent
9875543192
commit
731dd7de51
2 changed files with 84 additions and 1 deletions
|
|
@ -12,7 +12,7 @@
|
||||||
(name echo)
|
(name echo)
|
||||||
(flags :standard -warn-error -a+8)
|
(flags :standard -warn-error -a+8)
|
||||||
(modules echo vfs)
|
(modules echo vfs)
|
||||||
(libraries tiny_httpd logs tiny_httpd_camlzip))
|
(libraries tiny_httpd logs tiny_httpd_camlzip tiny_httpd.multipart-form-data))
|
||||||
|
|
||||||
(executable
|
(executable
|
||||||
(name writer)
|
(name writer)
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
open Tiny_httpd_core
|
open Tiny_httpd_core
|
||||||
module Log = Tiny_httpd.Log
|
module Log = Tiny_httpd.Log
|
||||||
|
module MFD = Tiny_httpd_multipart_form_data
|
||||||
|
|
||||||
let now_ = Unix.gettimeofday
|
let now_ = Unix.gettimeofday
|
||||||
|
|
||||||
|
|
@ -78,6 +79,58 @@ let setup_logging () =
|
||||||
Logs.set_reporter @@ Logs.format_reporter ();
|
Logs.set_reporter @@ Logs.format_reporter ();
|
||||||
Logs.set_level ~all:true (Some Logs.Debug)
|
Logs.set_level ~all:true (Some Logs.Debug)
|
||||||
|
|
||||||
|
let setup_upload server : unit =
|
||||||
|
Server.add_route_handler_stream ~meth:`POST server
|
||||||
|
Route.(exact "upload" @/ return)
|
||||||
|
(fun req ->
|
||||||
|
let (`boundary boundary) =
|
||||||
|
match MFD.parse_content_type req.headers with
|
||||||
|
| Some b -> b
|
||||||
|
| None -> Response.fail_raise ~code:400 "no boundary found"
|
||||||
|
in
|
||||||
|
|
||||||
|
let st = MFD.create ~boundary req.body in
|
||||||
|
let tbl = Hashtbl.create 16 in
|
||||||
|
let cur = ref "" in
|
||||||
|
let cur_kind = ref "" in
|
||||||
|
let buf = Buffer.create 16 in
|
||||||
|
let rec loop () =
|
||||||
|
match MFD.next st with
|
||||||
|
| End_of_input ->
|
||||||
|
if !cur <> "" then
|
||||||
|
Hashtbl.add tbl !cur (!cur_kind, Buffer.contents buf)
|
||||||
|
| Part headers ->
|
||||||
|
if !cur <> "" then
|
||||||
|
Hashtbl.add tbl !cur (!cur_kind, Buffer.contents buf);
|
||||||
|
(match MFD.Content_disposition.parse headers with
|
||||||
|
| Some { kind; name = Some name; filename = _ } ->
|
||||||
|
cur := name;
|
||||||
|
cur_kind := kind;
|
||||||
|
Buffer.clear buf;
|
||||||
|
loop ()
|
||||||
|
| _ -> Response.fail_raise ~code:400 "content disposition missing")
|
||||||
|
| Read sl ->
|
||||||
|
Buffer.add_subbytes buf sl.bytes sl.off sl.len;
|
||||||
|
loop ()
|
||||||
|
in
|
||||||
|
loop ();
|
||||||
|
|
||||||
|
let open Tiny_httpd_html in
|
||||||
|
let data =
|
||||||
|
Hashtbl.fold
|
||||||
|
(fun name (kind, data) acc ->
|
||||||
|
Printf.sprintf "%S (kind: %S): %S" name kind data :: acc)
|
||||||
|
tbl []
|
||||||
|
in
|
||||||
|
let html =
|
||||||
|
body []
|
||||||
|
[
|
||||||
|
pre []
|
||||||
|
[ txt (Printf.sprintf "{\n%s\n}" @@ String.concat "\n" data) ];
|
||||||
|
]
|
||||||
|
in
|
||||||
|
Response.make_string ~code:201 @@ Ok (to_string_top html))
|
||||||
|
|
||||||
let () =
|
let () =
|
||||||
let port_ = ref 8080 in
|
let port_ = ref 8080 in
|
||||||
let j = ref 32 in
|
let j = ref 32 in
|
||||||
|
|
@ -198,6 +251,8 @@ let () =
|
||||||
~dir_behavior:Tiny_httpd.Dir.Index_or_lists ())
|
~dir_behavior:Tiny_httpd.Dir.Index_or_lists ())
|
||||||
~vfs:Vfs.vfs ~prefix:"vfs";
|
~vfs:Vfs.vfs ~prefix:"vfs";
|
||||||
|
|
||||||
|
setup_upload server;
|
||||||
|
|
||||||
(* main page *)
|
(* main page *)
|
||||||
Server.add_route_handler server
|
Server.add_route_handler server
|
||||||
Route.(return)
|
Route.(return)
|
||||||
|
|
@ -267,6 +322,34 @@ let () =
|
||||||
txt " (POST) to log out";
|
txt " (POST) to log out";
|
||||||
];
|
];
|
||||||
];
|
];
|
||||||
|
li []
|
||||||
|
[
|
||||||
|
form
|
||||||
|
[
|
||||||
|
A.action "/upload";
|
||||||
|
A.enctype "multipart/form-data";
|
||||||
|
A.target "_self";
|
||||||
|
A.method_ "POST";
|
||||||
|
]
|
||||||
|
[
|
||||||
|
label [] [ txt "my beautiful form" ];
|
||||||
|
input [ A.type_ "file"; A.name "file1" ];
|
||||||
|
input [ A.type_ "file"; A.name "file2" ];
|
||||||
|
input
|
||||||
|
[
|
||||||
|
A.type_ "text";
|
||||||
|
A.name "a";
|
||||||
|
A.placeholder "text A";
|
||||||
|
];
|
||||||
|
input
|
||||||
|
[
|
||||||
|
A.type_ "text";
|
||||||
|
A.name "b";
|
||||||
|
A.placeholder "text B";
|
||||||
|
];
|
||||||
|
input [ A.type_ "submit" ];
|
||||||
|
];
|
||||||
|
];
|
||||||
];
|
];
|
||||||
];
|
];
|
||||||
]
|
]
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue