Apply PIE to nghttpx
[platform/upstream/nghttp2.git] / src / includes / nghttp2 / asio_http2_client.h
1 /*
2  * nghttp2 - HTTP/2 C Library
3  *
4  * Copyright (c) 2015 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_CLIENT_H
26 #define ASIO_HTTP2_CLIENT_H
27
28 #include <nghttp2/asio_http2.h>
29
30 namespace nghttp2 {
31
32 namespace asio_http2 {
33
34 namespace client {
35
36 class response_impl;
37
38 class response {
39 public:
40   // Application must not call this directly.
41   response();
42   ~response();
43
44   // Sets callback which is invoked when chunk of response body is
45   // received.
46   void on_data(data_cb cb) const;
47
48   // Returns status code.
49   int status_code() const;
50
51   // Returns content-length.  -1 if it is unknown.
52   int64_t content_length() const;
53
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;
57
58   // Application must not call this directly.
59   response_impl &impl() const;
60
61 private:
62   std::unique_ptr<response_impl> impl_;
63 };
64
65 class request;
66
67 using response_cb = std::function<void(const response &)>;
68 using request_cb = std::function<void(const request &)>;
69 using connect_cb =
70     std::function<void(boost::asio::ip::tcp::resolver::iterator)>;
71
72 class request_impl;
73
74 class request {
75 public:
76   // Application must not call this directly.
77   request();
78   ~request();
79
80   // Sets callback which is invoked when response header is received.
81   void on_response(response_cb cb) const;
82
83   // Sets callback which is invoked when push request header is
84   // received.
85   void on_push(request_cb cb) const;
86
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;
91
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()
95   // function.
96   void write_trailer(header_map h) const;
97
98   // Cancels this request and response with given error code.
99   void cancel(uint32_t error_code = NGHTTP2_INTERNAL_ERROR) const;
100
101   // Resumes deferred uploading.
102   void resume() const;
103
104   // Returns method (e.g., GET).
105   const std::string &method() const;
106
107   // Returns request URI, split into components.
108   const uri_ref &uri() const;
109
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;
113
114   // Application must not call this directly.
115   request_impl &impl() const;
116
117 private:
118   std::unique_ptr<request_impl> impl_;
119 };
120
121 // Wrapper around an nghttp2_priority_spec.
122 class priority_spec {
123 public:
124   // The default ctor is used only by sentinel values.
125   priority_spec() = default;
126
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);
130
131   // Return a pointer to a valid nghttp2 priority spec, or null.
132   const nghttp2_priority_spec *get() const;
133
134   // Indicates whether or not this spec is valid (i.e. was constructed with
135   // values).
136   bool valid() const;
137
138 private:
139   nghttp2_priority_spec spec_;
140   bool valid_ = false;
141 };
142
143 class session_impl;
144
145 class session {
146 public:
147   // Starts HTTP/2 session by connecting to |host| and |service|
148   // (e.g., "80") using clear text TCP connection with connect timeout
149   // 60 seconds.
150   session(boost::asio::io_service &io_service, const std::string &host,
151           const std::string &service);
152
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);
157
158   // Starts HTTP/2 session by connecting to |host| and |service|
159   // (e.g., "80") using clear text TCP connection with given connect
160   // timeout.
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);
164
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);
170
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);
177
178   // Starts HTTP/2 session by connecting to |host| and |service|
179   // (e.g., "443") using encrypted SSL/TLS connection with given
180   // connect timeout.
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);
185
186   ~session();
187
188   session(session &&other) noexcept;
189   session &operator=(session &&other) noexcept;
190
191   // Sets callback which is invoked after connection is established.
192   void on_connect(connect_cb cb) const;
193
194   // Sets callback which is invoked there is connection level error
195   // and session is terminated.
196   void on_error(error_cb cb) const;
197
198   // Sets read timeout, which defaults to 60 seconds.
199   void read_timeout(const boost::posix_time::time_duration &t);
200
201   // Shutdowns connection.
202   void shutdown() const;
203
204   // Returns underlying io_service object.
205   boost::asio::io_service &io_service() const;
206
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;
215
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;
225
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;
235
236 private:
237   std::shared_ptr<session_impl> impl_;
238 };
239
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);
245
246 } // namespace client
247
248 } // namespace asio_http2
249
250 } // namespace nghttp2
251
252 #endif // ASIO_HTTP2_CLIENT_H