2 * nghttp2 - HTTP/2 C Library
4 * Copyright (c) 2015 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_common.h"
35 namespace asio_http2 {
37 class nghttp2_category_impl : public boost::system::error_category {
39 const char *name() const noexcept { return "nghttp2"; }
40 std::string message(int ev) const { return nghttp2_strerror(ev); }
43 const boost::system::error_category &nghttp2_category() noexcept {
44 static nghttp2_category_impl cat;
48 boost::system::error_code make_error_code(nghttp2_error ev) {
49 return boost::system::error_code(static_cast<int>(ev), nghttp2_category());
52 class nghttp2_asio_category_impl : public boost::system::error_category {
54 const char *name() const noexcept { return "nghttp2_asio"; }
55 std::string message(int ev) const {
57 case NGHTTP2_ASIO_ERR_NO_ERROR:
59 case NGHTTP2_ASIO_ERR_TLS_NO_APP_PROTO_NEGOTIATED:
60 return "tls: no application protocol negotiated";
67 const boost::system::error_category &nghttp2_asio_category() noexcept {
68 static nghttp2_asio_category_impl cat;
72 boost::system::error_code make_error_code(nghttp2_asio_error ev) {
73 return boost::system::error_code(static_cast<int>(ev),
74 nghttp2_asio_category());
77 generator_cb string_generator(std::string data) {
78 auto strio = std::make_shared<std::pair<std::string, size_t>>(std::move(data),
80 return [strio](uint8_t *buf, size_t len, uint32_t *data_flags) {
81 auto &data = strio->first;
82 auto &left = strio->second;
83 auto n = std::min(len, left);
84 std::copy_n(data.c_str() + data.size() - left, n, buf);
87 *data_flags |= NGHTTP2_DATA_FLAG_EOF;
93 generator_cb deferred_generator() {
94 return [](uint8_t *buf, size_t len, uint32_t *data_flags) {
95 return NGHTTP2_ERR_DEFERRED;
99 template <typename F, typename... T>
100 std::shared_ptr<Defer<F, T...>> defer_shared(F &&f, T &&... t) {
101 return std::make_shared<Defer<F, T...>>(std::forward<F>(f),
102 std::forward<T>(t)...);
105 generator_cb file_generator(const std::string &path) {
106 auto fd = open(path.c_str(), O_RDONLY);
108 return generator_cb();
111 return file_generator_from_fd(fd);
114 generator_cb file_generator_from_fd(int fd) {
115 auto d = defer_shared(close, fd);
117 return [fd, d](uint8_t *buf, size_t len,
118 uint32_t *data_flags) -> generator_cb::result_type {
120 while ((n = read(fd, buf, len)) == -1 && errno == EINTR)
124 return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
128 *data_flags |= NGHTTP2_DATA_FLAG_EOF;
135 bool check_path(const std::string &path) { return util::check_path(path); }
137 std::string percent_decode(const std::string &s) {
138 return util::percent_decode(std::begin(s), std::end(s));
141 std::string http_date(int64_t t) { return util::http_date(t); }
143 boost::system::error_code host_service_from_uri(boost::system::error_code &ec,
146 std::string &service,
147 const std::string &uri) {
151 if (http_parser_parse_url(uri.c_str(), uri.size(), 0, &u) != 0) {
152 ec = make_error_code(boost::system::errc::invalid_argument);
156 if ((u.field_set & (1 << UF_SCHEMA)) == 0 ||
157 (u.field_set & (1 << UF_HOST)) == 0) {
158 ec = make_error_code(boost::system::errc::invalid_argument);
162 http2::copy_url_component(scheme, &u, UF_SCHEMA, uri.c_str());
163 http2::copy_url_component(host, &u, UF_HOST, uri.c_str());
165 if (u.field_set & (1 << UF_PORT)) {
166 http2::copy_url_component(service, &u, UF_PORT, uri.c_str());
174 bool tls_h2_negotiated(ssl_socket &socket) {
175 auto ssl = socket.native_handle();
177 const unsigned char *next_proto = nullptr;
178 unsigned int next_proto_len = 0;
180 #ifndef OPENSSL_NO_NEXTPROTONEG
181 SSL_get0_next_proto_negotiated(ssl, &next_proto, &next_proto_len);
182 #endif // !OPENSSL_NO_NEXTPROTONEG
183 #if OPENSSL_VERSION_NUMBER >= 0x10002000L
184 if (next_proto == nullptr) {
185 SSL_get0_alpn_selected(ssl, &next_proto, &next_proto_len);
187 #endif // OPENSSL_VERSION_NUMBER >= 0x10002000L
189 if (next_proto == nullptr) {
193 return util::check_h2_is_selected(StringRef{next_proto, next_proto_len});
196 } // namespace asio_http2
197 } // namespace nghttp2