This commit is contained in:
swrup 2025-11-11 02:07:51 +01:00
parent aa2ff7b2f0
commit 2f3113f55d
11742 changed files with 1223940 additions and 0 deletions

View file

@ -0,0 +1,9 @@
(executables
(libraries httpun httpun-lwt-unix httpun_examples base stdio lwt lwt.unix)
(names lwt_get lwt_get_pipelined lwt_post lwt_echo_post lwt_https_get
lwt_https_server))
(alias
(name examples)
(deps
(glob_files *.exe)))

View file

@ -0,0 +1,34 @@
open Base
open Lwt.Infix
module Arg = Stdlib.Arg
open Httpun_lwt_unix
let error_handler (_ : Unix.sockaddr) = Httpun_examples.Server.error_handler
let request_handler (_ : Unix.sockaddr) =
Httpun_examples.Server.echo_post
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)
>|= 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
;;

View file

@ -0,0 +1,45 @@
open Base
open Lwt.Infix
module Arg = Stdlib.Arg
open Httpun
open Httpun_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 =
Httpun_examples.Client.print ~on_eof:(Lwt.wakeup_later notify_finished)
in
let headers = Headers.of_list [ "host", host ] in
Client.create_connection socket >>= fun connection ->
let request_body =
Client.request
connection
~error_handler:Httpun_examples.Client.error_handler
~response_handler
(Request.create ~headers `GET "/")
in
Body.Writer.close request_body;
finished >>= fun () ->
Client.shutdown connection
;;
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)
;;

View file

@ -0,0 +1,61 @@
open Base
open Lwt.Infix
module Arg = Stdlib.Arg
open Httpun
open Httpun_lwt_unix
let error_handler _ = assert false
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 =
Httpun_examples.Client.print ~on_eof:(Lwt.wakeup_later notify_finished)
in
let request_headers =
Request.create ~headers:(Headers.of_list [ "host", host ]) `GET "/"
in
Client.create_connection socket >>= fun connection ->
let request_body =
Client.request
connection
~response_handler
~error_handler
request_headers
in
let finished', notify_finished' = Lwt.wait () in
let response_handler' =
Httpun_examples.Client.print ~on_eof:(Lwt.wakeup_later notify_finished')
in
let request_body' =
Client.request
connection
~response_handler:response_handler'
~error_handler
request_headers
in
Body.Writer.close request_body';
Body.Writer.close request_body;
Lwt.join [finished; finished'] >>= fun () ->
Client.shutdown connection
;;
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)
;;

View file

@ -0,0 +1,45 @@
open Base
open Lwt.Infix
module Arg = Stdlib.Arg
open Httpun
open Httpun_lwt_unix
let error_handler _ = assert false
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 =
Httpun_examples.Client.print ~on_eof:(Lwt.wakeup_later notify_finished)
in
let headers = Headers.of_list [ "host", host ] in
Client.TLS.create_connection_with_default socket >>= fun connection ->
let request_body = Client.TLS.request
connection
~error_handler
~response_handler
(Request.create ~headers `GET "/")
in
Body.Writer.close request_body;
finished
;;
let () =
let host = ref None in
let port = ref 443 in
Arg.parse
["-p", Set_int port, " Port number (443 by default)"]
(fun host_argument -> host := Some host_argument)
"lwt_https_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)
;;

View file

@ -0,0 +1,39 @@
open Base
open Lwt.Infix
module Arg = Stdlib.Arg
open Httpun_lwt_unix
let request_handler (_ : Unix.sockaddr) = Httpun_examples.Server.echo_post
let error_handler (_ : Unix.sockaddr) = Httpun_examples.Server.error_handler
let main port =
let listen_address = Unix.(ADDR_INET (inet_addr_loopback, port)) in
let certfile = "./certificates/server.pem" in
let keyfile = "./certificates/server.key" in
Lwt.async (fun () ->
Lwt_io.establish_server_with_client_socket
listen_address
(Server.TLS.create_connection_handler_with_default
~certfile
~keyfile
~request_handler
~error_handler)
>|= 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:8080\n\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
;;

View file

@ -0,0 +1,54 @@
open Base
open Lwt.Infix
module Arg = Stdlib.Arg
open Httpun
open Httpun_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 =
Httpun_examples.Client.print ~on_eof:(Lwt.wakeup_later notify_finished)
in
let headers =
Headers.of_list
[ "content-length" , (Int.to_string (String.length body))
; "connection" , "close"
; "host" , host
]
in
Client.create_connection socket >>= fun connection ->
let request_body =
Client.request
connection
~error_handler:Httpun_examples.Client.error_handler
~response_handler
(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)
;;