blob: 85cf4005eab715682c7dc044d26e9e103cb40d3a [file] [log] [blame]
David Reissf78ec2b2009-01-31 21:59:32 +00001#
David Reissea2cba82009-03-30 21:35:00 +00002# 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#
David Reissf78ec2b2009-01-31 21:59:32 +000019
James E. King III6f8c99e2018-03-24 16:32:02 -040020import ssl
21
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090022from six.moves import BaseHTTPServer
David Reissf78ec2b2009-01-31 21:59:32 +000023
24from thrift.server import TServer
25from thrift.transport import TTransport
26
Bryan Duxbury69720412012-01-03 17:32:30 +000027
David Reissa9ca25a2010-09-02 15:36:03 +000028class ResponseException(Exception):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090029 """Allows handlers to override the HTTP response
David Reissa9ca25a2010-09-02 15:36:03 +000030
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090031 Normally, THttpServer always sends a 200 response. If a handler wants
32 to override this behavior (e.g., to simulate a misconfigured or
33 overloaded web server during testing), it can raise a ResponseException.
34 The function passed to the constructor will be called with the
35 RequestHandler as its only argument.
36 """
37 def __init__(self, handler):
38 self.handler = handler
David Reissa9ca25a2010-09-02 15:36:03 +000039
40
David Reissf78ec2b2009-01-31 21:59:32 +000041class THttpServer(TServer.TServer):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090042 """A simple HTTP-based Thrift server
David Reissf78ec2b2009-01-31 21:59:32 +000043
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090044 This class is not very performant, but it is useful (for example) for
45 acting as a mock version of an Apache-based PHP Thrift endpoint.
Bryan Duxbury69720412012-01-03 17:32:30 +000046 """
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090047 def __init__(self,
48 processor,
49 server_address,
50 inputProtocolFactory,
51 outputProtocolFactory=None,
James E. King III6f8c99e2018-03-24 16:32:02 -040052 server_class=BaseHTTPServer.HTTPServer,
53 **kwargs):
54 """Set up protocol factories and HTTP (or HTTPS) server.
David Reissf78ec2b2009-01-31 21:59:32 +000055
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090056 See BaseHTTPServer for server_address.
57 See TServer for protocol factories.
James E. King III6f8c99e2018-03-24 16:32:02 -040058
59 To make a secure server, provide the named arguments:
60 * cafile - to validate clients [optional]
61 * cert_file - the server cert
62 * key_file - the server's key
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090063 """
64 if outputProtocolFactory is None:
65 outputProtocolFactory = inputProtocolFactory
David Reissf78ec2b2009-01-31 21:59:32 +000066
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090067 TServer.TServer.__init__(self, processor, None, None, None,
68 inputProtocolFactory, outputProtocolFactory)
David Reissf78ec2b2009-01-31 21:59:32 +000069
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090070 thttpserver = self
David Reissf78ec2b2009-01-31 21:59:32 +000071
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090072 class RequestHander(BaseHTTPServer.BaseHTTPRequestHandler):
73 def do_POST(self):
74 # Don't care about the request path.
75 itrans = TTransport.TFileObjectTransport(self.rfile)
76 otrans = TTransport.TFileObjectTransport(self.wfile)
77 itrans = TTransport.TBufferedTransport(
78 itrans, int(self.headers['Content-Length']))
79 otrans = TTransport.TMemoryBuffer()
80 iprot = thttpserver.inputProtocolFactory.getProtocol(itrans)
81 oprot = thttpserver.outputProtocolFactory.getProtocol(otrans)
82 try:
83 thttpserver.processor.process(iprot, oprot)
84 except ResponseException as exn:
85 exn.handler(self)
86 else:
87 self.send_response(200)
88 self.send_header("content-type", "application/x-thrift")
89 self.end_headers()
90 self.wfile.write(otrans.getvalue())
David Reissf78ec2b2009-01-31 21:59:32 +000091
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090092 self.httpd = server_class(server_address, RequestHander)
93
James E. King III6f8c99e2018-03-24 16:32:02 -040094 if (kwargs.get('cafile') or kwargs.get('cert_file') or kwargs.get('key_file')):
95 context = ssl.create_default_context(cafile=kwargs.get('cafile'))
96 context.check_hostname = False
97 context.load_cert_chain(kwargs.get('cert_file'), kwargs.get('key_file'))
98 context.verify_mode = ssl.CERT_REQUIRED if kwargs.get('cafile') else ssl.CERT_NONE
99 self.httpd.socket = context.wrap_socket(self.httpd.socket, server_side=True)
100
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900101 def serve(self):
102 self.httpd.serve_forever()
James E. King III6f8c99e2018-03-24 16:32:02 -0400103
104 def shutdown(self):
105 self.httpd.socket.close()
106 # self.httpd.shutdown() # hangs forever, python doesn't handle POLLNVAL properly!