]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/ocaml-http/http_user_agent.ml
ocaml 3.09 transition
[helm.git] / helm / DEVEL / ocaml-http / http_user_agent.ml
1
2 (*
3   OCaml HTTP - do it yourself (fully OCaml) HTTP daemon
4
5   Copyright (C) <2002-2005> 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 Library General Public License as
9   published by the Free Software Foundation, version 2.
10
11   This program is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU Library General Public License for more details.
15
16   You should have received a copy of the GNU Library General Public
17   License along with this program; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
19   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 submit_request kind url =
56   let (address, port, path) = parse_url url in
57   let (inchan, outchan) = init_socket address port in
58   let req_string = match kind with `GET -> "GET" | `HEAD -> "HEAD" in
59   output_string outchan (sprintf "%s %s HTTP/1.0\r\n" req_string path);
60   output_string outchan (sprintf "Host: %s\r\n\r\n" address);
61   flush outchan;
62   (inchan, outchan)
63
64 let head url =
65   let (inchan, outchan) = submit_request `HEAD url in
66   let (_, status) = Http_parser.parse_response_fst_line inchan in
67   (match code_of_status status with
68   | 200 -> ()
69   | code -> raise (Http_error (code, "")));
70   let buf = Http_misc.buf_of_inchan inchan in
71   close_in inchan; (* close also outchan, same fd *)
72   Buffer.contents buf
73
74 let get_iter ?(head_callback = fun _ _ -> ()) callback url =
75   let (inchan, outchan) = submit_request `GET url in
76   let buf = String.create tcp_bufsiz in
77   let (_, status) = Http_parser.parse_response_fst_line inchan in
78   (match code_of_status status with
79   | 200 -> ()
80   | code -> raise (Http_error (code, "")));
81   let headers = Http_parser.parse_headers inchan in
82   head_callback status headers;
83   (try
84     while true do
85       match input inchan buf 0 tcp_bufsiz with
86       | 0 -> raise End_of_file
87       | bytes when bytes = tcp_bufsiz ->  (* buffer full, no need to slice it *)
88           callback buf
89       | bytes when bytes < tcp_bufsiz ->  (* buffer not full, slice it *)
90           callback (String.sub buf 0 bytes)
91       | _ -> (* ( bytes < 0 ) || ( bytes > tcp_bufsiz ) *)
92           assert false
93     done
94   with End_of_file -> ());
95   close_in inchan (* close also outchan, same fd *)
96
97 let get ?head_callback url =
98   let buf = Buffer.create 10240 in
99   get_iter ?head_callback (Buffer.add_string buf) url;
100   Buffer.contents buf
101