From: Stefano Zacchiroli Date: Thu, 3 Feb 2005 22:06:01 +0000 (+0000) Subject: added head_callback to access response status and headers in requests X-Git-Tag: V_0_1_0~52 X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=commitdiff_plain;h=5db29580b497be5d9a53cc0848200656dc87b071;p=helm.git added head_callback to access response status and headers in requests --- diff --git a/helm/DEVEL/ocaml-http/http_user_agent.ml b/helm/DEVEL/ocaml-http/http_user_agent.ml index 5e74fbfaf..ebc09804a 100644 --- a/helm/DEVEL/ocaml-http/http_user_agent.ml +++ b/helm/DEVEL/ocaml-http/http_user_agent.ml @@ -71,14 +71,15 @@ let head url = close_in inchan; (* close also outchan, same fd *) Buffer.contents buf -let get_iter callback url = +let get_iter ?(head_callback = fun _ _ -> ()) callback url = let (inchan, outchan) = submit_request `GET url in let buf = String.create tcp_bufsiz in let (_, status) = Http_parser.parse_response_fst_line inchan in (match code_of_status status with | 200 -> () | code -> raise (Http_error (code, ""))); - ignore (Http_parser.parse_headers inchan); + let headers = Http_parser.parse_headers inchan in + head_callback status headers; (try while true do match input inchan buf 0 tcp_bufsiz with @@ -93,8 +94,8 @@ let get_iter callback url = with End_of_file -> ()); close_in inchan (* close also outchan, same fd *) -let get url = +let get ?head_callback url = let buf = Buffer.create 10240 in - get_iter (Buffer.add_string buf) url; + get_iter ?head_callback (Buffer.add_string buf) url; Buffer.contents buf diff --git a/helm/DEVEL/ocaml-http/http_user_agent.mli b/helm/DEVEL/ocaml-http/http_user_agent.mli index 580324677..b3e91b3c0 100644 --- a/helm/DEVEL/ocaml-http/http_user_agent.mli +++ b/helm/DEVEL/ocaml-http/http_user_agent.mli @@ -25,16 +25,26 @@ * this module is for performances and incremental elaboration of response's * bodies *) +open Http_types + exception Http_error of (int * string) (* code, body *) - (** @param url an HTTP url + (** @param head_callback optional calllback invoked on response's status and + * headers. If not provided no callback will be invoked + * @param url an HTTP url * @return HTTP response's body * @raise Http_error when response code <> 200 *) -val get: string -> string +val get: + ?head_callback:(status -> (string * string) list -> unit) -> + string -> + string (** as above but iter callback function on HTTP response's body instead of * returning it as a string *) -val get_iter: (string -> unit) -> string -> unit +val get_iter: + ?head_callback:(status -> (string * string) list -> unit) -> + (string -> unit) -> string -> + unit (** @param url an HTTP url * @return HTTP HEAD raw response