Class: PHPRPC::LSAPIServer

Inherits:
BaseServer show all
Defined in:
lib/phprpc/lsapi_server.rb

Instance Attribute Summary

Attributes inherited from BaseServer

#charset, #debug

Instance Method Summary collapse

Methods inherited from BaseServer

#add, #call, #call!

Constructor Details

#initialize(options = {}) ⇒ LSAPIServer

Returns a new instance of LSAPIServer.



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
# File 'lib/phprpc/lsapi_server.rb', line 37

def initialize(options = {})
  super()
  @options = {
    :session_mode         => :file,
    :path                 => "/",
    :expire_after         => 1800,
  }.update(options)
  @opts = OptionParser.new
  @opts.banner = "Usage: #{@opts.program_name} lsapi [options]"
  @opts.separator ""
  @opts.separator "Session options:"
  @opts.on('-s', '--session-mode MODE', [:file, :memcache, :pool], "Select Session mode (file, memcache, pool)", "(default: #{@options[:session_mode].to_s})") { |mode| @options[:session_mode] = mode }
  @opts.on('-e','--expire-after TIME', Integer, "Session expire after (default: #{@options[:expire_after]})") { |time| @options[:expire_after] = time }
  @opts.on('-m','--memcache_server SERVER', String, "Memcache server used by session in", "memcache mode (default: localhost:11211)") { |server| @options[:memcache_server] = server }
  @opts.on('--prefix PATH', String, "Mount the PHPRPC Server under PATH", "(start with /)") { |path| @options[:path] = path }
  @opts.separator ""
  @opts.separator "Common options:"
  @opts.on_tail("-D", "--debug", "Set debbuging on") { self.debug = true }
  @opts.on_tail('-?', '-h', '--help', "Show this help message.") { puts @opts; exit }
  begin
    @opts.parse!(ARGV)
  rescue OptionParser::ParseError
    puts @opts
    exit
  end
end

Instance Method Details

#startObject



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
# File 'lib/phprpc/lsapi_server.rb', line 64

def start
  app = self
  if [:memcache, :pool].include?(@options[:session_mode]) then
    begin
      require 'rack'
      if @options[:session_mode] == :memcache then
        app = Rack::Session::Memcache.new(self, @options)
      else
        app = Rack::Session::Pool.new(self, @options)
      end
    rescue Exception
      app = self
    end
  end
  require 'lsapi'
  while LSAPI.accept != nil
    env = ENV.to_hash
    env["rack.input"] = STDIN
    env["rack.multithread"] = false
    env["rack.url_scheme"] = ["yes", "on", "1"].include?(ENV["HTTPS"]) ? "https" : "http"
    env["QUERY_STRING"] ||= ""
    env["HTTP_VERSION"] ||= env["SERVER_PROTOCOL"]
    env["REQUEST_PATH"] ||= "/"
    status, headers, body = app.call(env)
    print "Status: #{status}#{EOL}"
    headers.each { |k, v|
      print "#{k}: #{v}#{EOL}"
    }
    print EOL
    STDOUT.flush
    print body
    STDOUT.flush
  end
end