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 {
on_frame_not_send_callback(nghttp2_session * session,const nghttp2_frame * frame,int lib_error_code,void * user_data)109 int on_frame_not_send_callback(nghttp2_session *session,
110 const nghttp2_frame *frame, int lib_error_code,
111 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 // request was not sent. Mark it as error.
119 client->on_stream_close(frame->hd.stream_id, false);
120
121 return 0;
122 }
123 } // namespace
124
125 namespace {
before_frame_send_callback(nghttp2_session * session,const nghttp2_frame * frame,void * user_data)126 int before_frame_send_callback(nghttp2_session *session,
127 const nghttp2_frame *frame, void *user_data) {
128 if (frame->hd.type != NGHTTP2_HEADERS ||
129 frame->headers.cat != NGHTTP2_HCAT_REQUEST) {
130 return 0;
131 }
132
133 auto client = static_cast<Client *>(user_data);
134 auto req_stat = client->get_req_stat(frame->hd.stream_id);
135 assert(req_stat);
136 client->record_request_time(req_stat);
137
138 return 0;
139 }
140 } // namespace
141
142 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)143 ssize_t file_read_callback(nghttp2_session *session, int32_t stream_id,
144 uint8_t *buf, size_t length, uint32_t *data_flags,
145 nghttp2_data_source *source, void *user_data) {
146 auto client = static_cast<Client *>(user_data);
147 auto config = client->worker->config;
148 auto req_stat = client->get_req_stat(stream_id);
149 assert(req_stat);
150 ssize_t nread;
151 while ((nread = pread(config->data_fd, buf, length, req_stat->data_offset)) ==
152 -1 &&
153 errno == EINTR)
154 ;
155
156 if (nread == -1) {
157 return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
158 }
159
160 req_stat->data_offset += nread;
161
162 if (req_stat->data_offset == config->data_length) {
163 *data_flags |= NGHTTP2_DATA_FLAG_EOF;
164 return nread;
165 }
166
167 if (req_stat->data_offset > config->data_length || nread == 0) {
168 return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
169 }
170
171 return nread;
172 }
173
174 } // namespace
175
176 namespace {
send_callback(nghttp2_session * session,const uint8_t * data,size_t length,int flags,void * user_data)177 ssize_t send_callback(nghttp2_session *session, const uint8_t *data,
178 size_t length, int flags, void *user_data) {
179 auto client = static_cast<Client *>(user_data);
180 auto &wb = client->wb;
181
182 if (wb.rleft() >= BACKOFF_WRITE_BUFFER_THRES) {
183 return NGHTTP2_ERR_WOULDBLOCK;
184 }
185
186 return wb.append(data, length);
187 }
188 } // namespace
189
on_connect()190 void Http2Session::on_connect() {
191 int rv;
192
193 // This is required with --disable-assert.
194 (void)rv;
195
196 nghttp2_session_callbacks *callbacks;
197
198 nghttp2_session_callbacks_new(&callbacks);
199
200 auto callbacks_deleter = defer(nghttp2_session_callbacks_del, callbacks);
201
202 nghttp2_session_callbacks_set_on_frame_recv_callback(callbacks,
203 on_frame_recv_callback);
204
205 nghttp2_session_callbacks_set_on_data_chunk_recv_callback(
206 callbacks, on_data_chunk_recv_callback);
207
208 nghttp2_session_callbacks_set_on_stream_close_callback(
209 callbacks, on_stream_close_callback);
210
211 nghttp2_session_callbacks_set_on_header_callback(callbacks,
212 on_header_callback);
213
214 nghttp2_session_callbacks_set_on_frame_not_send_callback(
215 callbacks, on_frame_not_send_callback);
216
217 nghttp2_session_callbacks_set_before_frame_send_callback(
218 callbacks, before_frame_send_callback);
219
220 nghttp2_session_callbacks_set_send_callback(callbacks, send_callback);
221
222 nghttp2_option *opt;
223
224 rv = nghttp2_option_new(&opt);
225 assert(rv == 0);
226
227 auto config = client_->worker->config;
228
229 if (config->encoder_header_table_size != NGHTTP2_DEFAULT_HEADER_TABLE_SIZE) {
230 nghttp2_option_set_max_deflate_dynamic_table_size(
231 opt, config->encoder_header_table_size);
232 }
233
234 nghttp2_session_client_new2(&session_, callbacks, client_, opt);
235
236 nghttp2_option_del(opt);
237
238 std::array<nghttp2_settings_entry, 3> iv;
239 size_t niv = 2;
240 iv[0].settings_id = NGHTTP2_SETTINGS_ENABLE_PUSH;
241 iv[0].value = 0;
242 iv[1].settings_id = NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE;
243 iv[1].value = (1 << config->window_bits) - 1;
244
245 if (config->header_table_size != NGHTTP2_DEFAULT_HEADER_TABLE_SIZE) {
246 iv[niv].settings_id = NGHTTP2_SETTINGS_HEADER_TABLE_SIZE;
247 iv[niv].value = config->header_table_size;
248 ++niv;
249 }
250
251 rv = nghttp2_submit_settings(session_, NGHTTP2_FLAG_NONE, iv.data(), niv);
252
253 assert(rv == 0);
254
255 auto connection_window = (1 << config->connection_window_bits) - 1;
256 nghttp2_session_set_local_window_size(session_, NGHTTP2_FLAG_NONE, 0,
257 connection_window);
258
259 client_->signal_write();
260 }
261
submit_request()262 int Http2Session::submit_request() {
263 if (nghttp2_session_check_request_allowed(session_) == 0) {
264 return -1;
265 }
266
267 auto config = client_->worker->config;
268 auto &nva = config->nva[client_->reqidx++];
269
270 if (client_->reqidx == config->nva.size()) {
271 client_->reqidx = 0;
272 }
273
274 nghttp2_data_provider prd{{0}, file_read_callback};
275
276 auto stream_id =
277 nghttp2_submit_request(session_, nullptr, nva.data(), nva.size(),
278 config->data_fd == -1 ? nullptr : &prd, nullptr);
279 if (stream_id < 0) {
280 return -1;
281 }
282
283 client_->on_request(stream_id);
284
285 return 0;
286 }
287
on_read(const uint8_t * data,size_t len)288 int Http2Session::on_read(const uint8_t *data, size_t len) {
289 auto rv = nghttp2_session_mem_recv(session_, data, len);
290 if (rv < 0) {
291 return -1;
292 }
293
294 assert(static_cast<size_t>(rv) == len);
295
296 if (nghttp2_session_want_read(session_) == 0 &&
297 nghttp2_session_want_write(session_) == 0 && client_->wb.rleft() == 0) {
298 return -1;
299 }
300
301 client_->signal_write();
302
303 return 0;
304 }
305
on_write()306 int Http2Session::on_write() {
307 auto rv = nghttp2_session_send(session_);
308 if (rv != 0) {
309 return -1;
310 }
311
312 if (nghttp2_session_want_read(session_) == 0 &&
313 nghttp2_session_want_write(session_) == 0 && client_->wb.rleft() == 0) {
314 return -1;
315 }
316
317 return 0;
318 }
319
terminate()320 void Http2Session::terminate() {
321 nghttp2_session_terminate_session(session_, NGHTTP2_NO_ERROR);
322 }
323
max_concurrent_streams()324 size_t Http2Session::max_concurrent_streams() {
325 return (size_t)client_->worker->config->max_concurrent_streams;
326 }
327
328 } // namespace h2load
329