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 -> ()
* 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
+