2 * nghttp2 - HTTP/2 C Library
4 * Copyright (c) 2014 Tatsuhiro Tsujikawa
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:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
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.
25 #include "h2load_http2_session.h"
35 using namespace nghttp2;
39 Http2Session::Http2Session(Client *client)
40 : client_(client), session_(nullptr) {}
42 Http2Session::~Http2Session() { nghttp2_session_del(session_); }
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,
49 auto client = static_cast<Client *>(user_data);
50 if (frame->hd.type != NGHTTP2_HEADERS ||
51 frame->headers.cat != NGHTTP2_HCAT_RESPONSE) {
54 client->on_header(frame->hd.stream_id, name, namelen, value, valuelen);
55 client->worker->stats.bytes_head_decomp += namelen + valuelen;
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);
61 std::cout.write(reinterpret_cast<const char *>(value), valuelen);
70 int on_frame_recv_callback(nghttp2_session *session, const nghttp2_frame *frame,
72 auto client = static_cast<Client *>(user_data);
73 if (frame->hd.type != NGHTTP2_HEADERS ||
74 frame->headers.cat != NGHTTP2_HCAT_RESPONSE) {
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();
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;
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);
109 int on_frame_not_send_callback(nghttp2_session *session,
110 const nghttp2_frame *frame, int lib_error_code,
112 if (frame->hd.type != NGHTTP2_HEADERS ||
113 frame->headers.cat != NGHTTP2_HCAT_REQUEST) {
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);
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) {
133 auto client = static_cast<Client *>(user_data);
134 auto req_stat = client->get_req_stat(frame->hd.stream_id);
136 client->record_request_time(req_stat);
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);
151 while ((nread = pread(config->data_fd, buf, length, req_stat->data_offset)) ==
157 return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
160 req_stat->data_offset += nread;
162 if (req_stat->data_offset == config->data_length) {
163 *data_flags |= NGHTTP2_DATA_FLAG_EOF;
167 if (req_stat->data_offset > config->data_length || nread == 0) {
168 return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
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;
182 if (wb.rleft() >= BACKOFF_WRITE_BUFFER_THRES) {
183 return NGHTTP2_ERR_WOULDBLOCK;
186 return wb.append(data, length);
190 void Http2Session::on_connect() {
193 // This is required with --disable-assert.
196 nghttp2_session_callbacks *callbacks;
198 nghttp2_session_callbacks_new(&callbacks);
200 auto callbacks_deleter = defer(nghttp2_session_callbacks_del, callbacks);
202 nghttp2_session_callbacks_set_on_frame_recv_callback(callbacks,
203 on_frame_recv_callback);
205 nghttp2_session_callbacks_set_on_data_chunk_recv_callback(
206 callbacks, on_data_chunk_recv_callback);
208 nghttp2_session_callbacks_set_on_stream_close_callback(
209 callbacks, on_stream_close_callback);
211 nghttp2_session_callbacks_set_on_header_callback(callbacks,
214 nghttp2_session_callbacks_set_on_frame_not_send_callback(
215 callbacks, on_frame_not_send_callback);
217 nghttp2_session_callbacks_set_before_frame_send_callback(
218 callbacks, before_frame_send_callback);
220 nghttp2_session_callbacks_set_send_callback(callbacks, send_callback);
224 rv = nghttp2_option_new(&opt);
227 auto config = client_->worker->config;
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);
234 nghttp2_session_client_new2(&session_, callbacks, client_, opt);
236 nghttp2_option_del(opt);
238 std::array<nghttp2_settings_entry, 3> iv;
240 iv[0].settings_id = NGHTTP2_SETTINGS_ENABLE_PUSH;
242 iv[1].settings_id = NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE;
243 iv[1].value = (1 << config->window_bits) - 1;
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;
251 rv = nghttp2_submit_settings(session_, NGHTTP2_FLAG_NONE, iv.data(), niv);
255 auto connection_window = (1 << config->connection_window_bits) - 1;
256 nghttp2_session_set_local_window_size(session_, NGHTTP2_FLAG_NONE, 0,
259 client_->signal_write();
262 int Http2Session::submit_request() {
263 if (nghttp2_session_check_request_allowed(session_) == 0) {
267 auto config = client_->worker->config;
268 auto &nva = config->nva[client_->reqidx++];
270 if (client_->reqidx == config->nva.size()) {
274 nghttp2_data_provider prd{{0}, file_read_callback};
277 nghttp2_submit_request(session_, nullptr, nva.data(), nva.size(),
278 config->data_fd == -1 ? nullptr : &prd, nullptr);
283 client_->on_request(stream_id);
288 int Http2Session::on_read(const uint8_t *data, size_t len) {
289 auto rv = nghttp2_session_mem_recv(session_, data, len);
294 assert(static_cast<size_t>(rv) == len);
296 if (nghttp2_session_want_read(session_) == 0 &&
297 nghttp2_session_want_write(session_) == 0 && client_->wb.rleft() == 0) {
301 client_->signal_write();
306 int Http2Session::on_write() {
307 auto rv = nghttp2_session_send(session_);
312 if (nghttp2_session_want_read(session_) == 0 &&
313 nghttp2_session_want_write(session_) == 0 && client_->wb.rleft() == 0) {
320 void Http2Session::terminate() {
321 nghttp2_session_terminate_session(session_, NGHTTP2_NO_ERROR);
324 size_t Http2Session::max_concurrent_streams() {
325 return (size_t)client_->worker->config->max_concurrent_streams;
328 } // namespace h2load