blob: 950e536e6fa91466bb786a63137e6e2beffe0816 [file] [log] [blame]
Vitaly Buka4615e0d2015-10-14 15:35:12 -07001// Copyright 2015 The Weave Authors. All rights reserved.
Vitaly Buka17b0a8a2015-08-31 19:12:35 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Johan Euphrosine3523fdd2015-10-14 20:46:05 -07005#ifndef LIBWEAVE_EXAMPLES_PROVIDER_EVENT_HTTP_SERVER_H_
6#define LIBWEAVE_EXAMPLES_PROVIDER_EVENT_HTTP_SERVER_H_
Vitaly Buka17b0a8a2015-08-31 19:12:35 -07007
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 Buka1e363672015-09-25 14:01:16 -070017#include <weave/provider/http_server.h>
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070018
19namespace weave {
20namespace examples {
21
Alex Vakulenko57fbee32015-09-21 11:04:46 -070022class EventTaskRunner;
23
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070024// HTTP/HTTPS server implemented with libevent.
Vitaly Buka1e363672015-09-25 14:01:16 -070025class HttpServerImpl : public provider::HttpServer {
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070026 public:
27 class RequestImpl;
28
Alex Vakulenko57fbee32015-09-21 11:04:46 -070029 explicit HttpServerImpl(EventTaskRunner* task_runner);
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070030
Vitaly Buka1a39c812015-10-08 21:20:58 -070031 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 Buka17b0a8a2015-08-31 19:12:35 -070035 uint16_t GetHttpPort() const override;
36 uint16_t GetHttpsPort() const override;
Alex Vakulenkoefee3a22015-11-17 15:08:38 -080037 base::TimeDelta GetRequestTimeout() const override;
Vitaly Buka138aec42015-10-08 10:17:48 -070038 std::vector<uint8_t> GetHttpsCertificateFingerprint() const override;
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070039
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 Buka01893cc2015-10-08 19:58:00 -070050 std::map<std::string, RequestHandlerCallback> handlers_;
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070051
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 Vakulenko57fbee32015-09-21 11:04:46 -070063 EventTaskRunner* task_runner_{nullptr};
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070064 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 Vakulenko57fbee32015-09-21 11:04:46 -070067
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070068 base::WeakPtrFactory<HttpServerImpl> weak_ptr_factory_{this};
69};
70
71} // namespace examples
72} // namespace weave
73
Johan Euphrosine3523fdd2015-10-14 20:46:05 -070074#endif // LIBWEAVE_EXAMPLES_PROVIDER_EVENT_HTTP_SERVER_H_