[ANN] tiny_httpd 0.14

Bonjour bonjour,

It’s with delectation that I announce the release of tiny_httpd 0.14. Tiny_httpd is a web server library that relies on threads[^1] to handle client connections. Overall Tiny_httpd aims at being self contained, reasonably simple, and performant enough for non-google scale.

This release brings a significant amount of improvements:

  • a Tiny_httpd_io module provides extensible input and output channels (as records of functions)

  • In terms of flexibility, a request handler can now choose to obtain an output channel (from the Tiny_httpd_io module above) into which to write the response’s body; as opposed to only being able to returning a string or a stream (as powerful but the inversion of control isn’t as easy to produce). This means that reading a file to return it can look like this:

    Tiny_httpd_server.(add_route_handler server
        Route.(exact "passwd" @/ return))
      @@ fun _req ->
      (* stream the content of the file *)
      let write oc =
        let buf = Bytes.create 4096 in
        let ic = open_in "/etc/passwd" in
        Fun.protect ~finally:(fun () -> close_in_noerr ic)
        @@ fun () ->
        while
          let n = input ic buf 0 (Bytes.length buf) in
          if n > 0 then IO.Output.output oc buf 0 n;
          n > 0
        do
          ()
        done
      in
    
      let writer = IO.Writer.make ~write () in
      Response.make_writer @@ Ok writer)
    
  • client address is passed to the handler throught the Request.t object;

  • performance was improved by setting TCP_NODELAY on the sockets and by adding a buffer pool to reduce memory churn;

  • some improvements to termination behavior were implemented by @VPhantom, so that the main thread waits for all connections to terminate before returning, and also handling signals better.

Full changelog here.

Cheerio!

9 Likes