Commits

camlspotter committed be1782e

addex xcurl

Comments (0)

Files changed (4)

+.*\.cm[a-z]+$
+.*~$
+\.(sp[io]t|annot|o|cm[a-z]+|orig|omc|lock|a|so)$
+\.(byt|opt|run)$
+\.omakedb$
+\.depend$
+CVS/.*
+gen_phantom$
+phantom\.ml.*
+OMakeroot
+setup.data
+setup.log
+^\#
+^\.\#
+.*\.auto\.mli
+# How-to-build using OMake
+#
+# yes no | omake --install # to create OMakeroot for the first time
+
+# If OMakeroot is here, include OMyMakefile
+if $(file-exists OMakeroot)
+   include OMyMakefile
+   export
+
+.PHONY: all install clean
+
+OCAMLPACKS[] =
+    spotlib
+    curl
+
+OCAMLFLAGS = -annot -w A-4-9-39 
+# OCAMLDEPFLAGS= -syntax camlp4o -package meta_conv.syntax
+# OCAMLPPFLAGS=  -syntax camlp4o -package meta_conv.syntax
+
+FILES[] =
+    xcurl
+
+MyOCamlPackage(spotxtras, $(FILES), $(EMPTY), $(EMPTY))
+let ok200 = function
+  | 200, v -> `Ok v
+  | n, mes -> `Error (`Http (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;
+  h#cleanup; (* Need to flush out cookies *)
+  ok200 (h#get_httpcode, 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;
+  h#cleanup; (* Need to flush out cookies *)
+  close_out oc;
+  match h#get_httpcode with
+  | 200 -> Unix.rename tmp dst; `Ok dst
+  | e -> `Error (`Http (e, tmp))
+  
+val ok200 : int * 'a -> [> `Ok of 'a | `Error of [> `Http of int * 'a ]]
+(** HTTP result to a result monad *)
+
+val get_string : (Curl.handle -> unit) -> [> `Error of [> `Http of int * string ] | `Ok of string ]
+(** Get string *)
+
+val download : string -> (Curl.handle -> unit) -> [> `Error of [> `Http of int * string ] | `Ok of string ]
+(** Download file. Return the final downloaded file path.
+    At error, the tmp file is not removed since it may contain error message. 
+*)