]> matita.cs.unibo.it Git - helm.git/blob - helm/http_getter/http_getter_cache.ml
8d180e1f86a18c52eeda7adac40a614583487947
[helm.git] / helm / http_getter / http_getter_cache.ml
1 (*
2  *  Copyright (C) 2003, 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_common;;
28 open Http_getter_misc;;
29 open Http_getter_types;;
30 open Printf;;
31
32 let resource_type_of_url = function
33   | url when Pcre.pmatch ~pat:"\\.xml\\.gz$" url -> Enc_gzipped
34   | url when Pcre.pmatch ~pat:"\\.xml$" url -> Enc_normal
35   | url -> raise (Http_getter_invalid_URL url)
36 let extension_of_resource_type = function
37   | Enc_normal -> "xml"
38   | Enc_gzipped -> "xml.gz"
39
40   (* basename = resource name without trailing ".gz", if any *)
41 let is_in_cache basename =
42   Sys.file_exists
43     (match Http_getter_env.cache_mode with
44     | Enc_normal -> basename
45     | Enc_gzipped -> basename ^ ".gz")
46
47 let respond_xml ?(enc = Enc_normal) ?(patch = true) ~url ~uri outchan =
48   let resource_type = resource_type_of_url url in
49   let extension = extension_of_resource_type resource_type in
50   let downloadname =
51     match http_getter_uri_of_string uri with  (* parse uri *)
52     | Xml_uri (Cic baseuri) | Xml_uri (Theory baseuri) ->
53           (* assumption: baseuri starts with "/" *)
54         sprintf "%s%s.%s" Http_getter_env.xml_dir baseuri extension
55     | Rdf_uri (prefix, ((Cic baseuri) as qbaseuri))
56     | Rdf_uri (prefix, ((Theory baseuri) as qbaseuri)) ->
57         let escaped_prefix =
58           (Pcre.replace ~pat:"/" ~templ:"_"
59             (Pcre.replace ~pat:"_" ~templ:"__"
60               (prefix ^
61               (match qbaseuri with
62               | Cic _ -> "//cic:"
63               | Theory _ -> "//theory:"))))
64         in
65         sprintf "%s/%s%s.%s"
66           Http_getter_env.rdf_dir escaped_prefix baseuri extension
67   in
68   let patch_fun =
69     if patch then Http_getter_common.patch_xml else (fun x -> x)
70   in
71   let basename = Pcre.replace ~pat:"\\.gz$" downloadname in
72   if not (is_in_cache basename) then begin (* download and fill cache *)
73     mkdir ~parents:true (Filename.dirname downloadname);
74     wget ~output:downloadname url;
75     match (resource_type, Http_getter_env.cache_mode) with
76     | Enc_normal, Enc_normal ->
77         (if enc = Enc_gzipped then gzip ~keep:true downloadname)
78     | Enc_gzipped, Enc_gzipped ->
79         (if enc = Enc_normal then gunzip ~keep:true downloadname)
80     | Enc_normal, Enc_gzipped -> gzip ~keep:(enc = Enc_normal) downloadname
81     | Enc_gzipped, Enc_normal -> gunzip ~keep:(enc = Enc_gzipped) downloadname
82   end else begin  (* resource already in cache *)
83     match (enc, Http_getter_env.cache_mode) with
84     | Enc_normal, Enc_normal | Enc_gzipped, Enc_gzipped -> ()
85     | Enc_normal, Enc_gzipped -> gunzip ~keep:true (basename ^ ".gz")
86     | Enc_gzipped, Enc_normal -> gzip ~keep:true basename
87   end;  (* now resource is in cache *)
88   (* invariant: file to be sent back to client is available on disk in the
89   format the client likes *)
90   (match enc with  (* send file to client *)
91   | Enc_normal ->
92       return_file ~fname:basename ~contype:"text/xml" ~patch_fun outchan
93   | Enc_gzipped ->
94       return_file
95         ~fname:(basename ^ ".gz") ~contype:"text/xml"  ~contenc:"x-gzip"
96         ~patch_fun outchan);
97   match (enc, Http_getter_env.cache_mode) with  (* remove temp files *)
98   | Enc_normal, Enc_normal | Enc_gzipped, Enc_gzipped -> ()
99   | Enc_normal, Enc_gzipped -> Sys.remove basename
100   | Enc_gzipped, Enc_normal -> Sys.remove (basename ^ ".gz")
101
102   (* TODO enc is not yet supported *)
103 let respond_xsl ?(enc = Enc_normal) ?(patch = true) ~url outchan =
104   let patch_fun =
105     if patch then Http_getter_common.patch_xsl else (fun x -> x)
106   in
107   let fname = tempfile () in
108   wget ~output:fname url;
109   return_file ~fname ~contype:"text/xml" ~patch_fun outchan;
110   Sys.remove fname
111
112   (* TODO enc is not yet supported *)
113 let respond_dtd ?(enc = Enc_normal) ?(patch = true) ~url outchan =
114   let patch_fun =
115     if patch then Http_getter_common.patch_dtd else (fun x -> x)
116   in
117   if Sys.file_exists url then
118     (* TODO check this: old getter here used text/xml *)
119     return_file ~fname:url ~contype:"text/plain" ~patch_fun outchan
120   else
121     return_html_error ("Can't find DTD: " ^ url) outchan
122