]> matita.cs.unibo.it Git - helm.git/blob - helm/hbugs/common/hbugs_misc.ml
30f46081553d85cd29112b8c2fa5986e4099d055
[helm.git] / helm / hbugs / common / hbugs_misc.ml
1 (*
2  *  Copyright (C) 2003, HELM Team.
3  *
4  *  This file is part of HELM, an Hypertextual, Electronic
5  *  Library of Mathematics, developed at the Computer Science
6  *  Department, University of Bologna, Italy.
7  *
8  *  HELM is free software; you can redistribute it and/or
9  *  modify it under the terms of the GNU General Public License
10  *  as published by the Free Software Foundation; either version 2
11  *  of the License, or (at your option) any later version.
12  *
13  *  HELM is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with HELM; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21  *  MA  02111-1307, USA.
22  *
23  *  For details, see the HELM World-Wide-Web page,
24  *  http://cs.unibo.it/helm/.
25  *)
26
27 open Printf;;
28
29 let body_sep_RE = Pcre.regexp "\r\n\r\n";;
30 let url_scheme_RE = Pcre.regexp ~flags:[`CASELESS] "http://";;
31 let url_RE = Pcre.regexp "^([\\w.]+)(:(\\d+))?(/.*)?$";;
32 let parse_url url =
33   try
34     let subs = Pcre.extract ~rex:url_RE (Pcre.replace ~rex:url_scheme_RE url) in
35     (subs.(1),
36     (if subs.(2) = "" then 80 else int_of_string subs.(3)),
37     (if subs.(4) = "" then "/" else subs.(4)))
38   with exc ->
39     failwith
40       (sprintf "Can't parse url: %s (exception: %s)"
41         url (Printexc.to_string exc))
42 ;;
43 let get_body answer =
44   match Pcre.split ~rex:body_sep_RE answer with
45   | [_; body] -> body
46   | _ -> failwith "Invalid response received: can't parse response's body"
47 ;;
48
49 let init_socket addr port =
50   let inet_addr = (Unix.gethostbyname addr).Unix.h_addr_list.(0) in
51   let sockaddr = Unix.ADDR_INET (inet_addr, port) in
52   let suck = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
53   Unix.connect suck sockaddr;
54   let outchan = Unix.out_channel_of_descr suck in
55   let inchan = Unix.in_channel_of_descr suck in
56   (inchan, outchan)
57 ;;
58 let rec retrieve inchan buf =
59   Buffer.add_string buf (input_line inchan ^ "\n");
60   retrieve inchan buf
61 ;;
62
63 let http_get url =
64   let (address, port, path) = parse_url url in
65   let (inchan, outchan) = init_socket address port in
66   output_string outchan (sprintf "GET %s HTTP/1.0\r\n\r\n" path);
67   flush outchan;
68   let buf = Buffer.create 1023 in
69   try
70     retrieve inchan buf
71   with End_of_file -> get_body (Buffer.contents buf)
72 ;;
73
74 let http_post ?(body = "") url =
75   let (address, port, path) = parse_url url in
76   let (inchan, outchan) = init_socket address port in
77   output_string outchan (sprintf "POST %s HTTP/1.0\r\n" path);
78   output_string outchan (sprintf "Content-Length: %d\r\n" (String.length body));
79   output_string outchan "\r\n";
80   output_string outchan body;
81   flush outchan;
82   let buf = Buffer.create 1023 in
83   try
84     retrieve inchan buf
85   with End_of_file -> get_body (Buffer.contents buf)
86 ;;
87
88 let rec hashtbl_remove_all tbl key =
89   if Hashtbl.mem tbl key then begin
90     Hashtbl.remove tbl key;
91     hashtbl_remove_all tbl key
92   end else
93     ()
94 ;;
95