-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
Copy pathasio_http2_client.h
241 lines (188 loc) · 8.12 KB
/
asio_http2_client.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
* nghttp2 - HTTP/2 C Library
*
* Copyright (c) 2015 Tatsuhiro Tsujikawa
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef ASIO_HTTP2_CLIENT_H
#define ASIO_HTTP2_CLIENT_H
#include <nghttp2/asio_http2.h>
namespace nghttp2 {
namespace asio_http2 {
namespace client {
class response_impl;
class response {
public:
// Application must not call this directly.
response();
~response();
// Sets callback which is invoked when chunk of response body is
// received.
void on_data(data_cb cb) const;
// Returns status code.
int status_code() const;
// Returns content-length. -1 if it is unknown.
int64_t content_length() const;
// Returns the response header fields. The pusedo header fields,
// which start with colon (:), are exluced from this list.
const header_map &header() const;
// Application must not call this directly.
response_impl &impl() const;
private:
std::unique_ptr<response_impl> impl_;
};
class request;
using response_cb = std::function<void(const response &)>;
using request_cb = std::function<void(const request &)>;
using connect_cb =
std::function<void(boost::asio::ip::tcp::resolver::iterator)>;
class request_impl;
class request {
public:
// Application must not call this directly.
request();
~request();
// Sets callback which is invoked when response header is received.
void on_response(response_cb cb) const;
// Sets callback which is invoked when push request header is
// received.
void on_push(request_cb cb) const;
// Sets callback which is invoked when this request and response are
// finished. After the invocation of this callback, the application
// must not access request and response object.
void on_close(close_cb cb) const;
// Write trailer part. This must be called after setting both
// NGHTTP2_DATA_FLAG_EOF and NGHTTP2_DATA_FLAG_NO_END_STREAM set in
// *data_flag parameter in generator_cb passed to session::submit()
// function.
void write_trailer(header_map h) const;
// Cancels this request and response with given error code.
void cancel(uint32_t error_code = NGHTTP2_INTERNAL_ERROR) const;
// Resumes deferred uploading.
void resume() const;
// Returns method (e.g., GET).
const std::string &method() const;
// Returns request URI, split into components.
const uri_ref &uri() const;
// Returns request header fields. The pusedo header fields, which
// start with colon (:), are exluced from this list.
const header_map &header() const;
// Application must not call this directly.
request_impl &impl() const;
private:
std::unique_ptr<request_impl> impl_;
};
// Wrapper around an nghttp2_priority_spec.
class priority_spec {
public:
// The default ctor is used only by sentinel values.
priority_spec() = default;
// Create a priority spec with the given priority settings.
explicit priority_spec(const int32_t stream_id, const int32_t weight,
const bool exclusive = false);
// Return a pointer to a valid nghttp2 priority spec, or null.
const nghttp2_priority_spec *get() const;
// Indicates whether or not this spec is valid (i.e. was constructed with
// values).
const bool valid() const;
private:
nghttp2_priority_spec spec_;
bool valid_ = false;
};
class session_impl;
class session {
public:
// Starts HTTP/2 session by connecting to |host| and |service|
// (e.g., "80") using clear text TCP connection with connect timeout
// 60 seconds.
session(boost::asio::io_service &io_service, const std::string &host,
const std::string &service);
// Starts HTTP/2 session by connecting to |host| and |service|
// (e.g., "80") using clear text TCP connection with given connect
// timeout.
session(boost::asio::io_service &io_service, const std::string &host,
const std::string &service,
const boost::posix_time::time_duration &connect_timeout);
// Starts HTTP/2 session by connecting to |host| and |service|
// (e.g., "443") using encrypted SSL/TLS connection with connect
// timeout 60 seconds.
session(boost::asio::io_service &io_service,
boost::asio::ssl::context &tls_context, const std::string &host,
const std::string &service);
// Starts HTTP/2 session by connecting to |host| and |service|
// (e.g., "443") using encrypted SSL/TLS connection with given
// connect timeout.
session(boost::asio::io_service &io_service,
boost::asio::ssl::context &tls_context, const std::string &host,
const std::string &service,
const boost::posix_time::time_duration &connect_timeout);
~session();
session(session &&other) noexcept;
session &operator=(session &&other) noexcept;
// Sets callback which is invoked after connection is established.
void on_connect(connect_cb cb) const;
// Sets callback which is invoked there is connection level error
// and session is terminated.
void on_error(error_cb cb) const;
// Sets read timeout, which defaults to 60 seconds.
void read_timeout(const boost::posix_time::time_duration &t);
// Shutdowns connection.
void shutdown() const;
// Returns underlying io_service object.
boost::asio::io_service &io_service() const;
// Submits request to server using |method| (e.g., "GET"), |uri|
// (e.g., "http://localhost/") and optionally additional header
// fields. This function returns pointer to request object if it
// succeeds, or nullptr and |ec| contains error message.
const request *submit(boost::system::error_code &ec,
const std::string &method, const std::string &uri,
header_map h = header_map{},
priority_spec prio = priority_spec()) const;
// Submits request to server using |method| (e.g., "GET"), |uri|
// (e.g., "http://localhost/") and optionally additional header
// fields. The |data| is request body. This function returns
// pointer to request object if it succeeds, or nullptr and |ec|
// contains error message.
const request *submit(boost::system::error_code &ec,
const std::string &method, const std::string &uri,
std::string data, header_map h = header_map{},
priority_spec prio = priority_spec()) const;
// Submits request to server using |method| (e.g., "GET"), |uri|
// (e.g., "http://localhost/") and optionally additional header
// fields. The |cb| is used to generate request body. This
// function returns pointer to request object if it succeeds, or
// nullptr and |ec| contains error message.
const request *submit(boost::system::error_code &ec,
const std::string &method, const std::string &uri,
generator_cb cb, header_map h = header_map{},
priority_spec prio = priority_spec()) const;
private:
std::shared_ptr<session_impl> impl_;
};
// configure |tls_ctx| for client use. Currently, we just set NPN
// callback for HTTP/2.
boost::system::error_code
configure_tls_context(boost::system::error_code &ec,
boost::asio::ssl::context &tls_ctx);
} // namespace client
} // namespace asio_http2
} // namespace nghttp2
#endif // ASIO_HTTP2_CLIENT_H