| 30 |
ashish |
1 |
#
|
|
|
2 |
# Licensed to the Apache Software Foundation (ASF) under one
|
|
|
3 |
# or more contributor license agreements. See the NOTICE file
|
|
|
4 |
# distributed with this work for additional information
|
|
|
5 |
# regarding copyright ownership. The ASF licenses this file
|
|
|
6 |
# to you under the Apache License, Version 2.0 (the
|
|
|
7 |
# "License"); you may not use this file except in compliance
|
|
|
8 |
# with the License. You may obtain a copy of the License at
|
|
|
9 |
#
|
|
|
10 |
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
11 |
#
|
|
|
12 |
# Unless required by applicable law or agreed to in writing,
|
|
|
13 |
# software distributed under the License is distributed on an
|
|
|
14 |
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
15 |
# KIND, either express or implied. See the License for the
|
|
|
16 |
# specific language governing permissions and limitations
|
|
|
17 |
# under the License.
|
|
|
18 |
#
|
|
|
19 |
|
|
|
20 |
require 'mongrel'
|
|
|
21 |
|
|
|
22 |
## Sticks a service on a URL, using mongrel to do the HTTP work
|
|
|
23 |
module Thrift
|
|
|
24 |
class MongrelHTTPServer < BaseServer
|
|
|
25 |
class Handler < Mongrel::HttpHandler
|
|
|
26 |
def initialize(processor, protocol_factory)
|
|
|
27 |
@processor = processor
|
|
|
28 |
@protocol_factory = protocol_factory
|
|
|
29 |
end
|
|
|
30 |
|
|
|
31 |
def process(request, response)
|
|
|
32 |
if request.params["REQUEST_METHOD"] == "POST"
|
|
|
33 |
response.start(200) do |head, out|
|
|
|
34 |
head["Content-Type"] = "application/x-thrift"
|
|
|
35 |
transport = IOStreamTransport.new request.body, out
|
|
|
36 |
protocol = @protocol_factory.get_protocol transport
|
|
|
37 |
@processor.process protocol, protocol
|
|
|
38 |
end
|
|
|
39 |
else
|
|
|
40 |
response.start(404) { }
|
|
|
41 |
end
|
|
|
42 |
end
|
|
|
43 |
end
|
|
|
44 |
|
|
|
45 |
def initialize(processor, opts={})
|
|
|
46 |
port = opts[:port] || 80
|
|
|
47 |
ip = opts[:ip] || "0.0.0.0"
|
|
|
48 |
path = opts[:path] || ""
|
|
|
49 |
protocol_factory = opts[:protocol_factory] || BinaryProtocolFactory.new
|
|
|
50 |
@server = Mongrel::HttpServer.new ip, port
|
|
|
51 |
@server.register "/#{path}", Handler.new(processor, protocol_factory)
|
|
|
52 |
end
|
|
|
53 |
|
|
|
54 |
def serve
|
|
|
55 |
@server.run.join
|
|
|
56 |
end
|
|
|
57 |
end
|
|
|
58 |
end
|