Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cohttp expose closefn function #1036

Open
wants to merge 3 commits into
base: v5-backports
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions cohttp-lwt-jsoo/src/cohttp_lwt_jsoo.ml
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ struct

let callv ?ctx:_ _uri _reqs = Lwt.fail Cohttp_lwt_xhr_callv_not_implemented

let call_with_closefn ?ctx:_ ?headers:_ ?body:_ ?chunked:_ _meth _uri =
Copy link
Author

Choose a reason for hiding this comment

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

Here we ask for the advice of the maintainers of the library, as we advocate that this function will never be called from this library, but we want to receive a second opinion.

assert false

(* ??? *)
end

Expand Down
10 changes: 7 additions & 3 deletions cohttp-lwt/src/client.ml
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ module Make (IO : S.IO) (Net : S.Net with module IO = IO) = struct
| `DELETE -> false
| _ -> true

let call ?(ctx = Net.default_ctx) ?headers ?(body = `Empty) ?chunked meth uri
=
let call_with_closefn ?(ctx = Net.default_ctx) ?headers ?(body = `Empty)
?chunked meth uri =
let headers = match headers with None -> Header.init () | Some h -> h in
Net.connect_uri ~ctx uri >>= fun (_conn, ic, oc) ->
let closefn () = Net.close ic oc in
Expand Down Expand Up @@ -88,7 +88,11 @@ module Make (IO : S.IO) (Net : S.Net with module IO = IO) = struct
|> fun t ->
Lwt.on_cancel t closefn;
Lwt.on_failure t (fun _exn -> closefn ());
t
Lwt.return (t, closefn)

let call ?(ctx = Net.default_ctx) ?headers ?(body = `Empty) ?chunked meth uri
=
call_with_closefn ~ctx ?headers ~body ?chunked meth uri >>= fun (t, _) -> t

(* The HEAD should not have a response body *)
let head ?ctx ?headers uri = call ?ctx ?headers `HEAD uri >|= fst
Expand Down
16 changes: 15 additions & 1 deletion cohttp-lwt/src/s.ml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,21 @@ module type Client = sig
(using [ocaml-tls]) or SSL (using [ocaml-ssl]), on [*:443] or on the
specified port by the user. If neitehr [ocaml-tls] or [ocaml-ssl] are
installed on the system, [cohttp]/[conduit] tries the usual ([*:80]) or
the specified port by the user in a non-secured way. *)
the specified port by the user in a non-secured way.

The function returns response and body. *)

val call_with_closefn :
?ctx:ctx ->
?headers:Cohttp.Header.t ->
?body:Body.t ->
?chunked:bool ->
Cohttp.Code.meth ->
Uri.t ->
((Cohttp.Response.t * Body.t) Lwt.t * (unit -> unit)) Lwt.t
(** [call_with_closefn ?ctx ?headers ?body ?chunked meth uri] is the same as
[call] but returns response, body and [close_fn] which force releases the
connection. *)

val head :
?ctx:ctx -> ?headers:Cohttp.Header.t -> Uri.t -> Cohttp.Response.t Lwt.t
Expand Down
Loading