Minimal HTTP server using good old threads + blocking IO, with a small request router.
Find a file
2019-12-02 21:15:18 -06:00
src feat(http_of_dir): use file to guess mime type of file 2019-12-02 21:15:18 -06:00
.gitignore initial commit 2019-11-13 23:38:38 -06:00
.travis.yml chore: update travis 2019-11-17 16:00:39 -06:00
dune-project initial commit 2019-11-13 23:38:38 -06:00
echo.sh initial commit 2019-11-13 23:38:38 -06:00
http_of_dir.sh wip: simple binary for serving a directory 2019-11-17 11:37:59 -06:00
Makefile initial commit 2019-11-13 23:38:38 -06:00
README.md update doc 2019-11-18 22:09:31 -06:00
tiny_httpd.opam relax opam constraint on dune 2019-11-29 15:17:50 -06:00

Tiny_httpd build status

Minimal HTTP server using good old threads and Scanf for routing.

Free from all forms of ppx, async monads, etc.

The basic echo server from src/examples/echo.ml:


module S = Tiny_httpd

let () =
  let server = S.create () in
  (* say hello *)
  S.add_path_handler ~meth:`GET server
    "/hello/%s@/" (fun name _req -> S.Response.make_ok ("hello " ^name ^"!\n"));
  (* echo request *)
  S.add_path_handler server
    "/echo" (fun req -> S.Response.make_ok (Format.asprintf "echo:@ %a@." S.Request.pp req));
  Printf.printf "listening on http://%s:%d\n%!" (S.addr server) (S.port server);
  match S.run server with
  | Ok () -> ()
  | Error e -> raise e
$ dune exec src/examples/echo.exe &
listening on http://127.0.0.1:8080

# the path "hello/name" greets you.
$ curl -X GET http://localhost:8080/hello/quadrarotaphile
hello quadrarotaphile!

# the path "echo" just prints the request.
$ curl -X GET http://localhost:8080/echo --data "howdy y'all" 
echo:
{meth=GET;
 headers=Host: localhost:8080
         User-Agent: curl/7.66.0
         Accept: */*
         Content-Length: 10
         Content-Type: application/x-www-form-urlencoded;
 path="/echo"; body="howdy y'all"}

http_of_dir

Similar to python -m http.server, a simple program http_of_dir is provided. It serves files from the current directory.

$ http_of_dir . -p 8080 &
$ curl -X GET http://localhost:8080
...
<html list of current dir>
...

Why?

Why not? If you just want a super basic local server (perhaps for exposing data from a local demon, like Cups or Syncthing do), no need for a ton of dependencies or high scalability libraries.

Documentation

See https://c-cube.github.io/tiny_httpd

License

MIT.