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
72
73
74
75
76
exception Not_a_directory of string
let of_dir dirname =
let dirname =
if Filename.is_relative dirname then
Filename.concat (Sys.getcwd()) dirname
else
dirname
in
let module I : Http.Cache_impl =
struct
type key = string
let key iri =
let str = Printf.sprintf "%s\n%s"
(Iri.to_string iri)
(Cohttp.Header.to_string headers)
in
Some (Digest.to_hex (Digest.string str))
let store key resp body =
let filename = Filename.concat dirname key in
let str = Marshal.to_string (resp, body) [] in
let%lwt () = Lwt_io.(with_file Output filename
(fun oc -> write oc str))
in
Lwt.return (resp, body)
let find key =
let filename = Filename.concat dirname key in
try%lwt
let%lwt str = Lwt_io.(with_file Input filename read) in
let (x : Cohttp.Response.t * string) = Marshal.from_string str 0 in
Lwt.return (Http.Found x)
with _ -> Lwt.return Http.Not_found
let clear () =
Lwt_stream.iter_p
(fun f -> try%lwt Lwt_unix.unlink f with _ -> Lwt.return_unit)
(Lwt_unix.files_of_directory dirname)
end
in
let%lwt () =
match Sys.is_directory dirname with
| true -> Lwt.return_unit
| false -> Lwt.fail (Not_a_directory dirname)
| exception _ -> Lwt_unix.mkdir dirname 0o750
in
let module C = Http.Make_cache (I) in
Lwt.return (module C : Http.Cache)