nghttp2/src/asio_http2_impl.cc

184 lines
5.5 KiB
C++
Raw Normal View History

/*
* nghttp2 - HTTP/2 C Library
*
* Copyright (c) 2014 Tatsuhiro Tsujikawa
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "asio_http2_impl.h"
#include <boost/asio/ssl.hpp>
#include <openssl/ssl.h>
#include <nghttp2/nghttp2.h>
#include "asio_server.h"
#include "util.h"
#include "ssl.h"
#include "template.h"
namespace nghttp2 {
namespace asio_http2 {
namespace server {
http2::http2() : impl_(make_unique<http2_impl>()) {}
2014-11-27 15:39:04 +01:00
http2::~http2() {}
2014-11-27 15:39:04 +01:00
void http2::listen(const std::string &address, uint16_t port, request_cb cb) {
impl_->listen(address, port, std::move(cb));
}
2014-11-27 15:39:04 +01:00
void http2::num_threads(size_t num_threads) { impl_->num_threads(num_threads); }
2014-11-27 15:39:04 +01:00
void http2::tls(std::string private_key_file, std::string certificate_file) {
impl_->tls(std::move(private_key_file), std::move(certificate_file));
}
2014-11-27 15:39:04 +01:00
void http2::num_concurrent_tasks(size_t num_concurrent_tasks) {
impl_->num_concurrent_tasks(num_concurrent_tasks);
}
2014-11-27 15:39:04 +01:00
void http2::backlog(int backlog) { impl_->backlog(backlog); }
2014-11-10 15:26:01 +01:00
http2_impl::http2_impl()
2014-11-27 15:39:04 +01:00
: num_threads_(1), num_concurrent_tasks_(1), backlog_(-1) {}
namespace {
2014-11-27 15:39:04 +01:00
std::vector<unsigned char> &get_alpn_token() {
2014-11-14 15:14:39 +01:00
static auto alpn_token = util::get_default_alpn();
return alpn_token;
}
} // namespace
2014-11-27 15:39:04 +01:00
void http2_impl::listen(const std::string &address, uint16_t port,
request_cb cb) {
std::unique_ptr<boost::asio::ssl::context> ssl_ctx;
2014-11-27 15:39:04 +01:00
if (!private_key_file_.empty() && !certificate_file_.empty()) {
ssl_ctx = make_unique<boost::asio::ssl::context>(
2014-11-27 15:39:04 +01:00
boost::asio::ssl::context::sslv23);
ssl_ctx->use_private_key_file(private_key_file_,
2014-11-27 15:39:04 +01:00
boost::asio::ssl::context::pem);
ssl_ctx->use_certificate_chain_file(certificate_file_);
auto ctx = ssl_ctx->native_handle();
2014-11-27 15:39:04 +01:00
SSL_CTX_set_options(ctx, SSL_OP_ALL | SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 |
SSL_OP_NO_COMPRESSION |
SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
SSL_OP_SINGLE_ECDH_USE | SSL_OP_NO_TICKET |
SSL_OP_CIPHER_SERVER_PREFERENCE);
SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY);
SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
SSL_CTX_set_mode(ctx, SSL_MODE_ENABLE_PARTIAL_WRITE);
SSL_CTX_set_cipher_list(ctx, ssl::DEFAULT_CIPHER_LIST);
auto ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
2014-11-27 15:39:04 +01:00
if (ecdh) {
SSL_CTX_set_tmp_ecdh(ctx, ecdh);
EC_KEY_free(ecdh);
}
2014-11-27 15:39:04 +01:00
SSL_CTX_set_next_protos_advertised_cb(
ctx,
[](SSL *s, const unsigned char **data, unsigned int *len, void *arg) {
auto &token = get_alpn_token();
2014-11-27 15:39:04 +01:00
*data = token.data();
*len = token.size();
2014-11-27 15:39:04 +01:00
return SSL_TLSEXT_ERR_OK;
},
nullptr);
}
2014-11-27 15:39:04 +01:00
server(address, port, num_threads_, num_concurrent_tasks_, std::move(cb),
std::move(ssl_ctx), backlog_).run();
}
2014-11-27 15:39:04 +01:00
void http2_impl::num_threads(size_t num_threads) { num_threads_ = num_threads; }
void http2_impl::tls(std::string private_key_file,
2014-11-27 15:39:04 +01:00
std::string certificate_file) {
private_key_file_ = std::move(private_key_file);
certificate_file_ = std::move(certificate_file);
}
2014-11-27 15:39:04 +01:00
void http2_impl::num_concurrent_tasks(size_t num_concurrent_tasks) {
num_concurrent_tasks_ = num_concurrent_tasks;
}
2014-11-27 15:39:04 +01:00
void http2_impl::backlog(int backlog) { backlog_ = backlog; }
2014-11-10 15:26:01 +01:00
} // namespace server
2014-11-27 15:39:04 +01:00
template <typename T, typename F>
std::shared_ptr<util::Defer<T, F>> defer_shared(T &&t, F f) {
return std::make_shared<util::Defer<T, F>>(std::forward<T>(t),
std::forward<F>(f));
}
2014-11-27 15:39:04 +01:00
read_cb file_reader(const std::string &path) {
auto fd = open(path.c_str(), O_RDONLY);
2014-11-27 15:39:04 +01:00
if (fd == -1) {
return read_cb();
}
return file_reader_from_fd(fd);
}
2014-11-27 15:39:04 +01:00
read_cb file_reader_from_fd(int fd) {
auto d = defer_shared(static_cast<int>(fd), close);
2014-11-27 15:39:04 +01:00
return [fd, d](uint8_t *buf, size_t len) -> read_cb::result_type {
int rv;
while ((rv = read(fd, buf, len)) == -1 && errno == EINTR)
;
2014-11-27 15:39:04 +01:00
if (rv == -1) {
return std::make_pair(-1, false);
}
2014-11-27 15:39:04 +01:00
if (rv == 0) {
return std::make_pair(rv, true);
}
2014-11-27 15:39:04 +01:00
return std::make_pair(rv, false);
};
}
2014-11-27 15:39:04 +01:00
bool check_path(const std::string &path) { return util::check_path(path); }
2014-11-27 15:39:04 +01:00
std::string percent_decode(const std::string &s) {
return util::percentDecode(std::begin(s), std::end(s));
}
2014-11-27 15:39:04 +01:00
std::string http_date(int64_t t) { return util::http_date(t); }
} // namespace asio_http2
} // namespace nghttp2