]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/ocaml-http/http_client.ml
first moogle template checkin
[helm.git] / helm / DEVEL / ocaml-http / http_client.ml
1
2 (*
3   OCaml HTTP - do it yourself (fully OCaml) HTTP daemon
4
5   Copyright (C) <2002-2003> Stefano Zacchiroli <zack@cs.unibo.it>
6
7   This program is free software; you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation; either version 2 of the License, or
10   (at your option) any later version.
11
12   This program is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with this program; if not, write to the Free Software
19   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20 *)
21
22 open Printf
23
24 open Http_common
25
26 exception Http_error of (int * string)  (* code, body *)
27
28 let http_scheme_RE = Pcre.regexp ~flags:[`CASELESS] "^http://"
29 let url_RE = Pcre.regexp "^([\\w.-]+)(:(\\d+))?(/.*)?$"
30
31 let tcp_bufsiz = 4096 (* for TCP I/O *)
32
33 let parse_url url =
34   try
35     let subs =
36       Pcre.extract ~rex:url_RE (Pcre.replace ~rex:http_scheme_RE url)
37     in
38     (subs.(1),
39     (if subs.(2) = "" then 80 else int_of_string subs.(3)),
40     (if subs.(4) = "" then "/" else subs.(4)))
41   with exc ->
42     failwith
43       (sprintf "Can't parse url: %s (exception: %s)"
44         url (Printexc.to_string exc))
45
46 let init_socket addr port =
47   let inet_addr = (Unix.gethostbyname addr).Unix.h_addr_list.(0) in
48   let sockaddr = Unix.ADDR_INET (inet_addr, port) in
49   let suck = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
50   Unix.connect suck sockaddr;
51   let outchan = Unix.out_channel_of_descr suck in
52   let inchan = Unix.in_channel_of_descr suck in
53   (inchan, outchan)
54
55 let http_get_iter callback url =
56   let (address, port, path) = parse_url url in
57   let buf = String.create tcp_bufsiz in
58   let (inchan, outchan) = init_socket address port in
59   output_string outchan (sprintf "GET %s HTTP/1.0\r\n" path);
60   output_string outchan (sprintf "Host: %s\r\n\r\n" address);
61
62   flush outchan;
63   let (_, status) = Http_parser.parse_response_fst_line inchan in
64   (match code_of_status status with
65   | 200 -> ()
66   | code -> raise (Http_error (code, "")));
67   ignore (Http_parser.parse_headers inchan);
68   (try
69     while true do
70       match input inchan buf 0 tcp_bufsiz with
71       | 0 -> raise End_of_file
72       | bytes when bytes = tcp_bufsiz ->  (* buffer full, no need to slice it *)
73           callback buf
74       | bytes when bytes < tcp_bufsiz ->  (* buffer not full, slice it *)
75           callback (String.sub buf 0 bytes)
76       | _ -> (* ( bytes < 0 ) || ( bytes > tcp_bufsiz ) *)
77           assert false
78     done
79   with End_of_file -> ());
80   close_in inchan (* close also outchan, same fd *)
81
82 let http_get url =
83   let buf = Buffer.create 10240 in
84   http_get_iter (Buffer.add_string buf) url;
85   Buffer.contents buf
86