Skip to content

Instantly share code, notes, and snippets.

@mefyl
Last active September 28, 2020 12:17
Show Gist options
  • Save mefyl/431c69acf32c8e6f205487a8a6ec42e3 to your computer and use it in GitHub Desktop.
Save mefyl/431c69acf32c8e6f205487a8a6ec42e3 to your computer and use it in GitHub Desktop.
Httpaf unresponsiveness minimal test.
open Httpaf
let ( let* ) = Lwt.( >>= )
let string_of_sockaddr = function
| Unix.ADDR_UNIX p -> p
| Unix.ADDR_INET (addr, port) ->
Format.sprintf "%s:%i" (Unix.string_of_inet_addr addr) port
let request_handler sockaddr reqd =
let { Request.meth; target; _ } = Reqd.request reqd in
let () =
Printf.printf "%s: %s %s\n%!"
(string_of_sockaddr sockaddr)
(Method.to_string meth) target
in
let body = target ^ "\n" in
let headers =
Headers.of_list
[
("Content-Length", string_of_int @@ String.length body);
("Connection", "Keep-Alive");
]
in
Reqd.respond_with_string reqd (Response.create ~headers `OK) body
let error_handler _ ?request:_ _ start_response =
let response_body = start_response Headers.empty in
Body.close_writer response_body
let () =
let main =
let handler =
Httpaf_lwt_unix.Server.create_connection_handler ~request_handler
~error_handler
in
let listen_address =
Unix.(ADDR_INET (Unix.inet_addr_of_string "127.0.0.1", 8080))
in
let _server =
Lwt_io.establish_server_with_client_socket listen_address handler
in
fst (Lwt.wait ())
in
Lwt_main.run main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment