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.
36 namespace asio_http2 {
43 typedef std::function<void(const uint8_t *, std::size_t)> data_cb;
44 typedef std::function<void(void)> void_cb;
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)>
63 // Application must not call this directly.
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);
73 // Application must not call this directly.
77 std::unique_ptr<channel_impl> impl_;
80 typedef std::function<void(channel &)> thread_cb;
89 // Application must not call this directly.
92 // Returns request headers. The pusedo headers, which start with
93 // colon (;), are exluced from this list.
94 const std::vector<header> &headers() const;
96 // Returns method (e.g., GET).
97 const std::string &method() const;
99 // Returns scheme (e.g., https).
100 const std::string &scheme() const;
102 // Returns authority (e.g., example.org). This could be empty
103 // string. In this case, check host().
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().
109 const std::string &host() const;
111 // Returns path (e.g., /index.html).
112 const std::string &path() const;
114 // Sets callback when chunk of request body is received.
115 void on_data(data_cb cb);
117 // Sets callback when request was completed.
118 void on_end(void_cb cb);
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 = {});
127 // Returns true if this is pushed request.
130 // Returns true if stream has been closed.
133 // Runs function |start| in one of background threads. Returns true
134 // if scheduling task was done successfully.
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);
144 // Application must not call this directly.
145 request_impl &impl();
148 std::unique_ptr<request_impl> impl_;
153 // Application must not call this directly.
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 = {});
160 // Sends |data| as request body. No further call of end() is
162 void end(std::string data = "");
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);
168 // Resumes deferred response.
171 // Returns status code.
172 unsigned int status_code() const;
174 // Returns true if response has been started.
175 bool started() const;
177 // Application must not call this directly.
178 response_impl &impl();
181 std::unique_ptr<response_impl> impl_;
184 // This is so called request callback. Called every time request is
186 typedef std::function<void(const std::shared_ptr<request> &,
187 const std::shared_ptr<response> &)> request_cb;
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);
200 // Sets number of native threads to handle incoming HTTP request.
202 void num_threads(size_t num_threads);
204 // Sets TLS private key file and certificate file. Both files must
206 void tls(std::string private_key_file, std::string certificate_file);
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
212 void num_concurrent_tasks(size_t num_concurrent_tasks);
214 // Sets the maximum length to which the queue of pending
216 void backlog(int backlog);
219 std::unique_ptr<http2_impl> impl_;
222 } // namespace server
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);
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);
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);
239 // Performs percent-decode against string |s|.
240 std::string percent_decode(const std::string &s);
242 // Returns HTTP date representation of current posix time |t|.
243 std::string http_date(int64_t t);
245 } // namespace asio_http2
247 } // namespace nghttp2
249 #endif // ASIO_HTTP2_H