]> matita.cs.unibo.it Git - helm.git/blob - helm/http_getter/http_getter_common.ml
- fixed helm web page url and copyright notice
[helm.git] / helm / http_getter / http_getter_common.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 Http_getter_types;;
30 open Printf;;
31
32 let string_of_ls_flag = function No -> "NO" | Yes -> "YES" | Ann -> "ANN"
33 let string_of_encoding = function
34   | Enc_normal -> "Normal"
35   | Enc_gzipped -> "GZipped"
36
37 let is_cic_uri uri = Pcre.pmatch ~pat:"^cic:" uri
38 let is_theory_uri uri = Pcre.pmatch ~pat:"^theory:" uri
39 let is_xml_uri uri = is_cic_uri uri || is_theory_uri uri
40 let is_rdf_uri uri = Pcre.pmatch ~pat:"^helm:rdf(.*):(.*)//(.*)" uri
41 let is_xsl_uri uri = Pcre.pmatch ~pat:"^\\w+\\.xsl" uri
42
43 let rec http_getter_uri_of_string = function
44   | uri when is_rdf_uri uri ->
45       (match Pcre.split ~pat:"//" uri with
46       | [ prefix; uri ] ->
47           let rest =
48             match http_getter_uri_of_string uri with
49             | Xml_uri xmluri -> xmluri
50             | _ -> raise (Http_getter_invalid_URI uri)
51           in
52           Rdf_uri (prefix, rest)
53       | _ -> raise (Http_getter_invalid_URI uri))
54   | uri when is_cic_uri uri -> Xml_uri (Cic (Pcre.replace ~pat:"^cic:" uri))
55   | uri when is_theory_uri uri ->
56       Xml_uri (Theory (Pcre.replace ~pat:"^theory:" uri))
57   | uri -> raise (Http_getter_invalid_URI uri)
58
59 let patch_xml line =
60   Pcre.replace
61     ~pat:(sprintf "DOCTYPE (.*) SYSTEM\\s+\"%s/" Http_getter_env.dtd_base_url)
62     ~templ:(
63       sprintf "DOCTYPE $1 SYSTEM \"%s/getdtd?uri=" Http_getter_env.my_own_url)
64     line
65 let patch_xsl =
66   let mk_patch_fun tag line =
67     Pcre.replace
68       ~pat:(sprintf "%s\\s+href=\"" tag)
69       ~templ:(
70         sprintf "%s href=\"%s/getxslt?uri=" tag Http_getter_env.my_own_url)
71       line
72   in
73   let (patch_import, patch_include) =
74     (mk_patch_fun "xsl:import", mk_patch_fun "xsl:include")
75   in
76   fun line -> patch_include (patch_import line)
77 let patch_dtd line =
78   Pcre.replace
79     ~pat:"ENTITY (.*) SYSTEM\\s+\""
80     ~templ:(
81       sprintf "ENTITY $1 SYSTEM \"%s/getdtd?uri=" Http_getter_env.my_own_url)
82     line
83
84 let pp_error s =
85   sprintf "<html><body><h1>Http Getter error: %s</h1></body></html>" s
86 let pp_internal_error s =
87   sprintf "<html><body><h1>Http Getter Internal error: %s</h1></body></html>" s
88 let pp_msg s = sprintf "<html><body><h1>%s</h1></body></html>" s
89 let null_pp s = s
90
91 let mk_return_fun pp_fun contype msg outchan =
92   Http_daemon.respond
93     ~body:(pp_fun msg) ~headers:["Content-Type", contype] outchan
94
95 let return_html_error = mk_return_fun pp_error "text/html"
96 let return_html_internal_error = mk_return_fun pp_internal_error "text/html"
97 let return_html_msg = mk_return_fun pp_msg "text/html"
98 let return_xml_msg = mk_return_fun null_pp "text/xml"
99 let return_file ~fname ?contype ?contenc ?(patch_fun = fun x -> x) outchan =
100   let headers =
101     match (contype, contenc) with
102     | (Some t, Some e) -> ["Content-Encoding", e; "Content-Type", t]
103     | (Some t, None) -> ["Content-Type" , t]
104     | (None, Some e) -> ["Content-Encoding", e]
105     | (None, None) -> []
106   in
107   Http_daemon.send_basic_headers ~code:200 outchan;
108   Http_daemon.send_headers headers outchan;
109   Http_daemon.send_CRLF outchan;
110   Http_getter_misc.iter_file
111     (fun line -> output_string outchan (patch_fun line ^ "\n"))
112     fname
113 let return_400 body outchan = Http_daemon.respond_error ~code:400 ~body outchan
114