diff --git a/src/HttpServer.cc b/src/HttpServer.cc index 60a27eb6..95584218 100644 --- a/src/HttpServer.cc +++ b/src/HttpServer.cc @@ -402,11 +402,11 @@ int Http2Handler::fill_wb() { int Http2Handler::read_clear() { int rv; - uint8_t buf[8192]; + std::array buf; for (;;) { ssize_t nread; - while ((nread = read(fd_, buf, sizeof(buf))) == -1 && errno == EINTR) + while ((nread = read(fd_, buf.data(), buf.size())) == -1 && errno == EINTR) ; if (nread == -1) { if (errno == EAGAIN || errno == EWOULDBLOCK) { @@ -417,7 +417,7 @@ int Http2Handler::read_clear() { if (nread == 0) { return -1; } - rv = nghttp2_session_mem_recv(session_, buf, nread); + rv = nghttp2_session_mem_recv(session_, buf.data(), nread); if (rv < 0) { if (rv != NGHTTP2_ERR_BAD_PREFACE) { std::cerr << "nghttp2_session_mem_recv() returned error: " @@ -516,12 +516,12 @@ int Http2Handler::tls_handshake() { } int Http2Handler::read_tls() { - uint8_t buf[8192]; + std::array buf; ERR_clear_error(); for (;;) { - auto rv = SSL_read(ssl_, buf, sizeof(buf)); + auto rv = SSL_read(ssl_, buf.data(), buf.size()); if (rv == 0) { return -1; @@ -541,7 +541,7 @@ int Http2Handler::read_tls() { } auto nread = rv; - rv = nghttp2_session_mem_recv(session_, buf, nread); + rv = nghttp2_session_mem_recv(session_, buf.data(), nread); if (rv < 0) { if (rv != NGHTTP2_ERR_BAD_PREFACE) { std::cerr << "nghttp2_session_mem_recv() returned error: " @@ -624,7 +624,7 @@ int Http2Handler::on_connect() { if (r != 0) { return r; } - nghttp2_settings_entry entry[4]; + std::array entry; size_t niv = 1; entry[0].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS; @@ -635,7 +635,7 @@ int Http2Handler::on_connect() { entry[niv].value = sessions_->get_config()->header_table_size; ++niv; } - r = nghttp2_submit_settings(session_, NGHTTP2_FLAG_NONE, entry, niv); + r = nghttp2_submit_settings(session_, NGHTTP2_FLAG_NONE, entry.data(), niv); if (r != 0) { return r; } @@ -682,20 +682,18 @@ int Http2Handler::submit_file_response(const std::string &status, nghttp2_data_provider *data_prd) { std::string content_length = util::utos(file_length); std::string last_modified_str; - nghttp2_nv nva[] = { - http2::make_nv_ls(":status", status), - http2::make_nv_ls("server", NGHTTPD_SERVER), - http2::make_nv_ls("content-length", content_length), - http2::make_nv_ll("cache-control", "max-age=3600"), - http2::make_nv_ls("date", sessions_->get_cached_date()), - http2::make_nv_ll("", ""), - }; + auto nva = make_array(http2::make_nv_ls(":status", status), + http2::make_nv_ls("server", NGHTTPD_SERVER), + http2::make_nv_ls("content-length", content_length), + http2::make_nv_ll("cache-control", "max-age=3600"), + http2::make_nv_ls("date", sessions_->get_cached_date()), + http2::make_nv_ll("", "")); size_t nvlen = 5; if (last_modified != 0) { last_modified_str = util::http_date(last_modified); nva[nvlen++] = http2::make_nv_ls("last-modified", last_modified_str); } - return nghttp2_submit_response(session_, stream->stream_id, nva, nvlen, + return nghttp2_submit_response(session_, stream->stream_id, nva.data(), nvlen, data_prd); } diff --git a/src/nghttp.cc b/src/nghttp.cc index 9a3f8061..6bed5e9d 100644 --- a/src/nghttp.cc +++ b/src/nghttp.cc @@ -113,13 +113,13 @@ namespace { // Returns numeric address string of |addr|. If getnameinfo() is // failed, "unknown" is returned. std::string numeric_name(addrinfo *addr) { - char host[NI_MAXHOST]; - auto rv = getnameinfo(addr->ai_addr, addr->ai_addrlen, host, sizeof(host), - nullptr, 0, NI_NUMERICHOST); + std::array host; + auto rv = getnameinfo(addr->ai_addr, addr->ai_addrlen, host.data(), + host.size(), nullptr, 0, NI_NUMERICHOST); if (rv != 0) { return "unknown"; } - return host; + return host.data(); } } // namespace @@ -622,11 +622,11 @@ void HttpClient::disconnect() { int HttpClient::read_clear() { ev_timer_again(loop, &rt); - uint8_t buf[8192]; + std::array buf; for (;;) { ssize_t nread; - while ((nread = read(fd, buf, sizeof(buf))) == -1 && errno == EINTR) + while ((nread = read(fd, buf.data(), buf.size())) == -1 && errno == EINTR) ; if (nread == -1) { if (errno == EAGAIN || errno == EWOULDBLOCK) { @@ -639,7 +639,7 @@ int HttpClient::read_clear() { return -1; } - if (on_readfn(*this, buf, nread) != 0) { + if (on_readfn(*this, buf.data(), nread) != 0) { return -1; } } @@ -773,17 +773,18 @@ int HttpClient::on_upgrade_connect() { ssize_t rv; record_handshake_time(); assert(!reqvec.empty()); - nghttp2_settings_entry iv[32]; - size_t niv = populate_settings(iv); - assert(sizeof(settings_payload) >= 8 * niv); - rv = nghttp2_pack_settings_payload(settings_payload, sizeof(settings_payload), - iv, niv); + std::array iv; + size_t niv = populate_settings(iv.data()); + assert(settings_payload.size() >= 8 * niv); + rv = nghttp2_pack_settings_payload(settings_payload.data(), + settings_payload.size(), iv.data(), niv); if (rv < 0) { return -1; } settings_payloadlen = rv; - auto token68 = base64::encode(&settings_payload[0], - &settings_payload[settings_payloadlen]); + auto token68 = + base64::encode(std::begin(settings_payload), + std::begin(settings_payload) + settings_payloadlen); util::to_token68(token68); std::string req; if (reqvec[0]->data_prd) { @@ -934,8 +935,8 @@ int HttpClient::on_connect() { if (!reqvec[0]->data_prd) { stream_user_data = reqvec[0].get(); } - rv = nghttp2_session_upgrade(session, settings_payload, settings_payloadlen, - stream_user_data); + rv = nghttp2_session_upgrade(session, settings_payload.data(), + settings_payloadlen, stream_user_data); if (rv != 0) { std::cerr << "[ERROR] nghttp2_session_upgrade() returned error: " << nghttp2_strerror(rv) << std::endl; @@ -953,9 +954,9 @@ int HttpClient::on_connect() { // HTTP2-Settings header field has already been submitted to // session object. if (!need_upgrade()) { - nghttp2_settings_entry iv[16]; - auto niv = populate_settings(iv); - rv = nghttp2_submit_settings(session, NGHTTP2_FLAG_NONE, iv, niv); + std::array iv; + auto niv = populate_settings(iv.data()); + rv = nghttp2_submit_settings(session, NGHTTP2_FLAG_NONE, iv.data(), niv); if (rv != 0) { return -1; } @@ -1112,9 +1113,9 @@ int HttpClient::read_tls() { ERR_clear_error(); - uint8_t buf[8192]; + std::array buf; for (;;) { - auto rv = SSL_read(ssl, buf, sizeof(buf)); + auto rv = SSL_read(ssl, buf.data(), buf.size()); if (rv == 0) { return -1; @@ -1133,7 +1134,7 @@ int HttpClient::read_tls() { } } - if (on_readfn(*this, buf, rv) != 0) { + if (on_readfn(*this, buf.data(), rv) != 0) { return -1; } } @@ -1523,10 +1524,11 @@ int on_data_chunk_recv_callback(nghttp2_session *session, uint8_t flags, if (req->inflater) { while (len > 0) { const size_t MAX_OUTLEN = 4096; - uint8_t out[MAX_OUTLEN]; + std::array out; size_t outlen = MAX_OUTLEN; size_t tlen = len; - int rv = nghttp2_gzip_inflate(req->inflater, out, &outlen, data, &tlen); + int rv = + nghttp2_gzip_inflate(req->inflater, out.data(), &outlen, data, &tlen); if (rv != 0) { nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE, stream_id, NGHTTP2_INTERNAL_ERROR); @@ -1536,10 +1538,10 @@ int on_data_chunk_recv_callback(nghttp2_session *session, uint8_t flags, req->response_len += outlen; if (!config.null_out) { - std::cout.write(reinterpret_cast(out), outlen); + std::cout.write(reinterpret_cast(out.data()), outlen); } - update_html_parser(client, req, out, outlen, 0); + update_html_parser(client, req, out.data(), outlen, 0); data += tlen; len -= tlen; } @@ -2197,9 +2199,10 @@ int run(char **uris, int n) { << std::endl; } while (1) { - char buf[1024]; + std::array buf; ssize_t rret, wret; - while ((rret = read(0, buf, sizeof(buf))) == -1 && errno == EINTR) + while ((rret = read(0, buf.data(), buf.size())) == -1 && + errno == EINTR) ; if (rret == 0) break; @@ -2208,7 +2211,8 @@ int run(char **uris, int n) { << std::endl; return 1; } - while ((wret = write(data_fd, buf, rret)) == -1 && errno == EINTR) + while ((wret = write(data_fd, buf.data(), rret)) == -1 && + errno == EINTR) ; if (wret != rret) { std::cerr << "[ERROR] I/O error while writing to temporary file" diff --git a/src/nghttp.h b/src/nghttp.h index af3516da..2ee7ffbc 100644 --- a/src/nghttp.h +++ b/src/nghttp.h @@ -263,7 +263,7 @@ struct HttpClient { bool upgrade_response_complete; RingBuf<65536> wb; // SETTINGS payload sent as token68 in HTTP Upgrade - uint8_t settings_payload[128]; + std::array settings_payload; enum { ERR_CONNECT_FAIL = -100 }; }; diff --git a/src/shrpx_http2_session.cc b/src/shrpx_http2_session.cc index 7c1b9660..7456819b 100644 --- a/src/shrpx_http2_session.cc +++ b/src/shrpx_http2_session.cc @@ -1212,7 +1212,7 @@ int Http2Session::on_connect() { flow_control_ = true; - nghttp2_settings_entry entry[3]; + std::array entry; entry[0].settings_id = NGHTTP2_SETTINGS_ENABLE_PUSH; entry[0].value = 0; entry[1].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS; @@ -1221,8 +1221,8 @@ int Http2Session::on_connect() { entry[2].settings_id = NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE; entry[2].value = (1 << get_config()->http2_downstream_window_bits) - 1; - rv = nghttp2_submit_settings(session_, NGHTTP2_FLAG_NONE, entry, - util::array_size(entry)); + rv = nghttp2_submit_settings(session_, NGHTTP2_FLAG_NONE, entry.data(), + entry.size()); if (rv != 0) { return -1; } diff --git a/src/shrpx_http2_upstream.cc b/src/shrpx_http2_upstream.cc index a7ee2564..b5f38d71 100644 --- a/src/shrpx_http2_upstream.cc +++ b/src/shrpx_http2_upstream.cc @@ -703,15 +703,15 @@ Http2Upstream::Http2Upstream(ClientHandler *handler) flow_control_ = true; // TODO Maybe call from outside? - nghttp2_settings_entry entry[2]; + std::array entry; entry[0].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS; entry[0].value = get_config()->http2_max_concurrent_streams; entry[1].settings_id = NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE; entry[1].value = (1 << get_config()->http2_upstream_window_bits) - 1; - rv = nghttp2_submit_settings(session_, NGHTTP2_FLAG_NONE, entry, - util::array_size(entry)); + rv = nghttp2_submit_settings(session_, NGHTTP2_FLAG_NONE, entry.data(), + entry.size()); if (rv != 0) { ULOG(ERROR, this) << "nghttp2_submit_settings() returned error: " << nghttp2_strerror(rv); diff --git a/src/shrpx_http_downstream_connection.cc b/src/shrpx_http_downstream_connection.cc index 0e6a6d2b..61873cad 100644 --- a/src/shrpx_http_downstream_connection.cc +++ b/src/shrpx_http_downstream_connection.cc @@ -637,13 +637,13 @@ http_parser_settings htp_hooks = { int HttpDownstreamConnection::on_read() { ev_timer_again(conn_.loop, &conn_.rt); - uint8_t buf[8192]; + std::array buf; int rv; if (downstream_->get_upgraded()) { // For upgraded connection, just pass data to the upstream. for (;;) { - auto nread = conn_.read_clear(buf, sizeof(buf)); + auto nread = conn_.read_clear(buf.data(), buf.size()); if (nread == 0) { return 0; @@ -653,8 +653,8 @@ int HttpDownstreamConnection::on_read() { return nread; } - rv = downstream_->get_upstream()->on_downstream_body(downstream_, buf, - nread, true); + rv = downstream_->get_upstream()->on_downstream_body( + downstream_, buf.data(), nread, true); if (rv != 0) { return rv; } @@ -667,7 +667,7 @@ int HttpDownstreamConnection::on_read() { } for (;;) { - auto nread = conn_.read_clear(buf, sizeof(buf)); + auto nread = conn_.read_clear(buf.data(), buf.size()); if (nread == 0) { return 0; @@ -677,8 +677,9 @@ int HttpDownstreamConnection::on_read() { return nread; } - auto nproc = http_parser_execute(&response_htp_, &htp_hooks, - reinterpret_cast(buf), nread); + auto nproc = + http_parser_execute(&response_htp_, &htp_hooks, + reinterpret_cast(buf.data()), nread); if (nproc != static_cast(nread)) { if (LOG_ENABLED(INFO)) { diff --git a/src/shrpx_spdy_upstream.cc b/src/shrpx_spdy_upstream.cc index 8e9d5475..ede2625b 100644 --- a/src/shrpx_spdy_upstream.cc +++ b/src/shrpx_spdy_upstream.cc @@ -438,7 +438,7 @@ SpdyUpstream::SpdyUpstream(uint16_t version, ClientHandler *handler) initial_window_size_ = 0; } // TODO Maybe call from outside? - spdylay_settings_entry entry[2]; + std::array entry; entry[0].settings_id = SPDYLAY_SETTINGS_MAX_CONCURRENT_STREAMS; entry[0].value = get_config()->http2_max_concurrent_streams; entry[0].flags = SPDYLAY_ID_FLAG_SETTINGS_NONE; @@ -447,8 +447,8 @@ SpdyUpstream::SpdyUpstream(uint16_t version, ClientHandler *handler) entry[1].value = initial_window_size_; entry[1].flags = SPDYLAY_ID_FLAG_SETTINGS_NONE; - rv = spdylay_submit_settings(session_, SPDYLAY_FLAG_SETTINGS_NONE, entry, - util::array_size(entry)); + rv = spdylay_submit_settings(session_, SPDYLAY_FLAG_SETTINGS_NONE, + entry.data(), entry.size()); assert(rv == 0); if (version >= SPDYLAY_PROTO_SPDY3_1 &&