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.
25 #ifndef ASIO_SERVER_HTTP2_HANDLER_H
26 #define ASIO_SERVER_HTTP2_HANDLER_H
28 #include "nghttp2_config.h"
34 #include <boost/array.hpp>
36 #include <nghttp2/asio_http2_server.h>
39 namespace asio_http2 {
46 struct callback_guard {
47 callback_guard(http2_handler &h);
49 http2_handler &handler;
52 using connection_write = std::function<void(void)>;
54 class http2_handler : public std::enable_shared_from_this<http2_handler> {
56 http2_handler(boost::asio::io_service &io_service, connection_write writefun,
63 stream *create_stream(int32_t stream_id);
64 void close_stream(int32_t stream_id);
65 stream *find_stream(int32_t stream_id);
67 void call_on_request(stream &s);
69 bool should_stop() const;
71 int start_response(stream &s);
73 int submit_trailer(stream &s, header_map h);
75 void stream_error(int32_t stream_id, uint32_t error_code);
77 void initiate_write();
79 void enter_callback();
80 void leave_callback();
82 void resume(stream &s);
84 response *push_promise(boost::system::error_code &ec, stream &s,
85 std::string method, std::string raw_path_query,
90 boost::asio::io_service &io_service();
92 const std::string &http_date();
95 int on_read(const boost::array<uint8_t, N> &buffer, std::size_t len) {
96 callback_guard cg(*this);
100 rv = nghttp2_session_mem_recv(session_, buffer.data(), len);
110 int on_write(boost::array<uint8_t, N> &buffer, std::size_t &len) {
111 callback_guard cg(*this);
116 std::copy_n(buf_, buflen_, std::begin(buffer));
126 auto nread = nghttp2_session_mem_send(session_, &data);
135 if (len + nread > buffer.size()) {
142 std::copy_n(data, nread, std::begin(buffer) + len);
151 std::map<int32_t, std::shared_ptr<stream>> streams_;
152 connection_write writefun_;
154 boost::asio::io_service &io_service_;
155 nghttp2_session *session_;
158 bool inside_callback_;
159 time_t tstamp_cached_;
160 std::string formatted_date_;
163 } // namespace server
164 } // namespace asio_http2
165 } // namespace nghttp
167 #endif // ASIO_SERVER_HTTP2_HANDLER_H