Sha256: a8e935de01b97e05e349c3566a8a0b886244664bb1737f91895f4f1907d3f97c

Contents?: true

Size: 1.77 KB

Versions: 3

Compression:

Stored size: 1.77 KB

Contents

require 'rack'
require 'rack/static'
require 'tempfile'
require 'child_labor'

module Spade
  module Server
    def self.run(working, port)
      rootdir = Spade.discover_root(working)
      static = Rack::Static.new(nil, :urls => ['/'], :root => rootdir)
      static = CommandRunner.new(static)
      static = Rack::ShowStatus.new(Rack::ShowExceptions.new(Rack::Chunked.new(Rack::ContentLength.new(static))))

      Rack::Handler::WEBrick.run static, :Port => port.to_i
    end

    def self.shutdown
      Rack::Handler::WEBrick.shutdown
    end

    class CommandRunner
      def initialize(app)
        @app = app
      end

      #FIXME: This is not very safe, we should have some restrictions
      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
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
spade-0.1.1.1 lib/spade/server.rb
spade-0.0.8.1 lib/spade/server.rb
spade-0.0.7 lib/spade/server.rb