Imported Upstream version 1.0.0
[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 class session_impl;
122
123 class session {
124 public:
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);
129
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);
135   ~session();
136
137   session(session &&other) noexcept;
138   session &operator=(session &&other) noexcept;
139
140   // Sets callback which is invoked after connection is established.
141   void on_connect(connect_cb cb) const;
142
143   // Sets callback which is invoked there is connection level error
144   // and session is terminated.
145   void on_error(error_cb cb) const;
146
147   // Shutdowns connection.
148   void shutdown() const;
149
150   // Returns underlying io_service object.
151   boost::asio::io_service &io_service() const;
152
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;
160
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;
169
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;
178
179 private:
180   std::unique_ptr<session_impl> impl_;
181 };
182
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);
188
189 } // namespace client
190
191 } // namespace asio_http2
192
193 } // namespace nghttp2
194
195 #endif // ASIO_HTTP2_CLIENT_H