1 /*
2  * nghttp2 - HTTP/2 C Library
3  *
4  * Copyright (c) 2015 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_common.h"
26 
27 #include <fcntl.h>
28 #include <memory>
29 
30 #include "util.h"
31 #include "template.h"
32 #include "http2.h"
33 
34 namespace nghttp2 {
35 namespace asio_http2 {
36 
37 class nghttp2_category_impl : public boost::system::error_category {
38 public:
name() const39   const char *name() const noexcept { return "nghttp2"; }
message(int ev) const40   std::string message(int ev) const { return nghttp2_strerror(ev); }
41 };
42 
nghttp2_category()43 const boost::system::error_category &nghttp2_category() noexcept {
44   static nghttp2_category_impl cat;
45   return cat;
46 }
47 
make_error_code(nghttp2_error ev)48 boost::system::error_code make_error_code(nghttp2_error ev) {
49   return boost::system::error_code(static_cast<int>(ev), nghttp2_category());
50 }
51 
52 class nghttp2_asio_category_impl : public boost::system::error_category {
53 public:
name() const54   const char *name() const noexcept { return "nghttp2_asio"; }
message(int ev) const55   std::string message(int ev) const {
56     switch (ev) {
57     case NGHTTP2_ASIO_ERR_NO_ERROR:
58       return "no error";
59     case NGHTTP2_ASIO_ERR_TLS_NO_APP_PROTO_NEGOTIATED:
60       return "tls: no application protocol negotiated";
61     default:
62       return "unknown";
63     }
64   }
65 };
66 
nghttp2_asio_category()67 const boost::system::error_category &nghttp2_asio_category() noexcept {
68   static nghttp2_asio_category_impl cat;
69   return cat;
70 }
71 
make_error_code(nghttp2_asio_error ev)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());
75 }
76 
string_generator(std::string data)77 generator_cb string_generator(std::string data) {
78   auto strio = std::make_shared<std::pair<std::string, size_t>>(std::move(data),
79                                                                 data.size());
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);
85     left -= n;
86     if (left == 0) {
87       *data_flags |= NGHTTP2_DATA_FLAG_EOF;
88     }
89     return n;
90   };
91 }
92 
deferred_generator()93 generator_cb deferred_generator() {
94   return [](uint8_t *buf, size_t len, uint32_t *data_flags) {
95     return NGHTTP2_ERR_DEFERRED;
96   };
97 }
98 
99 template <typename F, typename... T>
defer_shared(F && f,T &&...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)...);
103 }
104 
file_generator(const std::string & path)105 generator_cb file_generator(const std::string &path) {
106   auto fd = open(path.c_str(), O_RDONLY);
107   if (fd == -1) {
108     return generator_cb();
109   }
110 
111   return file_generator_from_fd(fd);
112 }
113 
file_generator_from_fd(int fd)114 generator_cb file_generator_from_fd(int fd) {
115   auto d = defer_shared(close, fd);
116 
117   return [fd, d](uint8_t *buf, size_t len,
118                  uint32_t *data_flags) -> generator_cb::result_type {
119     ssize_t n;
120     while ((n = read(fd, buf, len)) == -1 && errno == EINTR)
121       ;
122 
123     if (n == -1) {
124       return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
125     }
126 
127     if (n == 0) {
128       *data_flags |= NGHTTP2_DATA_FLAG_EOF;
129     }
130 
131     return n;
132   };
133 }
134 
check_path(const std::string & path)135 bool check_path(const std::string &path) { return util::check_path(path); }
136 
percent_decode(const std::string & s)137 std::string percent_decode(const std::string &s) {
138   return util::percent_decode(std::begin(s), std::end(s));
139 }
140 
http_date(int64_t t)141 std::string http_date(int64_t t) { return util::http_date(t); }
142 
host_service_from_uri(boost::system::error_code & ec,std::string & scheme,std::string & host,std::string & service,const std::string & uri)143 boost::system::error_code host_service_from_uri(boost::system::error_code &ec,
144                                                 std::string &scheme,
145                                                 std::string &host,
146                                                 std::string &service,
147                                                 const std::string &uri) {
148   ec.clear();
149 
150   http_parser_url u{};
151   if (http_parser_parse_url(uri.c_str(), uri.size(), 0, &u) != 0) {
152     ec = make_error_code(boost::system::errc::invalid_argument);
153     return ec;
154   }
155 
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);
159     return ec;
160   }
161 
162   http2::copy_url_component(scheme, &u, UF_SCHEMA, uri.c_str());
163   http2::copy_url_component(host, &u, UF_HOST, uri.c_str());
164 
165   if (u.field_set & (1 << UF_PORT)) {
166     http2::copy_url_component(service, &u, UF_PORT, uri.c_str());
167   } else {
168     service = scheme;
169   }
170 
171   return ec;
172 }
173 
tls_h2_negotiated(ssl_socket & socket)174 bool tls_h2_negotiated(ssl_socket &socket) {
175   auto ssl = socket.native_handle();
176 
177   const unsigned char *next_proto = nullptr;
178   unsigned int next_proto_len = 0;
179 
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);
186   }
187 #endif // OPENSSL_VERSION_NUMBER >= 0x10002000L
188 
189   if (next_proto == nullptr) {
190     return false;
191   }
192 
193   return util::check_h2_is_selected(StringRef{next_proto, next_proto_len});
194 }
195 
196 } // namespace asio_http2
197 } // namespace nghttp2
198