1 // Copyright 2015 The Weave Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef LIBWEAVE_EXAMPLES_PROVIDER_EVENT_DELETER_H 6 #define LIBWEAVE_EXAMPLES_PROVIDER_EVENT_DELETER_H 7 8 #include <memory> 9 10 #include <evhtp.h> 11 #include <event2/event.h> 12 #include <event2/event_struct.h> 13 #include <openssl/ssl.h> 14 15 namespace weave { 16 namespace examples { 17 18 // Defines overloaded deletion methods for various event_ objects 19 // so we can use one unique_ptr definition for all of them 20 class EventDeleter { 21 public: 22 void operator()(evbuffer* buf) { evbuffer_free(buf); } 23 void operator()(evhtp_t* evhtp) { 24 if (evhtp->ssl_ctx) { 25 // Work around a double-free bug in recent versions of libevhtp. 26 // https://github.com/ellzey/libevhtp/pull/208 27 SSL_CTX_free(evhtp->ssl_ctx); 28 evhtp->ssl_ctx = nullptr; 29 } 30 evhtp_unbind_socket(evhtp); 31 evhtp_free(evhtp); 32 } 33 void operator()(evhtp_connection_t* conn) { evhtp_connection_free(conn); } 34 void operator()(evhtp_request_t* req) { evhtp_request_free(req); } 35 void operator()(event_base* base) { event_base_free(base); } 36 void operator()(event* ev) { 37 event_del(ev); 38 event_free(ev); 39 } 40 }; 41 42 template <typename T> 43 using EventPtr = std::unique_ptr<T, EventDeleter>; 44 45 } // namespace examples 46 } // namespace weave 47 48 #endif // LIBWEAVE_EXAMPLES_PROVIDER_EVENT_DELETER_H 49