Sha256: c34039fad3598f69f8b6b49d0e0661b2bffc3c46e01405b49006c4f16fddc062

Contents?: true

Size: 1.54 KB

Versions: 12

Compression:

Stored size: 1.54 KB

Contents

#
# bitclust/interface.rb
#
# Copyright (c) 2006-2007 Minero Aoki
#
# This program is free software.
# You can distribute/modify this program under the Ruby License.
#

require 'webrick/cgi'
require 'webrick/httpservlet/abstract'
begin
  require 'fcgi'
rescue LoadError
end

module BitClust

  class Interface

    def initialize(webrick_conf = {})
      @webrick_conf = webrick_conf
      @handler = ($bitclust_context_cache ||= yield)
    end

    # for WEBrick servlet
    def get_instance(server)
      WEBrickServlet.new(server, @handler)
    end

    def main
      if fastcgi?
        FCGI.new(@webrick_conf).main(@handler)
      else
        # CGI, mod_ruby
        CGI.new(@webrick_conf).main(@handler)
      end
    end

    # for rack
    def call(env)
      @handler.handle(Rack::Request.new(env)).rack_finish
    end

    private

    def fastcgi?
      defined?(::FCGI) and ::FCGI.fastcgi?
    end

    def mod_ruby?
      false   # FIXME
    end

    class CGI < ::WEBrick::CGI
      def main(handler)
        @handler = handler
        start
      end

      def do_GET(wreq, wres)
        @handler.handle(wreq).update wres
      end

      alias do_POST do_GET
    end

    class FCGI < CGI
      def main(handler)
        @handler = handler
        ::FCGI.each_cgi_request do |req|
          start req.env, req.in, req.out
        end
      end
    end

    class WEBrickServlet < ::WEBrick::HTTPServlet::AbstractServlet
      def do_GET(wreq, wres)
        @options.first.handle(wreq).update wres
      end

      alias do_POST do_GET
    end
  
  end

end

Version data entries

12 entries across 8 versions & 1 rubygems

Version Path
bitclust-core-0.7.0 lib/bitclust/interface.rb
bitclust-core-0.6.0 lib/bitclust/interface.rb
bitclust-core-0.5.5 lib/bitclust/interface.rb
bitclust-core-0.5.4 lib/bitclust/interface.rb
bitclust-core-0.5.3 vendor/bundle/ruby/1.9.1/gems/bitclust-core-0.5.3/lib/bitclust/interface.rb
bitclust-core-0.5.3 vendor/bundle/ruby/1.9.1/gems/bitclust-core-0.5.1/lib/bitclust/interface.rb
bitclust-core-0.5.3 vendor/bundle/ruby/1.9.1/gems/bitclust-core-0.5.3/vendor/bundle/ruby/1.9.1/gems/bitclust-core-0.5.1/lib/bitclust/interface.rb
bitclust-core-0.5.3 lib/bitclust/interface.rb
bitclust-core-0.5.2 lib/bitclust/interface.rb
bitclust-core-0.5.2 vendor/bundle/ruby/1.9.1/gems/bitclust-core-0.5.1/lib/bitclust/interface.rb
bitclust-core-0.5.1 lib/bitclust/interface.rb
bitclust-core-0.5.0 lib/bitclust/interface.rb