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"
34 using namespace nghttp2;
38 Http2Session::Http2Session(Client *client)
39 : client_(client), session_(nullptr) {}
41 Http2Session::~Http2Session() { nghttp2_session_del(session_); }
44 int on_header_callback(nghttp2_session *session, const nghttp2_frame *frame,
45 const uint8_t *name, size_t namelen,
46 const uint8_t *value, size_t valuelen, uint8_t flags,
48 auto client = static_cast<Client *>(user_data);
49 if (frame->hd.type != NGHTTP2_HEADERS ||
50 frame->headers.cat != NGHTTP2_HCAT_RESPONSE) {
53 client->on_header(frame->hd.stream_id, name, namelen, value, valuelen);
59 int on_frame_recv_callback(nghttp2_session *session, const nghttp2_frame *frame,
61 auto client = static_cast<Client *>(user_data);
62 if (frame->hd.type != NGHTTP2_HEADERS ||
63 frame->headers.cat != NGHTTP2_HCAT_RESPONSE) {
66 client->worker->stats.bytes_head += frame->hd.length;
72 int on_data_chunk_recv_callback(nghttp2_session *session, uint8_t flags,
73 int32_t stream_id, const uint8_t *data,
74 size_t len, void *user_data) {
75 auto client = static_cast<Client *>(user_data);
76 client->worker->stats.bytes_body += len;
82 int on_stream_close_callback(nghttp2_session *session, int32_t stream_id,
83 uint32_t error_code, void *user_data) {
84 auto client = static_cast<Client *>(user_data);
85 auto req_stat = static_cast<RequestStat *>(
86 nghttp2_session_get_stream_user_data(session, stream_id));
90 client->on_stream_close(stream_id, error_code == NGHTTP2_NO_ERROR, req_stat);
96 int before_frame_send_callback(nghttp2_session *session,
97 const nghttp2_frame *frame, void *user_data) {
98 if (frame->hd.type != NGHTTP2_HEADERS ||
99 frame->headers.cat != NGHTTP2_HCAT_REQUEST) {
103 auto client = static_cast<Client *>(user_data);
104 client->on_request(frame->hd.stream_id);
105 auto req_stat = static_cast<RequestStat *>(
106 nghttp2_session_get_stream_user_data(session, frame->hd.stream_id));
108 client->record_request_time(req_stat);
115 ssize_t file_read_callback(nghttp2_session *session, int32_t stream_id,
116 uint8_t *buf, size_t length, uint32_t *data_flags,
117 nghttp2_data_source *source, void *user_data) {
118 auto client = static_cast<Client *>(user_data);
119 auto config = client->worker->config;
120 auto req_stat = static_cast<RequestStat *>(
121 nghttp2_session_get_stream_user_data(session, stream_id));
124 while ((nread = pread(config->data_fd, buf, length, req_stat->data_offset)) ==
130 return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
133 req_stat->data_offset += nread;
135 if (nread == 0 || req_stat->data_offset == config->data_length) {
136 *data_flags |= NGHTTP2_DATA_FLAG_EOF;
145 ssize_t send_callback(nghttp2_session *session, const uint8_t *data,
146 size_t length, int flags, void *user_data) {
147 auto client = static_cast<Client *>(user_data);
148 auto &wb = client->wb;
150 if (wb.wleft() == 0) {
151 return NGHTTP2_ERR_WOULDBLOCK;
154 return wb.write(data, length);
158 void Http2Session::on_connect() {
161 nghttp2_session_callbacks *callbacks;
163 nghttp2_session_callbacks_new(&callbacks);
165 auto callbacks_deleter = defer(nghttp2_session_callbacks_del, callbacks);
167 nghttp2_session_callbacks_set_on_frame_recv_callback(callbacks,
168 on_frame_recv_callback);
170 nghttp2_session_callbacks_set_on_data_chunk_recv_callback(
171 callbacks, on_data_chunk_recv_callback);
173 nghttp2_session_callbacks_set_on_stream_close_callback(
174 callbacks, on_stream_close_callback);
176 nghttp2_session_callbacks_set_on_header_callback(callbacks,
179 nghttp2_session_callbacks_set_before_frame_send_callback(
180 callbacks, before_frame_send_callback);
182 nghttp2_session_callbacks_set_send_callback(callbacks, send_callback);
184 nghttp2_session_client_new(&session_, callbacks, client_);
186 std::array<nghttp2_settings_entry, 2> iv;
187 iv[0].settings_id = NGHTTP2_SETTINGS_ENABLE_PUSH;
189 iv[1].settings_id = NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE;
190 iv[1].value = (1 << client_->worker->config->window_bits) - 1;
192 rv = nghttp2_submit_settings(session_, NGHTTP2_FLAG_NONE, iv.data(),
197 auto extra_connection_window =
198 (1 << client_->worker->config->connection_window_bits) - 1 -
199 NGHTTP2_INITIAL_CONNECTION_WINDOW_SIZE;
200 if (extra_connection_window != 0) {
201 nghttp2_submit_window_update(session_, NGHTTP2_FLAG_NONE, 0,
202 extra_connection_window);
205 client_->signal_write();
208 void Http2Session::submit_request(RequestStat *req_stat) {
209 auto config = client_->worker->config;
210 auto &nva = config->nva[client_->reqidx++];
212 if (client_->reqidx == config->nva.size()) {
216 nghttp2_data_provider prd{{0}, file_read_callback};
219 nghttp2_submit_request(session_, nullptr, nva.data(), nva.size(),
220 config->data_fd == -1 ? nullptr : &prd, req_stat);
221 assert(stream_id > 0);
224 int Http2Session::on_read(const uint8_t *data, size_t len) {
225 auto rv = nghttp2_session_mem_recv(session_, data, len);
230 assert(static_cast<size_t>(rv) == len);
232 if (nghttp2_session_want_read(session_) == 0 &&
233 nghttp2_session_want_write(session_) == 0 && client_->wb.rleft() == 0) {
237 client_->signal_write();
242 int Http2Session::on_write() {
243 auto rv = nghttp2_session_send(session_);
248 if (nghttp2_session_want_read(session_) == 0 &&
249 nghttp2_session_want_write(session_) == 0 && client_->wb.rleft() == 0) {
256 void Http2Session::terminate() {
257 nghttp2_session_terminate_session(session_, NGHTTP2_NO_ERROR);
260 } // namespace h2load