Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | daniel-mar | 1 | // |
2 | // Sajax Io Backend |
||
3 | // (C) Copyright 2005 ModernMethod, Inc. |
||
4 | // Released under BSD license |
||
5 | // |
||
6 | |||
7 | String interpolate := method( |
||
8 | work := self clone |
||
9 | last_idx := 0 |
||
10 | while (1, |
||
11 | idx := work find("<?io", last_idx) |
||
12 | if (idx == Nil, |
||
13 | // no more interps.. |
||
14 | break) |
||
15 | idx := idx + 3 |
||
16 | end_idx := work find("?>", idx + 1) |
||
17 | if (end_idx == Nil, |
||
18 | // invalid interp expression.. we're done. |
||
19 | break) |
||
20 | |||
21 | // in the case of an empty interpolated expression, |
||
22 | // lets move on |
||
23 | if (end_idx - idx == 1, |
||
24 | continue) |
||
25 | |||
26 | // otherwise proceed with replacement. |
||
27 | slotName := work substring(idx+1, end_idx) |
||
28 | find_str := "<?io" .. slotName .. "?>" |
||
29 | // write("doString(" .. slotName .. ")") |
||
30 | replace_str := sender doString(slotName) |
||
31 | work := work replace(find_str, replace_str) |
||
32 | last_idx = end_idx + 1 |
||
33 | ) |
||
34 | return work |
||
35 | ) |
||
36 | |||
37 | Sajax := Object clone do ( |
||
38 | |||
39 | debug_mode := 0 |
||
40 | export_list := List clone |
||
41 | js_has_been_shown := Nil |
||
42 | |||
43 | init := method( |
||
44 | return self |
||
45 | ) |
||
46 | |||
47 | handle_client_request := method( |
||
48 | |||
49 | if (my_getenv("QUERY_STRING") == Nil, |
||
50 | return |
||
51 | ) |
||
52 | |||
53 | form := CGI clone parse |
||
54 | if (form hasKey("rs") == Nil, |
||
55 | return |
||
56 | ) |
||
57 | |||
58 | write("Content-type: text/html\n"); |
||
59 | write("Expires: Mon, 26 Jul 1997 05:00:00 GMT\n"); |
||
60 | write("Cache-Control: no-cache, must-revalidate\n"); |
||
61 | write("Pragma: no-cache\n\n"); |
||
62 | |||
63 | func_name := form at("rs") |
||
64 | if (export_list contains(func_name) == Nil) then ( |
||
65 | write("-:$func_name not callable\n") |
||
66 | ) else ( |
||
67 | write("+:") |
||
68 | args := form at("rsargs[]") |
||
69 | result := sender performWithArgList(func_name, args) |
||
70 | write(result) |
||
71 | ) |
||
72 | exit; |
||
73 | } |
||
74 | |||
75 | show_common_js := method( |
||
76 | |||
77 | if (debug_mode == 1, |
||
78 | debugModeTrueFalse := "true", |
||
79 | debugModeTrueFalse := "false") |
||
80 | |||
81 | html := """ |
||
82 | |||
83 | // remote scripting library |
||
84 | // (c) copyright 2005 modernmethod, inc |
||
85 | var rs_debug_mode = <?io debugModeTrueFalse ?>; |
||
86 | |||
87 | function rs_debug(text) { |
||
88 | if (rs_debug_mode) |
||
89 | alert("RSD: " + text) |
||
90 | } |
||
91 | function rs_init_object() { |
||
92 | rs_debug("rs_init_object() called..") |
||
93 | |||
94 | var A; |
||
95 | try { |
||
96 | A=new ActiveXObject("Msxml2.XMLHTTP"); |
||
97 | } catch (e) { |
||
98 | try { |
||
99 | A=new ActiveXObject("Microsoft.XMLHTTP"); |
||
100 | } catch (oc) { |
||
101 | A=null; |
||
102 | } |
||
103 | } |
||
104 | if(!A && typeof XMLHttpRequest != "undefined") |
||
105 | A = new XMLHttpRequest(); |
||
106 | if (!A) |
||
107 | rs_debug("Could not create connection object."); |
||
108 | return A; |
||
109 | } |
||
110 | |||
111 | """ |
||
112 | write(html interpolate) |
||
113 | } |
||
114 | |||
115 | esc := method(val, |
||
116 | return val replace("\"", "\\\\\""); |
||
117 | ) |
||
118 | |||
119 | my_getenv := method(val, |
||
120 | my_get := Nil |
||
121 | if (?System) then ( |
||
122 | my_get := System getSlot("getenv") |
||
123 | ) else ( |
||
124 | my_get := Object getSlot("getenv") |
||
125 | ) |
||
126 | return my_get(val) |
||
127 | ) |
||
128 | |||
129 | get_my_uri := method( |
||
130 | uri := "http://www.example.com/" |
||
131 | if (my_getenv("REQUEST_URI") != Nil) then ( |
||
132 | uri := my_getenv("REQUEST_URI") |
||
133 | ) else ( |
||
134 | if (my_getenv("SCRIPT_NAME") != Nil) then ( |
||
135 | uri := my_getenv("SCRIPT_NAME") |
||
136 | if (my_getenv("QUERY_STRING") != Nil) then ( |
||
137 | uri := uri .. "?" .. my_getenv("QUERY_STRING") |
||
138 | ) |
||
139 | ) |
||
140 | ) |
||
141 | return uri |
||
142 | ) |
||
143 | |||
144 | show_one := method(func_name, |
||
145 | uri := get_my_uri |
||
146 | func_name_encoded := CGI encodeUrlParam(func_name) |
||
147 | if (uri contains("?") == Nil) then ( |
||
148 | uri = uri .. "?rs=" .. func_name_encoded |
||
149 | ) else ( |
||
150 | uri = uri .. "&rs=" .. func_name_encoded |
||
151 | ) |
||
152 | |||
153 | html := """ |
||
154 | // wrapper for <?io func_name ?> |
||
155 | |||
156 | function x_<?io func_name ?>() { |
||
157 | // count args; build URL |
||
158 | var i, x, n; |
||
159 | var url = "<?io esc(uri) ?>"; |
||
160 | var a = x_<?io func_name ?>.arguments; |
||
161 | for (i = 0; i < a.length-1; i++) |
||
162 | url = url + "&rsargs[]=" + escape(a[i]); |
||
163 | url = url + "&rsrnd=" + new Date().getTime(); |
||
164 | x = rs_init_object(); |
||
165 | x.open("GET", url, true); |
||
166 | x.onreadystatechange = function() { |
||
167 | if (x.readyState != 4) |
||
168 | return; |
||
169 | rs_debug("received " + x.responseText); |
||
170 | |||
171 | var status; |
||
172 | var data; |
||
173 | status = x.responseText.charAt(0); |
||
174 | data = x.responseText.substring(2); |
||
175 | if (status == "-") |
||
176 | alert("Error: " + callback_n); |
||
177 | else |
||
178 | a[a.length-1](data); |
||
179 | } |
||
180 | x.send(null); |
||
181 | rs_debug("x_<?io func_name ?> url = " + url); |
||
182 | rs_debug("x_<?io func_name ?> waiting.."); |
||
183 | delete x; |
||
184 | } |
||
185 | """ |
||
186 | write(html interpolate) |
||
187 | ) |
||
188 | |||
189 | export := method(func, |
||
190 | export_list add(func) |
||
191 | } |
||
192 | |||
193 | show_javascript := method( |
||
194 | if (js_has_been_shown == Nil, |
||
195 | show_common_js |
||
196 | ) |
||
197 | export_list foreach(index, func_name, |
||
198 | show_one(func_name) |
||
199 | ) |
||
200 | ) |
||
201 | |||
202 | ) |
||
203 |