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"
34 namespace asio_http2 {
36 class nghttp2_category_impl : public boost::system::error_category {
38 const char *name() const noexcept { return "nghttp2"; }
39 std::string message(int ev) const { return nghttp2_strerror(ev); }
42 const boost::system::error_category &nghttp2_category() noexcept {
43 static nghttp2_category_impl cat;
47 boost::system::error_code make_error_code(nghttp2_error ev) {
48 return boost::system::error_code(static_cast<int>(ev), nghttp2_category());
51 generator_cb string_generator(std::string data) {
52 auto strio = std::make_shared<std::pair<std::string, size_t>>(std::move(data),
54 return [strio](uint8_t *buf, size_t len, uint32_t *data_flags) {
55 auto &data = strio->first;
56 auto &left = strio->second;
57 auto n = std::min(len, left);
58 std::copy_n(data.c_str() + data.size() - left, n, buf);
61 *data_flags |= NGHTTP2_DATA_FLAG_EOF;
67 generator_cb deferred_generator() {
68 return [](uint8_t *buf, size_t len,
69 uint32_t *data_flags) { return NGHTTP2_ERR_DEFERRED; };
72 template <typename F, typename... T>
73 std::shared_ptr<Defer<F, T...>> defer_shared(F &&f, T &&... t) {
74 return std::make_shared<Defer<F, T...>>(std::forward<F>(f),
75 std::forward<T>(t)...);
78 generator_cb file_generator(const std::string &path) {
79 auto fd = open(path.c_str(), O_RDONLY);
81 return generator_cb();
84 return file_generator_from_fd(fd);
87 generator_cb file_generator_from_fd(int fd) {
88 auto d = defer_shared(close, fd);
90 return [fd, d](uint8_t *buf, size_t len, uint32_t *data_flags)
91 -> generator_cb::result_type {
93 while ((n = read(fd, buf, len)) == -1 && errno == EINTR)
97 return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
101 *data_flags |= NGHTTP2_DATA_FLAG_EOF;
108 bool check_path(const std::string &path) { return util::check_path(path); }
110 std::string percent_decode(const std::string &s) {
111 return util::percentDecode(std::begin(s), std::end(s));
114 std::string http_date(int64_t t) { return util::http_date(t); }
116 boost::system::error_code host_service_from_uri(boost::system::error_code &ec,
119 std::string &service,
120 const std::string &uri) {
124 if (http_parser_parse_url(uri.c_str(), uri.size(), 0, &u) != 0) {
125 ec = make_error_code(boost::system::errc::invalid_argument);
129 if ((u.field_set & (1 << UF_SCHEMA)) == 0 ||
130 (u.field_set & (1 << UF_HOST)) == 0) {
131 ec = make_error_code(boost::system::errc::invalid_argument);
135 http2::copy_url_component(scheme, &u, UF_SCHEMA, uri.c_str());
136 http2::copy_url_component(host, &u, UF_HOST, uri.c_str());
138 if (u.field_set & (1 << UF_PORT)) {
139 http2::copy_url_component(service, &u, UF_PORT, uri.c_str());
147 } // namespace asio_http2
148 } // namespace nghttp2