2 * nghttp2 - HTTP/2 C Library
4 * Copyright (c) 2015 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_HTTP2_CLIENT_H
26 #define ASIO_HTTP2_CLIENT_H
28 #include <nghttp2/asio_http2.h>
32 namespace asio_http2 {
40 // Application must not call this directly.
44 // Sets callback which is invoked when chunk of response body is
46 void on_data(data_cb cb) const;
48 // Returns status code.
49 int status_code() const;
51 // Returns content-length. -1 if it is unknown.
52 int64_t content_length() const;
54 // Returns the response header fields. The pusedo header fields,
55 // which start with colon (:), are exluced from this list.
56 const header_map &header() const;
58 // Application must not call this directly.
59 response_impl &impl() const;
62 std::unique_ptr<response_impl> impl_;
67 using response_cb = std::function<void(const response &)>;
68 using request_cb = std::function<void(const request &)>;
70 std::function<void(boost::asio::ip::tcp::resolver::iterator)>;
76 // Application must not call this directly.
80 // Sets callback which is invoked when response header is received.
81 void on_response(response_cb cb) const;
83 // Sets callback which is invoked when push request header is
85 void on_push(request_cb cb) const;
87 // Sets callback which is invoked when this request and response are
88 // finished. After the invocation of this callback, the application
89 // must not access request and response object.
90 void on_close(close_cb cb) const;
92 // Write trailer part. This must be called after setting both
93 // NGHTTP2_DATA_FLAG_EOF and NGHTTP2_DATA_FLAG_NO_END_STREAM set in
94 // *data_flag parameter in generator_cb passed to session::submit()
96 void write_trailer(header_map h) const;
98 // Cancels this request and response with given error code.
99 void cancel(uint32_t error_code = NGHTTP2_INTERNAL_ERROR) const;
101 // Resumes deferred uploading.
104 // Returns method (e.g., GET).
105 const std::string &method() const;
107 // Returns request URI, split into components.
108 const uri_ref &uri() const;
110 // Returns request header fields. The pusedo header fields, which
111 // start with colon (:), are exluced from this list.
112 const header_map &header() const;
114 // Application must not call this directly.
115 request_impl &impl() const;
118 std::unique_ptr<request_impl> impl_;
121 // Wrapper around an nghttp2_priority_spec.
122 class priority_spec {
124 // The default ctor is used only by sentinel values.
125 priority_spec() = default;
127 // Create a priority spec with the given priority settings.
128 explicit priority_spec(const int32_t stream_id, const int32_t weight,
129 const bool exclusive = false);
131 // Return a pointer to a valid nghttp2 priority spec, or null.
132 const nghttp2_priority_spec *get() const;
134 // Indicates whether or not this spec is valid (i.e. was constructed with
139 nghttp2_priority_spec spec_;
147 // Starts HTTP/2 session by connecting to |host| and |service|
148 // (e.g., "80") using clear text TCP connection with connect timeout
150 session(boost::asio::io_service &io_service, const std::string &host,
151 const std::string &service);
153 // Same as previous but with pegged local endpoint
154 session(boost::asio::io_service &io_service,
155 const boost::asio::ip::tcp::endpoint &local_endpoint,
156 const std::string &host, const std::string &service);
158 // Starts HTTP/2 session by connecting to |host| and |service|
159 // (e.g., "80") using clear text TCP connection with given connect
161 session(boost::asio::io_service &io_service, const std::string &host,
162 const std::string &service,
163 const boost::posix_time::time_duration &connect_timeout);
165 // Same as previous but with pegged local endpoint
166 session(boost::asio::io_service &io_service,
167 const boost::asio::ip::tcp::endpoint &local_endpoint,
168 const std::string &host, const std::string &service,
169 const boost::posix_time::time_duration &connect_timeout);
171 // Starts HTTP/2 session by connecting to |host| and |service|
172 // (e.g., "443") using encrypted SSL/TLS connection with connect
173 // timeout 60 seconds.
174 session(boost::asio::io_service &io_service,
175 boost::asio::ssl::context &tls_context, const std::string &host,
176 const std::string &service);
178 // Starts HTTP/2 session by connecting to |host| and |service|
179 // (e.g., "443") using encrypted SSL/TLS connection with given
181 session(boost::asio::io_service &io_service,
182 boost::asio::ssl::context &tls_context, const std::string &host,
183 const std::string &service,
184 const boost::posix_time::time_duration &connect_timeout);
188 session(session &&other) noexcept;
189 session &operator=(session &&other) noexcept;
191 // Sets callback which is invoked after connection is established.
192 void on_connect(connect_cb cb) const;
194 // Sets callback which is invoked there is connection level error
195 // and session is terminated.
196 void on_error(error_cb cb) const;
198 // Sets read timeout, which defaults to 60 seconds.
199 void read_timeout(const boost::posix_time::time_duration &t);
201 // Shutdowns connection.
202 void shutdown() const;
204 // Returns underlying io_service object.
205 boost::asio::io_service &io_service() const;
207 // Submits request to server using |method| (e.g., "GET"), |uri|
208 // (e.g., "http://localhost/") and optionally additional header
209 // fields. This function returns pointer to request object if it
210 // succeeds, or nullptr and |ec| contains error message.
211 const request *submit(boost::system::error_code &ec,
212 const std::string &method, const std::string &uri,
213 header_map h = header_map{},
214 priority_spec prio = priority_spec()) const;
216 // Submits request to server using |method| (e.g., "GET"), |uri|
217 // (e.g., "http://localhost/") and optionally additional header
218 // fields. The |data| is request body. This function returns
219 // pointer to request object if it succeeds, or nullptr and |ec|
220 // contains error message.
221 const request *submit(boost::system::error_code &ec,
222 const std::string &method, const std::string &uri,
223 std::string data, header_map h = header_map{},
224 priority_spec prio = priority_spec()) const;
226 // Submits request to server using |method| (e.g., "GET"), |uri|
227 // (e.g., "http://localhost/") and optionally additional header
228 // fields. The |cb| is used to generate request body. This
229 // function returns pointer to request object if it succeeds, or
230 // nullptr and |ec| contains error message.
231 const request *submit(boost::system::error_code &ec,
232 const std::string &method, const std::string &uri,
233 generator_cb cb, header_map h = header_map{},
234 priority_spec prio = priority_spec()) const;
237 std::shared_ptr<session_impl> impl_;
240 // configure |tls_ctx| for client use. Currently, we just set NPN
241 // callback for HTTP/2.
242 boost::system::error_code
243 configure_tls_context(boost::system::error_code &ec,
244 boost::asio::ssl::context &tls_ctx);
246 } // namespace client
248 } // namespace asio_http2
250 } // namespace nghttp2
252 #endif // ASIO_HTTP2_CLIENT_H