This commit is contained in:
parent
aa2ff7b2f0
commit
2f3113f55d
11742 changed files with 1223940 additions and 0 deletions
9
unikernel/duniverse/ocaml-h1/examples/lwt/dune
Normal file
9
unikernel/duniverse/ocaml-h1/examples/lwt/dune
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
(executables
|
||||
(libraries h1 h1-lwt-unix h1_examples base stdio lwt lwt.unix)
|
||||
(optional true)
|
||||
(names lwt_get lwt_post lwt_echo_post lwt_echo_upgrade lwt_chunked))
|
||||
|
||||
(alias
|
||||
(name runtest)
|
||||
(package h1-lwt-unix)
|
||||
(deps (glob_files *.exe)))
|
||||
40
unikernel/duniverse/ocaml-h1/examples/lwt/lwt_chunked.ml
Normal file
40
unikernel/duniverse/ocaml-h1/examples/lwt/lwt_chunked.ml
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
open Base
|
||||
open Lwt.Infix
|
||||
module Arg = Stdlib.Arg
|
||||
|
||||
open H1_lwt_unix
|
||||
|
||||
let request_handler (_ : Unix.sockaddr) reqd =
|
||||
let body = H1.Reqd.respond_with_streaming reqd (H1.Response.create ~headers:(H1.Headers.of_list ["connection", "close"]) `OK) in
|
||||
let rec respond_loop i =
|
||||
H1.Body.Writer.write_string body (Printf.sprintf "Chunk %i\n" i);
|
||||
H1.Body.Writer.flush_with_reason body (function
|
||||
| `Closed -> Stdio.print_endline "closed"
|
||||
| `Written -> Stdio.print_endline "written"; Lwt.bind (Lwt_unix.sleep 5.) (fun () -> respond_loop (i+1)) |> ignore
|
||||
);
|
||||
Lwt.return_unit
|
||||
in ignore (respond_loop 0)
|
||||
|
||||
let error_handler (_ : Unix.sockaddr) = H1_examples.Server.error_handler
|
||||
|
||||
let main port =
|
||||
let listen_address = Unix.(ADDR_INET (inet_addr_loopback, port)) in
|
||||
Lwt.async (fun () ->
|
||||
Lwt_io.establish_server_with_client_socket
|
||||
listen_address
|
||||
(Server.create_connection_handler ~upgrade_handler:None ~request_handler ~error_handler)
|
||||
>|= fun _server ->
|
||||
Stdio.printf "Listening on port %i.\n" port);
|
||||
let forever, _ = Lwt.wait () in
|
||||
Lwt_main.run forever
|
||||
;;
|
||||
|
||||
let () =
|
||||
Stdlib.Sys.set_signal Stdlib.Sys.sigpipe Stdlib.Sys.Signal_ignore;
|
||||
let port = ref 8080 in
|
||||
Arg.parse
|
||||
["-p", Arg.Set_int port, " Listening port number (8080 by default)"]
|
||||
ignore
|
||||
"Echoes POST requests. Runs forever.";
|
||||
main !port
|
||||
;;
|
||||
36
unikernel/duniverse/ocaml-h1/examples/lwt/lwt_echo_post.ml
Normal file
36
unikernel/duniverse/ocaml-h1/examples/lwt/lwt_echo_post.ml
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
open Base
|
||||
open Lwt.Infix
|
||||
module Arg = Stdlib.Arg
|
||||
|
||||
open H1_lwt_unix
|
||||
|
||||
let request_handler (_ : Unix.sockaddr) = H1_examples.Server.echo_post
|
||||
let error_handler (_ : Unix.sockaddr) = H1_examples.Server.error_handler
|
||||
|
||||
let main port =
|
||||
let listen_address = Unix.(ADDR_INET (inet_addr_loopback, port)) in
|
||||
Lwt.async (fun () ->
|
||||
Lwt_io.establish_server_with_client_socket
|
||||
listen_address
|
||||
(Server.create_connection_handler
|
||||
~request_handler
|
||||
~error_handler
|
||||
~upgrade_handler:None)
|
||||
>|= fun _server ->
|
||||
Stdio.printf "Listening on port %i and echoing POST requests.\n" port;
|
||||
Stdio.printf "To send a POST request, try one of the following\n\n";
|
||||
Stdio.printf " echo \"Testing echo POST\" | dune exec examples/async/async_post.exe\n";
|
||||
Stdio.printf " echo \"Testing echo POST\" | dune exec examples/lwt/lwt_post.exe\n";
|
||||
Stdio.printf " echo \"Testing echo POST\" | curl -XPOST --data @- http://localhost:%d\n\n%!" port);
|
||||
let forever, _ = Lwt.wait () in
|
||||
Lwt_main.run forever
|
||||
;;
|
||||
|
||||
let () =
|
||||
let port = ref 8080 in
|
||||
Arg.parse
|
||||
["-p", Arg.Set_int port, " Listening port number (8080 by default)"]
|
||||
ignore
|
||||
"Echoes POST requests. Runs forever.";
|
||||
main !port
|
||||
;;
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
open Base
|
||||
open Lwt.Infix
|
||||
module Arg = Stdlib.Arg
|
||||
|
||||
open H1_lwt_unix
|
||||
|
||||
let request_handler (_ : Unix.sockaddr) = H1_examples.Server.upgrade
|
||||
let error_handler (_ : Unix.sockaddr) = H1_examples.Server.error_handler
|
||||
|
||||
let upgrade_handler (_ : Unix.sockaddr) (fd : Lwt_unix.file_descr) =
|
||||
let input = Lwt_io.of_fd fd ~mode:Input in
|
||||
let output = Lwt_io.of_fd fd ~mode:Output in
|
||||
let rec loop () =
|
||||
Lwt_io.read input ~count:4096
|
||||
>>= function
|
||||
| "" -> Lwt.return_unit
|
||||
| data -> Lwt_io.write output data >>= loop
|
||||
in
|
||||
loop ()
|
||||
;;
|
||||
|
||||
let main port =
|
||||
let listen_address = Unix.(ADDR_INET (inet_addr_loopback, port)) in
|
||||
Lwt.async (fun () ->
|
||||
Lwt_io.establish_server_with_client_socket
|
||||
listen_address
|
||||
(Server.create_connection_handler
|
||||
~request_handler
|
||||
~error_handler
|
||||
~upgrade_handler:(Some upgrade_handler))
|
||||
>|= fun _server ->
|
||||
Stdio.printf "Listening on port %i, upgrading, and echoing data.\n" port;
|
||||
Stdio.printf "To send an interactive upgrade request, try\n\n";
|
||||
Stdio.printf " examples/script/upgrade-connect\n%!");
|
||||
let forever, _ = Lwt.wait () in
|
||||
Lwt_main.run forever
|
||||
;;
|
||||
|
||||
let () =
|
||||
let port = ref 8080 in
|
||||
Arg.parse
|
||||
["-p", Arg.Set_int port, " Listening port number (8080 by default)"]
|
||||
ignore
|
||||
"Echoes POST requests. Runs forever.";
|
||||
main !port
|
||||
;;
|
||||
|
||||
43
unikernel/duniverse/ocaml-h1/examples/lwt/lwt_get.ml
Normal file
43
unikernel/duniverse/ocaml-h1/examples/lwt/lwt_get.ml
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
open Base
|
||||
open Lwt.Infix
|
||||
module Arg = Stdlib.Arg
|
||||
|
||||
open H1
|
||||
open H1_lwt_unix
|
||||
|
||||
let main port host =
|
||||
Lwt_unix.getaddrinfo host (Int.to_string port) [Unix.(AI_FAMILY PF_INET)]
|
||||
>>= fun addresses ->
|
||||
let socket = Lwt_unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
|
||||
Lwt_unix.connect socket (List.hd_exn addresses).Unix.ai_addr
|
||||
>>= fun () ->
|
||||
let finished, notify_finished = Lwt.wait () in
|
||||
let response_handler =
|
||||
H1_examples.Client.print ~on_eof:(Lwt.wakeup_later notify_finished)
|
||||
in
|
||||
let headers = Httpun_types.Headers.of_list [ "host", host ] in
|
||||
let request_body =
|
||||
Client.request
|
||||
~error_handler:H1_examples.Client.error_handler
|
||||
~response_handler
|
||||
socket
|
||||
(Request.create ~headers `GET "/")
|
||||
in
|
||||
Body.Writer.close request_body;
|
||||
finished
|
||||
;;
|
||||
|
||||
let () =
|
||||
let host = ref None in
|
||||
let port = ref 80 in
|
||||
Arg.parse
|
||||
["-p", Set_int port, " Port number (80 by default)"]
|
||||
(fun host_argument -> host := Some host_argument)
|
||||
"lwt_get.exe [-p N] HOST";
|
||||
let host =
|
||||
match !host with
|
||||
| None -> failwith "No hostname provided"
|
||||
| Some host -> host
|
||||
in
|
||||
Lwt_main.run (main !port host)
|
||||
;;
|
||||
53
unikernel/duniverse/ocaml-h1/examples/lwt/lwt_post.ml
Normal file
53
unikernel/duniverse/ocaml-h1/examples/lwt/lwt_post.ml
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
open Base
|
||||
open Lwt.Infix
|
||||
module Arg = Stdlib.Arg
|
||||
|
||||
open H1
|
||||
open H1_lwt_unix
|
||||
|
||||
let main port host =
|
||||
Lwt_io.(read stdin)
|
||||
>>= fun body ->
|
||||
Lwt_unix.getaddrinfo host (Int.to_string port) [Unix.(AI_FAMILY PF_INET)]
|
||||
>>= fun addresses ->
|
||||
let socket = Lwt_unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
|
||||
Lwt_unix.connect socket (List.hd_exn addresses).Unix.ai_addr
|
||||
>>= fun () ->
|
||||
let finished, notify_finished = Lwt.wait () in
|
||||
let response_handler =
|
||||
H1_examples.Client.print ~on_eof:(Lwt.wakeup_later notify_finished)
|
||||
in
|
||||
let headers =
|
||||
Httpun_types.Headers.of_list
|
||||
[ "content-length" , (Int.to_string (String.length body))
|
||||
; "connection" , "close"
|
||||
; "host" , host
|
||||
]
|
||||
in
|
||||
let request_body =
|
||||
Client.request
|
||||
~error_handler:H1_examples.Client.error_handler
|
||||
~response_handler
|
||||
socket
|
||||
(Request.create ~headers `POST "/")
|
||||
in
|
||||
Body.Writer.write_string request_body body;
|
||||
Body.Writer.close request_body;
|
||||
finished
|
||||
;;
|
||||
|
||||
let () =
|
||||
let host = ref None in
|
||||
let port = ref 8080 in
|
||||
|
||||
Arg.parse
|
||||
["-p", Set_int port, " Port number (8080 by default)"]
|
||||
(fun host_argument -> host := Some host_argument)
|
||||
"lwt_get.exe [-p N] HOST";
|
||||
let host =
|
||||
match !host with
|
||||
| None -> failwith "No hostname provided"
|
||||
| Some host -> host
|
||||
in
|
||||
Lwt_main.run (main !port host)
|
||||
;;
|
||||
Loading…
Add table
Add a link
Reference in a new issue