Vitaly Buka | 4615e0d | 2015-10-14 15:35:12 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Weave Authors. All rights reserved. |
Vitaly Buka | 17b0a8a | 2015-08-31 19:12:35 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Johan Euphrosine | 3523fdd | 2015-10-14 20:46:05 -0700 | [diff] [blame] | 5 | #ifndef LIBWEAVE_EXAMPLES_PROVIDER_EVENT_HTTP_SERVER_H_ |
| 6 | #define LIBWEAVE_EXAMPLES_PROVIDER_EVENT_HTTP_SERVER_H_ |
Vitaly Buka | 17b0a8a | 2015-08-31 19:12:35 -0700 | [diff] [blame] | 7 | |
| 8 | #include <event2/http.h> |
| 9 | #include <evhttp.h> |
| 10 | #include <openssl/ssl.h> |
| 11 | |
| 12 | #include <map> |
| 13 | #include <string> |
| 14 | #include <vector> |
| 15 | |
| 16 | #include <base/memory/weak_ptr.h> |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 17 | #include <weave/provider/http_server.h> |
Vitaly Buka | 17b0a8a | 2015-08-31 19:12:35 -0700 | [diff] [blame] | 18 | |
| 19 | namespace weave { |
| 20 | namespace examples { |
| 21 | |
Alex Vakulenko | 57fbee3 | 2015-09-21 11:04:46 -0700 | [diff] [blame] | 22 | class EventTaskRunner; |
| 23 | |
Vitaly Buka | 17b0a8a | 2015-08-31 19:12:35 -0700 | [diff] [blame] | 24 | // HTTP/HTTPS server implemented with libevent. |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 25 | class HttpServerImpl : public provider::HttpServer { |
Vitaly Buka | 17b0a8a | 2015-08-31 19:12:35 -0700 | [diff] [blame] | 26 | public: |
| 27 | class RequestImpl; |
| 28 | |
Alex Vakulenko | 57fbee3 | 2015-09-21 11:04:46 -0700 | [diff] [blame] | 29 | explicit HttpServerImpl(EventTaskRunner* task_runner); |
Vitaly Buka | 17b0a8a | 2015-08-31 19:12:35 -0700 | [diff] [blame] | 30 | |
Vitaly Buka | 1a39c81 | 2015-10-08 21:20:58 -0700 | [diff] [blame] | 31 | void AddHttpRequestHandler(const std::string& path_prefix, |
| 32 | const RequestHandlerCallback& callback) override; |
| 33 | void AddHttpsRequestHandler(const std::string& path_prefix, |
| 34 | const RequestHandlerCallback& callback) override; |
Vitaly Buka | 17b0a8a | 2015-08-31 19:12:35 -0700 | [diff] [blame] | 35 | uint16_t GetHttpPort() const override; |
| 36 | uint16_t GetHttpsPort() const override; |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 37 | base::TimeDelta GetRequestTimeout() const override; |
Vitaly Buka | 138aec4 | 2015-10-08 10:17:48 -0700 | [diff] [blame] | 38 | std::vector<uint8_t> GetHttpsCertificateFingerprint() const override; |
Vitaly Buka | 17b0a8a | 2015-08-31 19:12:35 -0700 | [diff] [blame] | 39 | |
| 40 | private: |
| 41 | void GenerateX509(); |
| 42 | static void ProcessRequestCallback(evhttp_request* req, void* arg); |
| 43 | void ProcessRequest(evhttp_request* req); |
| 44 | void ProcessReply(std::shared_ptr<RequestImpl> request, |
| 45 | int status_code, |
| 46 | const std::string& data, |
| 47 | const std::string& mime_type); |
| 48 | void NotFound(evhttp_request* req); |
| 49 | |
Vitaly Buka | 01893cc | 2015-10-08 19:58:00 -0700 | [diff] [blame] | 50 | std::map<std::string, RequestHandlerCallback> handlers_; |
Vitaly Buka | 17b0a8a | 2015-08-31 19:12:35 -0700 | [diff] [blame] | 51 | |
| 52 | std::unique_ptr<EC_KEY, decltype(&EC_KEY_free)> ec_key_{nullptr, |
| 53 | &EC_KEY_free}; |
| 54 | |
| 55 | std::unique_ptr<EVP_PKEY, decltype(&EVP_PKEY_free)> pkey_{nullptr, |
| 56 | &EVP_PKEY_free}; |
| 57 | |
| 58 | std::unique_ptr<X509, decltype(&X509_free)> x509_{nullptr, &X509_free}; |
| 59 | |
| 60 | std::unique_ptr<SSL_CTX, decltype(&SSL_CTX_free)> ctx_{nullptr, |
| 61 | &SSL_CTX_free}; |
| 62 | std::vector<uint8_t> cert_fingerprint_; |
Alex Vakulenko | 57fbee3 | 2015-09-21 11:04:46 -0700 | [diff] [blame] | 63 | EventTaskRunner* task_runner_{nullptr}; |
Vitaly Buka | 17b0a8a | 2015-08-31 19:12:35 -0700 | [diff] [blame] | 64 | std::unique_ptr<evhttp, decltype(&evhttp_free)> httpd_{nullptr, &evhttp_free}; |
| 65 | std::unique_ptr<evhttp, decltype(&evhttp_free)> httpsd_{nullptr, |
| 66 | &evhttp_free}; |
Alex Vakulenko | 57fbee3 | 2015-09-21 11:04:46 -0700 | [diff] [blame] | 67 | |
Vitaly Buka | 17b0a8a | 2015-08-31 19:12:35 -0700 | [diff] [blame] | 68 | base::WeakPtrFactory<HttpServerImpl> weak_ptr_factory_{this}; |
| 69 | }; |
| 70 | |
| 71 | } // namespace examples |
| 72 | } // namespace weave |
| 73 | |
Johan Euphrosine | 3523fdd | 2015-10-14 20:46:05 -0700 | [diff] [blame] | 74 | #endif // LIBWEAVE_EXAMPLES_PROVIDER_EVENT_HTTP_SERVER_H_ |