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 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) {
116 auto client = static_cast<Client *>(user_data);
117 auto req_stat = client->get_req_stat(frame->hd.stream_id);
119 client->record_request_time(req_stat);
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);
134 while ((nread = pread(config->data_fd, buf, length, req_stat->data_offset)) ==
140 return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
143 req_stat->data_offset += nread;
145 if (req_stat->data_offset == config->data_length) {
146 *data_flags |= NGHTTP2_DATA_FLAG_EOF;
150 if (req_stat->data_offset > config->data_length || nread == 0) {
151 return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
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;
165 if (wb.rleft() >= BACKOFF_WRITE_BUFFER_THRES) {
166 return NGHTTP2_ERR_WOULDBLOCK;
169 return wb.append(data, length);
173 void Http2Session::on_connect() {
176 // This is required with --disable-assert.
179 nghttp2_session_callbacks *callbacks;
181 nghttp2_session_callbacks_new(&callbacks);
183 auto callbacks_deleter = defer(nghttp2_session_callbacks_del, callbacks);
185 nghttp2_session_callbacks_set_on_frame_recv_callback(callbacks,
186 on_frame_recv_callback);
188 nghttp2_session_callbacks_set_on_data_chunk_recv_callback(
189 callbacks, on_data_chunk_recv_callback);
191 nghttp2_session_callbacks_set_on_stream_close_callback(
192 callbacks, on_stream_close_callback);
194 nghttp2_session_callbacks_set_on_header_callback(callbacks,
197 nghttp2_session_callbacks_set_before_frame_send_callback(
198 callbacks, before_frame_send_callback);
200 nghttp2_session_callbacks_set_send_callback(callbacks, send_callback);
204 rv = nghttp2_option_new(&opt);
207 auto config = client_->worker->config;
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);
214 nghttp2_session_client_new2(&session_, callbacks, client_, opt);
216 nghttp2_option_del(opt);
218 std::array<nghttp2_settings_entry, 3> iv;
220 iv[0].settings_id = NGHTTP2_SETTINGS_ENABLE_PUSH;
222 iv[1].settings_id = NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE;
223 iv[1].value = (1 << config->window_bits) - 1;
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;
231 rv = nghttp2_submit_settings(session_, NGHTTP2_FLAG_NONE, iv.data(), niv);
235 auto connection_window = (1 << config->connection_window_bits) - 1;
236 nghttp2_session_set_local_window_size(session_, NGHTTP2_FLAG_NONE, 0,
239 client_->signal_write();
242 int Http2Session::submit_request() {
243 if (nghttp2_session_check_request_allowed(session_) == 0) {
247 auto config = client_->worker->config;
248 auto &nva = config->nva[client_->reqidx++];
250 if (client_->reqidx == config->nva.size()) {
254 nghttp2_data_provider prd{{0}, file_read_callback};
257 nghttp2_submit_request(session_, nullptr, nva.data(), nva.size(),
258 config->data_fd == -1 ? nullptr : &prd, nullptr);
263 client_->on_request(stream_id);
268 int Http2Session::on_read(const uint8_t *data, size_t len) {
269 auto rv = nghttp2_session_mem_recv(session_, data, len);
274 assert(static_cast<size_t>(rv) == len);
276 if (nghttp2_session_want_read(session_) == 0 &&
277 nghttp2_session_want_write(session_) == 0 && client_->wb.rleft() == 0) {
281 client_->signal_write();
286 int Http2Session::on_write() {
287 auto rv = nghttp2_session_send(session_);
292 if (nghttp2_session_want_read(session_) == 0 &&
293 nghttp2_session_want_write(session_) == 0 && client_->wb.rleft() == 0) {
300 void Http2Session::terminate() {
301 nghttp2_session_terminate_session(session_, NGHTTP2_NO_ERROR);
304 size_t Http2Session::max_concurrent_streams() {
305 return (size_t)client_->worker->config->max_concurrent_streams;
308 } // namespace h2load