blob: 47e817df7735a31f9c1e6572e235345dcbfd7dbc [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
James E. King III393f6c92019-02-09 10:35:44 -050024from thrift.Thrift import TMessageType
David Reissf78ec2b2009-01-31 21:59:32 +000025from thrift.server import TServer
26from thrift.transport import TTransport
27
Bryan Duxbury69720412012-01-03 17:32:30 +000028
David Reissa9ca25a2010-09-02 15:36:03 +000029class ResponseException(Exception):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090030 """Allows handlers to override the HTTP response
David Reissa9ca25a2010-09-02 15:36:03 +000031
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090032 Normally, THttpServer always sends a 200 response. If a handler wants
33 to override this behavior (e.g., to simulate a misconfigured or
34 overloaded web server during testing), it can raise a ResponseException.
35 The function passed to the constructor will be called with the
James E. King III393f6c92019-02-09 10:35:44 -050036 RequestHandler as its only argument. Note that this is irrelevant
37 for ONEWAY requests, as the HTTP response must be sent before the
38 RPC is processed.
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090039 """
40 def __init__(self, handler):
41 self.handler = handler
David Reissa9ca25a2010-09-02 15:36:03 +000042
43
David Reissf78ec2b2009-01-31 21:59:32 +000044class THttpServer(TServer.TServer):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090045 """A simple HTTP-based Thrift server
David Reissf78ec2b2009-01-31 21:59:32 +000046
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090047 This class is not very performant, but it is useful (for example) for
48 acting as a mock version of an Apache-based PHP Thrift endpoint.
James E. King III393f6c92019-02-09 10:35:44 -050049 Also important to note the HTTP implementation pretty much violates the
50 transport/protocol/processor/server layering, by performing the transport
51 functions here. This means things like oneway handling are oddly exposed.
Bryan Duxbury69720412012-01-03 17:32:30 +000052 """
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090053 def __init__(self,
54 processor,
55 server_address,
56 inputProtocolFactory,
57 outputProtocolFactory=None,
James E. King III6f8c99e2018-03-24 16:32:02 -040058 server_class=BaseHTTPServer.HTTPServer,
59 **kwargs):
60 """Set up protocol factories and HTTP (or HTTPS) server.
David Reissf78ec2b2009-01-31 21:59:32 +000061
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090062 See BaseHTTPServer for server_address.
63 See TServer for protocol factories.
James E. King III6f8c99e2018-03-24 16:32:02 -040064
65 To make a secure server, provide the named arguments:
66 * cafile - to validate clients [optional]
67 * cert_file - the server cert
68 * key_file - the server's key
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090069 """
70 if outputProtocolFactory is None:
71 outputProtocolFactory = inputProtocolFactory
David Reissf78ec2b2009-01-31 21:59:32 +000072
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090073 TServer.TServer.__init__(self, processor, None, None, None,
74 inputProtocolFactory, outputProtocolFactory)
David Reissf78ec2b2009-01-31 21:59:32 +000075
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090076 thttpserver = self
James E. King III393f6c92019-02-09 10:35:44 -050077 self._replied = None
David Reissf78ec2b2009-01-31 21:59:32 +000078
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090079 class RequestHander(BaseHTTPServer.BaseHTTPRequestHandler):
80 def do_POST(self):
81 # Don't care about the request path.
James E. King III393f6c92019-02-09 10:35:44 -050082 thttpserver._replied = False
83 iftrans = TTransport.TFileObjectTransport(self.rfile)
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090084 itrans = TTransport.TBufferedTransport(
James E. King III393f6c92019-02-09 10:35:44 -050085 iftrans, int(self.headers['Content-Length']))
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090086 otrans = TTransport.TMemoryBuffer()
87 iprot = thttpserver.inputProtocolFactory.getProtocol(itrans)
88 oprot = thttpserver.outputProtocolFactory.getProtocol(otrans)
89 try:
James E. King III393f6c92019-02-09 10:35:44 -050090 thttpserver.processor.on_message_begin(self.on_begin)
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090091 thttpserver.processor.process(iprot, oprot)
92 except ResponseException as exn:
93 exn.handler(self)
94 else:
James E. King III393f6c92019-02-09 10:35:44 -050095 if not thttpserver._replied:
96 # If the request was ONEWAY we would have replied already
97 data = otrans.getvalue()
98 self.send_response(200)
99 self.send_header("Content-Length", len(data))
100 self.send_header("Content-Type", "application/x-thrift")
101 self.end_headers()
102 self.wfile.write(data)
103
104 def on_begin(self, name, type, seqid):
105 """
106 Inspect the message header.
107
108 This allows us to post an immediate transport response
109 if the request is a ONEWAY message type.
110 """
111 if type == TMessageType.ONEWAY:
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900112 self.send_response(200)
James E. King III393f6c92019-02-09 10:35:44 -0500113 self.send_header("Content-Type", "application/x-thrift")
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900114 self.end_headers()
James E. King III393f6c92019-02-09 10:35:44 -0500115 thttpserver._replied = True
David Reissf78ec2b2009-01-31 21:59:32 +0000116
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900117 self.httpd = server_class(server_address, RequestHander)
118
James E. King III6f8c99e2018-03-24 16:32:02 -0400119 if (kwargs.get('cafile') or kwargs.get('cert_file') or kwargs.get('key_file')):
120 context = ssl.create_default_context(cafile=kwargs.get('cafile'))
121 context.check_hostname = False
122 context.load_cert_chain(kwargs.get('cert_file'), kwargs.get('key_file'))
123 context.verify_mode = ssl.CERT_REQUIRED if kwargs.get('cafile') else ssl.CERT_NONE
124 self.httpd.socket = context.wrap_socket(self.httpd.socket, server_side=True)
125
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900126 def serve(self):
127 self.httpd.serve_forever()
James E. King III6f8c99e2018-03-24 16:32:02 -0400128
129 def shutdown(self):
130 self.httpd.socket.close()
131 # self.httpd.shutdown() # hangs forever, python doesn't handle POLLNVAL properly!