Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bin/cohttp_server_lwt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,11 @@ let string_of_sockaddr = function
let start_server docroot port host index verbose cert key () =
printf "Listening for HTTP request on: %s %d\n" host port;
let info = sprintf "Served by Cohttp/Lwt listening on %s:%d" host port in
let conn_closed (ch,conn) () =
let conn_closed (ch,conn) =
printf "connection %s closed\n%!"
(Sexplib.Sexp.to_string_hum (Conduit_lwt_unix.sexp_of_flow ch)) in
let callback = handler ~info ~docroot ~verbose ~index in
let config = { Server.callback; conn_closed } in
let config = Server.make ~callback ~conn_closed in
let mode = match cert, key with
| Some c, Some k -> `TLS (`Crt_file_path c, `Key_file_path k, `No_password, `Port port)
| _ -> `TCP (`Port port)
Expand Down
3 changes: 1 addition & 2 deletions lib_test/test_net_lwt_client_and_server.ml
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,10 @@ let make_server () =
Server.respond_string ~status:`OK ~body:"nodrain" ()
|_ -> exit 0
in
let conn_closed _ () = () in
lwt ctx = Conduit_lwt_unix.init ~src:address () in
let ctx = Cohttp_lwt_unix_net.init ~ctx () in
let mode = `TCP (`Port port) in
let config = { Server.callback; conn_closed } in
let config = Server.make ~callback in
Server.create ~ctx ~mode config

let not_none n t fn =
Expand Down
4 changes: 2 additions & 2 deletions lib_test/test_net_lwt_server.ml
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ let make_server () =
let fname = Server.resolve_file ~docroot:"." ~uri:(Request.uri req) in
Server.respond_file ~fname ()
in
let conn_closed (ch,conn_id) () =
let conn_closed (ch,conn_id) =
Printf.eprintf "conn %s closed\n%!" (Connection.to_string conn_id)
in
let config = { Server.callback; conn_closed } in
let config = Server.make ~callback ~conn_closed in
let ctx = Cohttp_lwt_unix_net.init () in
let port = 8081 in
let tcp_mode = `TCP (`Port port) in
Expand Down
30 changes: 17 additions & 13 deletions lwt/cohttp_lwt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,14 @@ module type Server = sig
type ctx with sexp_of
val default_ctx : ctx

type t = {
callback :
(IO.conn * Cohttp.Connection.t) ->
Cohttp.Request.t ->
Cohttp_lwt_body.t ->
(Cohttp.Response.t * Cohttp_lwt_body.t) Lwt.t;
conn_closed:
(IO.conn * Cohttp.Connection.t) -> unit -> unit;
}
type conn = IO.conn * Cohttp.Connection.t

type t

val make : ?conn_closed:(conn -> unit)
-> callback:(conn -> Cohttp.Request.t -> Cohttp_lwt_body.t
-> (Cohttp.Response.t * Cohttp_lwt_body.t) Lwt.t)
-> t

val resolve_local_file : docroot:string -> uri:Uri.t -> string

Expand Down Expand Up @@ -273,16 +272,20 @@ module Make_server(IO:Cohttp.S.IO with type 'a t = 'a Lwt.t)
type ctx = Net.ctx with sexp_of
let default_ctx = Net.default_ctx

type conn = IO.conn * Cohttp.Connection.t

type t = {
callback :
(IO.conn * Cohttp.Connection.t) ->
conn ->
Cohttp.Request.t ->
Cohttp_lwt_body.t ->
(Cohttp.Response.t * Cohttp_lwt_body.t) Lwt.t;
conn_closed:
(IO.conn * Cohttp.Connection.t) -> unit -> unit;
conn_closed: conn -> unit;
}

let make ?(conn_closed=ignore) ~callback =
{ conn_closed ; callback }

module Transfer_IO = Transfer_io.Make(IO)

let resolve_local_file ~docroot ~uri =
Expand Down Expand Up @@ -327,6 +330,7 @@ module Make_server(IO:Cohttp.S.IO with type 'a t = 'a Lwt.t)
let callback spec =
let daemon_callback io_id ic oc =
let conn_id = Connection.create () in
let conn_closed () = spec.conn_closed (io_id,conn_id) in
let read_m = Lwt_mutex.create () in
(* If the request is HTTP version 1.0 then the request stream should be
considered closed after the first request/response. *)
Expand Down Expand Up @@ -372,7 +376,7 @@ module Make_server(IO:Cohttp.S.IO with type 'a t = 'a Lwt.t)
) req_stream in
(* Clean up resources when the response stream terminates and call
* the user callback *)
Lwt_stream.on_terminate res_stream (spec.conn_closed (io_id,conn_id));
Lwt_stream.on_terminate res_stream conn_closed;
(* Transmit the responses *)
for_lwt (res,body) in res_stream do
let flush = Response.flush res in
Expand Down
17 changes: 8 additions & 9 deletions lwt/cohttp_lwt.mli
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,14 @@ module type Server = sig
type ctx with sexp_of
val default_ctx : ctx

type t = {
callback :
(IO.conn * Cohttp.Connection.t) ->
Cohttp.Request.t ->
Cohttp_lwt_body.t ->
(Cohttp.Response.t * Cohttp_lwt_body.t) Lwt.t;
conn_closed:
(IO.conn * Cohttp.Connection.t) -> unit -> unit;
}
type conn = IO.conn * Cohttp.Connection.t

type t
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this, but it breaks a lot of existing code. Will do the upstream fixups before merging.


val make : ?conn_closed:(conn -> unit)
-> callback:(conn -> Cohttp.Request.t -> Cohttp_lwt_body.t
-> (Cohttp.Response.t * Cohttp_lwt_body.t) Lwt.t)
-> t

(** Resolve a URI and a docroot into a concrete local filename. *)
val resolve_local_file : docroot:string -> uri:Uri.t -> string
Expand Down