3 OCaml HTTP - do it yourself (fully OCaml) HTTP daemon
5 Copyright (C) <2002-2005> Stefano Zacchiroli <zack@cs.unibo.it>
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.
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.
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
26 exception Http_error of (int * string) (* code, body *)
28 let http_scheme_RE = Pcre.regexp ~flags:[`CASELESS] "^http://"
29 let url_RE = Pcre.regexp "^([\\w.-]+)(:(\\d+))?(/.*)?$"
31 let tcp_bufsiz = 4096 (* for TCP I/O *)
36 Pcre.extract ~rex:url_RE (Pcre.replace ~rex:http_scheme_RE url)
39 (if subs.(2) = "" then 80 else int_of_string subs.(3)),
40 (if subs.(4) = "" then "/" else subs.(4)))
43 (sprintf "Can't parse url: %s (exception: %s)"
44 url (Printexc.to_string exc))
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
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);
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
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 *)
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
80 | code -> raise (Http_error (code, "")));
81 let headers = Http_parser.parse_headers inchan in
82 head_callback status headers;
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 *)
89 | bytes when bytes < tcp_bufsiz -> (* buffer not full, slice it *)
90 callback (String.sub buf 0 bytes)
91 | _ -> (* ( bytes < 0 ) || ( bytes > tcp_bufsiz ) *)
94 with End_of_file -> ());
95 close_in inchan (* close also outchan, same fd *)
97 let get ?head_callback url =
98 let buf = Buffer.create 10240 in
99 get_iter ?head_callback (Buffer.add_string buf) url;