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