blob: 21fc3141843cbed959d82165146fb903265f1b2a [file] [log] [blame]
Gavin McDonald0b75e1a2010-10-28 02:12:01 +00001#
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
20import BaseHTTPServer
21
22from thrift.server import TServer
23from thrift.transport import TTransport
24
25class THttpServer(TServer.TServer):
26 """A simple HTTP-based Thrift server
27
28 This class is not very performant, but it is useful (for example) for
29 acting as a mock version of an Apache-based PHP Thrift endpoint."""
30
31 def __init__(self, processor, server_address,
32 inputProtocolFactory, outputProtocolFactory = None):
33 """Set up protocol factories and HTTP server.
34
35 See BaseHTTPServer for server_address.
36 See TServer for protocol factories."""
37
38 if outputProtocolFactory is None:
39 outputProtocolFactory = inputProtocolFactory
40
41 TServer.TServer.__init__(self, processor, None, None, None,
42 inputProtocolFactory, outputProtocolFactory)
43
44 thttpserver = self
45
46 class RequestHander(BaseHTTPServer.BaseHTTPRequestHandler):
47 def do_POST(self):
48 # Don't care about the request path.
49 self.send_response(200)
50 self.send_header("content-type", "application/x-thrift")
51 self.end_headers()
52
53 itrans = TTransport.TFileObjectTransport(self.rfile)
54 otrans = TTransport.TFileObjectTransport(self.wfile)
55 iprot = thttpserver.inputProtocolFactory.getProtocol(itrans)
56 oprot = thttpserver.outputProtocolFactory.getProtocol(otrans)
57 thttpserver.processor.process(iprot, oprot)
58 otrans.flush()
59
60 self.httpd = BaseHTTPServer.HTTPServer(server_address, RequestHander)
61
62 def serve(self):
63 self.httpd.serve_forever()