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 
23 #include <memory>
24 
25 struct event_base;
26 struct evhttp;
27 struct evhttp_request;
28 
29 namespace apache {
30 namespace thrift {
31 namespace async {
32 
33 class TAsyncBufferProcessor;
34 
35 class TEvhttpServer {
36 public:
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    */
44   TEvhttpServer(std::shared_ptr<TAsyncBufferProcessor> processor);
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    */
51   TEvhttpServer(std::shared_ptr<TAsyncBufferProcessor> processor, int port);
52 
53   ~TEvhttpServer();
54 
55   static void request(struct evhttp_request* req, void* self);
56   int serve();
57 
58   struct event_base* getEventBase();
59 
60 private:
61   struct RequestContext;
62 
63   void process(struct evhttp_request* req);
64   void complete(RequestContext* ctx, bool success);
65 
66   std::shared_ptr<TAsyncBufferProcessor> processor_;
67   struct event_base* eb_;
68   struct evhttp* eh_;
69 };
70 }
71 }
72 } // apache::thrift::async
73 
74 #endif // #ifndef _THRIFT_TEVHTTP_SERVER_H_
75