Imported Upstream version 1.0.0
[platform/upstream/nghttp2.git] / src / asio_server_http2_handler.h
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_SERVER_HTTP2_HANDLER_H
26 #define ASIO_SERVER_HTTP2_HANDLER_H
27
28 #include "nghttp2_config.h"
29
30 #include <map>
31 #include <functional>
32 #include <string>
33
34 #include <boost/array.hpp>
35
36 #include <nghttp2/asio_http2_server.h>
37
38 namespace nghttp2 {
39 namespace asio_http2 {
40 namespace server {
41
42 class http2_handler;
43 class stream;
44 class serve_mux;
45
46 struct callback_guard {
47   callback_guard(http2_handler &h);
48   ~callback_guard();
49   http2_handler &handler;
50 };
51
52 using connection_write = std::function<void(void)>;
53
54 class http2_handler : public std::enable_shared_from_this<http2_handler> {
55 public:
56   http2_handler(boost::asio::io_service &io_service, connection_write writefun,
57                 serve_mux &mux);
58
59   ~http2_handler();
60
61   int start();
62
63   stream *create_stream(int32_t stream_id);
64   void close_stream(int32_t stream_id);
65   stream *find_stream(int32_t stream_id);
66
67   void call_on_request(stream &s);
68
69   bool should_stop() const;
70
71   int start_response(stream &s);
72
73   int submit_trailer(stream &s, header_map h);
74
75   void stream_error(int32_t stream_id, uint32_t error_code);
76
77   void initiate_write();
78
79   void enter_callback();
80   void leave_callback();
81
82   void resume(stream &s);
83
84   response *push_promise(boost::system::error_code &ec, stream &s,
85                          std::string method, std::string raw_path_query,
86                          header_map h);
87
88   void signal_write();
89
90   boost::asio::io_service &io_service();
91
92   const std::string &http_date();
93
94   template <size_t N>
95   int on_read(const boost::array<uint8_t, N> &buffer, std::size_t len) {
96     callback_guard cg(*this);
97
98     int rv;
99
100     rv = nghttp2_session_mem_recv(session_, buffer.data(), len);
101
102     if (rv < 0) {
103       return -1;
104     }
105
106     return 0;
107   }
108
109   template <size_t N>
110   int on_write(boost::array<uint8_t, N> &buffer, std::size_t &len) {
111     callback_guard cg(*this);
112
113     len = 0;
114
115     if (buf_) {
116       std::copy_n(buf_, buflen_, std::begin(buffer));
117
118       len += buflen_;
119
120       buf_ = nullptr;
121       buflen_ = 0;
122     }
123
124     for (;;) {
125       const uint8_t *data;
126       auto nread = nghttp2_session_mem_send(session_, &data);
127       if (nread < 0) {
128         return -1;
129       }
130
131       if (nread == 0) {
132         break;
133       }
134
135       if (len + nread > buffer.size()) {
136         buf_ = data;
137         buflen_ = nread;
138
139         break;
140       }
141
142       std::copy_n(data, nread, std::begin(buffer) + len);
143
144       len += nread;
145     }
146
147     return 0;
148   }
149
150 private:
151   std::map<int32_t, std::shared_ptr<stream>> streams_;
152   connection_write writefun_;
153   serve_mux &mux_;
154   boost::asio::io_service &io_service_;
155   nghttp2_session *session_;
156   const uint8_t *buf_;
157   std::size_t buflen_;
158   bool inside_callback_;
159   time_t tstamp_cached_;
160   std::string formatted_date_;
161 };
162
163 } // namespace server
164 } // namespace asio_http2
165 } // namespace nghttp
166
167 #endif // ASIO_SERVER_HTTP2_HANDLER_H