]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/DEVEL/ocaml-http/http_client.ml
ported to ocaml 3.08
[helm.git] / helm / DEVEL / ocaml-http / http_client.ml
index f903add7a51ad3333ef77a756ab3571be68206ac..1a89bba8aa6d525dc0bd84d4b21723efe9ea15f2 100644 (file)
@@ -2,7 +2,7 @@
 (*
   OCaml HTTP - do it yourself (fully OCaml) HTTP daemon
 
-  Copyright (C) <2002-2003> Stefano Zacchiroli <zack@cs.unibo.it>
+  Copyright (C) <2002-2004> Stefano Zacchiroli <zack@cs.unibo.it>
 
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
@@ -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 -> ()