src: Prefer std::array

This commit is contained in:
Tatsuhiro Tsujikawa 2015-02-06 00:06:01 +09:00
parent 9a2d36fc6c
commit 4cda09beff
7 changed files with 66 additions and 63 deletions

View File

@ -402,11 +402,11 @@ int Http2Handler::fill_wb() {
int Http2Handler::read_clear() { int Http2Handler::read_clear() {
int rv; int rv;
uint8_t buf[8192]; std::array<uint8_t, 8192> buf;
for (;;) { for (;;) {
ssize_t nread; 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 (nread == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK) { if (errno == EAGAIN || errno == EWOULDBLOCK) {
@ -417,7 +417,7 @@ int Http2Handler::read_clear() {
if (nread == 0) { if (nread == 0) {
return -1; return -1;
} }
rv = nghttp2_session_mem_recv(session_, buf, nread); rv = nghttp2_session_mem_recv(session_, buf.data(), nread);
if (rv < 0) { if (rv < 0) {
if (rv != NGHTTP2_ERR_BAD_PREFACE) { if (rv != NGHTTP2_ERR_BAD_PREFACE) {
std::cerr << "nghttp2_session_mem_recv() returned error: " std::cerr << "nghttp2_session_mem_recv() returned error: "
@ -516,12 +516,12 @@ int Http2Handler::tls_handshake() {
} }
int Http2Handler::read_tls() { int Http2Handler::read_tls() {
uint8_t buf[8192]; std::array<uint8_t, 8192> buf;
ERR_clear_error(); ERR_clear_error();
for (;;) { for (;;) {
auto rv = SSL_read(ssl_, buf, sizeof(buf)); auto rv = SSL_read(ssl_, buf.data(), buf.size());
if (rv == 0) { if (rv == 0) {
return -1; return -1;
@ -541,7 +541,7 @@ int Http2Handler::read_tls() {
} }
auto nread = rv; 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 < 0) {
if (rv != NGHTTP2_ERR_BAD_PREFACE) { if (rv != NGHTTP2_ERR_BAD_PREFACE) {
std::cerr << "nghttp2_session_mem_recv() returned error: " std::cerr << "nghttp2_session_mem_recv() returned error: "
@ -624,7 +624,7 @@ int Http2Handler::on_connect() {
if (r != 0) { if (r != 0) {
return r; return r;
} }
nghttp2_settings_entry entry[4]; std::array<nghttp2_settings_entry, 4> entry;
size_t niv = 1; size_t niv = 1;
entry[0].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS; 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; entry[niv].value = sessions_->get_config()->header_table_size;
++niv; ++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) { if (r != 0) {
return r; return r;
} }
@ -682,20 +682,18 @@ int Http2Handler::submit_file_response(const std::string &status,
nghttp2_data_provider *data_prd) { nghttp2_data_provider *data_prd) {
std::string content_length = util::utos(file_length); std::string content_length = util::utos(file_length);
std::string last_modified_str; std::string last_modified_str;
nghttp2_nv nva[] = { auto nva = make_array(http2::make_nv_ls(":status", status),
http2::make_nv_ls(":status", status), http2::make_nv_ls("server", NGHTTPD_SERVER),
http2::make_nv_ls("server", NGHTTPD_SERVER), http2::make_nv_ls("content-length", content_length),
http2::make_nv_ls("content-length", content_length), http2::make_nv_ll("cache-control", "max-age=3600"),
http2::make_nv_ll("cache-control", "max-age=3600"), http2::make_nv_ls("date", sessions_->get_cached_date()),
http2::make_nv_ls("date", sessions_->get_cached_date()), http2::make_nv_ll("", ""));
http2::make_nv_ll("", ""),
};
size_t nvlen = 5; size_t nvlen = 5;
if (last_modified != 0) { if (last_modified != 0) {
last_modified_str = util::http_date(last_modified); last_modified_str = util::http_date(last_modified);
nva[nvlen++] = http2::make_nv_ls("last-modified", last_modified_str); 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); data_prd);
} }

View File

@ -113,13 +113,13 @@ namespace {
// Returns numeric address string of |addr|. If getnameinfo() is // Returns numeric address string of |addr|. If getnameinfo() is
// failed, "unknown" is returned. // failed, "unknown" is returned.
std::string numeric_name(addrinfo *addr) { std::string numeric_name(addrinfo *addr) {
char host[NI_MAXHOST]; std::array<char, NI_MAXHOST> host;
auto rv = getnameinfo(addr->ai_addr, addr->ai_addrlen, host, sizeof(host), auto rv = getnameinfo(addr->ai_addr, addr->ai_addrlen, host.data(),
nullptr, 0, NI_NUMERICHOST); host.size(), nullptr, 0, NI_NUMERICHOST);
if (rv != 0) { if (rv != 0) {
return "unknown"; return "unknown";
} }
return host; return host.data();
} }
} // namespace } // namespace
@ -622,11 +622,11 @@ void HttpClient::disconnect() {
int HttpClient::read_clear() { int HttpClient::read_clear() {
ev_timer_again(loop, &rt); ev_timer_again(loop, &rt);
uint8_t buf[8192]; std::array<uint8_t, 8192> buf;
for (;;) { for (;;) {
ssize_t nread; 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 (nread == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK) { if (errno == EAGAIN || errno == EWOULDBLOCK) {
@ -639,7 +639,7 @@ int HttpClient::read_clear() {
return -1; return -1;
} }
if (on_readfn(*this, buf, nread) != 0) { if (on_readfn(*this, buf.data(), nread) != 0) {
return -1; return -1;
} }
} }
@ -773,17 +773,18 @@ int HttpClient::on_upgrade_connect() {
ssize_t rv; ssize_t rv;
record_handshake_time(); record_handshake_time();
assert(!reqvec.empty()); assert(!reqvec.empty());
nghttp2_settings_entry iv[32]; std::array<nghttp2_settings_entry, 32> iv;
size_t niv = populate_settings(iv); size_t niv = populate_settings(iv.data());
assert(sizeof(settings_payload) >= 8 * niv); assert(settings_payload.size() >= 8 * niv);
rv = nghttp2_pack_settings_payload(settings_payload, sizeof(settings_payload), rv = nghttp2_pack_settings_payload(settings_payload.data(),
iv, niv); settings_payload.size(), iv.data(), niv);
if (rv < 0) { if (rv < 0) {
return -1; return -1;
} }
settings_payloadlen = rv; settings_payloadlen = rv;
auto token68 = base64::encode(&settings_payload[0], auto token68 =
&settings_payload[settings_payloadlen]); base64::encode(std::begin(settings_payload),
std::begin(settings_payload) + settings_payloadlen);
util::to_token68(token68); util::to_token68(token68);
std::string req; std::string req;
if (reqvec[0]->data_prd) { if (reqvec[0]->data_prd) {
@ -934,8 +935,8 @@ int HttpClient::on_connect() {
if (!reqvec[0]->data_prd) { if (!reqvec[0]->data_prd) {
stream_user_data = reqvec[0].get(); stream_user_data = reqvec[0].get();
} }
rv = nghttp2_session_upgrade(session, settings_payload, settings_payloadlen, rv = nghttp2_session_upgrade(session, settings_payload.data(),
stream_user_data); settings_payloadlen, stream_user_data);
if (rv != 0) { if (rv != 0) {
std::cerr << "[ERROR] nghttp2_session_upgrade() returned error: " std::cerr << "[ERROR] nghttp2_session_upgrade() returned error: "
<< nghttp2_strerror(rv) << std::endl; << nghttp2_strerror(rv) << std::endl;
@ -953,9 +954,9 @@ int HttpClient::on_connect() {
// HTTP2-Settings header field has already been submitted to // HTTP2-Settings header field has already been submitted to
// session object. // session object.
if (!need_upgrade()) { if (!need_upgrade()) {
nghttp2_settings_entry iv[16]; std::array<nghttp2_settings_entry, 16> iv;
auto niv = populate_settings(iv); auto niv = populate_settings(iv.data());
rv = nghttp2_submit_settings(session, NGHTTP2_FLAG_NONE, iv, niv); rv = nghttp2_submit_settings(session, NGHTTP2_FLAG_NONE, iv.data(), niv);
if (rv != 0) { if (rv != 0) {
return -1; return -1;
} }
@ -1112,9 +1113,9 @@ int HttpClient::read_tls() {
ERR_clear_error(); ERR_clear_error();
uint8_t buf[8192]; std::array<uint8_t, 8192> buf;
for (;;) { for (;;) {
auto rv = SSL_read(ssl, buf, sizeof(buf)); auto rv = SSL_read(ssl, buf.data(), buf.size());
if (rv == 0) { if (rv == 0) {
return -1; 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; return -1;
} }
} }
@ -1523,10 +1524,11 @@ int on_data_chunk_recv_callback(nghttp2_session *session, uint8_t flags,
if (req->inflater) { if (req->inflater) {
while (len > 0) { while (len > 0) {
const size_t MAX_OUTLEN = 4096; const size_t MAX_OUTLEN = 4096;
uint8_t out[MAX_OUTLEN]; std::array<uint8_t, MAX_OUTLEN> out;
size_t outlen = MAX_OUTLEN; size_t outlen = MAX_OUTLEN;
size_t tlen = len; 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) { if (rv != 0) {
nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE, stream_id, nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE, stream_id,
NGHTTP2_INTERNAL_ERROR); NGHTTP2_INTERNAL_ERROR);
@ -1536,10 +1538,10 @@ int on_data_chunk_recv_callback(nghttp2_session *session, uint8_t flags,
req->response_len += outlen; req->response_len += outlen;
if (!config.null_out) { if (!config.null_out) {
std::cout.write(reinterpret_cast<const char *>(out), outlen); std::cout.write(reinterpret_cast<const char *>(out.data()), outlen);
} }
update_html_parser(client, req, out, outlen, 0); update_html_parser(client, req, out.data(), outlen, 0);
data += tlen; data += tlen;
len -= tlen; len -= tlen;
} }
@ -2197,9 +2199,10 @@ int run(char **uris, int n) {
<< std::endl; << std::endl;
} }
while (1) { while (1) {
char buf[1024]; std::array<char, 1024> buf;
ssize_t rret, wret; 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) if (rret == 0)
break; break;
@ -2208,7 +2211,8 @@ int run(char **uris, int n) {
<< std::endl; << std::endl;
return 1; 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) { if (wret != rret) {
std::cerr << "[ERROR] I/O error while writing to temporary file" std::cerr << "[ERROR] I/O error while writing to temporary file"

View File

@ -263,7 +263,7 @@ struct HttpClient {
bool upgrade_response_complete; bool upgrade_response_complete;
RingBuf<65536> wb; RingBuf<65536> wb;
// SETTINGS payload sent as token68 in HTTP Upgrade // SETTINGS payload sent as token68 in HTTP Upgrade
uint8_t settings_payload[128]; std::array<uint8_t, 128> settings_payload;
enum { ERR_CONNECT_FAIL = -100 }; enum { ERR_CONNECT_FAIL = -100 };
}; };

View File

@ -1212,7 +1212,7 @@ int Http2Session::on_connect() {
flow_control_ = true; flow_control_ = true;
nghttp2_settings_entry entry[3]; std::array<nghttp2_settings_entry, 3> entry;
entry[0].settings_id = NGHTTP2_SETTINGS_ENABLE_PUSH; entry[0].settings_id = NGHTTP2_SETTINGS_ENABLE_PUSH;
entry[0].value = 0; entry[0].value = 0;
entry[1].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS; 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].settings_id = NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE;
entry[2].value = (1 << get_config()->http2_downstream_window_bits) - 1; entry[2].value = (1 << get_config()->http2_downstream_window_bits) - 1;
rv = nghttp2_submit_settings(session_, NGHTTP2_FLAG_NONE, entry, rv = nghttp2_submit_settings(session_, NGHTTP2_FLAG_NONE, entry.data(),
util::array_size(entry)); entry.size());
if (rv != 0) { if (rv != 0) {
return -1; return -1;
} }

View File

@ -703,15 +703,15 @@ Http2Upstream::Http2Upstream(ClientHandler *handler)
flow_control_ = true; flow_control_ = true;
// TODO Maybe call from outside? // TODO Maybe call from outside?
nghttp2_settings_entry entry[2]; std::array<nghttp2_settings_entry, 2> entry;
entry[0].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS; entry[0].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS;
entry[0].value = get_config()->http2_max_concurrent_streams; entry[0].value = get_config()->http2_max_concurrent_streams;
entry[1].settings_id = NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE; entry[1].settings_id = NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE;
entry[1].value = (1 << get_config()->http2_upstream_window_bits) - 1; entry[1].value = (1 << get_config()->http2_upstream_window_bits) - 1;
rv = nghttp2_submit_settings(session_, NGHTTP2_FLAG_NONE, entry, rv = nghttp2_submit_settings(session_, NGHTTP2_FLAG_NONE, entry.data(),
util::array_size(entry)); entry.size());
if (rv != 0) { if (rv != 0) {
ULOG(ERROR, this) << "nghttp2_submit_settings() returned error: " ULOG(ERROR, this) << "nghttp2_submit_settings() returned error: "
<< nghttp2_strerror(rv); << nghttp2_strerror(rv);

View File

@ -637,13 +637,13 @@ http_parser_settings htp_hooks = {
int HttpDownstreamConnection::on_read() { int HttpDownstreamConnection::on_read() {
ev_timer_again(conn_.loop, &conn_.rt); ev_timer_again(conn_.loop, &conn_.rt);
uint8_t buf[8192]; std::array<uint8_t, 8192> buf;
int rv; int rv;
if (downstream_->get_upgraded()) { if (downstream_->get_upgraded()) {
// For upgraded connection, just pass data to the upstream. // For upgraded connection, just pass data to the upstream.
for (;;) { for (;;) {
auto nread = conn_.read_clear(buf, sizeof(buf)); auto nread = conn_.read_clear(buf.data(), buf.size());
if (nread == 0) { if (nread == 0) {
return 0; return 0;
@ -653,8 +653,8 @@ int HttpDownstreamConnection::on_read() {
return nread; return nread;
} }
rv = downstream_->get_upstream()->on_downstream_body(downstream_, buf, rv = downstream_->get_upstream()->on_downstream_body(
nread, true); downstream_, buf.data(), nread, true);
if (rv != 0) { if (rv != 0) {
return rv; return rv;
} }
@ -667,7 +667,7 @@ int HttpDownstreamConnection::on_read() {
} }
for (;;) { for (;;) {
auto nread = conn_.read_clear(buf, sizeof(buf)); auto nread = conn_.read_clear(buf.data(), buf.size());
if (nread == 0) { if (nread == 0) {
return 0; return 0;
@ -677,8 +677,9 @@ int HttpDownstreamConnection::on_read() {
return nread; return nread;
} }
auto nproc = http_parser_execute(&response_htp_, &htp_hooks, auto nproc =
reinterpret_cast<char *>(buf), nread); http_parser_execute(&response_htp_, &htp_hooks,
reinterpret_cast<char *>(buf.data()), nread);
if (nproc != static_cast<size_t>(nread)) { if (nproc != static_cast<size_t>(nread)) {
if (LOG_ENABLED(INFO)) { if (LOG_ENABLED(INFO)) {

View File

@ -438,7 +438,7 @@ SpdyUpstream::SpdyUpstream(uint16_t version, ClientHandler *handler)
initial_window_size_ = 0; initial_window_size_ = 0;
} }
// TODO Maybe call from outside? // TODO Maybe call from outside?
spdylay_settings_entry entry[2]; std::array<spdylay_settings_entry, 2> entry;
entry[0].settings_id = SPDYLAY_SETTINGS_MAX_CONCURRENT_STREAMS; entry[0].settings_id = SPDYLAY_SETTINGS_MAX_CONCURRENT_STREAMS;
entry[0].value = get_config()->http2_max_concurrent_streams; entry[0].value = get_config()->http2_max_concurrent_streams;
entry[0].flags = SPDYLAY_ID_FLAG_SETTINGS_NONE; 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].value = initial_window_size_;
entry[1].flags = SPDYLAY_ID_FLAG_SETTINGS_NONE; entry[1].flags = SPDYLAY_ID_FLAG_SETTINGS_NONE;
rv = spdylay_submit_settings(session_, SPDYLAY_FLAG_SETTINGS_NONE, entry, rv = spdylay_submit_settings(session_, SPDYLAY_FLAG_SETTINGS_NONE,
util::array_size(entry)); entry.data(), entry.size());
assert(rv == 0); assert(rv == 0);
if (version >= SPDYLAY_PROTO_SPDY3_1 && if (version >= SPDYLAY_PROTO_SPDY3_1 &&