class WebApp::Manager

Public Instance Methods

run_cli() click to toggle source

CLI (command line interface)

# File webapp/cli.rb, line 170
def run_cli
  if ARGV[0] == 'server'
    ARGV.shift
    run_cli_server
  else
    run_cli_offline
  end
end
run_cli_offline() click to toggle source
# File webapp/cli.rb, line 217
def run_cli_offline
  opt_output = '-'
  opt_cern_meta = false
  opt_server_name = 'localhost'
  opt_server_port = 80
  opt_script_name = "/#{File.basename($0)}"
  opt_remote_addr = '127.0.0.1'
  opt_headers = []
  ARGV.options {|q|
    q.banner = "#{File.basename $0} [options] [/path_info] [?query_string]"
    q.def_option('-h', '--help', 'show this message') { puts q; exit(0) }
    q.def_option('-o FILE', '--output=FILE', 'set output file') {|arg| opt_output = arg.untaint }
    q.def_option('--cern-meta', 'output header as CERN httpd metafile') { opt_cern_meta = true }
    q.def_option('--server-name=STRING', 'set server name') {|arg| opt_server_name = arg }
    q.def_option('--server-port=INTEGER', 'set server port number') {|arg| opt_server_port = arg.to_i }
    q.def_option('--script-name=STRING', 'set script name') {|arg| opt_script_name = arg }
    q.def_option('--remote-addr=STRING', 'set remote IP address') {|arg| opt_remote_addr = arg }
    q.def_option('--header=NAME:BODY', 'set additional request header') {|arg| opt_headers << arg.split(/:/, 2) }
    q.parse!
  } or exit(1)
  if ARGV[0] && %r{\A/} =~ ARGV[0]
    path_info = ARGV.shift
  end
  if ARGV[0] && %r{\A\?} =~ ARGV[0]
    ARGV.shift
    query_string = $'
  end
  if !ARGV.empty?
    raise "extra arguments: #{ARGV.inspect[1..-2]}"
  end
  path_info ||= ''
  query_string ||= ''
  setup_request = lambda {|req|
    req.make_request_header_from_cgi_env({
      'REQUEST_METHOD' => 'GET',
      'SERVER_NAME' => opt_server_name,
      'SERVER_PORT' => opt_server_port,
      'SCRIPT_NAME' => opt_script_name,
      'PATH_INFO' => path_info,
      'QUERY_STRING' => query_string,
      'SERVER_PROTOCOL' => 'HTTP/1.0',
      'REMOTE_ADDR' => opt_remote_addr,
      'CONTENT_TYPE' => ''
    })
    opt_headers.each {|name, body|
      req.header_object.add name, body
    }
  }
  output_response = lambda {|res|
    if opt_output == '-'
      res.output_cgi_status_field($stdout)
      res.output_message($stdout)
    else
      if opt_cern_meta
        dir = "#{File.dirname(opt_output)}/.web"
        begin
          Dir.mkdir dir
        rescue Errno::EEXIST
        end
        open("#{dir}/#{File.basename(opt_output)}.meta", 'w') {|f|
          #res.output_cgi_status_field(f)
          res.output_header(f)
        }
        open(opt_output, 'w') {|f|
          res.output_body(f)
        }
      else
        open(opt_output, 'w') {|f|
          res.output_cgi_status_field(f)
          res.output_message(f)
        }
      end
    end
  }
  primitive_run(setup_request, output_response)
end
run_cli_server() click to toggle source
# File webapp/cli.rb, line 179
def run_cli_server
  require 'webrick'
  config = {}
  case ARGV[0]
  when nil
    config = {
      :Port => TCPServer.open(0) {|s| s.addr[1] }
    }
  when /:(\d+)\z/
    config = {
      :ServerName => $`,
      :BindAddress => $`,
      :Port => $1.to_i
    }
  when /\A\d+\z/
    config = {
      :Port => ARGV[0].to_i
    }
  else
    raise ArgumentError, "unexpected port: #{ARGV[0].inspect}"
  end

  servlet = WEBrick::HTTPServlet::ProcHandler.new(run_webrick)
  Thread.current[:webrick_load_servlet] = nil

  top_uri = "http://"
  top_uri << (config[:BindAddress] || config[:ServerName] || WEBrick::Utils.getservername)
  top_uri << ":#{config[:Port]}" if config[:Port] != 80
  top_uri << '/'
  puts top_uri

  httpd = WEBrick::HTTPServer.new(config)
  trap(:INT){ httpd.shutdown }
  httpd.mount("/", servlet)
  httpd.start
  exit 0
end