3 OCaml HTTP - do it yourself (fully OCaml) HTTP daemon
5 Copyright (C) <2002-2004> 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 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.
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.
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
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 http_get_iter 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 ignore (Http_parser.parse_headers inchan);
84 match input inchan buf 0 tcp_bufsiz with
85 | 0 -> raise End_of_file
86 | bytes when bytes = tcp_bufsiz -> (* buffer full, no need to slice it *)
88 | bytes when bytes < tcp_bufsiz -> (* buffer not full, slice it *)
89 callback (String.sub buf 0 bytes)
90 | _ -> (* ( bytes < 0 ) || ( bytes > tcp_bufsiz ) *)
93 with End_of_file -> ());
94 close_in inchan (* close also outchan, same fd *)
97 let buf = Buffer.create 10240 in
98 http_get_iter (Buffer.add_string buf) url;