summaryrefslogtreecommitdiffstats
path: root/src/h2load_http1_session.cc
blob: 6bdcd0471e62bee465ed68387f5831a3a32f413c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
 * nghttp2 - HTTP/2 C Library
 *
 * Copyright (c) 2015 British Broadcasting Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#include "h2load_http1_session.h"

#include <cassert>
#include <cerrno>

#include "h2load.h"
#include "util.h"
#include "template.h"

#include <iostream>
#include <fstream>

using namespace nghttp2;

namespace h2load {

namespace {
// HTTP response message begin
int htp_msg_begincb(llhttp_t *htp) {
  auto session = static_cast<Http1Session *>(htp->data);

  if (session->stream_resp_counter_ > session->stream_req_counter_) {
    return -1;
  }

  return 0;
}
} // namespace

namespace {
// HTTP response status code
int htp_statuscb(llhttp_t *htp, const char *at, size_t length) {
  auto session = static_cast<Http1Session *>(htp->data);
  auto client = session->get_client();

  if (htp->status_code / 100 == 1) {
    return 0;
  }

  client->on_status_code(session->stream_resp_counter_, htp->status_code);

  return 0;
}
} // namespace

namespace {
// HTTP response message complete
int htp_msg_completecb(llhttp_t *htp) {
  auto session = static_cast<Http1Session *>(htp->data);
  auto client = session->get_client();

  if (htp->status_code / 100 == 1) {
    return 0;
  }

  client->final = llhttp_should_keep_alive(htp) == 0;
  auto req_stat = client->get_req_stat(session->stream_resp_counter_);

  assert(req_stat);

  auto config = client->worker->config;
  if (req_stat->data_offset >= config->data_length) {
    client->on_stream_close(session->stream_resp_counter_, true, client->final);
  }

  session->stream_resp_counter_ += 2;

  if (client->final) {
    session->stream_req_counter_ = session->stream_resp_counter_;

    // Connection is going down.  If we have still request to do,
    // create new connection and keep on doing the job.
    if (client->req_left) {
      client->try_new_connection();
    }

    return HPE_PAUSED;
  }

  return 0;
}
} // namespace

namespace {
int htp_hdr_keycb(llhttp_t *htp, const char *data, size_t len) {
  auto session = static_cast<Http1Session *>(htp->data);
  auto client = session->get_client();

  client->worker->stats.bytes_head += len;
  client->worker->stats.bytes_head_decomp += len;
  return 0;
}
} // namespace

namespace {
int htp_hdr_valcb(llhttp_t *htp, const char *data, size_t len) {
  auto session = static_cast<Http1Session *>(htp->data);
  auto client = session->get_client();

  client->worker->stats.bytes_head += len;
  client->worker->stats.bytes_head_decomp += len;
  return 0;
}
} // namespace

namespace {
int htp_hdrs_completecb(llhttp_t *htp) {
  return !http2::expect_response_body(htp->status_code);
}
} // namespace

namespace {
int htp_body_cb(llhttp_t *htp, const char *data, size_t len) {
  auto session = static_cast<Http1Session *>(htp->data);
  auto client = session->get_client();

  client->record_ttfb();
  client->worker->stats.bytes_body += len;

  return 0;
}
} // namespace

namespace {
constexpr llhttp_settings_t htp_hooks = {
    htp_msg_begincb,     // llhttp_cb      on_message_begin;
    nullptr,             // llhttp_data_cb on_url;
    htp_statuscb,        // llhttp_data_cb on_status;
    nullptr,             // llhttp_data_cb on_method;
    nullptr,             // llhttp_data_cb on_version;
    htp_hdr_keycb,       // llhttp_data_cb on_header_field;
    htp_hdr_valcb,       // llhttp_data_cb on_header_value;
    nullptr,             // llhttp_data_cb on_chunk_extension_name;
    nullptr,             // llhttp_data_cb on_chunk_extension_value;
    htp_hdrs_completecb, // llhttp_cb      on_headers_complete;
    htp_body_cb,         // llhttp_data_cb on_body;
    htp_msg_completecb,  // llhttp_cb      on_message_complete;
    nullptr,             // llhttp_cb      on_url_complete;
    nullptr,             // llhttp_cb      on_status_complete;
    nullptr,             // llhttp_cb      on_method_complete;
    nullptr,             // llhttp_cb      on_version_complete;
    nullptr,             // llhttp_cb      on_header_field_complete;
    nullptr,             // llhttp_cb      on_header_value_complete;
    nullptr,             // llhttp_cb      on_chunk_extension_name_complete;
    nullptr,             // llhttp_cb      on_chunk_extension_value_complete;
    nullptr,             // llhttp_cb      on_chunk_header;
    nullptr,             // llhttp_cb      on_chunk_complete;
    nullptr,             // llhttp_cb      on_reset;
};
} // namespace

Http1Session::Http1Session(Client *client)
    : stream_req_counter_(1),
      stream_resp_counter_(1),
      client_(client),
      htp_(),
      complete_(false) {
  llhttp_init(&htp_, HTTP_RESPONSE, &htp_hooks);
  htp_.data = this;
}

Http1Session::~Http1Session() {}

void Http1Session::on_connect() { client_->signal_write(); }

int Http1Session::submit_request() {
  auto config = client_->worker->config;
  const auto &req = config->h1reqs[client_->reqidx];
  client_->reqidx++;

  if (client_->reqidx == config->h1reqs.size()) {
    client_->reqidx = 0;
  }

  client_->on_request(stream_req_counter_);

  auto req_stat = client_->get_req_stat(stream_req_counter_);

  client_->record_request_time(req_stat);
  client_->wb.append(req);

  if (config->data_fd == -1 || config->data_length == 0) {
    // increment for next request
    stream_req_counter_ += 2;

    return 0;
  }

  return on_write();
}

int Http1Session::on_read(const uint8_t *data, size_t len) {
  auto htperr =
      llhttp_execute(&htp_, reinterpret_cast<const char *>(data), len);
  auto nread = htperr == HPE_OK
                   ? len
                   : static_cast<size_t>(reinterpret_cast<const uint8_t *>(
                                             llhttp_get_error_pos(&htp_)) -
                                         data);

  if (client_->worker->config->verbose) {
    std::cout.write(reinterpret_cast<const char *>(data), nread);
  }

  if (htperr == HPE_PAUSED) {
    // pause is done only when connection: close is requested
    return -1;
  }

  if (htperr != HPE_OK) {
    std::cerr << "[ERROR] HTTP parse error: "
              << "(" << llhttp_errno_name(htperr) << ") "
              << llhttp_get_error_reason(&htp_) << std::endl;
    return -1;
  }

  return 0;
}

int Http1Session::on_write() {
  if (complete_) {
    return -1;
  }

  auto config = client_->worker->config;
  auto req_stat = client_->get_req_stat(stream_req_counter_);
  if (!req_stat) {
    return 0;
  }

  if (req_stat->data_offset < config->data_length) {
    auto req_stat = client_->get_req_stat(stream_req_counter_);
    auto &wb = client_->wb;

    // TODO unfortunately, wb has no interface to use with read(2)
    // family functions.
    std::array<uint8_t, 16_k> buf;

    ssize_t nread;
    while ((nread = pread(config->data_fd, buf.data(), buf.size(),
                          req_stat->data_offset)) == -1 &&
           errno == EINTR)
      ;

    if (nread == -1) {
      return -1;
    }

    req_stat->data_offset += nread;

    wb.append(buf.data(), nread);

    if (client_->worker->config->verbose) {
      std::cout << "[send " << nread << " byte(s)]" << std::endl;
    }

    if (req_stat->data_offset == config->data_length) {
      // increment for next request
      stream_req_counter_ += 2;

      if (stream_resp_counter_ == stream_req_counter_) {
        // Response has already been received
        client_->on_stream_close(stream_resp_counter_ - 2, true,
                                 client_->final);
      }
    }
  }

  return 0;
}

void Http1Session::terminate() { complete_ = true; }

Client *Http1Session::get_client() { return client_; }

size_t Http1Session::max_concurrent_streams() {
  auto config = client_->worker->config;

  return config->data_fd == -1 ? config->max_concurrent_streams : 1;
}

} // namespace h2load