2 * Copyright (C) 2000, 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_types;;
31 let resource_type_of_url = function
32 | url when Pcre.pmatch ~pat:"\\.xml\\.gz$" url -> Enc_gzipped
33 | url when Pcre.pmatch ~pat:"\\.xml$" url -> Enc_normal
34 | url -> raise (Http_getter_invalid_URL url)
35 let extension_of_resource_type = function
37 | Enc_gzipped -> "xml.gz"
39 (* basename = resource name without trailing ".gz", if any *)
40 let is_in_cache basename =
42 (match Http_getter_env.cache_mode with
43 | Enc_normal -> basename
44 | Enc_gzipped -> basename ^ ".gz")
46 let respond_xml ?(enc = Enc_normal) ?(patch_dtd = true) ~url ~uri outchan =
47 let resource_type = resource_type_of_url url in
48 let extension = extension_of_resource_type resource_type in
50 match http_getter_uri_of_string uri with
51 | Xml_uri (Cic baseuri) | Xml_uri (Theory baseuri) ->
52 (* assumption: baseuri starts with "/" *)
53 sprintf "%s%s.%s" Http_getter_env.xml_dir baseuri extension
54 | Rdf_uri (prefix, ((Cic baseuri) as qbaseuri))
55 | Rdf_uri (prefix, ((Theory baseuri) as qbaseuri)) ->
57 (Pcre.replace ~pat:"/" ~templ:"_"
58 (Pcre.replace ~pat:"_" ~templ:"__"
62 | Theory _ -> "//theory:"))))
65 Http_getter_env.rdf_dir escaped_prefix baseuri extension
68 if patch_dtd then Http_getter_common.patch_xml else (fun x -> x)
70 let basename = Pcre.replace ~pat:"\\.gz$" downloadname in
71 if not (is_in_cache basename) then begin (* download and fill cache *)
72 wget ~output:downloadname url;
73 match (resource_type, Http_getter_env.cache_mode) with
74 | Enc_normal, Enc_normal ->
75 (if enc = Enc_gzipped then gzip ~keep:true downloadname)
76 | Enc_gzipped, Enc_gzipped ->
77 (if enc = Enc_normal then gunzip ~keep:true downloadname)
78 | Enc_normal, Enc_gzipped -> gzip ~keep:(enc = Enc_normal) downloadname
79 | Enc_gzipped, Enc_normal -> gunzip ~keep:(enc = Enc_gzipped) downloadname
80 end else begin (* resource already in cache *)
81 match (enc, Http_getter_env.cache_mode) with
82 | Enc_normal, Enc_normal | Enc_gzipped, Enc_gzipped -> ()
83 | Enc_normal, Enc_gzipped -> gunzip ~keep:true (basename ^ ".gz")
84 | Enc_gzipped, Enc_normal -> gzip ~keep:true basename
85 end; (* now resource is in cache *)
86 (* invariant: file to be sent back to client is available on disk in the
87 format the client likes *)
88 (match enc with (* send file to client *)
90 return_file ~fname:basename ~contype:"text/xml" ~patch_fun outchan
93 ~fname:(basename ^ ".gz") ~contype:"text/xml" ~contenc:"x-gzip"
95 match (enc, Http_getter_env.cache_mode) with (* remove temp files *)
96 | Enc_normal, Enc_normal | Enc_gzipped, Enc_gzipped -> ()
97 | Enc_normal, Enc_gzipped -> Sys.remove basename
98 | Enc_gzipped, Enc_normal -> Sys.remove (basename ^ ".gz")
100 (* TODO enc is not yet supported *)
101 let respond_xsl ?(enc = Enc_normal) ?(patch_dtd = true) ~url outchan =
103 if patch_dtd then Http_getter_common.patch_xsl else (fun x -> x)
105 let fname = tempfile () in
106 wget ~output:fname url;
107 return_file ~fname ~contype:"text/xml" ~patch_fun outchan;
110 (* TODO patch_dtd and format are fooish, implement them! *)
111 let respond_dtd ?(enc = Enc_normal) ?(patch_dtd = true) ~url outchan =
113 if patch_dtd then Http_getter_common.patch_dtd else (fun x -> x)
115 if Sys.file_exists url then
116 (* TODO check this: old getter here use text/xml *)
117 return_file ~fname:url ~contype:"text/plain" ~patch_fun outchan
119 return_html_error ("Can't find DTD: " ^ url) outchan