]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/getter/http_getter_common.ml
ocaml 3.09 transition
[helm.git] / helm / ocaml / getter / http_getter_common.ml
1 (*
2  * Copyright (C) 2003-2004:
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   | `Normal -> "Normal"
35   | `Gzipped -> "GZipped"
36
37 let is_cic_obj_uri uri = Pcre.pmatch ~pat:"^cic:" uri
38 let is_theory_uri uri = Pcre.pmatch ~pat:"^theory:" uri
39 let is_cic_uri uri = is_cic_obj_uri uri || is_theory_uri uri
40 let is_nuprl_uri uri = Pcre.pmatch ~pat:"^nuprl:" uri
41 let is_rdf_uri uri = Pcre.pmatch ~pat:"^helm:rdf(.*):(.*)//(.*)" uri
42 let is_xsl_uri uri = Pcre.pmatch ~pat:"^\\w+\\.xsl" uri
43
44 let rec uri_of_string = function
45   | uri when is_rdf_uri uri ->
46       (match Pcre.split ~pat:"//" uri with
47       | [ prefix; uri ] ->
48           let rest =
49             match uri_of_string uri with
50             | Cic_uri xmluri -> xmluri
51             | _ -> raise (Invalid_URI uri)
52           in
53           Rdf_uri (prefix, rest)
54       | _ -> raise (Invalid_URI uri))
55   | uri when is_cic_obj_uri uri -> Cic_uri (Cic (Pcre.replace ~pat:"^cic:" uri))
56   | uri when is_nuprl_uri uri -> Nuprl_uri (Pcre.replace ~pat:"^nuprl:" uri)
57   | uri when is_theory_uri uri ->
58       Cic_uri (Theory (Pcre.replace ~pat:"^theory:" uri))
59   | uri -> raise (Invalid_URI uri)
60
61 let patch_xsl ?(via_http = true) () =
62   fun line ->
63     let mk_patch_fun tag line =
64       Pcre.replace
65         ~pat:(sprintf "%s\\s+href=\"" tag)
66         ~templ:(sprintf "%s href=\"%s/getxslt?uri="
67           tag (Lazy.force Http_getter_env.my_own_url))
68         line
69     in
70     let (patch_import, patch_include) =
71       (mk_patch_fun "xsl:import", mk_patch_fun "xsl:include")
72     in
73     patch_include (patch_import line)
74
75 let patch_system kind ?(via_http = true) () =
76   let rex =
77     Pcre.regexp (sprintf "%s (.*) SYSTEM\\s+\"((%s)/)?" kind
78       (String.concat "|" (Lazy.force Http_getter_env.dtd_base_urls)))
79   in
80   let templ =
81     if via_http then
82       sprintf "%s $1 SYSTEM \"%s/getdtd?uri=" kind
83         (Lazy.force Http_getter_env.my_own_url)
84     else
85       sprintf "%s $1 SYSTEM \"file://%s/" kind
86         (Lazy.force Http_getter_env.dtd_dir)
87   in
88   fun line -> Pcre.replace ~rex ~templ line
89
90 let patch_entity = patch_system "ENTITY"
91 let patch_doctype = patch_system "DOCTYPE"
92
93 let patch_xmlbase =
94   let rex = Pcre.regexp "^(\\s*<\\w[^ ]*)(\\s|>)" in
95   fun xmlbases baseurl baseuri s ->
96     let s' =
97       Pcre.replace ~rex
98         ~templ:(sprintf "$1 xml:base=\"%s\" helm:base=\"%s\"$2" baseurl baseuri)
99         s
100     in
101     if s <> s' then xmlbases := None;
102     s'
103
104 let patch_dtd = patch_entity
105 let patch_xml ?via_http ?xmlbases () =
106   let xmlbases = ref xmlbases in
107   fun line ->
108     match !xmlbases with
109     | None -> patch_doctype ?via_http () (patch_entity ?via_http () line)
110     | Some (xmlbaseuri, xmlbaseurl) ->
111         patch_xmlbase xmlbases xmlbaseurl xmlbaseuri
112           (patch_doctype ?via_http () (patch_entity ?via_http () line))
113
114 let return_file
115   ~fname ?contype ?contenc ?patch_fun ?(gunzip = false) ?(via_http = true)
116   ~enc outchan
117 =
118   if via_http then begin
119     let headers =
120       match (contype, contenc) with
121       | (Some t, Some e) -> ["Content-Encoding", e; "Content-Type", t]
122       | (Some t, None) -> ["Content-Type" , t]
123       | (None, Some e) -> ["Content-Encoding", e]
124       | (None, None) -> []
125     in
126     Http_daemon.send_basic_headers ~code:(`Code 200) outchan;
127     Http_daemon.send_headers headers outchan;
128     Http_daemon.send_CRLF outchan
129   end;
130   match gunzip, patch_fun with
131   | true, Some patch_fun ->
132       Http_getter_logger.log ~level:2
133         "Patch required, uncompress/compress cycle needed :-(";
134       (* gunzip needed, uncompress file, apply patch_fun to it, compress the
135        * result and sent it to client *)
136       let (tmp1, tmp2) =
137         (Http_getter_misc.tempfile (), Http_getter_misc.tempfile ())
138       in
139       (try
140         Http_getter_misc.gunzip ~keep:true ~output:tmp1 fname; (* gunzip tmp1 *)
141         let new_file = open_out tmp2 in
142         Http_getter_misc.iter_file  (* tmp2 = patch(tmp1) *)
143           (fun line ->
144             output_string new_file (patch_fun line ^ "\n");
145             flush outchan)
146           tmp1;
147         close_out new_file;
148         Http_getter_misc.gzip ~output:tmp1 tmp2;(* tmp1 = gzip(tmp2); rm tmp2 *)
149         Http_getter_misc.iter_file  (* send tmp1 to client as is*)
150           (fun line -> output_string outchan (line ^ "\n"); flush outchan)
151           tmp1;
152         Sys.remove tmp1       (* rm tmp1 *)
153       with e ->
154         Sys.remove tmp1;
155         raise e)
156   | false, Some patch_fun ->
157       (match enc with
158       | `Normal ->
159           Http_getter_misc.iter_file
160             (fun line -> output_string outchan (patch_fun (line ^ "\n")))
161             fname
162       | `Gzipped -> assert false)
163         (* dangerous case, if this happens it needs to be investigated *)
164   | _, None -> Http_getter_misc.iter_file_data (output_string outchan) fname
165 ;;
166