Source file jsonrpc.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
(*
 * Copyright (c) 2006-2009 Citrix Systems Inc.
 * Copyright (c) 2006-2014 Thomas Gazagnaire <thomas@gazagnaire.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *)

open Rpc

module Yojson_private = struct
  include Yojson.Safe

  let from_string ?(strict = true) ?buf ?fname ?lnum s =
    let open Yojson in
    try
      let lexbuf = Lexing.from_string s in
      let v = init_lexer ?buf ?fname ?lnum () in
      if strict then from_lexbuf v lexbuf else from_lexbuf v ~stream:true lexbuf
    with
    | End_of_input -> json_error "Blank input data"
end

module Y = Yojson_private
module U = Yojson.Basic.Util

type version =
  | V1
  | V2

let rec rpc_to_json t =
  match t with
  | Int i -> `Intlit (Int64.to_string i)
  | Int32 i -> `Int (Int32.to_int i)
  | Bool b -> `Bool b
  | Float r -> `Float r
  | String s -> `String s
  | DateTime d -> `String d
  | Base64 b -> `String b
  | Null -> `Null
  | Enum a -> `List (Rpcmarshal.tailrec_map rpc_to_json a)
  | Dict a -> `Assoc (Rpcmarshal.tailrec_map (fun (k, v) -> k, rpc_to_json v) a)


exception JsonToRpcError of Y.t

let rec json_to_rpc t =
  match t with
  | `Intlit i -> Int (Int64.of_string i)
  | `Int i -> Int (Int64.of_int i)
  | `Bool b -> Bool b
  | `Float r -> Float r
  | `String s -> (* TODO: check if it is a DateTime *) String s
  (* | DateTime d -> `String d *)
  (* | Base64 b -> `String b *)
  | `Null -> Null
  | `List a -> Enum (Rpcmarshal.tailrec_map json_to_rpc a)
  | `Assoc a -> Dict (Rpcmarshal.tailrec_map (fun (k, v) -> k, json_to_rpc v) a)
  | unsupported -> raise (JsonToRpcError unsupported)


let to_fct t f = rpc_to_json t |> Y.to_string |> f
let to_buffer t buf = to_fct t (fun s -> Buffer.add_string buf s)
let to_string t = rpc_to_json t |> Y.to_string

let to_a ~empty ~append t =
  let buf = empty () in
  to_fct t (fun s -> append buf s);
  buf


let new_id =
  let count = ref 0L in
  fun () ->
    count := Int64.add 1L !count;
    !count


let string_of_call ?(version = V1) call =
  let json =
    match version with
    | V1 -> [ "method", String call.name; "params", Enum call.params ]
    | V2 ->
      let params =
        match call.params with
        | [ Dict x ] -> Dict x
        | _ -> Enum call.params
      in
      [ "jsonrpc", String "2.0"; "method", String call.name; "params", params ]
  in
  let json =
    if not call.is_notification then json @ [ "id", Int (new_id ()) ] else json
  in
  to_string (Dict json)


let json_of_response ?(id = Int 0L) version response =
  if response.Rpc.success
  then (
    match version with
    | V1 -> Dict [ "result", response.Rpc.contents; "error", Null; "id", id ]
    | V2 -> Dict [ "jsonrpc", String "2.0"; "result", response.Rpc.contents; "id", id ])
  else (
    match version with
    | V1 -> Dict [ "result", Null; "error", response.Rpc.contents; "id", id ]
    | V2 -> Dict [ "jsonrpc", String "2.0"; "error", response.Rpc.contents; "id", id ])


let json_of_error_object ?(data = None) code message =
  let data_json =
    match data with
    | Some d -> [ "data", d ]
    | None -> []
  in
  Dict ([ "code", Int code; "message", String message ] @ data_json)


let string_of_response ?(id = Int 0L) ?(version = V1) response =
  let json = json_of_response ~id version response in
  to_string json


let a_of_response ?(id = Int 0L) ?(version = V1) ~empty ~append response =
  let json = json_of_response ~id version response in
  to_a ~empty ~append json


let of_string ?(strict = true) s = s |> Y.from_string ~strict |> json_to_rpc

let of_a ~next_char b =
  let buf = Buffer.create 2048 in
  let rec acc () =
    match next_char b with
    | Some c ->
      Buffer.add_char buf c;
      acc ()
    | None -> ()
  in
  acc ();
  Buffer.contents buf |> of_string


let get' name dict =
  try Some (List.assoc name dict) with
  | Not_found -> None


exception Malformed_method_request of string
exception Malformed_method_response of string
exception Missing_field of string

let get name dict =
  match get' name dict with
  | None ->
    if Rpc.get_debug () then Printf.eprintf "%s was not found in the dictionary\n" name;
    raise (Missing_field name)
  | Some v -> v


let version_id_and_call_of_string_option str =
  try
    match of_string str with
    | Dict d ->
      let name =
        match get "method" d with
        | String s -> s
        | _ -> raise (Malformed_method_request "Invalid field 'method' in request body")
      in
      let version =
        match get' "jsonrpc" d with
        | None -> V1
        | Some (String "2.0") -> V2
        | _ -> raise (Malformed_method_request "Invalid field 'jsonrpc' in request body")
      in
      let params =
        match version with
        | V1 ->
          (match get "params" d with
          | Enum l -> l
          | _ -> raise (Malformed_method_request "Invalid field 'params' in request body"))
        | V2 ->
          (match get' "params" d with
          | None | Some Null -> []
          | Some (Enum l) -> l
          | Some (Dict l) -> [ Dict l ]
          | _ -> raise (Malformed_method_request "Invalid field 'params' in request body"))
      in
      let id =
        match get' "id" d with
        | None | Some Null -> None (* is a notification *)
        | Some (Int a) -> Some (Int a)
        | Some (String a) -> Some (String a)
        | Some _ -> raise (Malformed_method_request "Invalid field 'id' in request body")
      in
      let c = call name params in
      version, id, { c with is_notification = id == None }
    | _ -> raise (Malformed_method_request "Invalid request body")
  with
  | Missing_field field ->
    raise (Malformed_method_request (Printf.sprintf "Required field %s is missing" field))
  | JsonToRpcError json ->
    raise
      (Malformed_method_request (Printf.sprintf "Unable to parse %s" (Y.to_string json)))


let version_id_and_call_of_string s =
  let version, id_, call = version_id_and_call_of_string_option s in
  match id_ with
  | Some id -> version, id, call
  | None -> raise (Malformed_method_request "Invalid field 'id' in request body")


let call_of_string str =
  let _, _, call = version_id_and_call_of_string str in
  call


(* This functions parses the json and tries to extract a valid jsonrpc response
 * (See http://www.jsonrpc.org/ for the exact specs). *)
let get_response extractor str =
  try
    match extractor str with
    | Dict d ->
      let _ =
        match get "id" d with
        | Int _ as x -> x
        | String _ as y -> y
        | _ -> raise (Malformed_method_response "id")
      in
      (match get' "jsonrpc" d with
      | None ->
        let result = get "result" d in
        let error = get "error" d in
        (match result, error with
        | v, Null -> success v
        | Null, v -> failure v
        | x, y ->
          raise
            (Malformed_method_response
               (Printf.sprintf
                  "<result=%s><error=%s>"
                  (Rpc.to_string x)
                  (Rpc.to_string y))))
      | Some (String "2.0") ->
        let result = get' "result" d in
        let error = get' "error" d in
        (match result, error with
        | Some v, None -> success v
        | None, Some v ->
          (match v with
          | Dict err ->
            let (_ : int64) =
              match get "code" err with
              | Int i -> i
              | _ -> raise (Malformed_method_response "Error code")
            in
            let _ =
              match get "message" err with
              | String s -> s
              | _ -> raise (Malformed_method_response "Error message")
            in
            failure v
          | _ -> raise (Malformed_method_response "Error object"))
        | Some x, Some y ->
          raise
            (Malformed_method_response
               (Printf.sprintf
                  "<result=%s><error=%s>"
                  (Rpc.to_string x)
                  (Rpc.to_string y)))
        | None, None ->
          raise
            (Malformed_method_response
               (Printf.sprintf "neither <result> nor <error> was found")))
      | _ -> raise (Malformed_method_response "jsonrpc"))
    | rpc ->
      raise
        (Malformed_method_response
           (Printf.sprintf "<response_of_stream(%s)>" (to_string rpc)))
  with
  | Missing_field field ->
    raise (Malformed_method_response (Printf.sprintf "<%s was not found>" field))
  | JsonToRpcError json ->
    raise
      (Malformed_method_response
         (Printf.sprintf "<unable to parse %s>" (Y.to_string json)))


let response_of_string ?(strict = true) str = get_response (of_string ~strict) str

let response_of_in_channel channel =
  let of_channel s = s |> Y.from_channel |> json_to_rpc in
  get_response of_channel channel