Imported Upstream version 2.0.1
[pkg-ocaml-eliom.git] / src / client / eliom_request_info.ml
blobf71546f43709b32ffc2e64d4b2e73846c2d13a1f
1 (* Ocsigen
2 * http://www.ocsigen.org
3 * Module eliom_sessions.ml
4 * Copyright (C) 2009 Vincent Balat
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, with linking exception;
9 * either version 2.1 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 (* TODO: add missing functions to get
22 (almost) the same interface as server side *)
24 (* - Part of sp is reconstructed client side
25 - Another part is sent as application parameter (sitedata)
26 - Antother part is sent with each request
29 open Eliom_pervasives
31 let (>>>) x f = f x
33 include Eliom_types
35 let get_sess_info = ref (fun () ->
36 failwith "Eliom_request_info.get_sess_info called before initialization")
38 let set_session_info si = get_sess_info := fun () -> si
40 let remove_first_slash path =
41 match path with
42 | ""::l -> l
43 | l -> l
45 let path_re =
46 jsnew Js.regExp (Js.bytestring "^/?([^\\?]*)(\\?.*)?$")
48 let current_path = ref (remove_first_slash Url.Current.path)
49 let set_current_path path =
50 let path =
51 Js.Opt.case (path_re##exec (Js.string path))
52 (fun () -> [])
53 (fun handle ->
54 let res = Js.match_result handle in
55 let path =
56 Js.to_bytestring
57 (Js.Optdef.get
58 (Js.array_get res 1)
59 (fun () -> Js.bytestring ""))
61 Url.split_path path)
63 current_path := path
65 let get_original_full_path_string () =
66 if Eliom_process.history_api then
67 match Url.Current.get () with
68 | Some (Url.Http url) | Some (Url.Https url) ->
69 String.concat "/" url.Url.hu_path
70 | _ -> assert false
71 else
72 String.concat "/" !current_path
73 let get_original_full_path_string_sp = get_original_full_path_string
75 let get_original_full_path_sp sp =
76 if Eliom_process.history_api then
77 match Url.Current.get () with
78 | Some (Url.Http url) | Some (Url.Https url) -> url.Url.hu_path
79 | _ -> assert false
80 else
81 !current_path
83 let get_other_get_params () =
84 (!get_sess_info ()).Eliom_common.si_other_get_params
85 let get_nl_get_params () = (!get_sess_info ()).Eliom_common.si_nl_get_params
86 let get_nl_get_params_sp = get_nl_get_params
88 let get_persistent_nl_get_params () =
89 Lazy.force (!get_sess_info ()).Eliom_common.si_persistent_nl_get_params
90 let get_persistent_nl_get_params_sp = get_persistent_nl_get_params
92 let get_nl_post_params () =
93 (!get_sess_info ()).Eliom_common.si_nl_post_params
95 let get_si () = !get_sess_info ()
97 let get_site_dir () = Eliom_process.sitedata.site_dir
98 let get_site_dir_string () =
99 Eliom_process.sitedata.site_dir_string
101 let get_sp_appl_name = Eliom_process.get_application_name
103 let ssl_ = match Url.Current.get () with
104 | Some (Url.Https _) -> true
105 | Some (Url.Http _) | Some (Url.File _) | None -> false
107 let get_csp_ssl () = ssl_
108 let get_csp_ssl_sp = get_csp_ssl
110 let host_ = Url.Current.host
112 let get_csp_hostname () = host_
113 let get_csp_hostname_sp = get_csp_hostname
115 let port_ = match Url.Current.port with
116 | Some p -> p
117 | None -> if ssl_ then 443 else 80
119 let get_csp_server_port () = port_
120 let get_csp_server_port_sp = get_csp_server_port
122 let get_csp_original_full_path () =
123 if Eliom_process.history_api then
124 match Url.Current.get () with
125 | Some (Url.Http url) | Some (Url.Https url) -> url.Url.hu_path
126 | _ -> assert false
127 else
128 remove_first_slash Url.Current.path
130 let get_csp_original_full_path_sp = get_csp_original_full_path
132 let get_request_cookies () = unmarshal_js_var "eliom_request_cookies"
133 let get_request_data () = Eliom_unwrap.unwrap (unmarshal_js_var "eliom_request_data")