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