From eafca094f67fa6d424620c3042fb2070b46926a4 Mon Sep 17 00:00:00 2001 From: Gabriel Moise Date: Thu, 18 Jul 2024 16:27:41 +0100 Subject: [PATCH 1/5] Manifest: Remove cohttp from data only dirs --- dune | 2 +- manifest/main.ml | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/dune b/dune index 7a04a7b7163e..c3c7d8e6c601 100644 --- a/dune +++ b/dune @@ -1,6 +1,6 @@ (vendored_dirs vendors) -(data_only_dirs _opam-repo-for-release cohttp) +(data_only_dirs _opam-repo-for-release) (env (_ diff --git a/manifest/main.ml b/manifest/main.ml index 841b46841365..d67806a132c0 100644 --- a/manifest/main.ml +++ b/manifest/main.ml @@ -72,8 +72,6 @@ let exclude filename = | ["opam"; "mandatory-for-make.opam"] -> true (* opam-repository is used by scripts/opam-release.sh *) | "opam-repository" :: _ -> true - (* cohttp is imported as data-only for now to ease review of its monorepotisation. *) - | "cohttp" :: _ -> true (* We need to tell Dune about excluding directories without defining targets in those directories. Therefore we hand write some Dune in these. *) | "src" :: "riscv" :: _ -> true -- GitLab From 0cee44bf125f65693b637df4828abd05f2861496 Mon Sep 17 00:00:00 2001 From: Gabriel Moise Date: Thu, 18 Jul 2024 16:48:46 +0100 Subject: [PATCH 2/5] Cohttp: Formatting --- cohttp/README.md | 12 +++-- cohttp/cohttp-lwt-unix/src/client.mli | 4 +- cohttp/cohttp-lwt-unix/src/cohttp_lwt_unix.ml | 2 + cohttp/cohttp-lwt-unix/src/debug.ml | 32 +++++++------ cohttp/cohttp-lwt-unix/src/debug.mli | 6 +-- cohttp/cohttp-lwt-unix/src/io.ml | 29 ++++++------ cohttp/cohttp-lwt-unix/src/net.ml | 10 ++-- cohttp/cohttp-lwt-unix/src/net.mli | 22 +++++---- cohttp/cohttp-lwt-unix/src/server.ml | 23 +++++++--- cohttp/cohttp-lwt-unix/src/server.mli | 20 ++++---- cohttp/cohttp-lwt/src/body.ml | 8 ++-- cohttp/cohttp-lwt/src/body.mli | 9 +++- cohttp/cohttp-lwt/src/client.ml | 36 +++++++++------ cohttp/cohttp-lwt/src/make.ml | 2 + cohttp/cohttp-lwt/src/s.ml | 44 ++++++++++-------- cohttp/cohttp-lwt/src/server.ml | 46 ++++++++++++------- cohttp/cohttp-lwt/src/string_io.ml | 6 +++ 17 files changed, 188 insertions(+), 123 deletions(-) diff --git a/cohttp/README.md b/cohttp/README.md index 5d7a606ecf34..dfa052423c88 100644 --- a/cohttp/README.md +++ b/cohttp/README.md @@ -25,18 +25,20 @@ You can find help from cohttp users and maintainers at the ## Table of contents +- [ocaml-cohttp -- an OCaml library for HTTP clients and servers ](#ocaml-cohttp----an-ocaml-library-for-http-clients-and-servers-) +- [Table of contents](#table-of-contents) - [Installation](#installation) - [Client Tutorial](#client-tutorial) - * [Compile and execute with ocamlbuild](#compile-and-execute-with-ocamlbuild) - * [Compile and execute with dune](#compile-and-execute-with-dune) + - [Compile and execute with ocamlbuild](#compile-and-execute-with-ocamlbuild) + - [Compile and execute with dune](#compile-and-execute-with-dune) - [Dealing with timeouts](#dealing-with-timeouts) - [Managing sessions](#managing-sessions) - [Multipart form data](#multipart-form-data) -- [Creating custom resolver: a Docker Socket Client example](#creating-custom-resolver--a-docker-socket-client-example) +- [Creating custom resolver: a Docker Socket Client example](#creating-custom-resolver-a-docker-socket-client-example) - [Dealing with redirects](#dealing-with-redirects) - [Basic Server Tutorial](#basic-server-tutorial) - * [Compile and execute with ocamlbuild](#compile-and-execute-with-ocamlbuild-1) - * [Compile and execute with dune](#compile-and-execute-with-dune-1) + - [Compile and execute with ocamlbuild](#compile-and-execute-with-ocamlbuild-1) + - [Compile and execute with dune](#compile-and-execute-with-dune-1) - [Installed Binaries](#installed-binaries) - [Debugging](#debugging) - [Important Links](#important-links) diff --git a/cohttp/cohttp-lwt-unix/src/client.mli b/cohttp/cohttp-lwt-unix/src/client.mli index af591aad0b78..6fe879e990fa 100644 --- a/cohttp/cohttp-lwt-unix/src/client.mli +++ b/cohttp/cohttp-lwt-unix/src/client.mli @@ -3,11 +3,11 @@ include Cohttp_lwt.S.Client with type ctx = Net.ctx -val custom_ctx : - ?ctx:Conduit_lwt_unix.ctx -> ?resolver:Resolver_lwt.t -> unit -> ctx (** [custom_ctx ?ctx ?resolver ()] will return a context that is the same as the {!default_ctx}, but with either the connection handling or resolution module overridden with [ctx] or [resolver] respectively. This is useful to supply a {!Conduit_lwt_unix.ctx} with a custom source network interface, or a {!Resolver_lwt.t} with a different name resolution strategy (for instance to override a hostname to point it to a Unix domain socket). *) +val custom_ctx : + ?ctx:Conduit_lwt_unix.ctx -> ?resolver:Resolver_lwt.t -> unit -> ctx diff --git a/cohttp/cohttp-lwt-unix/src/cohttp_lwt_unix.ml b/cohttp/cohttp-lwt-unix/src/cohttp_lwt_unix.ml index 14eb2cc29a73..5d9e9bb94b6f 100644 --- a/cohttp/cohttp-lwt-unix/src/cohttp_lwt_unix.ml +++ b/cohttp/cohttp-lwt-unix/src/cohttp_lwt_unix.ml @@ -16,11 +16,13 @@ module Request = struct include Cohttp.Request + include (Make (Io) : module type of Make (Io) with type t := t) end module Response = struct include Cohttp.Response + include (Make (Io) : module type of Make (Io) with type t := t) end diff --git a/cohttp/cohttp-lwt-unix/src/debug.ml b/cohttp/cohttp-lwt-unix/src/debug.ml index 2d550017ab7c..444267d4edd8 100644 --- a/cohttp/cohttp-lwt-unix/src/debug.ml +++ b/cohttp/cohttp-lwt-unix/src/debug.ml @@ -15,6 +15,7 @@ }}}*) let _debug_active = ref false + let debug_active () = !_debug_active open Lwt.Infix @@ -25,7 +26,7 @@ let reporter file_descr ppf = ( Fmt.with_buffer ~like:ppf buf, fun () -> let str = Buffer.contents buf in - Buffer.reset buf; + Buffer.reset buf ; str ) in let report src level ~over k msgf = @@ -39,7 +40,7 @@ let reporter file_descr ppf = go 0 (Bytes.length buf) in let clean () = - over (); + over () ; Lwt.return_unit in Lwt.async (fun () -> @@ -47,36 +48,39 @@ let reporter file_descr ppf = (fun () -> Lwt.finalize write clean) (fun exn -> Logs.warn (fun m -> - m "Flushing error: %s." (Printexc.to_string exn)); - Lwt.return_unit)); + m "Flushing error: %s." (Printexc.to_string exn)) ; + Lwt.return_unit)) ; k () in let with_metadata header _tags k ppf fmt = - Format.kfprintf k ppf + Format.kfprintf + k + ppf ("%a[%a]: " ^^ fmt ^^ "\n%!") - Logs_fmt.pp_header (level, header) + Logs_fmt.pp_header + (level, header) Fmt.(styled `Magenta string) (Logs.Src.name src) in msgf @@ fun ?header ?tags fmt -> with_metadata header tags k ppf fmt in - { Logs.report } + {Logs.report} let default_reporter = reporter Lwt_unix.stderr Fmt.stderr let set_logger = lazy (if - (* If no reporter has been set by the application, set default one - that prints to stderr *) - Logs.reporter () == Logs.nop_reporter - then Logs.set_reporter default_reporter) + (* If no reporter has been set by the application, set default one + that prints to stderr *) + Logs.reporter () == Logs.nop_reporter + then Logs.set_reporter default_reporter) let activate_debug () = if not !_debug_active then ( - _debug_active := true; - Lazy.force set_logger; - Logs.set_level ~all:true (Some Logs.Debug); + _debug_active := true ; + Lazy.force set_logger ; + Logs.set_level ~all:true (Some Logs.Debug) ; Logs.debug (fun f -> f "Cohttp debugging output is active")) let () = diff --git a/cohttp/cohttp-lwt-unix/src/debug.mli b/cohttp/cohttp-lwt-unix/src/debug.mli index d14e7124d37f..a81c7f7739bb 100644 --- a/cohttp/cohttp-lwt-unix/src/debug.mli +++ b/cohttp/cohttp-lwt-unix/src/debug.mli @@ -16,7 +16,6 @@ (** Debugging output for Cohttp Unix *) -val default_reporter : Logs.reporter (** [default_reporter] provides a simple reporter that sends the logging output to stderr. For example, the code below enables logging at level [level] to stderr, using coloured output if possible. @@ -26,14 +25,15 @@ val default_reporter : Logs.reporter Logs.set_level ~all:true (Some level); Logs.set_reporter Debug.default_reporter ]} *) +val default_reporter : Logs.reporter -val activate_debug : unit -> unit (** [activate_debug] enables debugging output that will be sent to standard error. *) +val activate_debug : unit -> unit -val debug_active : unit -> bool (** [debug_active] returns true if [activate_debug] has been called and false otherwise *) +val debug_active : unit -> bool (** {2 Selectively disable cohttp logging} *) diff --git a/cohttp/cohttp-lwt-unix/src/io.ml b/cohttp/cohttp-lwt-unix/src/io.ml index 70389dd90b1e..6deeeef841aa 100644 --- a/cohttp/cohttp-lwt-unix/src/io.ml +++ b/cohttp/cohttp-lwt-unix/src/io.ml @@ -18,17 +18,20 @@ exception IO_error of exn let () = Printexc.register_printer (function - | IO_error e -> Some ("IO error: " ^ Printexc.to_string e) - | _ -> None); + | IO_error e -> Some ("IO error: " ^ Printexc.to_string e) + | _ -> None) ; if Sys.os_type <> "Win32" then Sys.(set_signal sigpipe Signal_ignore) type 'a t = 'a Lwt.t let ( >>= ) = Lwt.bind + let return = Lwt.return type ic = Lwt_io.input_channel + type oc = Lwt_io.output_channel + type conn = Conduit_lwt_unix.flow let src = Logs.Src.create "cohttp.lwt.io" ~doc:"Cohttp Lwt IO module" @@ -39,35 +42,35 @@ let wrap_read f ~if_closed = (* TODO Use [Lwt_io.is_closed] when available: https://github.com/ocsigen/lwt/pull/635 *) Lwt.catch f (function - | Lwt_io.Channel_closed _ -> Lwt.return if_closed - | Unix.Unix_error _ as e -> Lwt.fail (IO_error e) - | exn -> raise exn) + | Lwt_io.Channel_closed _ -> Lwt.return if_closed + | Unix.Unix_error _ as e -> Lwt.fail (IO_error e) + | exn -> raise exn) let wrap_write f = Lwt.catch f (function - | Unix.Unix_error _ as e -> Lwt.fail (IO_error e) - | exn -> raise exn) + | Unix.Unix_error _ as e -> Lwt.fail (IO_error e) + | exn -> raise exn) let read_line ic = wrap_read ~if_closed:None (fun () -> Lwt_io.read_line_opt ic >>= function | None -> - Log.debug (fun f -> f "<<< EOF"); + Log.debug (fun f -> f "<<< EOF") ; Lwt.return_none | Some l as x -> - Log.debug (fun f -> f "<<< %s" l); + Log.debug (fun f -> f "<<< %s" l) ; Lwt.return x) let read ic count = let count = min count Sys.max_string_length in wrap_read ~if_closed:"" (fun () -> Lwt_io.read ~count ic >>= fun buf -> - Log.debug (fun f -> f "<<<[%d] %s" count buf); + Log.debug (fun f -> f "<<<[%d] %s" count buf) ; Lwt.return buf) let write oc buf = wrap_write @@ fun () -> - Log.debug (fun f -> f ">>> %s" (String.trim buf)); + Log.debug (fun f -> f ">>> %s" (String.trim buf)) ; Lwt_io.write oc buf let flush oc = wrap_write @@ fun () -> Lwt_io.flush oc @@ -76,7 +79,7 @@ type error = exn let catch f = Lwt.try_bind f Lwt.return_ok (function - | IO_error e -> Lwt.return_error e - | ex -> Lwt.fail ex) + | IO_error e -> Lwt.return_error e + | ex -> Lwt.fail ex) let pp_error = Fmt.exn diff --git a/cohttp/cohttp-lwt-unix/src/net.ml b/cohttp/cohttp-lwt-unix/src/net.ml index e32ca95c3648..30a850bb3624 100644 --- a/cohttp/cohttp-lwt-unix/src/net.ml +++ b/cohttp/cohttp-lwt-unix/src/net.ml @@ -20,12 +20,12 @@ open Lwt.Infix module IO = Io -type ctx = { ctx : Conduit_lwt_unix.ctx; resolver : Resolver_lwt.t } +type ctx = {ctx : Conduit_lwt_unix.ctx; resolver : Resolver_lwt.t} [@@deriving sexp_of] let init ?(ctx = Lazy.force Conduit_lwt_unix.default_ctx) ?(resolver = Resolver_lwt_unix.system) () = - { ctx; resolver } + {ctx; resolver} let default_ctx = { @@ -33,7 +33,7 @@ let default_ctx = ctx = Lazy.force Conduit_lwt_unix.default_ctx; } -let connect_uri ~ctx:{ ctx; resolver } uri = +let connect_uri ~ctx:{ctx; resolver} uri = Resolver_lwt.resolve_uri ~uri resolver >>= fun endp -> Conduit_lwt_unix.endp_to_client ~ctx endp >>= fun client -> Conduit_lwt_unix.connect ~ctx client @@ -42,9 +42,11 @@ let close c = Lwt.catch (fun () -> Lwt_io.close c) (fun e -> - Logs.warn (fun f -> f "Closing channel failed: %s" (Printexc.to_string e)); + Logs.warn (fun f -> f "Closing channel failed: %s" (Printexc.to_string e)) ; Lwt.return_unit) let close_in ic = Lwt.ignore_result (close ic) + let close_out oc = Lwt.ignore_result (close oc) + let close ic oc = Lwt.ignore_result (close ic >>= fun () -> close oc) diff --git a/cohttp/cohttp-lwt-unix/src/net.mli b/cohttp/cohttp-lwt-unix/src/net.mli index 956680ea56ab..1f01c5602c7d 100644 --- a/cohttp/cohttp-lwt-unix/src/net.mli +++ b/cohttp/cohttp-lwt-unix/src/net.mli @@ -18,28 +18,21 @@ module IO = Io -type ctx = { ctx : Conduit_lwt_unix.ctx; resolver : Resolver_lwt.t } +type ctx = {ctx : Conduit_lwt_unix.ctx; resolver : Resolver_lwt.t} [@@deriving sexp_of] -val default_ctx : ctx (** [default_ctx] is the default network context. It uses [Conduit_lwt_unix.default_ctx] and [Resolver_lwt_unix.system]. *) +val default_ctx : ctx -val init : ?ctx:Conduit_lwt_unix.ctx -> ?resolver:Resolver_lwt.t -> unit -> ctx (** [init ?ctx ?resolver ()] is a network context that is the same as the {!default_ctx}, but with either the connection handling or resolution module overridden with [ctx] or [resolver] respectively. This is useful to supply a {!Conduit_lwt_unix.resolver} with a custom source network interface, or a {!Resolver_lwt.t} with a different name resolution strategy (for instance to override a hostname to point it to a Unix domain socket). *) +val init : ?ctx:Conduit_lwt_unix.ctx -> ?resolver:Resolver_lwt.t -> unit -> ctx -val connect_uri : - ctx:ctx -> - Uri.t -> - (Conduit_lwt_unix.flow - * Lwt_io.input Lwt_io.channel - * Lwt_io.output Lwt_io.channel) - Lwt.t (** [connect_uri ~ctx uri] starts a {i flow} on the given [uri]. The choice of the protocol (with or without encryption) is done by the {i scheme} of the given [uri]: @@ -54,7 +47,16 @@ val connect_uri : communication with them first). By {i extension}, we mean that the user is able to fill its own [ctx] and we don't overlap resolution functions from the given [ctx]. *) +val connect_uri : + ctx:ctx -> + Uri.t -> + (Conduit_lwt_unix.flow + * Lwt_io.input Lwt_io.channel + * Lwt_io.output Lwt_io.channel) + Lwt.t val close_in : 'a Lwt_io.channel -> unit + val close_out : 'a Lwt_io.channel -> unit + val close : 'a Lwt_io.channel -> 'b Lwt_io.channel -> unit diff --git a/cohttp/cohttp-lwt-unix/src/server.ml b/cohttp/cohttp-lwt-unix/src/server.ml index e33280e0faec..70670779c082 100644 --- a/cohttp/cohttp-lwt-unix/src/server.ml +++ b/cohttp/cohttp-lwt-unix/src/server.ml @@ -34,8 +34,10 @@ let respond_file ?headers ~fname () = | buf -> Some buf) (fun exn -> Log.warn (fun m -> - m "Error resolving file %s (%s)" fname - (Printexc.to_string exn)); + m + "Error resolving file %s (%s)" + fname + (Printexc.to_string exn)) ; Lwt.return_none)) in Lwt.on_success (Lwt_stream.closed stream) (fun () -> @@ -44,8 +46,8 @@ let respond_file ?headers ~fname () = (fun () -> Lwt_io.close ic) (fun e -> Log.warn (fun f -> - f "Closing channel failed: %s" (Printexc.to_string e)); - Lwt.return_unit)); + f "Closing channel failed: %s" (Printexc.to_string e)) ; + Lwt.return_unit)) ; let body = Cohttp_lwt.Body.of_stream stream in let mime_type = Magic_mime.lookup fname in let headers = @@ -61,11 +63,20 @@ let respond_file ?headers ~fname () = let log_on_exn = function | Unix.Unix_error (error, func, arg) -> Log.warn (fun m -> - m "Client connection error %s: %s(%S)" (Unix.error_message error) func + m + "Client connection error %s: %s(%S)" + (Unix.error_message error) + func arg) | exn -> Log.err (fun m -> m "Unhandled exception: %a" Fmt.exn exn) let create ?timeout ?backlog ?stop ?(on_exn = log_on_exn) ?(ctx = Net.default_ctx) ?(mode = `TCP (`Port 8080)) spec = - Conduit_lwt_unix.serve ?backlog ?timeout ?stop ~on_exn ~ctx:ctx.Net.ctx ~mode + Conduit_lwt_unix.serve + ?backlog + ?timeout + ?stop + ~on_exn + ~ctx:ctx.Net.ctx + ~mode (callback spec) diff --git a/cohttp/cohttp-lwt-unix/src/server.mli b/cohttp/cohttp-lwt-unix/src/server.mli index 37d75150e696..c2387d207ab9 100644 --- a/cohttp/cohttp-lwt-unix/src/server.mli +++ b/cohttp/cohttp-lwt-unix/src/server.mli @@ -6,8 +6,8 @@ include Cohttp_lwt.S.Server with module IO = Io -val resolve_file : docroot:string -> uri:Uri.t -> string (** Deprecated. Please use Cohttp.Path.resolve_local_file. *) +val resolve_file : docroot:string -> uri:Uri.t -> string val respond_file : ?headers:Cohttp.Header.t -> @@ -15,15 +15,6 @@ val respond_file : unit -> (Cohttp.Response.t * Cohttp_lwt.Body.t) Lwt.t -val create : - ?timeout:int -> - ?backlog:int -> - ?stop:unit Lwt.t -> - ?on_exn:(exn -> unit) -> - ?ctx:Net.ctx -> - ?mode:Conduit_lwt_unix.server -> - t -> - unit Lwt.t (** [create ?timeout ?backlog ?stop ?on_exn ?mode t] is a new HTTP server. The user can decide to start a simple HTTP server (without encryption) or @@ -43,3 +34,12 @@ val create : via the callback defined in [t]. If the callback raises an exception, it is passed to [on_exn] (by default, to a function that logs the exception using the {!Logs} library). *) +val create : + ?timeout:int -> + ?backlog:int -> + ?stop:unit Lwt.t -> + ?on_exn:(exn -> unit) -> + ?ctx:Net.ctx -> + ?mode:Conduit_lwt_unix.server -> + t -> + unit Lwt.t diff --git a/cohttp/cohttp-lwt/src/body.ml b/cohttp/cohttp-lwt/src/body.ml index 2672d55f4e8a..520b98663fad 100644 --- a/cohttp/cohttp-lwt/src/body.ml +++ b/cohttp/cohttp-lwt/src/body.ml @@ -17,7 +17,7 @@ open Cohttp open Lwt -type t = [ Body.t | `Stream of (string Lwt_stream.t[@sexp.opaque]) ] +type t = [Body.t | `Stream of (string Lwt_stream.t[@sexp.opaque])] [@@deriving sexp] let empty = (Body.empty :> t) @@ -31,7 +31,7 @@ let create_stream fn arg = fn arg >>= function | Transfer.Done -> return_none | Transfer.Final_chunk c -> - fin := true; + fin := true ; return (Some c) | Transfer.Chunk c -> return (Some c))) @@ -61,7 +61,7 @@ let to_stream (body : t) = match body with | `Empty -> Lwt_stream.of_list [] | `Stream s -> s - | `String s -> Lwt_stream.of_list [ s ] + | `String s -> Lwt_stream.of_list [s] | `Strings sl -> Lwt_stream.of_list sl let drain_body (body : t) = @@ -70,6 +70,7 @@ let drain_body (body : t) = | `Stream s -> Lwt_stream.junk_while (fun _ -> true) s let of_string_list l = `Strings l + let of_stream s = `Stream s let transfer_encoding = function @@ -98,4 +99,5 @@ let map f t = | `Stream s -> `Stream (Lwt_stream.map f s) let to_form (body : t) = to_string body >|= Uri.query_of_encoded + let of_form ?scheme f = Uri.encoded_of_query ?scheme f |> of_string diff --git a/cohttp/cohttp-lwt/src/body.mli b/cohttp/cohttp-lwt/src/body.mli index e4535e054de1..3c2f458d991e 100644 --- a/cohttp/cohttp-lwt/src/body.mli +++ b/cohttp/cohttp-lwt/src/body.mli @@ -14,20 +14,27 @@ * }}}*) -type t = [ Cohttp.Body.t | `Stream of string Lwt_stream.t ] [@@deriving sexp] +type t = [Cohttp.Body.t | `Stream of string Lwt_stream.t] [@@deriving sexp] include Cohttp.S.Body with type t := t val is_empty : t -> bool Lwt.t + val to_string : t -> string Lwt.t + val to_string_list : t -> string list Lwt.t + val to_stream : t -> string Lwt_stream.t + val of_stream : string Lwt_stream.t -> t + val to_form : t -> (string * string list) list Lwt.t val create_stream : ('a -> Cohttp.Transfer.chunk Lwt.t) -> 'a -> string Lwt_stream.t val length : t -> (int64 * t) Lwt.t + val write_body : (string -> unit Lwt.t) -> t -> unit Lwt.t + val drain_body : t -> unit Lwt.t diff --git a/cohttp/cohttp-lwt/src/client.ml b/cohttp/cohttp-lwt/src/client.ml index 533da1df0779..cee09ecfa74b 100644 --- a/cohttp/cohttp-lwt/src/client.ml +++ b/cohttp/cohttp-lwt/src/client.ml @@ -22,8 +22,8 @@ module Make (IO : S.IO) (Net : S.Net with module IO = IO) = struct (* Lwt.on_success registers a callback in the stream. * The GC will still be able to collect stream. *) Lwt.on_success (Lwt_stream.closed stream) (fun () -> - closed := true; - closefn ()); + closed := true ; + closefn ()) ; (* finalise could run in a thread different from the lwt main thread. * You may therefore not call into Lwt from a finaliser. *) Gc.finalise_last @@ -34,10 +34,10 @@ module Make (IO : S.IO) (Net : S.Net with module IO = IO) = struct "Body not consumed, leaking stream! Refer to \ https://github.com/mirage/ocaml-cohttp/issues/730 for \ additional details")) - stream; + stream ; body | `No -> - closefn (); + closefn () ; `Empty let is_meth_chunked = function @@ -60,7 +60,8 @@ module Make (IO : S.IO) (Net : S.Net with module IO = IO) = struct let req = Request.make_for_client ~headers ~chunked meth uri in Request.write (fun writer -> Body.write_body (Request.write_body writer) body) - req oc + req + oc | false -> (* If chunked is not allowed, then obtain the body length and insert header *) @@ -70,7 +71,8 @@ module Make (IO : S.IO) (Net : S.Net with module IO = IO) = struct in Request.write (fun writer -> Body.write_body (Request.write_body writer) buf) - req oc + req + oc in sent >>= fun () -> (Response.read ic >>= function @@ -80,18 +82,19 @@ module Make (IO : S.IO) (Net : S.Net with module IO = IO) = struct | `Ok res -> ( match meth with | `HEAD -> - closefn (); + closefn () ; Lwt.return (res, `Empty) | _ -> let body = read_body ~closefn ic res in Lwt.return (res, body))) |> fun t -> - Lwt.on_cancel t closefn; - Lwt.on_failure t (fun _exn -> closefn ()); + Lwt.on_cancel t closefn ; + Lwt.on_failure t (fun _exn -> closefn ()) ; t (* The HEAD should not have a response body *) let head ?ctx ?headers uri = call ?ctx ?headers `HEAD uri >|= fst + let get ?ctx ?headers uri = call ?ctx ?headers `GET uri let delete ?ctx ?body ?chunked ?headers uri = @@ -108,7 +111,9 @@ module Make (IO : S.IO) (Net : S.Net with module IO = IO) = struct let post_form ?ctx ?headers ~params uri = let headers = - Header.add_opt_unless_exists headers "content-type" + Header.add_opt_unless_exists + headers + "content-type" "application/x-www-form-urlencoded" in let body = Body.of_string (Uri.encoded_of_query params) in @@ -122,7 +127,8 @@ module Make (IO : S.IO) (Net : S.Net with module IO = IO) = struct (fun (req, body) -> Request.write (fun writer -> Body.write_body (Request.write_body writer) body) - req oc + req + oc >>= fun () -> Lwt.return (Request.meth req)) reqs in @@ -143,17 +149,17 @@ module Make (IO : S.IO) (Net : S.Net with module IO = IO) = struct | `Ok res -> ( match meth with | `HEAD -> - closefn (); + closefn () ; Lwt.return (res, `Empty) | _ -> let body = read_body ~closefn ic res in Lwt.return (res, body))) |> fun t -> - Lwt.on_cancel t closefn; - Lwt.on_failure t (fun _exn -> closefn ()); + Lwt.on_cancel t closefn ; + Lwt.on_failure t (fun _exn -> closefn ()) ; t)) meth_stream in - Lwt.on_success (Lwt_stream.closed resps) (fun () -> Net.close ic oc); + Lwt.on_success (Lwt_stream.closed resps) (fun () -> Net.close ic oc) ; Lwt.return resps end diff --git a/cohttp/cohttp-lwt/src/make.ml b/cohttp/cohttp-lwt/src/make.ml index 964b0cf45fe0..6ec7a4a30c8b 100644 --- a/cohttp/cohttp-lwt/src/make.ml +++ b/cohttp/cohttp-lwt/src/make.ml @@ -1,9 +1,11 @@ module Request (IO : S.IO) = struct include Cohttp.Request + include (Make (IO) : module type of Make (IO) with type t := t) end module Response (IO : S.IO) = struct include Cohttp.Response + include (Make (IO) : module type of Make (IO) with type t := t) end diff --git a/cohttp/cohttp-lwt/src/s.ml b/cohttp/cohttp-lwt/src/s.ml index fe5b33d2cb60..fcddd36abf50 100644 --- a/cohttp/cohttp-lwt/src/s.ml +++ b/cohttp/cohttp-lwt/src/s.ml @@ -8,9 +8,9 @@ module type IO = sig type error - val catch : (unit -> 'a t) -> ('a, error) result t (** [catch f] is [f () >|= Result.ok], unless [f] fails with an IO error, in which case it returns the error. *) + val catch : (unit -> 'a t) -> ('a, error) result t val pp_error : Format.formatter -> error -> unit end @@ -23,9 +23,13 @@ module type Net = sig type ctx [@@deriving sexp_of] val default_ctx : ctx + val connect_uri : ctx:ctx -> Uri.t -> (IO.conn * IO.ic * IO.oc) Lwt.t + val close_in : IO.ic -> unit + val close_out : IO.oc -> unit + val close : IO.ic -> IO.oc -> unit end @@ -37,14 +41,6 @@ end module type Client = sig type ctx - val call : - ?ctx:ctx -> - ?headers:Cohttp.Header.t -> - ?body:Body.t -> - ?chunked:bool -> - Cohttp.Code.meth -> - Uri.t -> - (Cohttp.Response.t * Body.t) Lwt.t (** [call ?ctx ?headers ?body ?chunked meth uri] will resolve the [uri] to a concrete network endpoint using context [ctx]. It will then issue an HTTP request with method [meth], adding request headers from [headers] if @@ -75,6 +71,14 @@ module type Client = sig 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. *) + val call : + ?ctx:ctx -> + ?headers:Cohttp.Header.t -> + ?body:Body.t -> + ?chunked:bool -> + Cohttp.Code.meth -> + Uri.t -> + (Cohttp.Response.t * Body.t) Lwt.t val head : ?ctx:ctx -> ?headers:Cohttp.Header.t -> Uri.t -> Cohttp.Response.t Lwt.t @@ -137,9 +141,6 @@ module type Server = sig type conn = IO.conn * Cohttp.Connection.t - type response_action = - [ `Expert of Cohttp.Response.t * (IO.ic -> IO.oc -> unit Lwt.t) - | `Response of Cohttp.Response.t * Body.t ] (** A request handler can respond in two ways: - Using [`Response], with a {!Response.t} and a {!Body.t}. @@ -150,6 +151,9 @@ module type Server = sig (e.g. websockets). Processing of pipelined requests continue after the {!unit Lwt.t} is resolved. The connection can be closed by closing the {!IO.ic}. *) + type response_action = + [ `Expert of Cohttp.Response.t * (IO.ic -> IO.oc -> unit Lwt.t) + | `Response of Cohttp.Response.t * Body.t ] type t @@ -176,18 +180,11 @@ module type Server = sig unit -> t - val resolve_local_file : docroot:string -> uri:Uri.t -> string (** Resolve a URI and a docroot into a concrete local filename. Deprecated. Please use Cohttp.Path.resolve_local_file. *) + val resolve_local_file : docroot:string -> uri:Uri.t -> string - val respond : - ?headers:Cohttp.Header.t -> - ?flush:bool -> - status:Cohttp.Code.status_code -> - body:Body.t -> - unit -> - (Cohttp.Response.t * Body.t) Lwt.t (** [respond ?headers ?flush ~status ~body] will respond to an HTTP request with the given [status] code and response [body]. If [flush] is true, then every response chunk will be flushed to the network rather than being @@ -196,6 +193,13 @@ module type Server = sig determined immediately. You can override the encoding by supplying an appropriate [Content-length] or [Transfer-encoding] in the [headers] parameter. *) + val respond : + ?headers:Cohttp.Header.t -> + ?flush:bool -> + status:Cohttp.Code.status_code -> + body:Body.t -> + unit -> + (Cohttp.Response.t * Body.t) Lwt.t val respond_string : ?flush:bool -> diff --git a/cohttp/cohttp-lwt/src/server.ml b/cohttp/cohttp-lwt/src/server.ml index 922ee8851f2d..bffc23c0f90c 100644 --- a/cohttp/cohttp-lwt/src/server.ml +++ b/cohttp/cohttp-lwt/src/server.ml @@ -22,7 +22,7 @@ module Make (IO : S.IO) = struct } let make_response_action ?(conn_closed = ignore) ~callback () = - { conn_closed; callback } + {conn_closed; callback} let make ?conn_closed ~callback () = let callback conn req body = @@ -56,9 +56,12 @@ module Make (IO : S.IO) = struct let respond_string ?(flush = true) ?headers ~status ~body () = let res = - Response.make ~status ~flush + Response.make + ~status + ~flush ~encoding:(Cohttp.Transfer.Fixed (Int64.of_int (String.length body))) - ?headers () + ?headers + () in let body = Body.of_string body in Lwt.return (res, body) @@ -96,7 +99,7 @@ module Make (IO : S.IO) = struct | `No | `Unknown -> `Empty let handle_request callback conn req body = - Log.debug (fun m -> m "Handle request: %a." Request.pp_hum req); + Log.debug (fun m -> m "Handle request: %a." Request.pp_hum req) ; Lwt.finalize (fun () -> Lwt.catch @@ -105,8 +108,11 @@ module Make (IO : S.IO) = struct | Out_of_memory -> Lwt.fail Out_of_memory | exn -> Log.err (fun f -> - f "Error handling %a: %s" Request.pp_hum req - (Printexc.to_string exn)); + f + "Error handling %a: %s" + Request.pp_hum + req + (Printexc.to_string exn)) ; respond_error ~body:"Internal Server Error" () >|= fun rsp -> `Response rsp)) (fun () -> Body.drain_body body) @@ -114,9 +120,11 @@ module Make (IO : S.IO) = struct let handle_response ~keep_alive oc res body conn_closed handle_client = IO.catch (fun () -> let flush = Response.flush res in - Response.write ~flush + Response.write + ~flush (fun writer -> Body.write_body (Response.write_body writer) body) - res oc) + res + oc) >>= function | Ok () -> if keep_alive then handle_client oc @@ -124,25 +132,29 @@ module Make (IO : S.IO) = struct let () = conn_closed () in Lwt.return_unit | Error e -> - Log.info (fun m -> m "IO error while writing body: %a" IO.pp_error e); - conn_closed (); + Log.info (fun m -> m "IO error while writing body: %a" IO.pp_error e) ; + conn_closed () ; Body.drain_body body let rec handle_client ic oc conn spec = Request.read ic >>= function | `Eof -> - spec.conn_closed conn; + spec.conn_closed conn ; Lwt.return_unit | `Invalid data -> - Log.err (fun m -> m "invalid input %s while handling client" data); - spec.conn_closed conn; + Log.err (fun m -> m "invalid input %s while handling client" data) ; + spec.conn_closed conn ; Lwt.return_unit | `Ok req -> ( let body = read_body ic req in handle_request spec.callback conn req body >>= function | `Response (res, body) -> let keep_alive = Request.is_keep_alive req in - handle_response ~keep_alive oc res body + handle_response + ~keep_alive + oc + res + body (fun () -> spec.conn_closed conn) (fun oc -> handle_client ic oc conn spec) | `Expert (res, io_handler) -> @@ -159,10 +171,10 @@ module Make (IO : S.IO) = struct | Ok () -> Lwt.return_unit | Error e -> Log.info (fun m -> - m "IO error while handling client: %a" IO.pp_error e); - conn_closed (); + m "IO error while handling client: %a" IO.pp_error e) ; + conn_closed () ; Lwt.return_unit) (fun e -> - conn_closed (); + conn_closed () ; Lwt.fail e) end diff --git a/cohttp/cohttp-lwt/src/string_io.ml b/cohttp/cohttp-lwt/src/string_io.ml index ec94654eeeeb..7c7f53b8e9a4 100644 --- a/cohttp/cohttp-lwt/src/string_io.ml +++ b/cohttp/cohttp-lwt/src/string_io.ml @@ -18,15 +18,21 @@ type 'a t = 'a Lwt.t let return = Lwt.return + let ( >>= ) = Lwt.bind module Sio = Cohttp__String_io type ic = Sio.M.ic + type oc = Sio.M.oc + type conn = Sio.M.conn let read_line ic = return (Sio.M.read_line ic) + let read ic n = return (Sio.M.read ic n) + let write oc str = return (Sio.M.write oc str) + let flush oc = return (Sio.M.flush oc) -- GitLab From 4713a0bc26bf53b335b21bf4feb204f6f276760a Mon Sep 17 00:00:00 2001 From: Gabriel Moise Date: Thu, 18 Jul 2024 16:44:02 +0100 Subject: [PATCH 3/5] Cohttp: Use cohttp inside tezos --- cohttp/cohttp-lwt-unix/src/dune | 23 +++++++--- cohttp/cohttp-lwt/src/dune | 17 ++++--- contrib/RPC_toy/dune | 2 +- manifest/externals.ml | 6 +++ manifest/product_cohttp.ml | 45 ++++++++++++++++--- manifest/product_cohttp.mli | 2 + manifest/product_octez.ml | 12 ++++- opam/RPC-toy.opam | 1 - opam/octez-libs.opam | 14 +++--- opam/octez-smart-rollup-node-lib.opam | 1 - .../octez-smart-rollup-wasm-debugger-lib.opam | 1 - opam/tezt-tezos.opam | 1 - opam/virtual/octez-deps.opam | 7 ++- opam/virtual/octez-deps.opam.locked | 4 +- prometheus/app/dune | 6 +-- prometheus/examples/dune | 4 +- resto/src/dune | 4 +- resto/test/dune | 8 ++-- script-inputs/octez-source-content | 1 + src/lib_base/index.mld | 1 + src/lib_rpc_http/dune | 4 +- src/lib_smart_rollup_node/dune | 2 +- src/lib_wasm_debugger/dune | 2 +- tezt/lib_performance_regression/dune | 2 +- tezt/lib_tezos/dune | 2 +- 25 files changed, 122 insertions(+), 50 deletions(-) diff --git a/cohttp/cohttp-lwt-unix/src/dune b/cohttp/cohttp-lwt-unix/src/dune index 45f38cbcd241..91b0f648859c 100644 --- a/cohttp/cohttp-lwt-unix/src/dune +++ b/cohttp/cohttp-lwt-unix/src/dune @@ -1,8 +1,19 @@ +; This file was automatically generated, do not edit. +; Edit file manifest/main.ml instead. + (library (name cohttp_lwt_unix) - (public_name cohttp-lwt-unix) - (synopsis "Lwt/Unix backend for Cohttp") - (preprocess - (pps ppx_sexp_conv)) - (libraries fmt logs logs.lwt conduit-lwt magic-mime lwt.unix - conduit-lwt-unix cohttp cohttp-lwt logs.fmt)) + (public_name octez-libs.cohttp-lwt-unix) + (instrumentation (backend bisect_ppx)) + (libraries + fmt + logs + logs.lwt + conduit-lwt + magic-mime + lwt.unix + conduit-lwt-unix + cohttp + octez-libs.cohttp-lwt + logs.fmt) + (preprocess (pps ppx_sexp_conv))) diff --git a/cohttp/cohttp-lwt/src/dune b/cohttp/cohttp-lwt/src/dune index 432e4d981d96..61531b74f1aa 100644 --- a/cohttp/cohttp-lwt/src/dune +++ b/cohttp/cohttp-lwt/src/dune @@ -1,7 +1,14 @@ +; This file was automatically generated, do not edit. +; Edit file manifest/main.ml instead. + (library (name cohttp_lwt) - (public_name cohttp-lwt) - (synopsis "Lwt backend") - (preprocess - (pps ppx_sexp_conv)) - (libraries lwt uri cohttp logs logs.lwt)) + (public_name octez-libs.cohttp-lwt) + (instrumentation (backend bisect_ppx)) + (libraries + lwt + uri + cohttp + logs + logs.lwt) + (preprocess (pps ppx_sexp_conv))) diff --git a/contrib/RPC_toy/dune b/contrib/RPC_toy/dune index 965c4675f6d8..84a96cfabb4a 100644 --- a/contrib/RPC_toy/dune +++ b/contrib/RPC_toy/dune @@ -9,7 +9,7 @@ (libraries octez-libs.stdlib-unix octez-libs.base - cohttp-lwt-unix) + octez-libs.cohttp-lwt-unix) (link_flags (:standard) (:include %{workspace_root}/static-link-flags.sexp) diff --git a/manifest/externals.ml b/manifest/externals.ml index d22a5afaf761..e009bd94f65c 100644 --- a/manifest/externals.ml +++ b/manifest/externals.ml @@ -70,6 +70,10 @@ let checkseum_ocaml = external_sublib checkseum "checkseum.ocaml" let cmdliner = external_lib "cmdliner" V.(at_least "1.1.0") +let cohttp = external_lib "cohttp" V.(at_least "5.3.1") + +let conduit_lwt = external_lib "conduit-lwt" V.(exactly "6.2.2") + let conduit_lwt_unix = external_lib "conduit-lwt-unix" V.(exactly "6.2.2") let compiler_libs_common = external_lib "compiler-libs.common" V.True ~opam:"" @@ -149,6 +153,8 @@ let lwt_unix = external_sublib lwt "lwt.unix" let lwt_watcher = external_lib "lwt-watcher" V.(exactly "0.2") +let magic_mime = external_lib "magic-mime" V.(at_least "1.3.1") + let mtime = external_lib "mtime" V.(at_least "2.0.0") let mtime_clock_os = external_sublib mtime "mtime.clock.os" diff --git a/manifest/product_cohttp.ml b/manifest/product_cohttp.ml index 6fccd6454109..43e3f9a8983f 100644 --- a/manifest/product_cohttp.ml +++ b/manifest/product_cohttp.ml @@ -8,10 +8,43 @@ open Manifest open Externals -(* Ultimately, cohttp will use the local sources from `cohttp/`. - For now we still use the opam package (see below, uses of - `external_lib`). This temporary state is to minimise disruptions - for other developers and reducing the size of MRs. *) -let cohttp_lwt = external_lib "cohttp-lwt" V.(exactly "5.3.0") +let product_source = ["cohttp/"] -let cohttp_lwt_unix = external_lib "cohttp-lwt-unix" V.(exactly "5.3.0") +include Product (struct + let name = "octez" + + let source = product_source +end) + +let conflicts = + [external_lib "cohttp-lwt" V.True; external_lib "cohttp-lwt-unix" V.True] + +let cohttp_lwt = + public_lib + "octez-libs.cohttp-lwt" + ~internal_name:"cohttp_lwt" + ~path:"cohttp/cohttp-lwt/src" + ~preprocess:[pps ppx_sexp_conv] + ~deps:[lwt; uri; cohttp; logs; logs_lwt] + ~conflicts + +let cohttp_lwt_unix = + public_lib + "octez-libs.cohttp-lwt-unix" + ~internal_name:"cohttp_lwt_unix" + ~path:"cohttp/cohttp-lwt-unix/src" + ~preprocess:[pps ppx_sexp_conv] + ~deps: + [ + fmt; + logs; + logs_lwt; + conduit_lwt; + magic_mime; + lwt_unix; + conduit_lwt_unix; + cohttp; + cohttp_lwt; + logs_fmt; + ] + ~conflicts diff --git a/manifest/product_cohttp.mli b/manifest/product_cohttp.mli index 1849bebb5f7c..fb05df45104f 100644 --- a/manifest/product_cohttp.mli +++ b/manifest/product_cohttp.mli @@ -5,6 +5,8 @@ (* *) (*****************************************************************************) +val product_source : string list + val cohttp_lwt : Manifest.target val cohttp_lwt_unix : Manifest.target diff --git a/manifest/product_octez.ml b/manifest/product_octez.ml index b1adcae5114a..13a3a6fd6ec9 100644 --- a/manifest/product_octez.ml +++ b/manifest/product_octez.ml @@ -40,8 +40,8 @@ include Product (struct "brassaia/"; "rust-toolchain"; ] - @ Product_data_encoding.product_source @ Product_prometheus.product_source - @ Product_resto.product_source + @ Product_data_encoding.product_source @ Product_cohttp.product_source + @ Product_prometheus.product_source @ Product_resto.product_source end) module String_set = Set.Make (String) @@ -112,6 +112,14 @@ let () = ~target:"!module-Data_encoding" ~text:"Data_encoding" +(* Back-register the cohttp library which is currently maintained as its + own product but still attached to octez-libs. *) +let () = + Sub_lib.add_doc_link + registered_octez_libs + ~target:"!module-Cohttp" + ~text:"Cohttp" + (* Back-register the prometheus library which is currently maintained as its own product but still attached to octez-libs. *) let () = diff --git a/opam/RPC-toy.opam b/opam/RPC-toy.opam index 5415c7d6087a..618581bd10d0 100644 --- a/opam/RPC-toy.opam +++ b/opam/RPC-toy.opam @@ -11,7 +11,6 @@ depends: [ "dune" { >= "3.11.1" } "ocaml" { >= "4.14" } "octez-libs" { = version } - "cohttp-lwt-unix" { = "5.3.0" } ] build: [ ["rm" "-r" "vendors" "contrib"] diff --git a/opam/octez-libs.opam b/opam/octez-libs.opam index 5e872c6b9463..a39534e2aed8 100644 --- a/opam/octez-libs.opam +++ b/opam/octez-libs.opam @@ -20,17 +20,19 @@ depends: [ "zarith" { >= "1.13" & < "1.14" } "bigstringaf" { >= "0.5.0" } "ppx_expect" + "ppx_sexp_conv" "lwt" { >= "5.7.0" } + "cohttp" { >= "5.3.1" } + "logs" + "fmt" { >= "0.8.7" } + "conduit-lwt" { = "6.2.2" } + "magic-mime" { >= "1.3.1" } + "conduit-lwt-unix" { = "6.2.2" } "astring" "asetmap" { >= "0.8.1" } "re" { >= "1.10.0" } - "cohttp-lwt" { = "5.3.0" } - "fmt" { >= "0.8.7" } "cmdliner" { >= "1.1.0" } - "cohttp-lwt-unix" { = "5.3.0" } - "logs" "alcotest-lwt" { >= "1.5.0" } - "conduit-lwt-unix" { = "6.2.2" } "tezt" { >= "4.1.0" & < "5.0.0" } "qcheck-core" "qcheck-alcotest" { >= "0.20" } @@ -86,6 +88,8 @@ x-opam-monorepo-opam-provided: [ ] conflicts: [ "checkseum" { = "0.5.0" } + "cohttp-lwt" + "cohttp-lwt-unix" "data-encoding" "hacl_x25519" "json_data_encoding" diff --git a/opam/octez-smart-rollup-node-lib.opam b/opam/octez-smart-rollup-node-lib.opam index 987304982819..fb48dd7f3b5a 100644 --- a/opam/octez-smart-rollup-node-lib.opam +++ b/opam/octez-smart-rollup-node-lib.opam @@ -12,7 +12,6 @@ depends: [ "ocaml" { >= "4.14" } "octez-libs" { = version } "octez-shell-libs" { = version } - "cohttp-lwt-unix" { = "5.3.0" } "tezos-openapi" { = version } "octez-node-config" { = version } "camlzip" { >= "1.11" & < "1.12" } diff --git a/opam/octez-smart-rollup-wasm-debugger-lib.opam b/opam/octez-smart-rollup-wasm-debugger-lib.opam index 1f3a45602cec..f91a2a9336bd 100644 --- a/opam/octez-smart-rollup-wasm-debugger-lib.opam +++ b/opam/octez-smart-rollup-wasm-debugger-lib.opam @@ -12,7 +12,6 @@ depends: [ "ocaml" { >= "4.14" } "octez-libs" { = version } "octez-protocol-alpha-libs" { = version } - "cohttp-lwt-unix" { = "5.3.0" } "octez-l2-libs" { = version } "octez-version" { = version } "octez-smart-rollup-wasm-debugger-plugin" { = version } diff --git a/opam/tezt-tezos.opam b/opam/tezt-tezos.opam index e1e1f37a62be..d0e7f9f1075e 100644 --- a/opam/tezt-tezos.opam +++ b/opam/tezt-tezos.opam @@ -12,7 +12,6 @@ depends: [ "ocaml" { >= "4.14" } "octez-libs" { = version } "uri" { >= "3.1.0" } - "cohttp-lwt-unix" { = "5.3.0" } "tezt" { >= "4.1.0" & < "5.0.0" } "hex" { >= "1.3.0" } ] diff --git a/opam/virtual/octez-deps.opam b/opam/virtual/octez-deps.opam index 39f3eb4f6cce..59c129399f74 100644 --- a/opam/virtual/octez-deps.opam +++ b/opam/virtual/octez-deps.opam @@ -30,8 +30,8 @@ depends: [ "checkseum" { != "0.5.0" } "class_group_vdf" { >= "0.0.4" } "cmdliner" { >= "1.1.0" } - "cohttp-lwt" { = "5.3.0" } - "cohttp-lwt-unix" { = "5.3.0" } + "cohttp" { >= "5.3.1" } + "conduit-lwt" { = "6.2.2" } "conduit-lwt-unix" { = "6.2.2" } "conf-libev" "conf-rust" @@ -63,6 +63,7 @@ depends: [ "lwt-canceler" { >= "0.3" & < "0.4" } "lwt-exit" "lwt-watcher" { = "0.2" } + "magic-mime" { >= "1.3.1" } "mtime" { >= "2.0.0" } "num" "ocaml-migrate-parsetree" @@ -109,6 +110,8 @@ depends: [ ] conflicts: [ "checkseum" { = "0.5.0" } + "cohttp-lwt" + "cohttp-lwt-unix" "data-encoding" "hacl_x25519" "json_data_encoding" diff --git a/opam/virtual/octez-deps.opam.locked b/opam/virtual/octez-deps.opam.locked index ba4cc75ac5fa..3961d8999475 100644 --- a/opam/virtual/octez-deps.opam.locked +++ b/opam/virtual/octez-deps.opam.locked @@ -44,8 +44,6 @@ depends: [ "class_group_vdf" {= "0.0.4"} "cmdliner" {= "1.2.0"} "cohttp" {= "5.3.1"} - "cohttp-lwt" {= "5.3.0"} - "cohttp-lwt-unix" {= "5.3.0"} "conduit" {= "6.2.2"} "conduit-lwt" {= "6.2.2"} "conduit-lwt-unix" {= "6.2.2"} @@ -224,6 +222,8 @@ depends: [ ] conflicts: [ "checkseum" {= "0.5.0"} + "cohttp-lwt" + "cohttp-lwt-unix" "data-encoding" "hacl_x25519" "json_data_encoding" diff --git a/prometheus/app/dune b/prometheus/app/dune index 39204876c6e4..7e3270687b00 100644 --- a/prometheus/app/dune +++ b/prometheus/app/dune @@ -8,7 +8,7 @@ (libraries octez-libs.prometheus lwt - cohttp-lwt + octez-libs.cohttp-lwt astring asetmap fmt @@ -24,8 +24,8 @@ octez-libs.prometheus octez-libs.prometheus-app cmdliner - cohttp-lwt - cohttp-lwt-unix + octez-libs.cohttp-lwt + octez-libs.cohttp-lwt-unix logs.fmt fmt.tty) (wrapped false) diff --git a/prometheus/examples/dune b/prometheus/examples/dune index 9d7fde9a29b9..80b82fb70627 100644 --- a/prometheus/examples/dune +++ b/prometheus/examples/dune @@ -8,5 +8,5 @@ octez-libs.prometheus octez-libs.prometheus-app.unix cmdliner - cohttp-lwt - cohttp-lwt-unix)) + octez-libs.cohttp-lwt + octez-libs.cohttp-lwt-unix)) diff --git a/resto/src/dune b/resto/src/dune index d84684e76c4f..8b4ee5293805 100644 --- a/resto/src/dune +++ b/resto/src/dune @@ -26,7 +26,7 @@ (libraries octez-libs.resto octez-libs.resto-directory - cohttp-lwt) + octez-libs.cohttp-lwt) (modules media_type cors)) (library @@ -45,7 +45,7 @@ octez-libs.resto-directory octez-libs.resto-acl octez-libs.resto-cohttp - cohttp-lwt-unix + octez-libs.cohttp-lwt-unix conduit-lwt-unix) (flags (:standard) diff --git a/resto/test/dune b/resto/test/dune index 980256716bc6..e894177b1bc8 100644 --- a/resto/test/dune +++ b/resto/test/dune @@ -40,8 +40,8 @@ lwt.unix octez-libs.json-data-encoding ezjsonm - cohttp-lwt - cohttp-lwt-unix + octez-libs.cohttp-lwt + octez-libs.cohttp-lwt-unix octez-libs.resto-json octez-libs.resto octez-libs.resto-acl @@ -61,8 +61,8 @@ lwt.unix octez-libs.json-data-encoding ezjsonm - cohttp-lwt - cohttp-lwt-unix + octez-libs.cohttp-lwt + octez-libs.cohttp-lwt-unix octez-libs.resto-json octez-libs.resto-acl octez-libs.resto-cohttp-server diff --git a/script-inputs/octez-source-content b/script-inputs/octez-source-content index ceb97341967e..b9c83f8685ad 100644 --- a/script-inputs/octez-source-content +++ b/script-inputs/octez-source-content @@ -13,5 +13,6 @@ irmin brassaia rust-toolchain data-encoding +cohttp prometheus resto diff --git a/src/lib_base/index.mld b/src/lib_base/index.mld index 058ace959260..a49b7e27b814 100644 --- a/src/lib_base/index.mld +++ b/src/lib_base/index.mld @@ -54,6 +54,7 @@ It contains the following libraries: - {{!module-Tezos_version}Tezos_version}: Version information generated from Git - {{!page-tezos_workers}Tezos_workers}: Worker library - {{!module-Tezt_wrapper}Tezt_wrapper} +- {{!module-Cohttp}Cohttp} - {{!module-Data_encoding}Data_encoding} - {{!module-Prometheus}Prometheus} - {{!module-Resto}Resto} diff --git a/src/lib_rpc_http/dune b/src/lib_rpc_http/dune index 88f73770c232..e95e149f2939 100644 --- a/src/lib_rpc_http/dune +++ b/src/lib_rpc_http/dune @@ -37,7 +37,7 @@ (libraries octez-libs.stdlib-unix octez-libs.base - cohttp-lwt-unix + octez-libs.cohttp-lwt-unix octez-libs.resto-cohttp-client octez-libs.rpc octez-libs.rpc-http-client) @@ -54,7 +54,7 @@ (libraries octez-libs.base octez-libs.stdlib-unix - cohttp-lwt-unix + octez-libs.cohttp-lwt-unix octez-libs.resto-cohttp-server octez-libs.resto-acl octez-libs.rpc diff --git a/src/lib_smart_rollup_node/dune b/src/lib_smart_rollup_node/dune index f7b757fa02cb..bfeea52dc19c 100644 --- a/src/lib_smart_rollup_node/dune +++ b/src/lib_smart_rollup_node/dune @@ -12,7 +12,7 @@ octez-libs.crypto octez-shell-libs.client-base octez-shell-libs.client-base-unix - cohttp-lwt-unix + octez-libs.cohttp-lwt-unix tezos-openapi octez-node-config octez-libs.prometheus-app diff --git a/src/lib_wasm_debugger/dune b/src/lib_wasm_debugger/dune index abdf9d69101f..df7da94f15b2 100644 --- a/src/lib_wasm_debugger/dune +++ b/src/lib_wasm_debugger/dune @@ -11,7 +11,7 @@ octez-libs.tree-encoding octez-libs.base.unix octez-protocol-alpha-libs.client - cohttp-lwt-unix + octez-libs.cohttp-lwt-unix octez-l2-libs.scoru-wasm octez-l2-libs.scoru-wasm-helpers octez-l2-libs.smart-rollup diff --git a/tezt/lib_performance_regression/dune b/tezt/lib_performance_regression/dune index 1dd82c556248..2c7f422d1241 100644 --- a/tezt/lib_performance_regression/dune +++ b/tezt/lib_performance_regression/dune @@ -7,7 +7,7 @@ (libraries octez-libs.tezt-wrapper uri - cohttp-lwt-unix) + octez-libs.cohttp-lwt-unix) (flags (:standard) -open Tezt_wrapper diff --git a/tezt/lib_tezos/dune b/tezt/lib_tezos/dune index e406f0557820..b5357a4d9227 100644 --- a/tezt/lib_tezos/dune +++ b/tezt/lib_tezos/dune @@ -13,7 +13,7 @@ octez-libs.crypto-dal octez-libs.base octez-libs.base.unix - cohttp-lwt-unix) + octez-libs.cohttp-lwt-unix) (flags (:standard) -open Tezt_wrapper -- GitLab From aeada340adbc33f2a46c846383bd5d5c9c21af4a Mon Sep 17 00:00:00 2001 From: Gabriel Moise Date: Thu, 18 Jul 2024 16:54:34 +0100 Subject: [PATCH 4/5] Docker: Add cohttp to build file for docker --- build.Dockerfile | 1 + 1 file changed, 1 insertion(+) diff --git a/build.Dockerfile b/build.Dockerfile index e0d07bfa1b3f..59deb72f9729 100644 --- a/build.Dockerfile +++ b/build.Dockerfile @@ -37,6 +37,7 @@ COPY --chown=tezos:nogroup dune-workspace tezos/dune-workspace COPY --chown=tezos:nogroup dune-project tezos/dune-project COPY --chown=tezos:nogroup vendors tezos/vendors COPY --chown=tezos:nogroup rust-toolchain tezos/rust-toolchain +COPY --chown=tezos:nogroup cohttp tezos/cohttp COPY --chown=tezos:nogroup resto tezos/resto COPY --chown=tezos:nogroup prometheus tezos/prometheus ENV GIT_SHORTREF=${GIT_SHORTREF} -- GitLab From 64a8118550111eeaca2da6001f94f83e3d1f7b5f Mon Sep 17 00:00:00 2001 From: Gabriel Moise Date: Thu, 18 Jul 2024 17:51:08 +0100 Subject: [PATCH 5/5] Cohttp: Adapt documentation for CI --- cohttp/cohttp-lwt/src/s.ml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cohttp/cohttp-lwt/src/s.ml b/cohttp/cohttp-lwt/src/s.ml index fcddd36abf50..196435f759ba 100644 --- a/cohttp/cohttp-lwt/src/s.ml +++ b/cohttp/cohttp-lwt/src/s.ml @@ -146,11 +146,11 @@ module type Server = sig - Using [`Response], with a {!Response.t} and a {!Body.t}. - Using [`Expert], with a {!Response.t} and an IO function that is expected to write the response body. The IO function has access to the - underlying {!IO.ic} and {!IO.oc}, which allows writing a response body + underlying [!IO.ic] and [!IO.oc], which allows writing a response body more efficiently, stream a response or to switch protocols entirely (e.g. websockets). Processing of pipelined requests continue after the {!unit Lwt.t} is resolved. The connection can be closed by closing the - {!IO.ic}. *) + [!IO.ic]. *) type response_action = [ `Expert of Cohttp.Response.t * (IO.ic -> IO.oc -> unit Lwt.t) | `Response of Cohttp.Response.t * Body.t ] -- GitLab