2 * nghttp2 - HTTP/2 C Library
4 * Copyright (c) 2014 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.
35 #include <boost/system/error_code.hpp>
36 #include <boost/asio.hpp>
37 #include <boost/asio/ssl.hpp>
39 #include <nghttp2/nghttp2.h>
44 template <> struct is_error_code_enum<nghttp2_error> {
45 BOOST_STATIC_CONSTANT(bool, value = true);
53 namespace asio_http2 {
58 // true if the header field value is sensitive information, such as
59 // authorization information or short length secret cookies. If
60 // true, those header fields are not indexed by HPACK (but still
61 // huffman-encoded), which results in lesser compression.
65 // header fields. The header field name must be lower-cased.
66 using header_map = std::multimap<std::string, header_value>;
68 const boost::system::error_category &nghttp2_category() noexcept;
73 // form after percent-encoding decoded
75 // original path, percent-encoded
77 // original query, percent-encoded
78 std::string raw_query;
82 // Callback function when data is arrived. EOF is indicated by
83 // passing 0 to the second parameter.
84 typedef std::function<void(const uint8_t *, std::size_t)> data_cb;
85 typedef std::function<void(void)> void_cb;
86 typedef std::function<void(const boost::system::error_code &ec)> error_cb;
87 // Callback function when request and response are finished. The
88 // parameter indicates the cause of closure.
89 typedef std::function<void(uint32_t)> close_cb;
91 // Callback function to generate response body. This function has the
92 // same semantics with nghttp2_data_source_read_callback. Just source
93 // and user_data parameters are removed.
95 // Basically, write at most |len| bytes to |data| and returns the
96 // number of bytes written. If there is no data left to send, set
97 // NGHTTP2_DATA_FLAG_EOF to *data_flags (e.g., *data_flags |=
98 // NGHTTP2_DATA_FLAG_EOF). If there is still data to send but they
99 // are not available right now, return NGHTTP2_ERR_DEFERRED. In case
100 // of the error and request/response must be closed, return
101 // NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE.
102 typedef std::function<
103 ssize_t(uint8_t *buf, std::size_t len, uint32_t *data_flags)> generator_cb;
105 // Convenient function to create function to read file denoted by
106 // |path|. This can be passed to response::end().
107 generator_cb file_generator(const std::string &path);
109 // Like file_generator(const std::string&), but it takes opened file
110 // descriptor. The passed descriptor will be closed when returned
111 // function object is destroyed.
112 generator_cb file_generator_from_fd(int fd);
114 // Validates path so that it does not contain directory traversal
115 // vector. Returns true if path is safe. The |path| must start with
116 // "/" otherwise returns false. This function should be called after
117 // percent-decode was performed.
118 bool check_path(const std::string &path);
120 // Performs percent-decode against string |s|.
121 std::string percent_decode(const std::string &s);
123 // Returns HTTP date representation of current posix time |t|.
124 std::string http_date(int64_t t);
126 // Parses |uri| and extract scheme, host and service. The service is
127 // port component of URI (e.g., "8443") if available, otherwise it is
128 // scheme (e.g., "https").
129 boost::system::error_code host_service_from_uri(boost::system::error_code &ec,
132 std::string &service,
133 const std::string &uri);
135 } // namespace asio_http2
137 } // namespace nghttp2
139 #endif // ASIO_HTTP2_H