src: Use std::array instead of std::vector if size is compile time constant
This commit is contained in:
parent
54851ef7a6
commit
ab6663c785
|
@ -77,7 +77,7 @@ void print_session_id(int64_t id) { std::cout << "[id=" << id << "] "; }
|
|||
} // namespace
|
||||
|
||||
namespace {
|
||||
void append_nv(Stream *stream, const std::vector<nghttp2_nv> &nva) {
|
||||
template <typename Array> void append_nv(Stream *stream, const Array &nva) {
|
||||
for (size_t i = 0; i < nva.size(); ++i) {
|
||||
auto &nv = nva[i];
|
||||
auto token = http2::lookup_token(nv.name, nv.namelen);
|
||||
|
@ -702,10 +702,11 @@ int Http2Handler::submit_file_response(const std::string &status,
|
|||
int Http2Handler::submit_response(const std::string &status, int32_t stream_id,
|
||||
const Headers &headers,
|
||||
nghttp2_data_provider *data_prd) {
|
||||
auto nva = std::vector<nghttp2_nv>{
|
||||
http2::make_nv_ls(":status", status),
|
||||
http2::make_nv_ls("server", NGHTTPD_SERVER),
|
||||
http2::make_nv_ls("date", sessions_->get_cached_date())};
|
||||
auto nva = std::vector<nghttp2_nv>();
|
||||
nva.reserve(3 + headers.size());
|
||||
nva.push_back(http2::make_nv_ls(":status", status));
|
||||
nva.push_back(http2::make_nv_ls("server", NGHTTPD_SERVER));
|
||||
nva.push_back(http2::make_nv_ls("date", sessions_->get_cached_date()));
|
||||
for (auto &nv : headers) {
|
||||
nva.push_back(http2::make_nv(nv.name, nv.value, nv.no_index));
|
||||
}
|
||||
|
@ -716,16 +717,15 @@ int Http2Handler::submit_response(const std::string &status, int32_t stream_id,
|
|||
|
||||
int Http2Handler::submit_response(const std::string &status, int32_t stream_id,
|
||||
nghttp2_data_provider *data_prd) {
|
||||
auto nva =
|
||||
std::vector<nghttp2_nv>{http2::make_nv_ls(":status", status),
|
||||
http2::make_nv_ls("server", NGHTTPD_SERVER)};
|
||||
auto nva = make_array(http2::make_nv_ls(":status", status),
|
||||
http2::make_nv_ls("server", NGHTTPD_SERVER));
|
||||
return nghttp2_submit_response(session_, stream_id, nva.data(), nva.size(),
|
||||
data_prd);
|
||||
}
|
||||
|
||||
int Http2Handler::submit_non_final_response(const std::string &status,
|
||||
int32_t stream_id) {
|
||||
auto nva = std::vector<nghttp2_nv>{http2::make_nv_ls(":status", status)};
|
||||
auto nva = make_array(http2::make_nv_ls(":status", status));
|
||||
return nghttp2_submit_headers(session_, NGHTTP2_FLAG_NONE, stream_id, nullptr,
|
||||
nva.data(), nva.size(), nullptr);
|
||||
}
|
||||
|
@ -740,12 +740,12 @@ int Http2Handler::submit_push_promise(Stream *stream,
|
|||
http2::get_header(stream->hdidx, http2::HD_HOST, stream->headers);
|
||||
}
|
||||
|
||||
auto nva = std::vector<nghttp2_nv>{
|
||||
http2::make_nv_ll(":method", "GET"),
|
||||
auto nva =
|
||||
make_array(http2::make_nv_ll(":method", "GET"),
|
||||
http2::make_nv_ls(":path", push_path),
|
||||
get_config()->no_tls ? http2::make_nv_ll(":scheme", "http")
|
||||
: http2::make_nv_ll(":scheme", "https"),
|
||||
http2::make_nv_ls(":authority", authority->value)};
|
||||
http2::make_nv_ls(":authority", authority->value));
|
||||
|
||||
auto promised_stream_id = nghttp2_submit_push_promise(
|
||||
session_, NGHTTP2_FLAG_END_HEADERS, stream->stream_id, nva.data(),
|
||||
|
|
|
@ -1249,7 +1249,7 @@ int main(int argc, char **argv) {
|
|||
|
||||
// list overridalbe headers
|
||||
auto override_hdrs =
|
||||
std::vector<std::string>{":authority", ":host", ":method", ":scheme"};
|
||||
make_array<std::string>(":authority", ":host", ":method", ":scheme");
|
||||
|
||||
for (auto &kv : config.custom_headers) {
|
||||
if (std::find(std::begin(override_hdrs), std::end(override_hdrs),
|
||||
|
|
|
@ -1139,11 +1139,11 @@ int Http2Upstream::error_reply(Downstream *downstream,
|
|||
|
||||
auto content_length = util::utos(html.size());
|
||||
auto status_code_str = util::utos(status_code);
|
||||
auto nva = std::vector<nghttp2_nv>{
|
||||
http2::make_nv_ls(":status", status_code_str),
|
||||
auto nva =
|
||||
make_array(http2::make_nv_ls(":status", status_code_str),
|
||||
http2::make_nv_ll("content-type", "text/html; charset=UTF-8"),
|
||||
http2::make_nv_lc("server", get_config()->server_name),
|
||||
http2::make_nv_ls("content-length", content_length)};
|
||||
http2::make_nv_ls("content-length", content_length));
|
||||
|
||||
rv = nghttp2_submit_response(session_, downstream->get_stream_id(),
|
||||
nva.data(), nva.size(), &data_prd);
|
||||
|
|
|
@ -28,6 +28,7 @@
|
|||
#include "nghttp2_config.h"
|
||||
|
||||
#include <memory>
|
||||
#include <array>
|
||||
|
||||
namespace nghttp2 {
|
||||
|
||||
|
@ -43,6 +44,12 @@ make_unique(size_t size) {
|
|||
return std::unique_ptr<T>(new typename std::remove_extent<T>::type[size]());
|
||||
}
|
||||
|
||||
template <typename T, typename... Rest>
|
||||
std::array<T, sizeof...(Rest)+1> make_array(T &&t, Rest &&... rest) {
|
||||
return std::array<T, sizeof...(Rest)+1>{
|
||||
{std::forward<T>(t), std::forward<Rest>(rest)...}};
|
||||
}
|
||||
|
||||
} // namespace nghttp2
|
||||
|
||||
#endif // TEMPLATE_H
|
||||
|
|
Loading…
Reference in New Issue