]> matita.cs.unibo.it Git - helm.git/blob - helm/http_getter/http_getter_common.ml
added http_getter OCaml implementation
[helm.git] / helm / http_getter / http_getter_common.ml
1 (*
2  *  Copyright (C) 2000, 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 Http_getter_types;;
28 open Printf;;
29
30 let string_of_ls_flag = function No -> "NO" | Yes -> "YES" | Ann -> "ANN"
31 let string_of_encoding = function
32   | Enc_normal -> "Normal"
33   | Enc_gzipped -> "GZipped"
34
35 let is_cic_uri uri = Pcre.pmatch ~pat:"^cic:" uri
36 let is_theory_uri uri = Pcre.pmatch ~pat:"^theory:" uri
37 let is_xml_uri uri = is_cic_uri uri || is_theory_uri uri
38 let is_rdf_uri uri = Pcre.pmatch ~pat:"^helm:rdf(.*):(.*)//(.*)" uri
39 let is_xsl_uri uri = Pcre.pmatch ~pat:"^\\w+\\.xsl" uri
40
41 let rec http_getter_uri_of_string = function
42   | uri when is_rdf_uri uri ->
43       (match Pcre.split ~pat:"//" uri with
44       | [ prefix; uri ] ->
45           let rest =
46             match http_getter_uri_of_string uri with
47             | Xml_uri xmluri -> xmluri
48             | _ -> raise (Http_getter_invalid_URI uri)
49           in
50           Rdf_uri (prefix, rest)
51       | _ -> raise (Http_getter_invalid_URI uri))
52   | uri when is_cic_uri uri -> Xml_uri (Cic (Pcre.replace ~pat:"^cic:" uri))
53   | uri when is_theory_uri uri ->
54       Xml_uri (Theory (Pcre.replace ~pat:"^theory:" uri))
55   | uri -> raise (Http_getter_invalid_URI uri)
56
57 let patch_xml line =
58   Pcre.replace
59     ~pat:(sprintf "DOCTYPE (.*) SYSTEM\\s+\"%s/" Http_getter_env.dtd_base_url)
60     ~templ:(
61       sprintf "DOCTYPE $1 SYSTEM \"%s/getdtd?uri=" Http_getter_env.my_own_url)
62     line
63 let patch_xsl =
64   let mk_patch_fun tag line =
65     Pcre.replace
66       ~pat:(sprintf "%s\\s+href=\"" tag)
67       ~templ:(
68         sprintf "%s href=\"%s/getxslt?uri=" Http_getter_env.my_own_url tag)
69       line
70   in
71   let (patch_import, patch_include) =
72     (mk_patch_fun "xsl:import", mk_patch_fun "xsl:include")
73   in
74   fun line -> patch_include (patch_import line)
75 let patch_dtd line =
76   Pcre.replace
77     ~pat:"ENTITY (.*) SYSTEM\\s+\""
78     ~templ:(
79       sprintf "ENTITY $1 SYSTEM \"%s/getdtd?uri=" Http_getter_env.my_own_url)
80     line
81
82 let pp_error =
83   sprintf "<html><body><h1>Http Getter error: %s</h1></body></html>"
84 let pp_internal_error =
85   sprintf "<html><body><h1>Http Getter Internal error: %s</h1></body></html>"
86 let pp_msg = sprintf "<html><body><h1>%s</h1></body></html>"
87
88 let mk_return_fun pp_fun contype msg outchan =
89   Http_daemon.respond
90     ~body:(pp_fun msg)
91     ~headers:["Content-Type", contype]
92     outchan
93
94 let return_html_error = mk_return_fun pp_error "text/html"
95 let return_html_internal_error = mk_return_fun pp_internal_error "text/html"
96 let return_html_msg = mk_return_fun pp_msg "text/html"
97 let return_xml_msg = mk_return_fun pp_msg "text/xml"
98   (**
99   @param fname name of the file to be sent
100   @param contype Content-Type header value
101   @param contenc Content-Enconding header value
102   @param patch_fun function used to patch file contents
103   @param outchan output channel over which sent file fname *)
104 let return_file ~fname ?contype ?contenc ?(patch_fun = fun x -> x) outchan =
105   let headers =
106     match (contype, contenc) with
107     | (Some t, Some e) -> [ "Content-Type", t; "Content-Enconding", e ]
108     | (Some t, None) -> [ "Content-Type" , t ]
109     | (None, Some e) -> [ "Content-Enconding", e ]
110     | (None, None) -> []
111   in
112   Http_daemon.send_basic_headers outchan;
113   Http_daemon.send_headers headers outchan;
114   Http_daemon.send_CRLF outchan;
115   Http_getter_misc.iter_file
116     (fun line -> output_string outchan (patch_fun line ^ "\n"))
117     fname
118  (* return a bad request http response *)
119 let return_400 body outchan = Http_daemon.respond_error ~code:400 ~body outchan
120
121 let wget ?output url =
122   let flags =
123     (match output with Some file -> ["-O " ^ file] | None -> []) @ [url]
124   in
125   Shell.call
126     ~stdout:Shell.to_dev_null ~stderr:Shell.to_dev_null [Shell.cmd "wget" flags]
127
128   (* TODO gzip and gunzip create executables file, but umask seems to be
129   correctly inherited from the shell .... boh *)
130
131   (* stderr shown as usual *)
132 let gzip ?(keep = false) fname =
133   if keep then  (* keep original file *)
134     Shell.call
135       ~stdout:(Shell.to_file (fname ^ ".gz"))
136       [Shell.cmd "gzip" ["-f"; "-c"; fname]]
137   else  (* don't keep original file *)
138     Shell.call [Shell.cmd "gzip" ["-f"; fname]]
139
140   (* stderr shown as usual *)
141 let gunzip ?(keep = false) fname =
142   if not (Pcre.pmatch ~pat:"\\.gz$" fname) then
143     failwith "gunzip: source file doesn't end with '.gz'";
144   let basename = Pcre.replace ~pat:"\\.gz$" fname in
145   if keep then  (* keep original file *)
146     Shell.call
147       ~stdout:(Shell.to_file basename)
148       [Shell.cmd "gunzip" ["-f"; "-c"; fname]]
149   else  (* don't keep original file *)
150     Shell.call [Shell.cmd "gunzip" ["-f"; fname]]
151
152 let tempfile () =
153   let buf = Buffer.create 28 in (* strlen("/tmp/fileSzb3Mw_http_getter") *)
154   Shell.call
155     ~stdout:(Shell.to_buffer buf)
156     [Shell.cmd "tempfile" ["--suffix=_http_getter"]];
157   Pcre.replace ~pat:"\n" (Buffer.contents buf)
158