David Reiss | 5ddabb8 | 2010-10-06 17:09:37 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 20 | #ifndef _THRIFT_TEVHTTP_SERVER_H_ |
| 21 | #define _THRIFT_TEVHTTP_SERVER_H_ 1 |
| 22 | |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 23 | #include <thrift/stdcxx.h> |
David Reiss | 5ddabb8 | 2010-10-06 17:09:37 +0000 | [diff] [blame] | 24 | |
| 25 | struct event_base; |
| 26 | struct evhttp; |
| 27 | struct evhttp_request; |
| 28 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 29 | namespace apache { |
| 30 | namespace thrift { |
| 31 | namespace async { |
David Reiss | 5ddabb8 | 2010-10-06 17:09:37 +0000 | [diff] [blame] | 32 | |
| 33 | class TAsyncBufferProcessor; |
| 34 | |
| 35 | class TEvhttpServer { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 36 | public: |
David Reiss | 5ddabb8 | 2010-10-06 17:09:37 +0000 | [diff] [blame] | 37 | /** |
| 38 | * Create a TEvhttpServer for use with an external evhttp instance. |
| 39 | * Must be manually installed with evhttp_set_cb, using |
| 40 | * TEvhttpServer::request as the callback and the |
| 41 | * address of the server as the extra arg. |
| 42 | * Do not call "serve" on this server. |
| 43 | */ |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 44 | TEvhttpServer(stdcxx::shared_ptr<TAsyncBufferProcessor> processor); |
David Reiss | 5ddabb8 | 2010-10-06 17:09:37 +0000 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * Create a TEvhttpServer with an embedded event_base and evhttp, |
| 48 | * listening on port and responding on the endpoint "/". |
| 49 | * Call "serve" on this server to serve forever. |
| 50 | */ |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 51 | TEvhttpServer(stdcxx::shared_ptr<TAsyncBufferProcessor> processor, int port); |
David Reiss | 5ddabb8 | 2010-10-06 17:09:37 +0000 | [diff] [blame] | 52 | |
| 53 | ~TEvhttpServer(); |
| 54 | |
| 55 | static void request(struct evhttp_request* req, void* self); |
| 56 | int serve(); |
| 57 | |
| 58 | struct event_base* getEventBase(); |
| 59 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 60 | private: |
David Reiss | 5ddabb8 | 2010-10-06 17:09:37 +0000 | [diff] [blame] | 61 | struct RequestContext; |
| 62 | |
| 63 | void process(struct evhttp_request* req); |
| 64 | void complete(RequestContext* ctx, bool success); |
| 65 | |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 66 | stdcxx::shared_ptr<TAsyncBufferProcessor> processor_; |
David Reiss | 5ddabb8 | 2010-10-06 17:09:37 +0000 | [diff] [blame] | 67 | struct event_base* eb_; |
| 68 | struct evhttp* eh_; |
| 69 | }; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | } // apache::thrift::async |
David Reiss | 5ddabb8 | 2010-10-06 17:09:37 +0000 | [diff] [blame] | 73 | |
| 74 | #endif // #ifndef _THRIFT_TEVHTTP_SERVER_H_ |