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 | |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 20 | from six.moves import BaseHTTPServer |
David Reiss | f78ec2b | 2009-01-31 21:59:32 +0000 | [diff] [blame] | 21 | |
| 22 | from thrift.server import TServer |
| 23 | from thrift.transport import TTransport |
| 24 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 25 | |
David Reiss | a9ca25a | 2010-09-02 15:36:03 +0000 | [diff] [blame] | 26 | class ResponseException(Exception): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 27 | """Allows handlers to override the HTTP response |
David Reiss | a9ca25a | 2010-09-02 15:36:03 +0000 | [diff] [blame] | 28 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 29 | Normally, THttpServer always sends a 200 response. If a handler wants |
| 30 | to override this behavior (e.g., to simulate a misconfigured or |
| 31 | overloaded web server during testing), it can raise a ResponseException. |
| 32 | The function passed to the constructor will be called with the |
| 33 | RequestHandler as its only argument. |
| 34 | """ |
| 35 | def __init__(self, handler): |
| 36 | self.handler = handler |
David Reiss | a9ca25a | 2010-09-02 15:36:03 +0000 | [diff] [blame] | 37 | |
| 38 | |
David Reiss | f78ec2b | 2009-01-31 21:59:32 +0000 | [diff] [blame] | 39 | class THttpServer(TServer.TServer): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 40 | """A simple HTTP-based Thrift server |
David Reiss | f78ec2b | 2009-01-31 21:59:32 +0000 | [diff] [blame] | 41 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 42 | This class is not very performant, but it is useful (for example) for |
| 43 | acting as a mock version of an Apache-based PHP Thrift endpoint. |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 44 | """ |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 45 | def __init__(self, |
| 46 | processor, |
| 47 | server_address, |
| 48 | inputProtocolFactory, |
| 49 | outputProtocolFactory=None, |
| 50 | server_class=BaseHTTPServer.HTTPServer): |
| 51 | """Set up protocol factories and HTTP server. |
David Reiss | f78ec2b | 2009-01-31 21:59:32 +0000 | [diff] [blame] | 52 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 53 | See BaseHTTPServer for server_address. |
| 54 | See TServer for protocol factories. |
| 55 | """ |
| 56 | if outputProtocolFactory is None: |
| 57 | outputProtocolFactory = inputProtocolFactory |
David Reiss | f78ec2b | 2009-01-31 21:59:32 +0000 | [diff] [blame] | 58 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 59 | TServer.TServer.__init__(self, processor, None, None, None, |
| 60 | inputProtocolFactory, outputProtocolFactory) |
David Reiss | f78ec2b | 2009-01-31 21:59:32 +0000 | [diff] [blame] | 61 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 62 | thttpserver = self |
David Reiss | f78ec2b | 2009-01-31 21:59:32 +0000 | [diff] [blame] | 63 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 64 | class RequestHander(BaseHTTPServer.BaseHTTPRequestHandler): |
| 65 | def do_POST(self): |
| 66 | # Don't care about the request path. |
| 67 | itrans = TTransport.TFileObjectTransport(self.rfile) |
| 68 | otrans = TTransport.TFileObjectTransport(self.wfile) |
| 69 | itrans = TTransport.TBufferedTransport( |
| 70 | itrans, int(self.headers['Content-Length'])) |
| 71 | otrans = TTransport.TMemoryBuffer() |
| 72 | iprot = thttpserver.inputProtocolFactory.getProtocol(itrans) |
| 73 | oprot = thttpserver.outputProtocolFactory.getProtocol(otrans) |
| 74 | try: |
| 75 | thttpserver.processor.process(iprot, oprot) |
| 76 | except ResponseException as exn: |
| 77 | exn.handler(self) |
| 78 | else: |
| 79 | self.send_response(200) |
| 80 | self.send_header("content-type", "application/x-thrift") |
| 81 | self.end_headers() |
| 82 | self.wfile.write(otrans.getvalue()) |
David Reiss | f78ec2b | 2009-01-31 21:59:32 +0000 | [diff] [blame] | 83 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 84 | self.httpd = server_class(server_address, RequestHander) |
| 85 | |
| 86 | def serve(self): |
| 87 | self.httpd.serve_forever() |