tizen 2.4 release
[external/nghttp2.git] / src / includes / nghttp2 / asio_http2.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_HTTP2_H
26 #define ASIO_HTTP2_H
27
28 #include <cstdint>
29 #include <memory>
30 #include <string>
31 #include <vector>
32 #include <functional>
33
34 namespace nghttp2 {
35
36 namespace asio_http2 {
37
38 struct header {
39   std::string name;
40   std::string value;
41 };
42
43 typedef std::function<void(const uint8_t *, std::size_t)> data_cb;
44 typedef std::function<void(void)> void_cb;
45
46 // Callback function to generate response body.  The implementation of
47 // this callback must fill at most |len| bytes data to |buf|.  The
48 // return value is pair of written bytes and bool value indicating
49 // that this is the end of the body.  If the end of the body was
50 // reached, return true.  If there is error and application wants to
51 // terminate stream, return std::make_pair(-1, false).  Returning
52 // std::make_pair(0, false) tells the library that don't call this
53 // callback until application calls response::resume().  This is
54 // useful when there is no data to send at the moment but there will
55 // be more to come in near future.
56 typedef std::function<std::pair<ssize_t, bool>(uint8_t *buf, std::size_t len)>
57     read_cb;
58
59 class channel_impl;
60
61 class channel {
62 public:
63   // Application must not call this directly.
64   channel();
65
66   // Schedules the execution of callback |cb| in the same thread where
67   // request callback is called.  Therefore, it is same to use request
68   // or response object in |cb|.  The callbacks are executed in the
69   // same order they are posted though same channel object if they are
70   // posted from the same thread.
71   void post(void_cb cb);
72
73   // Application must not call this directly.
74   channel_impl &impl();
75
76 private:
77   std::unique_ptr<channel_impl> impl_;
78 };
79
80 typedef std::function<void(channel &)> thread_cb;
81
82 namespace server {
83
84 class request_impl;
85 class response_impl;
86
87 class request {
88 public:
89   // Application must not call this directly.
90   request();
91
92   // Returns request headers.  The pusedo headers, which start with
93   // colon (;), are exluced from this list.
94   const std::vector<header> &headers() const;
95
96   // Returns method (e.g., GET).
97   const std::string &method() const;
98
99   // Returns scheme (e.g., https).
100   const std::string &scheme() const;
101
102   // Returns authority (e.g., example.org).  This could be empty
103   // string.  In this case, check host().
104
105   const std::string &authority() const;
106   // Returns host (e.g., example.org).  If host header field is not
107   // present, this value is copied from authority().
108
109   const std::string &host() const;
110
111   // Returns path (e.g., /index.html).
112   const std::string &path() const;
113
114   // Sets callback when chunk of request body is received.
115   void on_data(data_cb cb);
116
117   // Sets callback when request was completed.
118   void on_end(void_cb cb);
119
120   // Pushes resource denoted by |path| using |method|.  The additional
121   // headers can be given in |headers|.  request_cb will be called for
122   // pushed resource later on.  This function returns true if it
123   // succeeds, or false.
124   bool push(std::string method, std::string path,
125             std::vector<header> headers = {});
126
127   // Returns true if this is pushed request.
128   bool pushed() const;
129
130   // Returns true if stream has been closed.
131   bool closed() const;
132
133   // Runs function |start| in one of background threads.  Returns true
134   // if scheduling task was done successfully.
135   //
136   // Since |start| is called in different thread, calling any method
137   // of request or response object in the callback may cause undefined
138   // behavior.  To safely use them, use channel::post().  A callback
139   // passed to channel::post() is executed in the same thread where
140   // request callback is called, so it is safe to use request or
141   // response object.  Example::
142   bool run_task(thread_cb start);
143
144   // Application must not call this directly.
145   request_impl &impl();
146
147 private:
148   std::unique_ptr<request_impl> impl_;
149 };
150
151 class response {
152 public:
153   // Application must not call this directly.
154   response();
155
156   // Write response header using |status_code| (e.g., 200) and
157   // additional headers in |headers|.
158   void write_head(unsigned int status_code, std::vector<header> headers = {});
159
160   // Sends |data| as request body.  No further call of end() is
161   // allowed.
162   void end(std::string data = "");
163
164   // Sets callback |cb| as a generator of the response body.  No
165   // further call of end() is allowed.
166   void end(read_cb cb);
167
168   // Resumes deferred response.
169   void resume();
170
171   // Returns status code.
172   unsigned int status_code() const;
173
174   // Returns true if response has been started.
175   bool started() const;
176
177   // Application must not call this directly.
178   response_impl &impl();
179
180 private:
181   std::unique_ptr<response_impl> impl_;
182 };
183
184 // This is so called request callback.  Called every time request is
185 // received.
186 typedef std::function<void(const std::shared_ptr<request> &,
187                            const std::shared_ptr<response> &)> request_cb;
188
189 class http2_impl;
190
191 class http2 {
192 public:
193   http2();
194   ~http2();
195
196   // Starts listening connection on given address and port.  The
197   // incoming requests are handled by given callback |cb|.
198   void listen(const std::string &address, uint16_t port, request_cb cb);
199
200   // Sets number of native threads to handle incoming HTTP request.
201   // It defaults to 1.
202   void num_threads(size_t num_threads);
203
204   // Sets TLS private key file and certificate file.  Both files must
205   // be in PEM format.
206   void tls(std::string private_key_file, std::string certificate_file);
207
208   // Sets number of background threads to run concurrent tasks (see
209   // request::run_task()).  It defaults to 1.  This is not the number
210   // of thread to handle incoming HTTP request.  For this purpose, see
211   // num_threads().
212   void num_concurrent_tasks(size_t num_concurrent_tasks);
213
214   // Sets the maximum length to which the queue of pending
215   // connections.
216   void backlog(int backlog);
217
218 private:
219   std::unique_ptr<http2_impl> impl_;
220 };
221
222 } // namespace server
223
224 // Convenient function to create function to read file denoted by
225 // |path|.  This can be passed to response::end().
226 read_cb file_reader(const std::string &path);
227
228 // Like file_reader(const std::string&), but it takes opened file
229 // descriptor.  The passed descriptor will be closed when returned
230 // function object is destroyed.
231 read_cb file_reader_from_fd(int fd);
232
233 // Validates path so that it does not contain directory traversal
234 // vector.  Returns true if path is safe.  The |path| must start with
235 // "/" otherwise returns false.  This function should be called after
236 // percent-decode was performed.
237 bool check_path(const std::string &path);
238
239 // Performs percent-decode against string |s|.
240 std::string percent_decode(const std::string &s);
241
242 // Returns HTTP date representation of current posix time |t|.
243 std::string http_date(int64_t t);
244
245 } // namespace asio_http2
246
247 } // namespace nghttp2
248
249 #endif // ASIO_HTTP2_H