summaryrefslogtreecommitdiff
path: root/scripts/context/ruby/www/exa.rb
blob: c6d59d86434ab73cd2660e531101d64e0dceb6a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
require 'fileutils'
require 'www/lib'
require 'www/dir'
require 'www/common'
require 'www/admin'

class WWW

    include Common

    def handle_exadefault
        check_template_file('exalogin','exalogin-template.htm')
        if id = logged_in_session(true) then
            finish_login
        else
            message('Error', 'No default login permitted.')
        end
    end

    def handle_exalogin
        check_template_file('exalogin','exalogin-template.htm')
        if id = logged_in_session(false) then
            finish_login
        else
            message('Error', 'No default login permitted.')
        end
    end

    def finish_login
        get_gui()
        filename, path, task = @session.get('gui'), @session.checked('path','.'), @session.get('task')
        if ! task.empty? then
            save_session
            handle_exatask
        elsif filename and not filename.empty? then
            save_session
            fullname = filename.gsub(/\.\./,'')
            fullname = File.join(path,filename)                                     unless FileTest.file?(fullname)
            fullname = File.join(@interface.get('path:interfaces'), filename)       unless FileTest.file?(fullname)
            fullname = File.join(@interface.get('path:interfaces'), path, filename) unless FileTest.file?(fullname)
            if FileTest.file?(fullname) then
                send_file(fullname,true)
            else
                message('Interface', 'Invalid interface request, no valid interface file.' )
            end
        else
            message('Interface', 'Invalid interface request, no default interface file.')
        end
    end

    def handle_exainterface()
        check_template_file('text','exalogin-template.htm')
        if id = valid_session() then
            filename = @interface.get('process:uri').to_s # kind of dup
            if ! filename.empty? && filename.sub!(/^.*\//,'') then
                path = @session.checked('path', '.')
                fullname = filename.gsub(/\.\./,'')
                fullname = File.join(path,filename) unless FileTest.file?(fullname)
                fullname = File.join(@interface.get('path:interfaces'),filename) unless FileTest.file?(fullname)
                fullname = File.join(@interface.get('path:interfaces'),path,filename) unless FileTest.file?(fullname)
                if FileTest.file?(fullname) then
                    save_session
                    send_file(fullname,true)
                else
                    get_file(filename)
                    filename, path = @session.get('gui'), @session.checked('path','.')
                    if filename and not filename.empty? then
                        save_session
                        fullname = filename.gsub(/\.\./,'')
                        fullname = File.join(path,filename) unless FileTest.file?(fullname)
                        fullname = File.join(@interface.get('path:interfaces'),filename) unless FileTest.file?(fullname)
                        fullname = File.join(@interface.get('path:interfaces'),path,filename) unless FileTest.file?(fullname)
                        send_file(fullname,true) if FileTest.file?(fullname)
                    else
                        message('Interface', 'Invalid interface request, no interface file.')
                    end
                end
            else
                message('Interface', 'Invalid interface request, no resource file.')
            end
        else
            message('Interface', 'Invalid interface request, no login.')
        end
    end

    def handle_exarequest() # todo: check if request is 'command'
        check_template_file('exalogin','exalogin-template.htm')
        if id = client_session() then
            client = true
            @interface.set('log:kind', "remote client request: #{id}")
        elsif id = valid_session() then
            client = false
            @interface.set('log:kind', "remote browser request: #{id}")
        else
            client, id = false, nil
            @interface.set('log:kind', 'unknown kind of request')
        end
        if id then
            dir, tmp = dirname, tmp_path(dirname)
            requestname, replyname = 'request.exa', 'reply.exa'
            requestfile, replyfile = File.join(tmp,requestname), File.join(tmp,replyname)
            lockfile = File.join(dirname,lckname)
            action, filename, command, url, req = '', '', '', '', ''
            extract_sent_files(tmp)
            @variables.each do |key, value|
                case key
                    when 'exa:request' then
                        req = value.dup
                    when 'exa:action' then
                        action = value.dup
                    # when 'exa:command' then
                        # command = value.dup
                    # when 'exa:url' then
                        # url = value.dup
                    when 'exa:filename' then
                        filename = value.dup
                    when 'exa:threshold' then
                        @interface.set('process:threshold', value.dup)
                    when /^fakename/o then
                        @variables.set(key, File.basename(value))
                    when /^filename\-/o then
                        @variables.set(key, filename = File.basename(value))
                    when /^dataname\-/o then
                        @variables.set(key)
                    else # remove varname- prefix from value
                        @variables.set(key, @variables.get(key).sub(/#{key}\-/,''))
                end
            end
            @variables.check('exa:filename', filename)
            @variables.check('exa:action', action)
            if @variables.empty?('exa:filename') then
                @variables.set('exa:filename', @interface.get('log:attachments').split('|').first || '')
            end
            req.gsub!(/<exa:data\s*\/>/i, '')
            dat = "<exa:data>\n"
            @variables.each do |key, value|
                if ['password','exa:request'].include?(key) then
                    # skip
                elsif ! value || value.empty? then
                    dat << "<exa:variable label='#{key}'/>\n"
                else # todo: escape 'm
                    dat << "<exa:variable label='#{key}'>#{value}</exa:variable>\n"
                end
            end
            dat << "</exa:data>\n"
            if req.empty? then
                req << "<?xml version='1.0' ?>\n"
                req << "<exa:request #{@@namespace}'>\n"
                req << "<exa:application>\n"
                req << "<exa:action>'#{action}</exa:action>\n"     unless action.empty?
                # req << "<exa:command>'#{command}</exa:command>\n"  unless command.empty?
                # req << "<exa:url>'#{url}</exa:url>\n"              unless url.empty?
                req << "<exa:application>\n"
                req << "<exa:comment>constructed request</exa:comment>\n"
                req << dat
                req << "</exa:request>\n"
            else
                # better use rexml but slower
                if req =~ /<exa:request[^>]*>.*?\s*<exa:threshold>\s*(.*?)\s*<\/exa:threshold>\s*.*?<\/exa:request>/mois then
                    threshold = $1
                    unless threshold.empty? then
                        @interface.set('process:threshold', threshold)
                        @session.set('threshold', threshold)
                    end
                end
                req.sub!(/(<exa:request[^>]*>.*?)\s*<exa:option>\s*\-\-action\=(.*?)\s*<\/exa:option>\s*(.*?<\/exa:request>)/mois) do
                    pre, act, pos = $1, $2, $3
                    action = act.sub(/\.exa$/,'') if action.empty?
                    str = "#{pre}<exa:action>#{action}</exa:action>#{pos}"
                    str.sub(/\s*<exa:command>.*?<\/exa:command>\s*/mois ,'')
                end
                req.sub!(/(<exa:request[^>]*>.*?)<exa:action>\s*(.*?)\s*<\/exa:action>(.*?<\/exa:request>)/mois) do
                    pre, act, pos = $1, $2, $3
                    action = act.sub(/\.exa$/,'') if action.empty?
                    str = "#{pre}<exa:action>#{action}</exa:action>#{pos}"
                    str.sub(/\s*<exa:command>.*?<\/exa:command>\s*/mois ,'')
                end
                unless req =~ /<exa:data>(.*?)<\/exa:data>/mois then
                    req.sub!(/(<\/exa:request>)/) do dat + $1 end
                end
            end
            req.sub!(/<exa:filename>.*?<\/exa:filename>/mois, '')
            unless @variables.empty?('exa:filename') then
                req.sub!(/(<\/exa:application>)/mois) do
                    "<exa:filename>#{@variables.get('exa:filename')}<\/exa:filename>" + $1
                end
            end
            @variables.set('exa:action', action)
            @interface.set("log:#{requestname}", req)
            begin
                File.open(requestfile,'w') do |f|
                    f << req
                end
            rescue
                message('Error', 'There is a problem in handling this request (working path access).')
                return
            end
            File.delete(replyfile) rescue false
            @interface.set('log:action',action)
            get_command(action)
            logdata = ''
            begin
                command = @session.get('command')
                @interface.set('log:command',if command.empty? then '[no command]' else command end)
                if ! command.empty? then
                    @session.set('starttime', Time.now.to_i.to_s) # can be variables and in save list
                    if @interface.true?('process:background') then
                        # background
                        @session.set('status',  'running: background')
                        @session.set('maxtime', @interface.get('process:timeout'))
                        @session.set('threshold', @interface.get('process:threshold'))
                        save_session
                        timeout(@@watch_delay) do
                            save_environment(@interface,tmp)
                            begin
                                starttime = File.mtime(@session_file)
                                # crap
                                loop do
                                    sleep(1)
                                    if starttime != File.mtime(@session_file) then
                                        break unless FileTest.file?(lockfile)
                                    end
                                end
                            rescue TimeoutError
                                if client then
                                    send_reply()
                                else
                                    message('Status', 'Processing your request takes a while',true,5,'exastatus')
                                end
                                return
                            rescue
                            end
                        end
                        if client then send_reply() else send_result() end
                    else
                        # foreground
                        status = 'running: foreground'
                        @session.set('status',  status)
                        @session.set('maxtime', @interface.get('process:timeout'))
                        @session.set('threshold', @interface.get('process:threshold'))
                        save_session
                        timeout(@interface.get('process:timeout').to_i) do
                            begin
                                status = 'running: foreground'
                                set_environment(@interface)
                                save_environment(@interface,tmp)
                                command = command_string(tmp,command)
                                logdata = `#{command}`
                            rescue TimeoutError
                                status = 'running: timeout'
                                logdata = "timeout: #{@interface.get('process:timeout')} seconds"
                            rescue
                                status = 'running: aborted'
                                logdata = 'fatal runtime error'
                            else
                                @session.set('endtime', Time.now.to_i.to_s)
                                status = 'running: finished'
                            end
                        end
                        @session.set('status', status)
                        save_session
                        case @session.get('status')
                            when 'running: finished' then
                                if client then send_reply(logdata) else send_result(logdata) end
                            when 'running: timeout' then
                                message('Error', 'There is a problem in handling this request (timeout).')
                            when 'running: aborted' then
                                message('Error', 'There is a problem in handling this request (aborted).')
                            else
                                message('Error', 'There is a problem in handling this request (unknown).')
                        end
                    end
                else
                    message('Error', 'There is a problem in handling this request (no runner).')
                end
            rescue
                message('Error', 'There is a problem in handling this request (no run).' + $!)
            end
        else
            message('Error', 'Invalid session.')
        end
    end

    def handle_exacommand() # shares code with exarequest
        check_template_file('exalogin','exalogin-template.htm')
        if id = client_session() then
            client = true
            @interface.set('log:kind', "remote client request: #{id}")
        elsif id = valid_session() then
            client = false
            @interface.set('log:kind', "remote browser request: #{id}")
        else
            client, id = false, nil
            @interface.set('log:kind', 'unknown kind of request')
        end
        if id then
            dir, tmp = dirname, tmp_path(dirname)
            requestname, replyname = 'request.exa', 'reply.exa'
            requestfile, replyfile = File.join(tmp,requestname), File.join(tmp,replyname)
            req, command, url = '', '', ''
            @variables.each do |key, value|
                case key
                    when 'exa:request' then
                        req = value.dup
                    when 'exa:command' then
                        command = value.dup
                    when 'exa:threshold' then
                        @interface.set('process:threshold', value.dup)
                    when 'exa:url' then
                        url = value.dup
                end
            end
            unless req.empty? then
                # better use rexml but slower / reuse these : command = filter_from_request('exa:command')
                if req =~ /<exa:request[^>]*>.*?\s*<exa:command>\s*(.*?)\s*<\/exa:command>\s*.*?<\/exa:request>/mois then
                    command = $1
                end
                if req =~ /<exa:request[^>]*>.*?\s*<exa:url>\s*(.*?)\s*<\/exa:url>\s*.*?<\/exa:request>/mois then
                    url = $1
                end
                if req =~ /<exa:request[^>]*>.*?\s*<exa:threshold>\s*(.*?)\s*<\/exa:threshold>\s*.*?<\/exa:request>/mois then
                    threshold = $1
                    unless threshold.empty? then
                        @interface.set('process:threshold', threshold)
                        @session.set('threshold', threshold)
                    end
                end
            end
            @variables.check('exa:command', command)
            @variables.check('exa:url', url)
            File.delete(replyfile) rescue false
            case @variables.get('exa:command')
                when 'fetch' then
                    if @variables.empty?('exa:url') then
                        message('Error', "Problems with fetching, no file given")
                    else
                        # the action starts here
                        filename = @variables.get('exa:url').to_s # kind of dup
                        unless filename.empty? then
                            get_path(filename) # also registers filename as url
                            path = @session.checked('path', '')
                            fullname = filename.gsub(/\.\./,'')
                            fullname = File.join(path,fullname) unless path.empty?
                            if FileTest.file?(fullname) then
                                if client then
                                    send_url(fullname)
                                else
                                    send_file(fullname,true)
                                end
                                @session.set('threshold', @interface.get('process:threshold'))
                                @session.set('url',filename)
                                save_session
                            else
                                message('Error', "Problems with fetching, unknown file #{fullname}.")
                                # message('Error', "Problems with fetching, unknown file #{filename}.")
                            end
                        else
                           message('Error', "Problems with fetching, invalid file #{filename}.")
                        end
                        # and ends here
                    end
            else
                message('Error', "Invalid command #{command}.")
            end
        else
            message('Error', 'Invalid session.')
        end
    end

    def handle_exastatus
        if request_variable('id').empty? then
            if id = valid_session() then
                send_result()
            else
                message('Error', 'Invalid session.')
            end
        else
            if id = valid_session() then
                send_reply()
            else
                send_reply('invalid session')
            end
        end
    end

end