Source

ocaml-bitbucket / xcurl.ml

camlspotter 710bd90 
camlspotter 7197bd2 

camlspotter 710bd90 
camlspotter ab68943 





camlspotter 74f9d22 
camlspotter 7197bd2 
camlspotter 1a24c47 
camlspotter 7197bd2 
camlspotter ab68943 
camlspotter 1a24c47 







camlspotter 74f9d22 
camlspotter 7197bd2 
camlspotter 1a24c47 

camlspotter 74f9d22 
camlspotter c8515ba 
camlspotter 7197bd2 
camlspotter 1a24c47 
let ok200 = function
  | _, 200, v -> `Ok v
  | url, n, mes -> `Error (`Http (url, n, mes))

let get_string f = 
  let h = new Curl.handle in
  f h;
  let buf = Buffer.create 100 in
  h#set_writefunction (fun s -> Buffer.add_string buf s; String.length s);
  h#perform;
  let code = h#get_httpcode in
  let url = h#get_effectiveurl in
  h#cleanup; (* Need to flush out cookies *)
  ok200 (url, code, Buffer.contents buf)

let download dst f =
  let h = new Curl.handle in
  f h;
  let tmp = dst ^ ".tmp" in
  let oc = open_out_bin tmp in
  h#set_writefunction (fun s -> 
    output_string oc s; String.length s);
  h#perform;
  let code = h#get_httpcode in
  let url = h#get_effectiveurl in
  h#cleanup; (* Need to flush out cookies *)
  close_out oc;
  match code with
  | 200 -> Unix.rename tmp dst; `Ok dst
  | e -> `Error (`Http (url, e, tmp))