package curly

  1. Overview
  2. Docs

Source file curly.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
module Result = struct
  include Result
  let (>>=)
    : type a b e. (a, e) result -> (a -> (b, e) result) -> (b, e) result
    = fun r f ->
      match r with
      | Ok x -> f x
      | (Error _) as e -> e
end

open Result

module Meth = struct
  type t =
    [ `GET
    | `POST
    | `HEAD
    | `PUT
    | `DELETE
    | `OPTIONS
    | `TRACE
    | `CONNECT
    | `PATCH
    | `Other of string
    ]

  let to_string = function
    | `GET -> "GET"
    | `POST -> "POST"
    | `HEAD -> "HEAD"
    | `PUT -> "PUT"
    | `DELETE -> "DELETE"
    | `OPTIONS -> "OPTIONS"
    | `TRACE -> "TRACE"
    | `CONNECT -> "CONNECT"
    | `PATCH -> "PATCH"
    | `Other s -> s

  let pp fmt t = Format.fprintf fmt "%s" (to_string t)
end

module Header = struct
  type t = (string * string) list

  let empty = []

  let to_cmd t =
    t
    |> List.map (fun (k, v) -> ["-H"; Printf.sprintf "%s: %s" k v])
    |> List.concat

  let pp fmt t =
    Format.pp_print_list
      ~pp_sep:Format.pp_print_newline
      (fun fmt (k ,v) -> Format.fprintf fmt "%s: %s\n" k v)
      fmt t
end

module Response = struct
  type t = Http.response =
    { code: int
    ; headers: Header.t
    ; body: string
    }

  let default =
    { code = 0
    ; headers = []
    ; body = "" }

  let of_stdout s =
    let lexbuf = Lexing.from_string s in
    try Ok (Http.response default lexbuf)
    with e -> Error e

  let pp fmt t =
    Format.fprintf fmt "{code=%d;@ headers=%a;@ body=\"%s\"}"
      t.code Header.pp t.headers t.body
end

module Process_result = struct
  type t =
    { status: Unix.process_status
    ; stderr: string
    ; stdout: string
    }

  let pp_process_status fmt = function
    | Unix.WEXITED n -> Format.fprintf fmt "Exit code %d" n
    | Unix.WSIGNALED n -> Format.fprintf fmt "Signal %d" n
    | Unix.WSTOPPED n -> Format.fprintf fmt "Stopped %d" n

  let pp fmt t =
    Format.fprintf fmt "{status=%a;@ stderr=\"%s\";@ stdout=\"%s\"}"
      pp_process_status t.status t.stderr t.stdout
end

module Error = struct
  type t =
    | Invalid_request of string
    | Bad_exit of Process_result.t
    | Failed_to_read_response of exn * Process_result.t
    | Exn of exn

  let pp fmt = function
    | Bad_exit p ->
      Format.fprintf fmt "Non 0 exit code %a@.%a"
        Process_result.pp_process_status p.Process_result.status
        Process_result.pp p
    | Failed_to_read_response (e, _)  ->
      Format.fprintf fmt "Couldn't read response:@ %s" (Printexc.to_string e)
    | Invalid_request r -> Format.fprintf fmt "Invalid request: %s" r
    | Exn e -> Format.fprintf fmt "Exception: %s" (Printexc.to_string e)
end

module Request = struct
  type t =
    { meth: Meth.t
    ; url: string
    ; headers: Header.t
    ; body: string
    }

  let make ?(headers=Header.empty) ?(body="") ~url ~meth () =
    { meth
    ; url
    ; headers
    ; body }

  let has_body t = String.length t.body > 0

  let validate t =
    if has_body t && List.mem t.meth [`GET; `HEAD] then
      Error (Error.Invalid_request "No body is allowed with GET/HEAD methods")
    else
      Ok t

  let to_cmd_args t =
    List.concat
      [ ["-X"; Meth.to_string t.meth]
      ; Header.to_cmd t.headers
      ; [t.url]
      ; (if has_body t then
           ["--data-binary"; "@-"]
         else
           [])
      ]

  let pp fmt t =
    Format.fprintf fmt
      "{@ meth=%a;@ url=\"%s\";@ headers=\"%a\";@ body=\"%s\"@ }"
      Meth.pp t.meth t.url Header.pp t.headers t.body
end

let result_of_process_result t =
  match t.Process_result.status with
  | Unix.WEXITED 0 -> Ok t
  | _ -> Error (Error.Bad_exit  t)

let array_filter f a =
  Array.to_list a
  |> List.filter f
  |> Array.of_list

let is_prefix_ci s ~prefix =
  let s_len = String.length s in
  let prefix_len = String.length prefix in
  let s_lc = String.lowercase_ascii s in
  let prefix_lc = String.lowercase_ascii prefix in
  (s_len >= prefix_len) && String.equal (String.sub s_lc 0 prefix_len) prefix_lc

let var_in_ci vars env_string =
  List.exists (fun var -> is_prefix_ci ~prefix:(var ^ "=") env_string) vars

let curl_env () =
  let kept_variables = ["PATH"; "SYSTEMROOT"] in
  Unix.environment ()
  |> array_filter (var_in_ci kept_variables)

let run prog args stdin_str =
  let (stdout, stdin, stderr) =
    let prog =
      prog :: (List.map Filename.quote args)
      |> String.concat " " in
    Unix.open_process_full prog (curl_env ()) in
  if String.length stdin_str > 0 then (
    output_string stdin stdin_str
  );
  begin
    try close_out stdin;
    with _ -> ()
  end;
  let stdout_fd = Unix.descr_of_in_channel stdout in
  let stderr_fd = Unix.descr_of_in_channel stderr in
  let (in_buf, err_buf) = Buffer.(create 128, create 128) in
  let read_buf_len = 512 in
  let read_buf = Bytes.create read_buf_len in
  let input ch =
    match input ch read_buf 0 read_buf_len with
    | 0 -> Error `Eof
    | s -> Ok s in
  let rec loop = function
    | [] -> ()
    | read_list ->
      let can_read, _, _ = Unix.select read_list [] [] 1.0 in
      let to_remove =
        List.fold_left (fun to_remove fh ->
            let (rr, buf) =
              if fh = stderr_fd then (
                (input stderr, err_buf)
              ) else (
                (input stdout, in_buf)
              ) in
            begin match rr with
              | Ok len ->
                Buffer.add_subbytes buf read_buf 0 len;
                to_remove
              | Error `Eof ->
                fh :: to_remove
            end
          ) [] can_read in
      read_list
      |> List.filter (fun fh -> not (List.mem fh to_remove))
      |> loop
  in
  ignore (loop [ stdout_fd ; stderr_fd ]);
  let status = Unix.close_process_full (stdout, stdin, stderr) in
  { Process_result.
    status
  ; stdout = Buffer.contents in_buf
  ; stderr = Buffer.contents err_buf
  }

let is_redirect_code status =
  status <= 308 && status >= 300

let run ?(exe="curl") ?(args=[]) ?(follow_redirects=false) req =
  Request.validate req >>= fun req ->
  let args =
    if follow_redirects then
      "-L" :: "-si" :: (Request.to_cmd_args req) @ args
    else
      "-si" :: (Request.to_cmd_args req) @ args
  in
  let res =
    try
      result_of_process_result (run exe args req.Request.body)
    with e ->
      Error (Error.Exn e)
  in
  let rec handle_res res =
    match Response.of_stdout res.Process_result.stdout with
    | Ok r ->
       if follow_redirects && is_redirect_code r.Response.code then
         handle_res { res with stdout = r.Response.body }
       else Ok r
    | Error e -> Error (Error.Failed_to_read_response (e, res))
  in
  res >>= handle_res
  
let get ?exe ?args ?headers ?follow_redirects url =
  run ?exe ?args ?follow_redirects (Request.make ?headers ~url ~meth:`GET ())
let head ?exe ?args ?headers ?follow_redirects url =
  run ?exe ?args ?follow_redirects (Request.make ?headers ~url ~meth:`HEAD ())
let delete ?exe ?args ?headers ?follow_redirects url =
  run ?exe ?args ?follow_redirects (Request.make ?headers ~url ~meth:`DELETE ())
let post ?exe ?args ?headers ?body ?follow_redirects url =
  run ?exe ?args ?follow_redirects (Request.make ?body ?headers ~url ~meth:`POST ())
let put ?exe ?args ?headers ?body ?follow_redirects url =
  run ?exe ?args ?follow_redirects (Request.make ?body ?headers ~url ~meth:`PUT ())