Sha256: dceff351bb76c0652c8d08319ec1f90feda0597774b71de98c120456c6816223

Contents?: true

Size: 1.78 KB

Versions: 63

Compression:

Stored size: 1.78 KB

Contents

#!/usr/bin/env ruby

require 'stringio'
require 'fileutils'
require 'fcgi_handler'

def message(s)
  $stderr.puts "listener: #{s}" if ENV && ENV["DEBUG_GATEWAY"]
end

class RemoteCGI < CGI
  attr_accessor :stdinput, :stdoutput, :env_table
  def initialize(env_table, input = nil, output = nil)
    self.env_table = env_table
    self.stdinput = input || StringIO.new
    self.stdoutput = output || StringIO.new
    super()
  end

  def out(stream) # Ignore the requested output stream
    super(stdoutput)
  end
end

class Listener
  include DRbUndumped

  def initialize(timeout, socket_path)
    @socket = File.expand_path(socket_path)
    @mutex = Mutex.new
    @active = false
    @timeout = timeout

    @handler = RailsFCGIHandler.new
    @handler.extend DRbUndumped

    message 'opening socket'
    DRb.start_service("drbunix:#{@socket}", self)

    message 'entering process loop'
    @handler.process! self
  end

  def each_cgi(&cgi_block)
    @cgi_block = cgi_block
    message 'entering idle loop'
    loop do
      sleep @timeout rescue nil
      die! unless @active
      @active = false
    end
  end

  def process(env, input)
    message 'received request'
    @mutex.synchronize do
      @active = true

      message 'creating input stream'
      input_stream = StringIO.new(input)
      message 'building CGI instance'
      cgi = RemoteCGI.new(eval(env), input_stream)

      message 'yielding to fcgi handler'
      @cgi_block.call cgi
      message 'yield finished -- sending output'

      cgi.stdoutput.seek(0)
      output = cgi.stdoutput.read

      return output
    end
  end

  def die!
    message 'shutting down'
    DRb.stop_service
    FileUtils.rm_f @socket
    Kernel.exit 0
  end
end

socket_path = ARGV.shift
timeout = (ARGV.shift || 90).to_i

Listener.new(timeout, socket_path)

Version data entries

63 entries across 63 versions & 10 rubygems

Version Path
rails-2.3.18 lib/commands/ncgi/listener
rails_csi-2.3.5.p8 lib/commands/ncgi/listener
rails-2.3.17 lib/commands/ncgi/listener
rails_csi-2.3.5.p7 lib/commands/ncgi/listener
rails_csi-2.3.5.p6 lib/commands/ncgi/listener
rails-2.3.16 lib/commands/ncgi/listener
rails-2.3.15 lib/commands/ncgi/listener
radiant-1.0.0 ruby-debug/ruby/1.8/gems/rails-2.3.14/lib/commands/ncgi/listener
vanity-1.7.1 vendor/ruby/1.9.1/gems/rails-2.3.12/lib/commands/ncgi/listener
rails-2.3.14 lib/commands/ncgi/listener
kajam-1.0.3.rc2 vendor/rails/railties/lib/commands/ncgi/listener
rails-2.3.12 lib/commands/ncgi/listener
radiant-1.0.0.rc2 vendor/rails/railties/lib/commands/ncgi/listener
radiant-1.0.0.rc1 vendor/rails/railties/lib/commands/ncgi/listener
webroar-0.5.0 src/admin_panel/vendor/rails/railties/lib/commands/ncgi/listener
rails-2.3.11 lib/commands/ncgi/listener
radiantcms-couchrest_model-0.2.4 vendor/rails/railties/lib/commands/ncgi/listener
radiantcms-couchrest_model-0.2.2 vendor/rails/railties/lib/commands/ncgi/listener
radiantcms-couchrest_model-0.2.1 vendor/rails/railties/lib/commands/ncgi/listener
radiantcms-couchrest_model-0.2 vendor/rails/railties/lib/commands/ncgi/listener