package ez_api

  1. Overview
  2. Docs

doc/src/ez_api.server_utils/directory.ml.html

Source file directory.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
open EzAPI
open Lwt.Infix

module StringMap = Map.Make(String)
module MethMap = Map.Make(struct type t = Meth.t let compare = compare end)

module Step = struct
  type t =
    | Static of string
    | Dynamic of Arg.descr
end

type conflict =
  | CService
  | CDir
  | CBuilder
  | CCustom
  | CTypes of Arg.descr * Arg.descr
  | CType of Arg.descr * string list

and 'a directory = {
  services: 'a registered_service MethMap.t ;
  subdirs: 'a subdirectories option
}

and _ subdirectories =
  | Suffixes: 'a directory StringMap.t -> 'a subdirectories
  | Arg: 'a1 Arg.t * ('a * 'a1) directory -> 'a subdirectories

and _ registered_service =
  | Http : {
      service : ('a, 'i, 'o, 'e, 's) Service.t;
      handler : ('a -> 'i -> ('o, 'e) result Answer.t Lwt.t);
    } -> 'a registered_service
  | Websocket : {
      service : ('a, 'i, 'o, 'e, 's) Service.t;
      react : ('a -> 'i -> ('o, 'e) result Lwt.t);
      bg : ('a -> (('o, 'e) result -> unit) -> unit Lwt.t);
      onclose : ('a -> unit Lwt.t) option;
      step : float option;
    } -> 'a registered_service

let empty = { services = MethMap.empty ; subdirs = None }

type t = Req.t directory

type resolved_directory =
    Dir: 'a directory * 'a -> resolved_directory

type lookup_error = [
  | `Not_found
  | `Cannot_parse of Arg.descr * string * string list
  | `Method_not_allowed ]

type handler_error = [
  | EzEncoding.destruct_error
  | `unsupported of string option
  | `handler_exn of exn
  | `handler_error of string ]

type ws_frame = [ `binary of string | `text of string | `none ]

type lookup_ok = [
  | `head | `options of (string * string) list
  | `http of (string -> (string Answer.t, handler_error) result Lwt.t)
  | `ws of ((string -> (ws_frame, handler_error) result Lwt.t) *
            (((ws_frame, handler_error) result -> unit) -> unit Lwt.t) *
            (unit -> unit Lwt.t) option * float option) ]

let rec resolve :
  type a. string list -> a directory -> a -> string list ->
  (resolved_directory, lookup_error) result Lwt.t =
  fun prefix dir args path ->
  match path, dir with
  | [], dir -> Lwt.return_ok (Dir (dir, args))
  | _name :: _path, { subdirs = None; _ } -> Lwt.return_error `Not_found
  | name :: path, { subdirs = Some (Suffixes static); _ } ->
    begin match StringMap.find_opt name static with
      | None -> Lwt.return_error `Not_found
      | Some dir -> resolve (name :: prefix) dir args path
    end
  | name :: path, { subdirs = Some (Arg (arg, dir)); _ } ->
    match arg.Arg.destruct name with
    | Ok x -> resolve (name :: prefix) dir (args, x) path
    | Error msg -> Lwt.return_error @@
      `Cannot_parse (arg.Arg.description, msg, name :: prefix)

let io_to_answer : type a. code:int -> a io -> a -> string Answer.t = fun ~code io body ->
  match io with
  | Empty -> {Answer.code; body=""; headers=[]}
  | Raw l ->
    let content_type = match l with
      | [] -> "application/octet-stream"
      | h :: _ -> Mime.to_string h in
    {Answer.code; body; headers=["content-type", content_type]}
  | Json enc ->
    {Answer.code; body = EzEncoding.construct enc body;
     headers=["content-type", "application/json"]}

let ser_handler :
  type i o e. ?content_type:string -> ('a -> i -> (o, e) result Answer.t Lwt.t) -> 'a ->
  i io -> o io -> e Json_encoding.encoding ->
  string -> (string Answer.t, handler_error) result Lwt.t =
  fun ?content_type handler args input output errors ->
  let handle_result {Answer.code; body; _} = match body with
    | Ok o -> io_to_answer ~code output o
    | Error e ->
      {Answer.code; body = EzEncoding.construct errors e;
       headers=["content-type", "application/json"]} in
  match input with
  | Empty -> (fun _ ->
      Lwt.catch
        (fun () -> handler args () >|= fun r -> Ok (handle_result r))
        (fun exn -> Lwt.return_error (`handler_exn exn)))
  | Raw mimes -> (fun s ->
      if not (Mime.allowed mimes content_type) then
        Lwt.return_error (`unsupported content_type)
      else
        Lwt.catch
          (fun () -> handler args s >|= fun r -> Ok (handle_result r))
          (fun exn -> Lwt.return_error (`handler_exn exn)))
  | Json enc -> (fun (s : string) ->
      match EzEncoding.destruct_res enc s with
      | Ok i ->
        Lwt.catch
          (fun () -> handler args i >|= fun r -> Ok (handle_result r))
          (fun exn -> Lwt.return_error (`handler_exn exn))
      | Error e -> Lwt.return_error e)

let io_to_ws_frame : type a. a io -> a -> ws_frame = fun io a ->
  match io with
  | Empty -> `none
  | Raw _ -> `binary a
  | Json enc -> `text (EzEncoding.construct enc a)

let ser_websocket react bg args input output errors =
  let handle_result r = match r with
    | Ok x -> Ok (io_to_ws_frame output x)
    | Error e -> Error (`handler_error (EzEncoding.construct errors e)) in
  let bg send = bg args (fun r -> send @@ handle_result r) in
  let react s =
    Lwt.catch
      (fun () ->
         match IO.from_string input (fun i -> react args i >|= handle_result) s with
         | Ok p -> p
         | Error e -> Lwt.return_error e)
      (fun exn -> Lwt.return_error (`handler_exn exn)) in
  react, bg

let lookup ?meth ?content_type dir r path : (lookup_ok, lookup_error) result Lwt.t =
  resolve [] dir r path >>= function
  | Error _ as err -> Lwt.return err
  | Ok (Dir (dir, args)) ->
    match meth with
    | None | Some `OPTIONS ->
      begin match MethMap.bindings dir.services with
        | [] -> Lwt.return_error `Not_found
        | l ->
          let meths = Meth.headers @@ List.map fst l in
          let sec_set = List.fold_left (fun acc (_, rs) -> match rs with
              | Http {service; _} -> Security.StringSet.union acc (Security.headers (Service.security service))
              | Websocket _ -> acc) Security.StringSet.empty l in
          Lwt.return_ok @@ `options (meths @ (Security.header sec_set))
      end
    | Some `HEAD -> Lwt.return_ok `head
    | Some (#Meth.t as m) ->
      match m, MethMap.find_opt m dir.services with
      | _, Some (Http {service; handler}) ->
        let input = Service.input service in
        let output = Service.output service in
        let errors = Service.errors_encoding service in
        let h = ser_handler ?content_type handler args input output errors in
        Lwt.return_ok @@ `http h
      | `GET, Some (Websocket {service; react; bg; onclose; step}) ->
        let input = Service.input service in
        let output = Service.output service in
        let errors = Service.errors_encoding service in
        let react, bg = ser_websocket react bg args input output errors in
        let onclose = match onclose with None -> None | Some f -> Some (fun () -> f args) in
        Lwt.return_ok @@ `ws (react, bg, onclose, step)
      | _ -> Lwt.return_error `Method_not_allowed

let step_of_path path =
  let rec aux : type r p. (r, p) Path.t -> Step.t list -> Step.t list = fun path acc ->
    match path with
    | Path.Root -> acc
    | Path.Static (path, name) -> aux path (Step.Static name :: acc)
    | Path.Dynamic (path, arg) -> aux path (Step.Dynamic arg.Arg.description :: acc) in
  aux path []

let conflict path kind = Error (step_of_path path, kind)

let rec insert
  : type r a.
    (r, a) Path.t -> r directory ->
    (a directory * (a directory -> r directory), Step.t list * conflict) result
  = fun path dir ->
    match path with
    | Path.Root -> Ok (dir, (fun x -> x))
    | Path.Static (subpath, name) -> begin
        match insert subpath dir with
        | Error c -> Error c
        | Ok (subdir, rebuild) ->
          let r = match subdir with
            | { subdirs = None; services } -> Ok (StringMap.empty, services)
            | { subdirs = Some (Suffixes m); services } -> Ok (m, services)
            | { subdirs = Some (Arg (arg, _)); _ } ->
              conflict path (CType (arg.Arg.description, [name])) in
          match r with
          | Error c -> Error c
          | Ok (dirmap, services) ->
            let dir = match StringMap.find_opt name dirmap with
              | None -> empty
              | Some dir -> dir in
            let rebuild s =
              let subdirs = Some (Suffixes (StringMap.add name s dirmap)) in
              rebuild ({ subdirs ; services }) in
            Ok (dir, rebuild)
      end
    | Path.Dynamic (subpath, arg) -> begin
        match insert subpath dir with
        | Error c -> Error c
        | Ok (subdir, rebuild) ->
          let r = match subdir with
            | { subdirs = None ; services } -> Ok (empty, services)
            | { subdirs = Some (Arg (arg', dir)); services } -> begin
                try
                  let Arg.Ty.Eq = Arg.Ty.eq arg.Arg.id arg'.Arg.id in
                  Ok ((dir :> a directory), services)
                with Arg.Ty.Not_equal ->
                  conflict path (CTypes (arg.Arg.description, arg'.Arg.description))
              end
            | { subdirs = Some (Suffixes m); _ } ->
              conflict path
                (CType (arg.Arg.description, List.map fst (StringMap.bindings m))) in
          match r with
          | Error c -> Error c
          | Ok (dir, services) ->
            let rebuild s =
              let subdirs = Some (Arg (arg, s)) in
              rebuild { subdirs ; services } in
            Ok (dir, rebuild)
      end

let register :
  type a.
  t -> (a, _, _, _, _) Service.t ->
  ((a, _, _, _, _) Service.t -> a registered_service) ->
  (t, Step.t list * conflict) result =
  fun root service f ->
  let path = Service.path service in
  match insert path root with
  | Error c-> Error c
  | Ok (dir, insert) ->
    let rs = f service in
    let meth = Service.meth service in
    match dir with
    | { services ; subdirs = _ } as dir when not (MethMap.mem meth services) ->
      Ok (insert { dir with services = MethMap.add meth rs services })
    | _ -> conflict path CService

let register_http root service handler =
  register root service (fun service -> Http {service; handler})

let register_ws root ?onclose ?step ~react ~bg service =
  register root service (fun service -> Websocket {service; react; bg; onclose; step})
OCaml

Innovation. Community. Security.