]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/DEVEL/ocaml-http/http_client.ml
added support for HEAD requests
[helm.git] / helm / DEVEL / ocaml-http / http_client.ml
index f903add7a51ad3333ef77a756ab3571be68206ac..d0b4f433add6f072c5326b51cc62aafe6dcf9044 100644 (file)
@@ -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 -> ()