Class: Spade::Server::CommandRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/spade/server.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ CommandRunner

Returns a new instance of CommandRunner.



22
23
24
# File 'lib/spade/server.rb', line 22

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object

FIXME: This is not very safe, we should have some restrictions



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
# File 'lib/spade/server.rb', line 27

def call(env)
  if env['PATH_INFO'] == '/_spade/command'
    rack_input = env["rack.input"].read
    params = Rack::Utils.parse_query(rack_input, "&")

    command_path = params['command']
    return [500, {}, "command required"] if command_path.nil? || command_path.empty?

    if ((root = params['pkgRoot']) && !root.nil?)
      command_path = File.expand_path(File.join(command_path), root)
    end

    tempfile = Tempfile.new('spade-server')
    tempfile.write(params['code'])
    tempfile.close


    puts "Running: #{command_path}"

    output, error = nil
    process = ChildLabor.subprocess("#{command_path} < #{tempfile.path}") do |p|
      output = p.read
      error = p.read_stderr
    end

    tempfile.delete

    if process.exit_status == 0
      [200, {}, output]
    else
      [500, {}, error]
    end
  else
    env['PATH_INFO'] == '/index.html' if env['PATH_INFO'] == '/'
    @app.call(env)
  end
end