c3f98e7cd31a3e9c34be8a3b27be23104afd9bd2
[platform/upstream/nghttp2.git] / src / asio_common.cc
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 <memory>
28
29 #include "util.h"
30 #include "template.h"
31 #include "http2.h"
32
33 namespace nghttp2 {
34 namespace asio_http2 {
35
36 class nghttp2_category_impl : public boost::system::error_category {
37 public:
38   const char *name() const noexcept { return "nghttp2"; }
39   std::string message(int ev) const { return nghttp2_strerror(ev); }
40 };
41
42 const boost::system::error_category &nghttp2_category() noexcept {
43   static nghttp2_category_impl cat;
44   return cat;
45 }
46
47 boost::system::error_code make_error_code(nghttp2_error ev) {
48   return boost::system::error_code(static_cast<int>(ev), nghttp2_category());
49 }
50
51 generator_cb string_generator(std::string data) {
52   auto strio = std::make_shared<std::pair<std::string, size_t>>(std::move(data),
53                                                                 data.size());
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);
59     left -= n;
60     if (left == 0) {
61       *data_flags |= NGHTTP2_DATA_FLAG_EOF;
62     }
63     return n;
64   };
65 }
66
67 generator_cb deferred_generator() {
68   return [](uint8_t *buf, size_t len,
69             uint32_t *data_flags) { return NGHTTP2_ERR_DEFERRED; };
70 }
71
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)...);
76 }
77
78 generator_cb file_generator(const std::string &path) {
79   auto fd = open(path.c_str(), O_RDONLY);
80   if (fd == -1) {
81     return generator_cb();
82   }
83
84   return file_generator_from_fd(fd);
85 }
86
87 generator_cb file_generator_from_fd(int fd) {
88   auto d = defer_shared(close, fd);
89
90   return [fd, d](uint8_t *buf, size_t len, uint32_t *data_flags)
91       -> generator_cb::result_type {
92     ssize_t n;
93     while ((n = read(fd, buf, len)) == -1 && errno == EINTR)
94       ;
95
96     if (n == -1) {
97       return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
98     }
99
100     if (n == 0) {
101       *data_flags |= NGHTTP2_DATA_FLAG_EOF;
102     }
103
104     return n;
105   };
106 }
107
108 bool check_path(const std::string &path) { return util::check_path(path); }
109
110 std::string percent_decode(const std::string &s) {
111   return util::percentDecode(std::begin(s), std::end(s));
112 }
113
114 std::string http_date(int64_t t) { return util::http_date(t); }
115
116 boost::system::error_code host_service_from_uri(boost::system::error_code &ec,
117                                                 std::string &scheme,
118                                                 std::string &host,
119                                                 std::string &service,
120                                                 const std::string &uri) {
121   ec.clear();
122
123   http_parser_url u{};
124   if (http_parser_parse_url(uri.c_str(), uri.size(), 0, &u) != 0) {
125     ec = make_error_code(boost::system::errc::invalid_argument);
126     return ec;
127   }
128
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);
132     return ec;
133   }
134
135   http2::copy_url_component(scheme, &u, UF_SCHEMA, uri.c_str());
136   http2::copy_url_component(host, &u, UF_HOST, uri.c_str());
137
138   if (u.field_set & (1 << UF_PORT)) {
139     http2::copy_url_component(service, &u, UF_PORT, uri.c_str());
140   } else {
141     service = scheme;
142   }
143
144   return ec;
145 }
146
147 } // namespace asio_http2
148 } // namespace nghttp2