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