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_;
125 // Starts HTTP/2 session by connecting to |host| and |service|
126 // (e.g., "80") using clear text TCP connection.
127 session(boost::asio::io_service &io_service, const std::string &host,
128 const std::string &service);
130 // Starts HTTP/2 session by connecting to |host| and |service|
131 // (e.g., "443") using encrypted SSL/TLS connection.
132 session(boost::asio::io_service &io_service,
133 boost::asio::ssl::context &tls_context, const std::string &host,
134 const std::string &service);
137 session(session &&other) noexcept;
138 session &operator=(session &&other) noexcept;
140 // Sets callback which is invoked after connection is established.
141 void on_connect(connect_cb cb) const;
143 // Sets callback which is invoked there is connection level error
144 // and session is terminated.
145 void on_error(error_cb cb) const;
147 // Shutdowns connection.
148 void shutdown() const;
150 // Returns underlying io_service object.
151 boost::asio::io_service &io_service() const;
153 // Submits request to server using |method| (e.g., "GET"), |uri|
154 // (e.g., "http://localhost/") and optionally additional header
155 // fields. This function returns pointer to request object if it
156 // succeeds, or nullptr and |ec| contains error message.
157 const request *submit(boost::system::error_code &ec,
158 const std::string &method, const std::string &uri,
159 header_map h = header_map{}) const;
161 // Submits request to server using |method| (e.g., "GET"), |uri|
162 // (e.g., "http://localhost/") and optionally additional header
163 // fields. The |data| is request body. This function returns
164 // pointer to request object if it succeeds, or nullptr and |ec|
165 // contains error message.
166 const request *submit(boost::system::error_code &ec,
167 const std::string &method, const std::string &uri,
168 std::string data, header_map h = header_map{}) const;
170 // Submits request to server using |method| (e.g., "GET"), |uri|
171 // (e.g., "http://localhost/") and optionally additional header
172 // fields. The |cb| is used to generate request body. This
173 // function returns pointer to request object if it succeeds, or
174 // nullptr and |ec| contains error message.
175 const request *submit(boost::system::error_code &ec,
176 const std::string &method, const std::string &uri,
177 generator_cb cb, header_map h = header_map{}) const;
180 std::unique_ptr<session_impl> impl_;
183 // configure |tls_ctx| for client use. Currently, we just set NPN
184 // callback for HTTP/2.
185 boost::system::error_code
186 configure_tls_context(boost::system::error_code &ec,
187 boost::asio::ssl::context &tls_ctx);
189 } // namespace client
191 } // namespace asio_http2
193 } // namespace nghttp2
195 #endif // ASIO_HTTP2_CLIENT_H