From 74a4b5a985f8ee0887e82fd74884204d4af149fc Mon Sep 17 00:00:00 2001 From: Stefano Zacchiroli Date: Thu, 20 May 2004 15:44:01 +0000 Subject: [PATCH] added support for HEAD requests --- helm/DEVEL/ocaml-http/http_client.ml | 22 ++++++++++++++++++---- helm/DEVEL/ocaml-http/http_client.mli | 5 +++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/helm/DEVEL/ocaml-http/http_client.ml b/helm/DEVEL/ocaml-http/http_client.ml index f903add7a..d0b4f433a 100644 --- a/helm/DEVEL/ocaml-http/http_client.ml +++ b/helm/DEVEL/ocaml-http/http_client.ml @@ -52,14 +52,28 @@ let init_socket addr port = let inchan = Unix.in_channel_of_descr suck in (inchan, outchan) -let http_get_iter callback url = +let submit_request kind url = let (address, port, path) = parse_url url in - let buf = String.create tcp_bufsiz in let (inchan, outchan) = init_socket address port in - output_string outchan (sprintf "GET %s HTTP/1.0\r\n" path); + let req_string = match kind with `GET -> "GET" | `HEAD -> "HEAD" in + output_string outchan (sprintf "%s %s HTTP/1.0\r\n" req_string path); output_string outchan (sprintf "Host: %s\r\n\r\n" address); - flush outchan; + (inchan, outchan) + +let http_head url = + let (inchan, outchan) = submit_request `HEAD url in + let (_, status) = Http_parser.parse_response_fst_line inchan in + (match code_of_status status with + | 200 -> () + | code -> raise (Http_error (code, ""))); + let buf = Http_misc.buf_of_inchan inchan in + close_in inchan; (* close also outchan, same fd *) + Buffer.contents buf + +let http_get_iter 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 -> () diff --git a/helm/DEVEL/ocaml-http/http_client.mli b/helm/DEVEL/ocaml-http/http_client.mli index 1b92ba772..bd18f7735 100644 --- a/helm/DEVEL/ocaml-http/http_client.mli +++ b/helm/DEVEL/ocaml-http/http_client.mli @@ -36,3 +36,8 @@ val http_get: string -> string * returning it as a string *) val http_get_iter: (string -> unit) -> string -> unit + (** @param url an HTTP url + * @return HTTP HEAD raw response + * @raise Http_error when response code <> 200 *) +val http_head: string -> string + -- 2.39.2