Class: PHPRPC::FCGIServer

Inherits:
BaseServer show all
Defined in:
lib/phprpc/fcgi_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 = {}) ⇒ FCGIServer

Returns a new instance of FCGIServer.



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

def initialize(options = {})
  super()
  @options = {
    :session_mode         => :file,
    :path                 => "/",
    :expire_after         => 1800,
  }.update(options)
  @opts = OptionParser.new
  @opts.banner = "Usage: #{@opts.program_name} fcgi [options]"
  @opts.separator ""
  @opts.separator "Server options:"
  @opts.on('-a', '--address IP', String, "Bind FCGI to the specified ip.") { |ip| @options[:host] = ip }
  @opts.on('-p', '--port PORT', Integer, "Run FCGI on the specified port.") { |port| @options[:port] = port }
  @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



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

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
  begin
    if @options[:port] then
      @options[:host] = '0.0.0.0' if @options[:host].nil?
      puts "## PHPRPC FCGI Server 3.0.0"
      STDIN.reopen(TCPServer.new(@options[:host], @options[:port]))
      puts "## Listening on #{@options[:host]}:#{@options[:port]}, CTRL+C to stop"
    end
    trap(:INT) { exit }
    FCGI.each { |request|
      env = request.env
      env["rack.input"] = request.in
      env["rack.multithread"] = false # this variable only used for rack pool session on debug mode
      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)
      begin
        out = request.out
        out.print "Status: #{status}\r\n"
        headers.each { |k, v|
          out.print "#{k}: #{v}\r\n"
        }
        out.print "\r\n"
        out.flush
        out.print body
        out.flush
      ensure
        request.finish
      end
    }
  rescue SystemExit
    exit
  rescue Exception => e
    puts "## #{@options[:host]}:#{@options[:port]} #{e.message}"
    puts @opts
    exit
  end
end