David Reiss | f78ec2b | 2009-01-31 21:59:32 +0000 | [diff] [blame] | 1 | # |
David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 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 | # |
David Reiss | f78ec2b | 2009-01-31 21:59:32 +0000 | [diff] [blame] | 19 | |
James E. King III | 6f8c99e | 2018-03-24 16:32:02 -0400 | [diff] [blame] | 20 | import ssl |
| 21 | |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 22 | from six.moves import BaseHTTPServer |
David Reiss | f78ec2b | 2009-01-31 21:59:32 +0000 | [diff] [blame] | 23 | |
| 24 | from thrift.server import TServer |
| 25 | from thrift.transport import TTransport |
| 26 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 27 | |
David Reiss | a9ca25a | 2010-09-02 15:36:03 +0000 | [diff] [blame] | 28 | class ResponseException(Exception): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 29 | """Allows handlers to override the HTTP response |
David Reiss | a9ca25a | 2010-09-02 15:36:03 +0000 | [diff] [blame] | 30 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 31 | 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 Reiss | a9ca25a | 2010-09-02 15:36:03 +0000 | [diff] [blame] | 39 | |
| 40 | |
David Reiss | f78ec2b | 2009-01-31 21:59:32 +0000 | [diff] [blame] | 41 | class THttpServer(TServer.TServer): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 42 | """A simple HTTP-based Thrift server |
David Reiss | f78ec2b | 2009-01-31 21:59:32 +0000 | [diff] [blame] | 43 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 44 | 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 Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 46 | """ |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 47 | def __init__(self, |
| 48 | processor, |
| 49 | server_address, |
| 50 | inputProtocolFactory, |
| 51 | outputProtocolFactory=None, |
James E. King III | 6f8c99e | 2018-03-24 16:32:02 -0400 | [diff] [blame] | 52 | server_class=BaseHTTPServer.HTTPServer, |
| 53 | **kwargs): |
| 54 | """Set up protocol factories and HTTP (or HTTPS) server. |
David Reiss | f78ec2b | 2009-01-31 21:59:32 +0000 | [diff] [blame] | 55 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 56 | See BaseHTTPServer for server_address. |
| 57 | See TServer for protocol factories. |
James E. King III | 6f8c99e | 2018-03-24 16:32:02 -0400 | [diff] [blame] | 58 | |
| 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 Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 63 | """ |
| 64 | if outputProtocolFactory is None: |
| 65 | outputProtocolFactory = inputProtocolFactory |
David Reiss | f78ec2b | 2009-01-31 21:59:32 +0000 | [diff] [blame] | 66 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 67 | TServer.TServer.__init__(self, processor, None, None, None, |
| 68 | inputProtocolFactory, outputProtocolFactory) |
David Reiss | f78ec2b | 2009-01-31 21:59:32 +0000 | [diff] [blame] | 69 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 70 | thttpserver = self |
David Reiss | f78ec2b | 2009-01-31 21:59:32 +0000 | [diff] [blame] | 71 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 72 | 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 Reiss | f78ec2b | 2009-01-31 21:59:32 +0000 | [diff] [blame] | 91 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 92 | self.httpd = server_class(server_address, RequestHander) |
| 93 | |
James E. King III | 6f8c99e | 2018-03-24 16:32:02 -0400 | [diff] [blame] | 94 | 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 Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 101 | def serve(self): |
| 102 | self.httpd.serve_forever() |
James E. King III | 6f8c99e | 2018-03-24 16:32:02 -0400 | [diff] [blame] | 103 | |
| 104 | def shutdown(self): |
| 105 | self.httpd.socket.close() |
| 106 | # self.httpd.shutdown() # hangs forever, python doesn't handle POLLNVAL properly! |