tizen 2.4 release
[external/nghttp2.git] / src / asio_http2_impl.cc
1 /*
2  * nghttp2 - HTTP/2 C Library
3  *
4  * Copyright (c) 2014 Tatsuhiro Tsujikawa
5  *
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:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
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.
24  */
25 #include "asio_http2_impl.h"
26
27 #include <boost/asio/ssl.hpp>
28
29 #include <openssl/ssl.h>
30
31 #include <nghttp2/nghttp2.h>
32
33 #include "asio_server.h"
34 #include "util.h"
35 #include "ssl.h"
36 #include "template.h"
37
38 namespace nghttp2 {
39
40 namespace asio_http2 {
41
42 namespace server {
43
44 http2::http2() : impl_(make_unique<http2_impl>()) {}
45
46 http2::~http2() {}
47
48 void http2::listen(const std::string &address, uint16_t port, request_cb cb) {
49   impl_->listen(address, port, std::move(cb));
50 }
51
52 void http2::num_threads(size_t num_threads) { impl_->num_threads(num_threads); }
53
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));
56 }
57
58 void http2::num_concurrent_tasks(size_t num_concurrent_tasks) {
59   impl_->num_concurrent_tasks(num_concurrent_tasks);
60 }
61
62 void http2::backlog(int backlog) { impl_->backlog(backlog); }
63
64 http2_impl::http2_impl()
65     : num_threads_(1), num_concurrent_tasks_(1), backlog_(-1) {}
66
67 namespace {
68 std::vector<unsigned char> &get_alpn_token() {
69   static auto alpn_token = util::get_default_alpn();
70   return alpn_token;
71 }
72 } // namespace
73
74 void http2_impl::listen(const std::string &address, uint16_t port,
75                         request_cb cb) {
76   std::unique_ptr<boost::asio::ssl::context> ssl_ctx;
77
78   if (!private_key_file_.empty() && !certificate_file_.empty()) {
79     ssl_ctx = make_unique<boost::asio::ssl::context>(
80         boost::asio::ssl::context::sslv23);
81
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_);
85
86     auto ctx = ssl_ctx->native_handle();
87
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);
96
97     SSL_CTX_set_cipher_list(ctx, ssl::DEFAULT_CIPHER_LIST);
98
99     auto ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
100     if (ecdh) {
101       SSL_CTX_set_tmp_ecdh(ctx, ecdh);
102       EC_KEY_free(ecdh);
103     }
104
105     SSL_CTX_set_next_protos_advertised_cb(
106         ctx,
107         [](SSL *s, const unsigned char **data, unsigned int *len, void *arg) {
108           auto &token = get_alpn_token();
109
110           *data = token.data();
111           *len = token.size();
112
113           return SSL_TLSEXT_ERR_OK;
114         },
115         nullptr);
116   }
117
118   server(address, port, num_threads_, num_concurrent_tasks_, std::move(cb),
119          std::move(ssl_ctx), backlog_).run();
120 }
121
122 void http2_impl::num_threads(size_t num_threads) { num_threads_ = num_threads; }
123
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);
128 }
129
130 void http2_impl::num_concurrent_tasks(size_t num_concurrent_tasks) {
131   num_concurrent_tasks_ = num_concurrent_tasks;
132 }
133
134 void http2_impl::backlog(int backlog) { backlog_ = backlog; }
135
136 } // namespace server
137
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)...);
142 }
143
144 read_cb file_reader(const std::string &path) {
145   auto fd = open(path.c_str(), O_RDONLY);
146   if (fd == -1) {
147     return read_cb();
148   }
149
150   return file_reader_from_fd(fd);
151 }
152
153 read_cb file_reader_from_fd(int fd) {
154   auto d = defer_shared(close, fd);
155
156   return [fd, d](uint8_t *buf, size_t len) -> read_cb::result_type {
157     int rv;
158     while ((rv = read(fd, buf, len)) == -1 && errno == EINTR)
159       ;
160
161     if (rv == -1) {
162       return std::make_pair(-1, false);
163     }
164
165     if (rv == 0) {
166       return std::make_pair(rv, true);
167     }
168
169     return std::make_pair(rv, false);
170   };
171 }
172
173 bool check_path(const std::string &path) { return util::check_path(path); }
174
175 std::string percent_decode(const std::string &s) {
176   return util::percentDecode(std::begin(s), std::end(s));
177 }
178
179 std::string http_date(int64_t t) { return util::http_date(t); }
180
181 } // namespace asio_http2
182
183 } // namespace nghttp2