1 /* 2 * nghttp2 - HTTP/2 C Library 3 * 4 * Copyright (c) 2014 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 #ifndef ASIO_HTTP2_H 26 #define ASIO_HTTP2_H 27 28 #include <cstdint> 29 #include <memory> 30 #include <string> 31 #include <vector> 32 #include <functional> 33 #include <map> 34 35 #include <boost/system/error_code.hpp> 36 #include <boost/asio.hpp> 37 #include <boost/asio/ssl.hpp> 38 39 #include <nghttp2/nghttp2.h> 40 41 namespace nghttp2 { 42 43 namespace asio_http2 { 44 45 struct header_value { 46 // header field value 47 std::string value; 48 // true if the header field value is sensitive information, such as 49 // authorization information or short length secret cookies. If 50 // true, those header fields are not indexed by HPACK (but still 51 // huffman-encoded), which results in lesser compression. 52 bool sensitive; 53 }; 54 55 // header fields. The header field name must be lower-cased. 56 using header_map = std::multimap<std::string, header_value>; 57 58 const boost::system::error_category &nghttp2_category() noexcept; 59 60 struct uri_ref { 61 std::string scheme; 62 std::string host; 63 // form after percent-encoding decoded 64 std::string path; 65 // original path, percent-encoded 66 std::string raw_path; 67 // original query, percent-encoded 68 std::string raw_query; 69 std::string fragment; 70 }; 71 72 // Callback function when data is arrived. EOF is indicated by 73 // passing 0 to the second parameter. 74 typedef std::function<void(const uint8_t *, std::size_t)> data_cb; 75 typedef std::function<void(void)> void_cb; 76 typedef std::function<void(const boost::system::error_code &ec)> error_cb; 77 // Callback function when request and response are finished. The 78 // parameter indicates the cause of closure. 79 typedef std::function<void(uint32_t)> close_cb; 80 81 // Callback function to generate response body. This function has the 82 // same semantics with nghttp2_data_source_read_callback. Just source 83 // and user_data parameters are removed. 84 // 85 // Basically, write at most |len| bytes to |data| and returns the 86 // number of bytes written. If there is no data left to send, set 87 // NGHTTP2_DATA_FLAG_EOF to *data_flags (e.g., *data_flags |= 88 // NGHTTP2_DATA_FLAG_EOF). If there is still data to send but they 89 // are not available right now, return NGHTTP2_ERR_DEFERRED. In case 90 // of the error and request/response must be closed, return 91 // NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE. 92 typedef std::function<ssize_t(uint8_t *buf, std::size_t len, 93 uint32_t *data_flags)> 94 generator_cb; 95 96 // Convenient function to create function to read file denoted by 97 // |path|. This can be passed to response::end(). 98 generator_cb file_generator(const std::string &path); 99 100 // Like file_generator(const std::string&), but it takes opened file 101 // descriptor. The passed descriptor will be closed when returned 102 // function object is destroyed. 103 generator_cb file_generator_from_fd(int fd); 104 105 // Validates path so that it does not contain directory traversal 106 // vector. Returns true if path is safe. The |path| must start with 107 // "/" otherwise returns false. This function should be called after 108 // percent-decode was performed. 109 bool check_path(const std::string &path); 110 111 // Performs percent-decode against string |s|. 112 std::string percent_decode(const std::string &s); 113 114 // Returns HTTP date representation of current posix time |t|. 115 std::string http_date(int64_t t); 116 117 // Parses |uri| and extract scheme, host and service. The service is 118 // port component of URI (e.g., "8443") if available, otherwise it is 119 // scheme (e.g., "https"). 120 boost::system::error_code host_service_from_uri(boost::system::error_code &ec, 121 std::string &scheme, 122 std::string &host, 123 std::string &service, 124 const std::string &uri); 125 126 enum nghttp2_asio_error { 127 NGHTTP2_ASIO_ERR_NO_ERROR = 0, 128 NGHTTP2_ASIO_ERR_TLS_NO_APP_PROTO_NEGOTIATED = 1, 129 }; 130 131 } // namespace asio_http2 132 133 } // namespace nghttp2 134 135 namespace boost { 136 137 namespace system { 138 139 template <> struct is_error_code_enum<nghttp2_error> { 140 BOOST_STATIC_CONSTANT(bool, value = true); 141 }; 142 143 template <> struct is_error_code_enum<nghttp2::asio_http2::nghttp2_asio_error> { 144 BOOST_STATIC_CONSTANT(bool, value = true); 145 }; 146 147 } // namespace system 148 149 } // namespace boost 150 151 #endif // ASIO_HTTP2_H 152