blob: 1bf58f6ca51eb875970fe1750791315b2a0b985d [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#include "examples/provider/event_http_server.h"
Vitaly Buka17b0a8a2015-08-31 19:12:35 -07006
7#include <vector>
8
9#include <base/bind.h>
10#include <base/time/time.h>
11#include <event2/bufferevent_ssl.h>
Jacob Marble33135582016-01-28 10:29:23 -080012#include <evhtp.h>
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070013#include <openssl/err.h>
14
Johan Euphrosine3523fdd2015-10-14 20:46:05 -070015#include "examples/provider/event_task_runner.h"
Alex Vakulenko57fbee32015-09-21 11:04:46 -070016
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070017namespace weave {
18namespace examples {
19
20namespace {
21
22std::string GetSslError() {
23 char error[1000] = {};
24 ERR_error_string_n(ERR_get_error(), error, sizeof(error));
25 return error;
26}
27
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070028} // namespace
29
30class HttpServerImpl::RequestImpl : public Request {
31 public:
Jacob Marble33135582016-01-28 10:29:23 -080032 RequestImpl(EventPtr<evhtp_request_t> req) : req_(std::move(req)) {
33 evbuf_t* input_buffer =
34 bufferevent_get_input(evhtp_request_get_bev(req_.get()));
35 data_.resize(evbuffer_get_length(input_buffer));
36 evbuffer_remove(input_buffer, &data_[0], data_.size());
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070037 }
38
39 ~RequestImpl() {}
40
Jacob Marble33135582016-01-28 10:29:23 -080041 std::string GetPath() const override { return req_->uri->path->path; }
42
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070043 std::string GetFirstHeader(const std::string& name) const override {
Jacob Marble33135582016-01-28 10:29:23 -080044 const char* header = evhtp_header_find(req_->headers_in, name.c_str());
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070045 if (!header)
46 return {};
47 return header;
48 }
Jacob Marble33135582016-01-28 10:29:23 -080049
Vitaly Buka533dd422015-10-09 18:57:51 -070050 std::string GetData() { return data_; }
Vitaly Buka5fe76092015-10-08 13:37:53 -070051
52 void SendReply(int status_code,
53 const std::string& data,
54 const std::string& mime_type) override {
Jacob Marble33135582016-01-28 10:29:23 -080055 EventPtr<evbuffer> buf{evbuffer_new()};
Vitaly Buka5fe76092015-10-08 13:37:53 -070056 evbuffer_add(buf.get(), data.data(), data.size());
Jacob Marble33135582016-01-28 10:29:23 -080057 evhtp_header_key_add(req_->headers_out, "Content-Type", 0);
58 evhtp_header_val_add(req_->headers_out, mime_type.c_str(), 1);
59 evhtp_send_reply_start(req_.get(), status_code);
60 evhtp_send_reply_body(req_.get(), buf.get());
61 evhtp_send_reply_end(req_.get());
Alex Vakulenko57fbee32015-09-21 11:04:46 -070062 }
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070063
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070064 private:
Jacob Marble33135582016-01-28 10:29:23 -080065 EventPtr<evhtp_request_t> req_;
Vitaly Buka533dd422015-10-09 18:57:51 -070066 std::string data_;
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070067};
68
Alex Vakulenko57fbee32015-09-21 11:04:46 -070069HttpServerImpl::HttpServerImpl(EventTaskRunner* task_runner)
70 : task_runner_{task_runner} {
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070071 SSL_load_error_strings();
72 SSL_library_init();
73
Jacob Marble33135582016-01-28 10:29:23 -080074 std::unique_ptr<SSL_CTX, decltype(&SSL_CTX_free)> ctx{
75 SSL_CTX_new(TLSv1_2_server_method()), &SSL_CTX_free};
76 CHECK(ctx);
77 SSL_CTX_set_options(ctx.get(), SSL_OP_SINGLE_DH_USE | SSL_OP_SINGLE_ECDH_USE |
78 SSL_OP_NO_SSLv2);
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070079
Jacob Marble33135582016-01-28 10:29:23 -080080 std::unique_ptr<EC_KEY, decltype(&EC_KEY_free)> ec_key{
81 EC_KEY_new_by_curve_name(NID_X9_62_prime256v1), &EC_KEY_free};
82 CHECK(ec_key) << GetSslError();
83 CHECK_EQ(1, SSL_CTX_set_tmp_ecdh(ctx.get(), ec_key.get())) << GetSslError();
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070084
Jacob Marble33135582016-01-28 10:29:23 -080085 std::unique_ptr<X509, decltype(&X509_free)> x509{X509_new(), &X509_free};
86 CHECK(x509);
87 std::unique_ptr<EVP_PKEY, decltype(&EVP_PKEY_free)> pkey{EVP_PKEY_new(),
88 &EVP_PKEY_free};
89 CHECK(pkey);
90 GenerateX509(x509.get(), pkey.get());
91 CHECK_EQ(1, SSL_CTX_use_PrivateKey(ctx.get(), pkey.get())) << GetSslError();
92 CHECK_EQ(1, SSL_CTX_use_certificate(ctx.get(), x509.get())) << GetSslError();
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070093
Jacob Marble33135582016-01-28 10:29:23 -080094 CHECK_EQ(1, SSL_CTX_check_private_key(ctx.get())) << GetSslError();
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070095
Jacob Marble33135582016-01-28 10:29:23 -080096 httpd_.reset(evhtp_new(task_runner_->GetEventBase(), nullptr));
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070097 CHECK(httpd_);
Jacob Marble33135582016-01-28 10:29:23 -080098 httpsd_.reset(evhtp_new(task_runner_->GetEventBase(), nullptr));
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070099 CHECK(httpsd_);
100
Jacob Marble33135582016-01-28 10:29:23 -0800101 httpsd_.get()->ssl_ctx = ctx.release();
Vitaly Buka17b0a8a2015-08-31 19:12:35 -0700102
Jacob Marble33135582016-01-28 10:29:23 -0800103 CHECK_EQ(0, evhtp_bind_socket(httpd_.get(), "0.0.0.0", GetHttpPort(), -1));
104 CHECK_EQ(0, evhtp_bind_socket(httpsd_.get(), "0.0.0.0", GetHttpsPort(), -1));
Vitaly Buka17b0a8a2015-08-31 19:12:35 -0700105}
106
Jacob Marble33135582016-01-28 10:29:23 -0800107void HttpServerImpl::GenerateX509(X509* x509, EVP_PKEY* pkey) {
108 CHECK(x509) << GetSslError();
Vitaly Buka17b0a8a2015-08-31 19:12:35 -0700109
Jacob Marble33135582016-01-28 10:29:23 -0800110 X509_set_version(x509, 2);
Vitaly Buka17b0a8a2015-08-31 19:12:35 -0700111
Jacob Marble33135582016-01-28 10:29:23 -0800112 X509_gmtime_adj(X509_get_notBefore(x509), 0);
113 X509_gmtime_adj(X509_get_notAfter(x509),
Vitaly Buka17b0a8a2015-08-31 19:12:35 -0700114 base::TimeDelta::FromDays(365).InSeconds());
115
Jacob Marble33135582016-01-28 10:29:23 -0800116 CHECK(pkey) << GetSslError();
Vitaly Buka17b0a8a2015-08-31 19:12:35 -0700117 std::unique_ptr<BIGNUM, decltype(&BN_free)> big_num(BN_new(), &BN_free);
118 CHECK(BN_set_word(big_num.get(), 65537)) << GetSslError();
119 auto rsa = RSA_new();
120 RSA_generate_key_ex(rsa, 2048, big_num.get(), nullptr);
Jacob Marble33135582016-01-28 10:29:23 -0800121 CHECK(EVP_PKEY_assign_RSA(pkey, rsa)) << GetSslError();
Vitaly Buka17b0a8a2015-08-31 19:12:35 -0700122
Jacob Marble33135582016-01-28 10:29:23 -0800123 X509_set_pubkey(x509, pkey);
Vitaly Buka17b0a8a2015-08-31 19:12:35 -0700124
Jacob Marble33135582016-01-28 10:29:23 -0800125 CHECK(X509_sign(x509, pkey, EVP_sha256())) << GetSslError();
Vitaly Buka17b0a8a2015-08-31 19:12:35 -0700126
127 cert_fingerprint_.resize(EVP_MD_size(EVP_sha256()));
128 uint32_t len = 0;
Jacob Marble33135582016-01-28 10:29:23 -0800129 CHECK(X509_digest(x509, EVP_sha256(), cert_fingerprint_.data(), &len));
Vitaly Buka17b0a8a2015-08-31 19:12:35 -0700130 CHECK_EQ(len, cert_fingerprint_.size());
131}
132
Jacob Marble33135582016-01-28 10:29:23 -0800133void HttpServerImpl::NotFound(evhtp_request_t* req) {
134 EventPtr<evbuffer> buf{evbuffer_new()};
135 evbuffer_add_printf(buf.get(), "404 Not Found: %s\n", req->uri->path->path);
136 evhtp_send_reply_start(req, 404);
137 evhtp_send_reply_body(req, buf.get());
138 evhtp_send_reply_end(req);
Vitaly Buka17b0a8a2015-08-31 19:12:35 -0700139}
140
Jacob Marble33135582016-01-28 10:29:23 -0800141void HttpServerImpl::ProcessRequest(evhtp_request_t* req) {
142 std::unique_ptr<RequestImpl> request{new RequestImpl{EventPtr<evhtp_request_t>{req}}};
Vitaly Buka1a39c812015-10-08 21:20:58 -0700143 std::string path = request->GetPath();
144 auto it = handlers_.find(path);
145 if (it != handlers_.end()) {
146 return it->second.Run(std::move(request));
Vitaly Buka17b0a8a2015-08-31 19:12:35 -0700147 }
148 NotFound(req);
149}
Vitaly Buka1a39c812015-10-08 21:20:58 -0700150
Jacob Marble33135582016-01-28 10:29:23 -0800151void HttpServerImpl::ProcessRequestCallback(evhtp_request_t* req, void* arg) {
152 static_cast<HttpServerImpl*>(arg)->ProcessRequest(req);
153}
Vitaly Buka17b0a8a2015-08-31 19:12:35 -0700154
Vitaly Buka1a39c812015-10-08 21:20:58 -0700155void HttpServerImpl::AddHttpRequestHandler(
156 const std::string& path,
157 const RequestHandlerCallback& callback) {
Vitaly Buka52d006a2015-11-21 17:14:51 -0800158 handlers_.insert(std::make_pair(path, callback));
Jacob Marble33135582016-01-28 10:29:23 -0800159 evhtp_set_cb(httpd_.get(), path.c_str(), &ProcessRequestCallback, this);
Vitaly Buka1a39c812015-10-08 21:20:58 -0700160}
161
162void HttpServerImpl::AddHttpsRequestHandler(
163 const std::string& path,
164 const RequestHandlerCallback& callback) {
Vitaly Buka52d006a2015-11-21 17:14:51 -0800165 handlers_.insert(std::make_pair(path, callback));
Jacob Marble33135582016-01-28 10:29:23 -0800166 evhtp_set_cb(httpsd_.get(), path.c_str(), &ProcessRequestCallback, this);
Vitaly Buka17b0a8a2015-08-31 19:12:35 -0700167}
168
Jacob Marble33135582016-01-28 10:29:23 -0800169void HttpServerImpl::ProcessReply(std::shared_ptr<RequestImpl> request,
170 int status_code,
171 const std::string& data,
172 const std::string& mime_type) {}
173
Vitaly Buka17b0a8a2015-08-31 19:12:35 -0700174uint16_t HttpServerImpl::GetHttpPort() const {
175 return 7780;
176}
177
178uint16_t HttpServerImpl::GetHttpsPort() const {
179 return 7781;
180}
181
Alex Vakulenkoefee3a22015-11-17 15:08:38 -0800182base::TimeDelta HttpServerImpl::GetRequestTimeout() const {
183 return base::TimeDelta::Max();
184}
185
Vitaly Buka138aec42015-10-08 10:17:48 -0700186std::vector<uint8_t> HttpServerImpl::GetHttpsCertificateFingerprint() const {
Vitaly Buka17b0a8a2015-08-31 19:12:35 -0700187 return cert_fingerprint_;
188}
189
190} // namespace examples
191} // namespace weave