Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | daniel-mar | 1 | module ("sajax") |
2 | |||
3 | local export_list = {} |
||
4 | local request_uri = "" |
||
5 | |||
6 | function init () |
||
7 | end |
||
8 | |||
9 | function handle_client_request () |
||
10 | |||
11 | if not cgi.rs then return end |
||
12 | |||
13 | -- Bust cache in the head |
||
14 | cgilua.header ("Expires", "Mon, 26 Jul 1997 05:00:00 GMT") -- Date in the past |
||
15 | cgilua.header ("Last-Modified", os.date ("!%a, %d %b %Y %H:%M:%S GMT")) |
||
16 | -- always modified |
||
17 | cgilua.header ("Cache-Control", "no-cache, must-revalidate") -- HTTP/1.1 |
||
18 | cgilua.header ("Pragma", "no-cache") -- HTTP/1.0 |
||
19 | |||
20 | local funcname = cgi.rs |
||
21 | |||
22 | if not export_list[funcname] then |
||
23 | cgilua.put (string.format ("-:%s not callable", funcname)) |
||
24 | else |
||
25 | local func = export_list[funcname] |
||
26 | local rsargs = cgi["rsargs[]"] |
||
27 | local result |
||
28 | |||
29 | if not rsargs then |
||
30 | result = func() |
||
31 | elseif type (rsargs) == "string" then |
||
32 | result = func (rsargs) |
||
33 | elseif type (rsargs) == "table" then |
||
34 | result = func (unpack (rsargs)) |
||
35 | else |
||
36 | return |
||
37 | end |
||
38 | |||
39 | cgilua.put ("+:") |
||
40 | cgilua.put (result) |
||
41 | end |
||
42 | |||
43 | return true |
||
44 | end |
||
45 | |||
46 | local function show_common_js () |
||
47 | cgilua.put [[ |
||
48 | // remote scripting library |
||
49 | // (c) copyright 2005 modernmethod, inc |
||
50 | var rs_debug_mode = false; |
||
51 | |||
52 | function rs_debug(text) { |
||
53 | if (rs_debug_mode) |
||
54 | alert("RSD: " + text) |
||
55 | } |
||
56 | function rs_init_object() { |
||
57 | rs_debug("rs_init_object() called..") |
||
58 | |||
59 | var A; |
||
60 | try { |
||
61 | A=new ActiveXObject("Msxml2.XMLHTTP"); |
||
62 | } catch (e) { |
||
63 | try { |
||
64 | A=new ActiveXObject("Microsoft.XMLHTTP"); |
||
65 | } catch (oc) { |
||
66 | A=null; |
||
67 | } |
||
68 | } |
||
69 | if(!A && typeof XMLHttpRequest != "undefined") |
||
70 | A = new XMLHttpRequest(); |
||
71 | if (!A) |
||
72 | rs_debug("Could not create connection object."); |
||
73 | return A; |
||
74 | } |
||
75 | ]] |
||
76 | end |
||
77 | |||
78 | local function show_one (funcname) |
||
79 | local uri = request_uri |
||
80 | if string.find (uri, "?") then |
||
81 | uri = uri .. "&rs=" .. cgilua.urlcode.escape (funcname) |
||
82 | else |
||
83 | uri = uri .. "?rs=" .. cgilua.urlcode.escape (funcname) |
||
84 | end |
||
85 | cgilua.put (string.format ([[ |
||
86 | // wrapper for %s |
||
87 | |||
88 | function x_%s() { |
||
89 | // count args; build URL |
||
90 | var i, x, n; |
||
91 | var url = "%s"; |
||
92 | var a = x_%s.arguments; |
||
93 | for (i = 0; i < a.length-1; i++) |
||
94 | url = url + "&rsargs[]=" + escape(a[i]); |
||
95 | url = url + "&rsrnd=" + new Date().getTime(); |
||
96 | x = rs_init_object(); |
||
97 | x.open("GET", url, true); |
||
98 | x.onreadystatechange = function() { |
||
99 | if (x.readyState != 4) |
||
100 | return; |
||
101 | rs_debug("received " + x.responseText); |
||
102 | |||
103 | var status; |
||
104 | var data; |
||
105 | status = x.responseText.charAt(0); |
||
106 | data = x.responseText.substring(2); |
||
107 | if (status == "-") |
||
108 | alert("Error: " + callback_n); |
||
109 | else |
||
110 | a[a.length-1](data); |
||
111 | } |
||
112 | x.send(null); |
||
113 | rs_debug("x_%s url = " + url); |
||
114 | rs_debug("x_%s waiting.."); |
||
115 | delete x; |
||
116 | } |
||
117 | ]], funcname, funcname, uri, funcname, funcname, funcname)) |
||
118 | end |
||
119 | |||
120 | function export (funcname, func) |
||
121 | export_list[funcname] = func |
||
122 | end |
||
123 | |||
124 | local js_has_been_shown = false |
||
125 | |||
126 | function show_javascript () |
||
127 | if not js_has_been_shown then |
||
128 | show_common_js () |
||
129 | js_has_been_shown = true |
||
130 | end |
||
131 | |||
132 | for fn,_ in pairs (export_list) do |
||
133 | show_one (fn) |
||
134 | end |
||
135 | end |