1 /*
2 * nghttp2 - HTTP/2 C Library
3 *
4 * Copyright (c) 2014 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 #include "h2load_http2_session.h"
26
27 #include <cassert>
28 #include <cerrno>
29 #include <iostream>
30
31 #include "h2load.h"
32 #include "util.h"
33 #include "template.h"
34
35 using namespace nghttp2;
36
37 namespace h2load {
38
Http2Session(Client * client)39 Http2Session::Http2Session(Client *client)
40 : client_(client), session_(nullptr) {}
41
~Http2Session()42 Http2Session::~Http2Session() { nghttp2_session_del(session_); }
43
44 namespace {
on_header_callback(nghttp2_session * session,const nghttp2_frame * frame,const uint8_t * name,size_t namelen,const uint8_t * value,size_t valuelen,uint8_t flags,void * user_data)45 int on_header_callback(nghttp2_session *session, const nghttp2_frame *frame,
46 const uint8_t *name, size_t namelen,
47 const uint8_t *value, size_t valuelen, uint8_t flags,
48 void *user_data) {
49 auto client = static_cast<Client *>(user_data);
50 if (frame->hd.type != NGHTTP2_HEADERS ||
51 frame->headers.cat != NGHTTP2_HCAT_RESPONSE) {
52 return 0;
53 }
54 client->on_header(frame->hd.stream_id, name, namelen, value, valuelen);
55 client->worker->stats.bytes_head_decomp += namelen + valuelen;
56
57 if (client->worker->config->verbose) {
58 std::cout << "[stream_id=" << frame->hd.stream_id << "] ";
59 std::cout.write(reinterpret_cast<const char *>(name), namelen);
60 std::cout << ": ";
61 std::cout.write(reinterpret_cast<const char *>(value), valuelen);
62 std::cout << "\n";
63 }
64
65 return 0;
66 }
67 } // namespace
68
69 namespace {
on_frame_recv_callback(nghttp2_session * session,const nghttp2_frame * frame,void * user_data)70 int on_frame_recv_callback(nghttp2_session *session, const nghttp2_frame *frame,
71 void *user_data) {
72 auto client = static_cast<Client *>(user_data);
73 if (frame->hd.type != NGHTTP2_HEADERS ||
74 frame->headers.cat != NGHTTP2_HCAT_RESPONSE) {
75 return 0;
76 }
77 client->worker->stats.bytes_head +=
78 frame->hd.length - frame->headers.padlen -
79 ((frame->hd.flags & NGHTTP2_FLAG_PRIORITY) ? 5 : 0);
80 if (frame->hd.flags & NGHTTP2_FLAG_END_STREAM) {
81 client->record_ttfb();
82 }
83 return 0;
84 }
85 } // namespace
86
87 namespace {
on_data_chunk_recv_callback(nghttp2_session * session,uint8_t flags,int32_t stream_id,const uint8_t * data,size_t len,void * user_data)88 int on_data_chunk_recv_callback(nghttp2_session *session, uint8_t flags,
89 int32_t stream_id, const uint8_t *data,
90 size_t len, void *user_data) {
91 auto client = static_cast<Client *>(user_data);
92 client->record_ttfb();
93 client->worker->stats.bytes_body += len;
94 return 0;
95 }
96 } // namespace
97
98 namespace {
on_stream_close_callback(nghttp2_session * session,int32_t stream_id,uint32_t error_code,void * user_data)99 int on_stream_close_callback(nghttp2_session *session, int32_t stream_id,
100 uint32_t error_code, void *user_data) {
101 auto client = static_cast<Client *>(user_data);
102 client->on_stream_close(stream_id, error_code == NGHTTP2_NO_ERROR);
103
104 return 0;
105 }
106 } // namespace
107
108 namespace {
before_frame_send_callback(nghttp2_session * session,const nghttp2_frame * frame,void * user_data)109 int before_frame_send_callback(nghttp2_session *session,
110 const nghttp2_frame *frame, void *user_data) {
111 if (frame->hd.type != NGHTTP2_HEADERS ||
112 frame->headers.cat != NGHTTP2_HCAT_REQUEST) {
113 return 0;
114 }
115
116 auto client = static_cast<Client *>(user_data);
117 auto req_stat = client->get_req_stat(frame->hd.stream_id);
118 assert(req_stat);
119 client->record_request_time(req_stat);
120
121 return 0;
122 }
123 } // namespace
124
125 namespace {
file_read_callback(nghttp2_session * session,int32_t stream_id,uint8_t * buf,size_t length,uint32_t * data_flags,nghttp2_data_source * source,void * user_data)126 ssize_t file_read_callback(nghttp2_session *session, int32_t stream_id,
127 uint8_t *buf, size_t length, uint32_t *data_flags,
128 nghttp2_data_source *source, void *user_data) {
129 auto client = static_cast<Client *>(user_data);
130 auto config = client->worker->config;
131 auto req_stat = client->get_req_stat(stream_id);
132 assert(req_stat);
133 ssize_t nread;
134 while ((nread = pread(config->data_fd, buf, length, req_stat->data_offset)) ==
135 -1 &&
136 errno == EINTR)
137 ;
138
139 if (nread == -1) {
140 return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
141 }
142
143 req_stat->data_offset += nread;
144
145 if (req_stat->data_offset == config->data_length) {
146 *data_flags |= NGHTTP2_DATA_FLAG_EOF;
147 return nread;
148 }
149
150 if (req_stat->data_offset > config->data_length || nread == 0) {
151 return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
152 }
153
154 return nread;
155 }
156
157 } // namespace
158
159 namespace {
send_callback(nghttp2_session * session,const uint8_t * data,size_t length,int flags,void * user_data)160 ssize_t send_callback(nghttp2_session *session, const uint8_t *data,
161 size_t length, int flags, void *user_data) {
162 auto client = static_cast<Client *>(user_data);
163 auto &wb = client->wb;
164
165 if (wb.rleft() >= BACKOFF_WRITE_BUFFER_THRES) {
166 return NGHTTP2_ERR_WOULDBLOCK;
167 }
168
169 return wb.append(data, length);
170 }
171 } // namespace
172
on_connect()173 void Http2Session::on_connect() {
174 int rv;
175
176 // This is required with --disable-assert.
177 (void)rv;
178
179 nghttp2_session_callbacks *callbacks;
180
181 nghttp2_session_callbacks_new(&callbacks);
182
183 auto callbacks_deleter = defer(nghttp2_session_callbacks_del, callbacks);
184
185 nghttp2_session_callbacks_set_on_frame_recv_callback(callbacks,
186 on_frame_recv_callback);
187
188 nghttp2_session_callbacks_set_on_data_chunk_recv_callback(
189 callbacks, on_data_chunk_recv_callback);
190
191 nghttp2_session_callbacks_set_on_stream_close_callback(
192 callbacks, on_stream_close_callback);
193
194 nghttp2_session_callbacks_set_on_header_callback(callbacks,
195 on_header_callback);
196
197 nghttp2_session_callbacks_set_before_frame_send_callback(
198 callbacks, before_frame_send_callback);
199
200 nghttp2_session_callbacks_set_send_callback(callbacks, send_callback);
201
202 nghttp2_option *opt;
203
204 rv = nghttp2_option_new(&opt);
205 assert(rv == 0);
206
207 auto config = client_->worker->config;
208
209 if (config->encoder_header_table_size != NGHTTP2_DEFAULT_HEADER_TABLE_SIZE) {
210 nghttp2_option_set_max_deflate_dynamic_table_size(
211 opt, config->encoder_header_table_size);
212 }
213
214 nghttp2_session_client_new2(&session_, callbacks, client_, opt);
215
216 nghttp2_option_del(opt);
217
218 std::array<nghttp2_settings_entry, 4> iv;
219 size_t niv = 2;
220 iv[0].settings_id = NGHTTP2_SETTINGS_ENABLE_PUSH;
221 iv[0].value = 0;
222 iv[1].settings_id = NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE;
223 iv[1].value = (1 << config->window_bits) - 1;
224
225 if (config->header_table_size != NGHTTP2_DEFAULT_HEADER_TABLE_SIZE) {
226 iv[niv].settings_id = NGHTTP2_SETTINGS_HEADER_TABLE_SIZE;
227 iv[niv].value = config->header_table_size;
228 ++niv;
229 }
230 if (config->max_frame_size != 16_k) {
231 iv[niv].settings_id = NGHTTP2_SETTINGS_MAX_FRAME_SIZE;
232 iv[niv].value = config->max_frame_size;
233 ++niv;
234 }
235
236 rv = nghttp2_submit_settings(session_, NGHTTP2_FLAG_NONE, iv.data(), niv);
237
238 assert(rv == 0);
239
240 auto connection_window = (1 << config->connection_window_bits) - 1;
241 nghttp2_session_set_local_window_size(session_, NGHTTP2_FLAG_NONE, 0,
242 connection_window);
243
244 client_->signal_write();
245 }
246
submit_request()247 int Http2Session::submit_request() {
248 if (nghttp2_session_check_request_allowed(session_) == 0) {
249 return -1;
250 }
251
252 auto config = client_->worker->config;
253 auto &nva = config->nva[client_->reqidx++];
254
255 if (client_->reqidx == config->nva.size()) {
256 client_->reqidx = 0;
257 }
258
259 nghttp2_data_provider prd{{0}, file_read_callback};
260
261 auto stream_id =
262 nghttp2_submit_request(session_, nullptr, nva.data(), nva.size(),
263 config->data_fd == -1 ? nullptr : &prd, nullptr);
264 if (stream_id < 0) {
265 return -1;
266 }
267
268 client_->on_request(stream_id);
269
270 return 0;
271 }
272
on_read(const uint8_t * data,size_t len)273 int Http2Session::on_read(const uint8_t *data, size_t len) {
274 auto rv = nghttp2_session_mem_recv(session_, data, len);
275 if (rv < 0) {
276 return -1;
277 }
278
279 assert(static_cast<size_t>(rv) == len);
280
281 if (nghttp2_session_want_read(session_) == 0 &&
282 nghttp2_session_want_write(session_) == 0 && client_->wb.rleft() == 0) {
283 return -1;
284 }
285
286 client_->signal_write();
287
288 return 0;
289 }
290
on_write()291 int Http2Session::on_write() {
292 auto rv = nghttp2_session_send(session_);
293 if (rv != 0) {
294 return -1;
295 }
296
297 if (nghttp2_session_want_read(session_) == 0 &&
298 nghttp2_session_want_write(session_) == 0 && client_->wb.rleft() == 0) {
299 return -1;
300 }
301
302 return 0;
303 }
304
terminate()305 void Http2Session::terminate() {
306 nghttp2_session_terminate_session(session_, NGHTTP2_NO_ERROR);
307 }
308
max_concurrent_streams()309 size_t Http2Session::max_concurrent_streams() {
310 return (size_t)client_->worker->config->max_concurrent_streams;
311 }
312
313 } // namespace h2load
314