2 * nghttp2 - HTTP/2 C Library
4 * Copyright (c) 2014 Tatsuhiro Tsujikawa
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 #include "asio_http2_impl.h"
27 #include <boost/asio/ssl.hpp>
29 #include <openssl/ssl.h>
31 #include <nghttp2/nghttp2.h>
33 #include "asio_server.h"
40 namespace asio_http2 {
44 http2::http2() : impl_(make_unique<http2_impl>()) {}
48 void http2::listen(const std::string &address, uint16_t port, request_cb cb) {
49 impl_->listen(address, port, std::move(cb));
52 void http2::num_threads(size_t num_threads) { impl_->num_threads(num_threads); }
54 void http2::tls(std::string private_key_file, std::string certificate_file) {
55 impl_->tls(std::move(private_key_file), std::move(certificate_file));
58 void http2::num_concurrent_tasks(size_t num_concurrent_tasks) {
59 impl_->num_concurrent_tasks(num_concurrent_tasks);
62 void http2::backlog(int backlog) { impl_->backlog(backlog); }
64 http2_impl::http2_impl()
65 : num_threads_(1), num_concurrent_tasks_(1), backlog_(-1) {}
68 std::vector<unsigned char> &get_alpn_token() {
69 static auto alpn_token = util::get_default_alpn();
74 void http2_impl::listen(const std::string &address, uint16_t port,
76 std::unique_ptr<boost::asio::ssl::context> ssl_ctx;
78 if (!private_key_file_.empty() && !certificate_file_.empty()) {
79 ssl_ctx = make_unique<boost::asio::ssl::context>(
80 boost::asio::ssl::context::sslv23);
82 ssl_ctx->use_private_key_file(private_key_file_,
83 boost::asio::ssl::context::pem);
84 ssl_ctx->use_certificate_chain_file(certificate_file_);
86 auto ctx = ssl_ctx->native_handle();
88 SSL_CTX_set_options(ctx, SSL_OP_ALL | SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 |
89 SSL_OP_NO_COMPRESSION |
90 SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
91 SSL_OP_SINGLE_ECDH_USE | SSL_OP_NO_TICKET |
92 SSL_OP_CIPHER_SERVER_PREFERENCE);
93 SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY);
94 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
95 SSL_CTX_set_mode(ctx, SSL_MODE_ENABLE_PARTIAL_WRITE);
97 SSL_CTX_set_cipher_list(ctx, ssl::DEFAULT_CIPHER_LIST);
99 auto ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
101 SSL_CTX_set_tmp_ecdh(ctx, ecdh);
105 SSL_CTX_set_next_protos_advertised_cb(
107 [](SSL *s, const unsigned char **data, unsigned int *len, void *arg) {
108 auto &token = get_alpn_token();
110 *data = token.data();
113 return SSL_TLSEXT_ERR_OK;
118 server(address, port, num_threads_, num_concurrent_tasks_, std::move(cb),
119 std::move(ssl_ctx), backlog_).run();
122 void http2_impl::num_threads(size_t num_threads) { num_threads_ = num_threads; }
124 void http2_impl::tls(std::string private_key_file,
125 std::string certificate_file) {
126 private_key_file_ = std::move(private_key_file);
127 certificate_file_ = std::move(certificate_file);
130 void http2_impl::num_concurrent_tasks(size_t num_concurrent_tasks) {
131 num_concurrent_tasks_ = num_concurrent_tasks;
134 void http2_impl::backlog(int backlog) { backlog_ = backlog; }
136 } // namespace server
138 template <typename F, typename... T>
139 std::shared_ptr<Defer<F, T...>> defer_shared(F &&f, T &&... t) {
140 return std::make_shared<Defer<F, T...>>(std::forward<F>(f),
141 std::forward<T>(t)...);
144 read_cb file_reader(const std::string &path) {
145 auto fd = open(path.c_str(), O_RDONLY);
150 return file_reader_from_fd(fd);
153 read_cb file_reader_from_fd(int fd) {
154 auto d = defer_shared(close, fd);
156 return [fd, d](uint8_t *buf, size_t len) -> read_cb::result_type {
158 while ((rv = read(fd, buf, len)) == -1 && errno == EINTR)
162 return std::make_pair(-1, false);
166 return std::make_pair(rv, true);
169 return std::make_pair(rv, false);
173 bool check_path(const std::string &path) { return util::check_path(path); }
175 std::string percent_decode(const std::string &s) {
176 return util::percentDecode(std::begin(s), std::end(s));
179 std::string http_date(int64_t t) { return util::http_date(t); }
181 } // namespace asio_http2
183 } // namespace nghttp2