2 * Copyright (C) 2003, HELM Team.
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.
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.
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.
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,
23 * For details, see the HELM World-Wide-Web page,
24 * http://cs.unibo.it/helm/.
27 open Http_getter_common;;
28 open Http_getter_misc;;
29 open Http_getter_types;;
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
38 | Enc_gzipped -> "xml.gz"
40 (* basename = resource name without trailing ".gz", if any *)
41 let is_in_cache basename =
43 (match Http_getter_env.cache_mode with
44 | Enc_normal -> basename
45 | Enc_gzipped -> basename ^ ".gz")
47 let respond_xml ?(enc = Enc_normal) ?(patch_dtd = true) ~url ~uri outchan =
48 let resource_type = resource_type_of_url url in
49 let extension = extension_of_resource_type resource_type in
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)) ->
58 (Pcre.replace ~pat:"/" ~templ:"_"
59 (Pcre.replace ~pat:"_" ~templ:"__"
63 | Theory _ -> "//theory:"))))
66 Http_getter_env.rdf_dir escaped_prefix baseuri extension
69 if patch_dtd then Http_getter_common.patch_xml else (fun x -> x)
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 *)
92 return_file ~fname:basename ~contype:"text/xml" ~patch_fun outchan
95 ~fname:(basename ^ ".gz") ~contype:"text/xml" ~contenc:"x-gzip"
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")
102 (* TODO enc is not yet supported *)
103 let respond_xsl ?(enc = Enc_normal) ?(patch_dtd = true) ~url outchan =
105 if patch_dtd then Http_getter_common.patch_xsl else (fun x -> x)
107 let fname = tempfile () in
108 wget ~output:fname url;
109 return_file ~fname ~contype:"text/xml" ~patch_fun outchan;
112 (* TODO enc is not yet supported *)
113 let respond_dtd ?(enc = Enc_normal) ?(patch_dtd = true) ~url outchan =
115 if patch_dtd then Http_getter_common.patch_dtd else (fun x -> x)
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
121 return_html_error ("Can't find DTD: " ^ url) outchan