From e87f913d46f9867d3dde026bd3ddb404efd174f3 Mon Sep 17 00:00:00 2001 From: Marc Biedermann Date: Thu, 23 Jan 2025 09:53:03 +0100 Subject: [PATCH] update dependencies, use digestif for md5 and sha1, update deprecations of Term and remove dependency to mirage-crypto --- README.md | 4 +-- dune-project | 12 +++---- example/graphql/main.ml | 7 ++-- opium-graphql/src/opium_graphql.ml | 5 ++- opium.opam | 2 +- opium/src/app.ml | 35 +++++++++---------- opium/src/cookie.ml | 13 ++----- opium/src/middlewares/middleware_etag.ml | 7 ++-- .../src/middlewares/middleware_static_unix.ml | 5 ++- 9 files changed, 40 insertions(+), 50 deletions(-) diff --git a/README.md b/README.md index 4638dbb7..86cebe6e 100644 --- a/README.md +++ b/README.md @@ -195,10 +195,10 @@ $ dune build example/simple_middleware/main.ml ``` Here we also use the ability of Opium to generate a cmdliner term to run your -app. Run your executable with `--help` to see the options that are available to you. +app. Run your executable with `-h` to see the options that are available to you. For example: ``` # run in debug mode on port 9000 -$ dune exec example/simple_middleware/main.exe -- -p 9000 -d +$ dune exec dune build example/simple_middleware/main.exe -- -p 9000 -d ``` diff --git a/dune-project b/dune-project index 616ffcea..336ab463 100644 --- a/dune-project +++ b/dune-project @@ -18,9 +18,9 @@ (package (name rock) (synopsis - "Minimalist framework to build extensible HTTP servers and clients") + "Minimalist framework to build extensible HTTP servers and clients") (description - "Rock is a Unix indpendent API to build extensible HTTP servers and clients. It provides building blocks such as middlewares and handlers (a.k.a controllers).") + "Rock is a Unix indpendent API to build extensible HTTP servers and clients. It provides building blocks such as middlewares and handlers (a.k.a controllers).") (depends (ocaml (>= 4.08)) @@ -37,7 +37,7 @@ (name opium) (synopsis "OCaml web framework") (description - "Opium is a web framework for OCaml that provides everything you need to build safe, fast and extensible web applications.") + "Opium is a web framework for OCaml that provides everything you need to build safe, fast and extensible web applications.") (depends (ocaml (>= 4.08)) @@ -54,7 +54,7 @@ magic-mime yojson tyxml - mirage-crypto + digestif (base64 (>= 3.0.0)) astring @@ -71,7 +71,7 @@ (name opium-testing) (synopsis "Testing library for Opium") (description - "A library that provides helpers to easily test your Opium applications.") + "A library that provides helpers to easily test your Opium applications.") (depends (ocaml (>= 4.08)) @@ -85,7 +85,7 @@ (name opium-graphql) (synopsis "Run GraphQL servers with Opium") (description - "This package allows you to execute Opium requests against GraphQL schemas built with `graphql`.") + "This package allows you to execute Opium requests against GraphQL schemas built with `graphql`.") (depends (ocaml (>= 4.08)) diff --git a/example/graphql/main.ml b/example/graphql/main.ml index b5da0bcb..0ad4d5cd 100644 --- a/example/graphql/main.ml +++ b/example/graphql/main.ml @@ -29,7 +29,10 @@ module Schema = struct let user : (context, user option) Graphql_lwt.Schema.typ = Schema.( - obj "user" ~doc:"A user in the system" ~fields:(fun _ -> + obj + "user" + ~doc:"A user in the system" + ~fields: [ field "id" ~doc:"Unique user identifier" @@ -46,7 +49,7 @@ module Schema = struct ~typ:(non_null role) ~args:Arg.[] ~resolve:(fun _info p -> p.role) - ])) + ]) ;; let schema = diff --git a/opium-graphql/src/opium_graphql.ml b/opium-graphql/src/opium_graphql.ml index b1da0cc4..9b16827c 100644 --- a/opium-graphql/src/opium_graphql.ml +++ b/opium-graphql/src/opium_graphql.ml @@ -139,9 +139,8 @@ let make_handler let graphiql_etag = Asset.read "graphiql.html" |> Option.get - |> Cstruct.of_string - |> Mirage_crypto.Hash.digest `MD5 - |> Cstruct.to_string + |> Digestif.MD5.digest_string + |> Digestif.MD5.to_raw_string |> Base64.encode_exn ;; diff --git a/opium.opam b/opium.opam index 85229237..2037442d 100644 --- a/opium.opam +++ b/opium.opam @@ -23,7 +23,7 @@ depends: [ "magic-mime" "yojson" "tyxml" - "mirage-crypto" + "digestif" "base64" {>= "3.0.0"} "astring" "re" diff --git a/opium/src/app.ml b/opium/src/app.ml index 87e353f3..121bfda4 100644 --- a/opium/src/app.ml +++ b/opium/src/app.ml @@ -340,8 +340,8 @@ module Cmds = struct let term = let open Cmdliner.Term in fun app -> - pure setup_app - $ pure app + const setup_app + $ const app $ port app.port $ jobs app.jobs $ host app.host @@ -354,31 +354,30 @@ module Cmds = struct let info name = let doc = Printf.sprintf "%s (Opium App)" name in - let man = [] in - Term.info name ~doc ~man + Cmd.info name ~doc ~man:[] ;; end let run_command' app = let open Cmdliner in - let cmd = Cmds.term app in - match Term.eval (cmd, Cmds.info app.name) with - | `Ok a -> + let cmd = Cmd.v (Cmds.info app.name) (Cmds.term app) in + match Cmd.eval_value cmd with + | Ok (`Ok a) -> Lwt.async (fun () -> - let* _server = start a in - Lwt.return_unit); + let* _server = start a in + Lwt.return_unit); let forever, _ = Lwt.wait () in `Ok forever - | `Error _ -> `Error - | _ -> `Not_running + | Error _ -> `Error + | Ok (`Version | `Help) -> `Not_running ;; let run_command app = match app |> run_command' with | `Ok a -> Lwt.async (fun () -> - let* _server = a in - Lwt.return_unit); + let* _server = a in + Lwt.return_unit); let forever, _ = Lwt.wait () in Lwt_main.run forever | `Error -> exit 1 @@ -387,9 +386,9 @@ let run_command app = let run_multicore app = let open Cmdliner in - let cmd = Cmds.term app in - match Term.eval (cmd, Cmds.info app.name) with - | `Ok a -> start_multicore a - | `Error _ -> exit 1 - | _ -> exit 0 + let cmd = Cmd.v (Cmds.info app.name) (Cmds.term app) in + match Cmd.eval_value cmd with + | Ok (`Ok a) -> start_multicore a + | Error _ -> exit 1 + | Ok (`Version | `Help) -> exit 0 ;; diff --git a/opium/src/cookie.ml b/opium/src/cookie.ml index bc8dde07..ca71b1e6 100644 --- a/opium/src/cookie.ml +++ b/opium/src/cookie.ml @@ -65,18 +65,11 @@ module Signer = struct else constant_time_compare' a b 0 ;; - let derive_key t = - Mirage_crypto.Hash.mac - `SHA1 - ~key:(Cstruct.of_string t.secret) - (Cstruct.of_string t.salt) - ;; + let derive_key t = Digestif.SHA1.(hmac_string ~key:t.secret t.salt |> to_hex) let get_signature t value = - value - |> Cstruct.of_string - |> Mirage_crypto.Hash.mac `SHA1 ~key:(derive_key t) - |> Cstruct.to_string + Digestif.SHA1.hmac_string ~key:(derive_key t) value + |> Digestif.SHA1.to_raw_string |> Base64.encode_exn ;; diff --git a/opium/src/middlewares/middleware_etag.ml b/opium/src/middlewares/middleware_etag.ml index fbb273d7..cd8e5ceb 100644 --- a/opium/src/middlewares/middleware_etag.ml +++ b/opium/src/middlewares/middleware_etag.ml @@ -5,11 +5,8 @@ open Import let etag_of_body body = let encode s = - s - |> Cstruct.of_string - |> Mirage_crypto.Hash.digest `MD5 - |> Cstruct.to_string - |> Base64.encode_exn + let open Digestif.MD5 in + s |> digest_string |> to_raw_string |> Base64.encode_exn in match body.Body.content with | `String s -> Some (encode s) diff --git a/opium/src/middlewares/middleware_static_unix.ml b/opium/src/middlewares/middleware_static_unix.ml index d4f3caea..8df18145 100644 --- a/opium/src/middlewares/middleware_static_unix.ml +++ b/opium/src/middlewares/middleware_static_unix.ml @@ -8,9 +8,8 @@ let default_etag ~local_path fname = let* stat = Lwt_unix.stat fpath in let hash = Marshal.to_string stat.st_mtime [] - |> Cstruct.of_string - |> Mirage_crypto.Hash.digest `MD5 - |> Cstruct.to_string + |> Digestif.MD5.digest_string + |> Digestif.MD5.to_raw_string |> Base64.encode_exn in Lwt.return_some hash