package activitypub_client

  1. Overview
  2. Docs

Source file http.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
(*********************************************************************************)
(*                OCaml-ActivityPub                                              *)
(*                                                                               *)
(*    Copyright (C) 2023-2024 INRIA All rights reserved.                         *)
(*    Author: Maxence Guesdon, INRIA Saclay                                      *)
(*                                                                               *)
(*    This program is free software; you can redistribute it and/or modify       *)
(*    it under the terms of the GNU Lesser General Public License version        *)
(*    3 as published by the Free Software Foundation.                            *)
(*                                                                               *)
(*    This program is distributed in the hope that it will be useful,            *)
(*    but WITHOUT ANY WARRANTY; without even the implied warranty of             *)
(*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *)
(*    GNU General Public License for more details.                               *)
(*                                                                               *)
(*    You should have received a copy of the GNU General Public License          *)
(*    along with this program; if not, write to the Free Software                *)
(*    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA                   *)
(*    02111-1307  USA                                                            *)
(*                                                                               *)
(*    Contact: maxence.guesdon@inria.fr                                          *)
(*                                                                               *)
(*********************************************************************************)

(** HTTP requests as a given actor *)

(** Note that the {!Ldp.Http.user_agent} is set to ["ocaml-activitypub"]. *)

module type T = sig
  include Ldp.Http.Http
  end

(**/**)

let () = Ldp.Http.user_agent := "ocaml-activitypub"

let mk_cache actor_iri ~root ~delay =
  let%lwt c = Activitypub.Cache.mk_cache ~root ~delay in
  let root_iri = Iri.(with_path actor_iri (Absolute [])) in
  let module C = struct
    include (val c : Ldp.Http.Cache_impl)
    let key h iri =
      match Activitypub.Utils.is_prefix_iri root_iri iri with
      | true -> None (* no cache for actor server *)
      | false -> key h iri
  end
  in
  Lwt.return (module C : Ldp.Http.Cache_impl)

(**/**)

(** [mk_http actor_iri actor_conf] creates a {!T} module to perform
  HTTP queries as the given actor. *)
let mk_http actor_iri actor_conf =
  let%lwt cache_impl =
    match actor_conf.Conf.cache_dir with
    | None ->
        Activitypub.Log.info (fun m -> m "no cache");
        Lwt.return_none
    | Some root ->
        let%lwt c = mk_cache actor_iri ~root ~delay:actor_conf.cache_delay in
        let module C = Ldp.Http.Make_cache (val c : Ldp.Http.Cache_impl) in
        Lwt.return_some (module C : Ldp.Http.Cache)
  in
  let%lwt h = Ldp_tls.make
    ?cache_impl
    ~dbg:(fun str -> Activitypub.Log.debug (fun m -> m "%s" str); Lwt.return_unit)
    ()
  in
  Lwt.return h