3 * Stefano Zacchiroli <zack@cs.unibo.it>
4 * for the HELM Team http://helm.cs.unibo.it/
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.
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.
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.
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,
25 * For details, see the HELM World-Wide-Web page,
26 * http://helm.cs.unibo.it/
29 open Http_getter_types;;
32 let string_of_ls_flag = function No -> "NO" | Yes -> "YES" | Ann -> "ANN"
33 let string_of_encoding = function
34 | Enc_normal -> "Normal"
35 | Enc_gzipped -> "GZipped"
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
45 let blank_line_RE = Pcre.regexp "(^#)|(^\\s*$)" in
47 Pcre.pmatch ~rex:blank_line_RE line
49 let rec http_getter_uri_of_string = function
50 | uri when is_rdf_uri uri ->
51 (match Pcre.split ~pat:"//" uri with
54 match http_getter_uri_of_string uri with
55 | Cic_uri xmluri -> xmluri
56 | _ -> raise (Http_getter_invalid_URI uri)
58 Rdf_uri (prefix, rest)
59 | _ -> raise (Http_getter_invalid_URI uri))
60 | uri when is_cic_uri uri -> Cic_uri (Cic (Pcre.replace ~pat:"^cic:" uri))
61 | uri when is_nuprl_uri uri -> Nuprl_uri (Pcre.replace ~pat:"^nuprl:" uri)
62 | uri when is_theory_uri uri ->
63 Cic_uri (Theory (Pcre.replace ~pat:"^theory:" uri))
64 | uri -> raise (Http_getter_invalid_URI uri)
68 ~pat:(sprintf "DOCTYPE (.*) SYSTEM\\s+\"%s/" Http_getter_env.dtd_base_url)
70 sprintf "DOCTYPE $1 SYSTEM \"%s/getdtd?uri=" Http_getter_env.my_own_url)
73 let mk_patch_fun tag line =
75 ~pat:(sprintf "%s\\s+href=\"" tag)
77 sprintf "%s href=\"%s/getxslt?uri=" tag Http_getter_env.my_own_url)
80 let (patch_import, patch_include) =
81 (mk_patch_fun "xsl:import", mk_patch_fun "xsl:include")
83 fun line -> patch_include (patch_import line)
86 ~pat:"ENTITY (.*) SYSTEM\\s+\""
88 sprintf "ENTITY $1 SYSTEM \"%s/getdtd?uri=" Http_getter_env.my_own_url)
92 sprintf "<html><body><h1>Http Getter error: %s</h1></body></html>" s
93 let pp_internal_error s =
94 sprintf "<html><body><h1>Http Getter Internal error: %s</h1></body></html>" s
95 let pp_msg s = sprintf "<html><body><h1>%s</h1></body></html>" s
98 let mk_return_fun pp_fun contype msg outchan =
100 ~body:(pp_fun msg) ~headers:["Content-Type", contype] outchan
102 let return_html_error = mk_return_fun pp_error "text/html"
103 let return_html_internal_error = mk_return_fun pp_internal_error "text/html"
104 let return_html_msg = mk_return_fun pp_msg "text/html"
105 let return_html_raw = mk_return_fun null_pp "text/html"
106 let return_xml_raw = mk_return_fun null_pp "text/xml"
108 ~fname ?contype ?contenc ?(patch_fun = fun x -> x) ?(gunzip = false) outchan
111 match (contype, contenc) with
112 | (Some t, Some e) -> ["Content-Encoding", e; "Content-Type", t]
113 | (Some t, None) -> ["Content-Type" , t]
114 | (None, Some e) -> ["Content-Encoding", e]
117 Http_daemon.send_basic_headers ~code:200 outchan;
118 Http_daemon.send_headers headers outchan;
119 Http_daemon.send_CRLF outchan;
120 if gunzip then begin (* gunzip needed, uncompress file, apply patch_fun to
121 it, compress the result and sent it to client *)
123 (Http_getter_misc.tempfile (), Http_getter_misc.tempfile ())
125 Http_getter_misc.gunzip ~keep:true ~output:tmp1 fname; (* gunzip to tmp1 *)
126 let new_file = open_out tmp2 in
127 Http_getter_misc.iter_file (* tmp2 = patch(tmp1) *)
128 (fun line -> output_string new_file (patch_fun line ^ "\n"))
131 Http_getter_misc.gzip ~output:tmp1 tmp2; (* tmp1 = gzip(tmp2); rm tmp2 *)
132 Http_getter_misc.iter_file (* send tmp1 to client as is*)
133 (fun line -> output_string outchan (line ^ "\n"))
135 Sys.remove tmp1 (* rm tmp1 *)
136 end else (* no need to gunzip, apply patch_fun directly to file *)
137 Http_getter_misc.iter_file
138 (fun line -> output_string outchan (patch_fun line ^ "\n"))
141 let return_400 body outchan = Http_daemon.respond_error ~code:400 ~body outchan