|
| 1 | +(* |
| 2 | + * Copyright (C) Citrix Systems Inc. |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU Lesser General Public License as published |
| 6 | + * by the Free Software Foundation; version 2.1 only. with the special |
| 7 | + * exception on linking described in file LICENSE. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU Lesser General Public License for more details. |
| 13 | + *) |
| 14 | + |
| 15 | +(* Generic RPC marshalling functions for XCP services *) |
| 16 | + |
| 17 | +module Request = Cohttp.Request.Make (Cohttp_posix_io.Buffered_IO) |
| 18 | +module Response = Cohttp.Response.Make (Cohttp_posix_io.Buffered_IO) |
| 19 | + |
| 20 | +let get_user_agent () = Sys.argv.(0) |
| 21 | + |
| 22 | +let switch_path = ref "/var/run/message-switch/sock" |
| 23 | + |
| 24 | +let use_switch = ref true |
| 25 | + |
| 26 | +let get_ok = function |
| 27 | + | `Ok x -> |
| 28 | + x |
| 29 | + | `Error e -> |
| 30 | + let b = Buffer.create 16 in |
| 31 | + let fmt = Format.formatter_of_buffer b in |
| 32 | + Message_switch_unix.Protocol_unix.Client.pp_error fmt e ; |
| 33 | + Format.pp_print_flush fmt () ; |
| 34 | + failwith (Buffer.contents b) |
| 35 | + |
| 36 | +let switch_rpc ?timeout queue_name string_of_call response_of_string = |
| 37 | + let t = |
| 38 | + get_ok |
| 39 | + (Message_switch_unix.Protocol_unix.Client.connect ~switch:!switch_path ()) |
| 40 | + in |
| 41 | + fun call -> |
| 42 | + response_of_string |
| 43 | + (get_ok |
| 44 | + (Message_switch_unix.Protocol_unix.Client.rpc ~t ?timeout |
| 45 | + ~queue:queue_name ~body:(string_of_call call) ())) |
| 46 | + |
| 47 | +let split_colon str = |
| 48 | + try |
| 49 | + let x = String.index str ':' in |
| 50 | + let uname = String.sub str 0 x in |
| 51 | + let passwd = String.sub str (x + 1) (String.length str - x - 1) in |
| 52 | + [uname; passwd] |
| 53 | + with Not_found -> [str] |
| 54 | + |
| 55 | +(* Use HTTP to frame RPC messages *) |
| 56 | +[@@@ocaml.warning "-27"] |
| 57 | + |
| 58 | +let http_rpc string_of_call response_of_string ?(srcstr = "unset") |
| 59 | + ?(dststr = "unset") url call = |
| 60 | + let uri = Uri.of_string (url ()) in |
| 61 | + let req = string_of_call call in |
| 62 | + let headers = |
| 63 | + Cohttp.Header.of_list |
| 64 | + [ |
| 65 | + ("User-agent", get_user_agent ()) |
| 66 | + ; ("content-length", string_of_int (String.length req)) |
| 67 | + ] |
| 68 | + in |
| 69 | + (* If we have a username:password@ then use basic authentication *) |
| 70 | + let userinfo = Uri.userinfo uri in |
| 71 | + let headers = |
| 72 | + match userinfo with |
| 73 | + | Some x -> ( |
| 74 | + match split_colon x with |
| 75 | + | [username; password] -> |
| 76 | + Cohttp.Header.add_authorization headers (`Basic (username, password)) |
| 77 | + | _ -> |
| 78 | + headers |
| 79 | + ) |
| 80 | + | None -> |
| 81 | + headers |
| 82 | + in |
| 83 | + let http_req = |
| 84 | + Cohttp.Request.make ~meth:`POST ~version:`HTTP_1_1 ~headers uri |
| 85 | + in |
| 86 | + Open_uri_patched.with_open_uri uri (fun fd -> |
| 87 | + let ic = Unix.in_channel_of_descr fd in |
| 88 | + let oc = Unix.out_channel_of_descr fd in |
| 89 | + Request.write (fun writer -> Request.write_body writer req) http_req oc ; |
| 90 | + match Response.read ic with |
| 91 | + | `Eof -> |
| 92 | + failwith |
| 93 | + (Printf.sprintf "Failed to read HTTP response from: %s" (url ())) |
| 94 | + | `Invalid x -> |
| 95 | + failwith |
| 96 | + (Printf.sprintf "Failed to read HTTP response from: %s (got '%s')" |
| 97 | + (url ()) x) |
| 98 | + | `Ok response -> ( |
| 99 | + let body = Buffer.create 16 in |
| 100 | + let reader = Response.make_body_reader response ic in |
| 101 | + let rec loop () = |
| 102 | + match Response.read_body_chunk reader with |
| 103 | + | Cohttp.Transfer.Chunk x -> |
| 104 | + Buffer.add_string body x ; loop () |
| 105 | + | Cohttp.Transfer.Final_chunk x -> |
| 106 | + Buffer.add_string body x |
| 107 | + | Cohttp.Transfer.Done -> |
| 108 | + () |
| 109 | + in |
| 110 | + loop () ; |
| 111 | + let body = Buffer.contents body |> response_of_string in |
| 112 | + match Cohttp.Response.status response with |
| 113 | + | `OK -> |
| 114 | + body |
| 115 | + | bad -> |
| 116 | + failwith |
| 117 | + (Printf.sprintf "Unexpected HTTP response code: %s" |
| 118 | + (Cohttp.Code.string_of_status bad)) |
| 119 | + )) |
| 120 | + |
| 121 | +let xml_http_rpc = http_rpc Xmlrpc.string_of_call Xmlrpc.response_of_string |
| 122 | + |
| 123 | +let json_switch_rpc ?timeout queue_name = |
| 124 | + switch_rpc ?timeout queue_name Jsonrpc.string_of_call |
| 125 | + Jsonrpc.response_of_string |
| 126 | + |
| 127 | +let () = |
| 128 | + Printexc.register_printer (function |
| 129 | + | Xmlm.Error ((line, col), error) -> |
| 130 | + Some |
| 131 | + (Printf.sprintf "Xmlm.Error(%d:%d, \"%s\")" line col |
| 132 | + (Xmlm.error_message error)) |
| 133 | + | _ -> |
| 134 | + None) |
| 135 | + |
| 136 | +(* Use a binary 16-byte length to frame RPC messages *) |
| 137 | +let binary_rpc string_of_call response_of_string ?(srcstr = "unset") |
| 138 | + ?(dststr = "unset") url (call : Rpc.call) : Rpc.response = |
| 139 | + let uri = Uri.of_string (url ()) in |
| 140 | + Open_uri.with_open_uri uri (fun fd -> |
| 141 | + let ic = Unix.in_channel_of_descr fd in |
| 142 | + let oc = Unix.out_channel_of_descr fd in |
| 143 | + let msg_buf = string_of_call call in |
| 144 | + let len = Printf.sprintf "%016d" (String.length msg_buf) in |
| 145 | + output_string oc len ; |
| 146 | + output_string oc msg_buf ; |
| 147 | + flush oc ; |
| 148 | + let len_buf = Bytes.make 16 '\000' in |
| 149 | + really_input ic len_buf 0 16 ; |
| 150 | + let len = int_of_string (Bytes.unsafe_to_string len_buf) in |
| 151 | + let msg_buf = Bytes.make len '\000' in |
| 152 | + really_input ic msg_buf 0 len ; |
| 153 | + let (response : Rpc.response) = |
| 154 | + response_of_string (Bytes.unsafe_to_string msg_buf) |
| 155 | + in |
| 156 | + response) |
| 157 | + |
| 158 | +let json_binary_rpc = |
| 159 | + binary_rpc Jsonrpc.string_of_call Jsonrpc.response_of_string |
0 commit comments