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_HTTP_SERVER_H_ 6 #define LIBWEAVE_EXAMPLES_PROVIDER_EVENT_HTTP_SERVER_H_ 7 8 #include <evhtp.h> 9 #include <openssl/ssl.h> 10 11 #include <map> 12 #include <string> 13 #include <vector> 14 15 #include <base/memory/weak_ptr.h> 16 #include <weave/provider/http_server.h> 17 18 #include "examples/provider/event_deleter.h" 19 20 namespace weave { 21 namespace examples { 22 23 class EventTaskRunner; 24 25 // HTTP/HTTPS server implemented with libevhtp. 26 class HttpServerImpl : public provider::HttpServer { 27 public: 28 class RequestImpl; 29 30 explicit HttpServerImpl(EventTaskRunner* task_runner); 31 32 void AddHttpRequestHandler(const std::string& path_prefix, 33 const RequestHandlerCallback& callback) override; 34 void AddHttpsRequestHandler(const std::string& path_prefix, 35 const RequestHandlerCallback& callback) override; 36 uint16_t GetHttpPort() const override; 37 uint16_t GetHttpsPort() const override; 38 base::TimeDelta GetRequestTimeout() const override; 39 std::vector<uint8_t> GetHttpsCertificateFingerprint() const override; 40 41 private: 42 void GenerateX509(X509* x509, EVP_PKEY* pkey); 43 static void ProcessRequestCallback(evhtp_request_t* req, void* arg); 44 void ProcessRequest(evhtp_request_t* req); 45 void ProcessReply(std::shared_ptr<RequestImpl> request, 46 int status_code, 47 const std::string& data, 48 const std::string& mime_type); 49 void NotFound(evhtp_request_t* req); 50 51 std::map<std::string, RequestHandlerCallback> handlers_; 52 53 std::vector<uint8_t> cert_fingerprint_; 54 EventTaskRunner* task_runner_{nullptr}; 55 EventPtr<evhtp_t> httpd_; 56 EventPtr<evhtp_t> httpsd_; 57 58 base::WeakPtrFactory<HttpServerImpl> weak_ptr_factory_{this}; 59 }; 60 61 } // namespace examples 62 } // namespace weave 63 64 #endif // LIBWEAVE_EXAMPLES_PROVIDER_EVENT_HTTP_SERVER_H_ 65