require 'thrift/protocol'
require 'thrift/protocol/binaryprotocol'
require 'thrift/transport'

require 'mongrel'

## Sticks a service on a URL, using mongrel to do the HTTP work
module Thrift
  class SimpleMongrelHTTPServer
    class Handler < Mongrel::HttpHandler
      def initialize(processor, protocol_factory)
        @processor = processor
        @protocol_factory = protocol_factory
      end

      def process(request, response)
        unless request.params["REQUEST_METHOD"] == "POST"
          response.start(404) { } # better way?
          return
        end
        response.start(200) do |head, out|
          head["Content-Type"] = "application/x-thrift"
          transport = TIOStreamTransport.new request.body, out
          protocol = @protocol_factory.get_protocol transport
          @processor.process protocol, protocol
        end
      end
    end

    def initialize(processor, opts={})
      port = opts[:port] || 80
      ip = opts[:ip] || "0.0.0.0"
      path = opts[:path] || ""
      protocol_factory = opts[:protocol_factory] || TBinaryProtocolFactory.new
      @server = Mongrel::HttpServer.new ip, port
      @server.register "/#{path}", Handler.new(processor, protocol_factory)
    end

    def serve
      @server.run.join
    end
  end
  deprecate_class! :TSimpleMongrelHTTPServer => SimpleMongrelHTTPServer
end

require 'thrift/protocol'
require 'thrift/protocol/binaryprotocol'
require 'thrift/transport'

require 'mongrel'

## Sticks a service on a URL, using mongrel to do the HTTP work
module Thrift
  class SimpleMongrelHTTPServer
    class Handler < Mongrel::HttpHandler
      def initialize(processor, protocol_factory)
        @processor = processor
        @protocol_factory = protocol_factory
      end

      def process(request, response)
        unless request.params["REQUEST_METHOD"] == "POST"
          response.start(404) { } # better way?
          return
        end
        response.start(200) do |head, out|
          head["Content-Type"] = "application/x-thrift"
          transport = IOStreamTransport.new request.body, out
          protocol = @protocol_factory.get_protocol transport
          @processor.process protocol, protocol
        end
      end
    end

    def initialize(processor, opts={})
      port = opts[:port] || 80
      ip = opts[:ip] || "0.0.0.0"
      path = opts[:path] || ""
      protocol_factory = opts[:protocol_factory] || BinaryProtocolFactory.new
      @server = Mongrel::HttpServer.new ip, port
      @server.register "/#{path}", Handler.new(processor, protocol_factory)
    end

    def serve
      @server.run.join
    end
  end
  deprecate_class! :TSimpleMongrelHTTPServer => SimpleMongrelHTTPServer
end

Stop using deprecated classes in SimpleMongrelHTTPServer
Stop using deprecated classes in SimpleMongrelHTTPServer


git-svn-id: 8d8e29b1fb681c884914062b88f3633e3a187774@668962 13f79535-47bb-0310-9956-ffa450edef68
