Skip to content

Commit

Permalink
chore: document cohttp-server-lwt-unix
Browse files Browse the repository at this point in the history
Signed-off-by: Rudi Grinberg <[email protected]>

<!-- ps-id: 5c2c6e6d-4f85-41ae-a877-dbd3e8020e57 -->
  • Loading branch information
rgrinberg committed Jun 27, 2024
1 parent fe388bb commit 679a5a2
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ libraries:
* `Cohttp_curl` uses `libcurl`, via `ocurl`, as backend. It also comes
with lwt (`Cohttp_curl_lwt`) and async backends (`Cohttp_curl_async`).
* `Cohttp_eio` uses `eio` to leverage new features from multicore ocaml 5.0.
* `Cohttp_server_lwt_unix` uses lwt to implement a more efficient web server
with a minimal interface.

You can implement other targets using the parser very easily. Look at the `IO`
signature in `lib/s.mli` and implement that in the desired backend.
Expand Down
68 changes: 68 additions & 0 deletions cohttp-server-lwt-unix/src/cohttp_server_lwt_unix.mli
Original file line number Diff line number Diff line change
@@ -1,34 +1,102 @@
(** High performance lwt server
This module is an alternative to the server offered in
[Cohttp_lwt_unix.Server]. It's a simplified implementation that has less
functionality but offers more control and better performance. The
differences are as follows:
- Vastly improved performance due to optimized buffer handling
- No dependency on conduit
- No builtin logging
An example server: {[
open Lwt.Syntax
let server_callback ctx =
Lwt.join
[
Cohttp_server_lwt_unix.ontext.discard_body ctx;
Cohttp_server_lwt_unix.ontext.respond ctx
(Http.Response.make ())
(Cohttp_server_lwt_unix.Body.string "hello world");
]
let main () =
let* _server =
let listen_address = Unix.(ADDR_INET (inet_addr_loopback, 8080)) in
let server = Cohttp_server_lwt_unix.create server_callback in
Lwt_io.establish_server_with_client_address ~backlog:10_000 listen_address
(fun _addr ch -> Cohttp_server_lwt_unix.handle_connection server ch)
in
let forever, _ = Lwt.wait () in
forever
let () = ignore (Lwt_main.run (main ()))
]}
*)

module Body : sig
module Encoding : sig
type t
(** HTTP body encoding *)

val fixed : int64 -> t
val chunked : t
end

type t
(** A response body *)

val string : ?encoding:Encoding.t -> string -> t
(** [string ?encoding s] respond with body [s].
[?encoding] the encoding to use. by default this is [Encoding.fixed] *)

module Substring : sig
type t = { base : string; pos : int; len : int }
end

val stream : ?encoding:Encoding.t -> (unit -> Substring.t option Lwt.t) -> t
(** [stream ?encoding f] respond with body generated by repeatedly applying
[f]. When [f] returns [None], it will be considered terminated.
[?encoding] is the encoding to use. By deafult this is [Encoding.chunked]. *)
end

module Context : sig
type t
(** A request context *)

val request : t -> Http.Request.t
(** [request t] returns the HTTP request *)

val read_body : t -> string Lwt.t
(** [read_body t] read the request body as a string *)

val discard_body : t -> unit Lwt.t
(** [discard_body t] discard the request body *)

val respond : t -> Http.Response.t -> Body.t -> unit Lwt.t
(** [respond t response body] respond to the request with [response] and
[body] *)
end

type t
(** The type of an HTTP server able to handle requests. *)

val create : ?on_exn:(exn -> unit) -> (Context.t -> unit Lwt.t) -> t
(** [create ?on_exn f] creates an HTTP server that will handle every incoming
request with [f] concurrently.
[on_exn] will be called on exceptions not caught in [f] or raisedd by the
server itself. If [on_exn] isn't provided [Lwt.async_exception_hook] will be
used. *)

val handle_connection :
t -> Lwt_io.input_channel * Lwt_io.output_channel -> unit Lwt.t
(** [handle_connection t (i, o)] will handle all HTTP requests incoming from [i]
and write them to [o].
This function should be used with
[Lwt_io.establish_server_with_client_address] to setup a running HTTP
server. *)

0 comments on commit 679a5a2

Please sign in to comment.