From d16ff1f5192e537ee93f545c9b6463a923f2dd5a Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sun, 17 Jan 2016 01:15:11 +0900 Subject: [PATCH] nghttpx: Use StringAdaptor for Config::server_name --- src/shrpx.cc | 2 +- src/shrpx_config.h | 2 +- src/shrpx_http.cc | 3 ++- src/shrpx_http2_upstream.cc | 6 +++--- src/shrpx_https_upstream.cc | 10 ++++++---- src/shrpx_spdy_upstream.cc | 6 +++--- src/template.h | 2 ++ 7 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/shrpx.cc b/src/shrpx.cc index 4a2103fa..356d9c0f 100644 --- a/src/shrpx.cc +++ b/src/shrpx.cc @@ -1039,7 +1039,7 @@ void fill_default_config() { namespace { void print_version(std::ostream &out) { - out << get_config()->server_name << std::endl; + out << get_config()->server_name.c_str() << std::endl; } } // namespace diff --git a/src/shrpx_config.h b/src/shrpx_config.h index bc9c774f..39197676 100644 --- a/src/shrpx_config.h +++ b/src/shrpx_config.h @@ -311,6 +311,7 @@ struct Config { // string is provided. std::string forwarded_for_obfuscated; std::string backend_tls_sni_name; + StringAdaptor server_name; std::chrono::seconds tls_session_timeout; ev_tstamp http2_upstream_read_timeout; ev_tstamp upstream_read_timeout; @@ -363,7 +364,6 @@ struct Config { nghttp2_option *http2_option; nghttp2_option *http2_client_option; const EVP_CIPHER *tls_ticket_key_cipher; - const char *server_name; char **original_argv; char **argv; char *cwd; diff --git a/src/shrpx_http.cc b/src/shrpx_http.cc index 8407f10d..79524fe9 100644 --- a/src/shrpx_http.cc +++ b/src/shrpx_http.cc @@ -44,7 +44,8 @@ std::string create_error_html(unsigned int status_code) { res += "

"; res += status; res += "

"; diff --git a/src/shrpx_http2_upstream.cc b/src/shrpx_http2_upstream.cc index 8cdbbdd7..24dbf85c 100644 --- a/src/shrpx_http2_upstream.cc +++ b/src/shrpx_http2_upstream.cc @@ -1337,7 +1337,7 @@ int Http2Upstream::send_reply(Downstream *downstream, const uint8_t *body, if (!resp.fs.header(http2::HD_SERVER)) { nva.push_back( - http2::make_nv_lc_nocopy("server", get_config()->server_name)); + http2::make_nv_ls_nocopy("server", get_config()->server_name)); } rv = nghttp2_submit_response(session_, downstream->get_stream_id(), @@ -1386,7 +1386,7 @@ int Http2Upstream::error_reply(Downstream *downstream, : http2::make_nv_ls(":status", (status_code_str = util::utos(status_code))), http2::make_nv_ll("content-type", "text/html; charset=UTF-8"), - http2::make_nv_lc_nocopy("server", get_config()->server_name), + http2::make_nv_ls_nocopy("server", get_config()->server_name), http2::make_nv_ls("content-length", content_length), http2::make_nv_ls("date", lgconf->time_http_str)); @@ -1507,7 +1507,7 @@ int Http2Upstream::on_downstream_header_complete(Downstream *downstream) { if (!get_config()->http2_proxy && !get_config()->client_proxy) { nva.push_back( - http2::make_nv_lc_nocopy("server", get_config()->server_name)); + http2::make_nv_ls_nocopy("server", get_config()->server_name)); } else { auto server = resp.fs.header(http2::HD_SERVER); if (server) { diff --git a/src/shrpx_https_upstream.cc b/src/shrpx_https_upstream.cc index 18cbab30..e38fe526 100644 --- a/src/shrpx_https_upstream.cc +++ b/src/shrpx_https_upstream.cc @@ -802,8 +802,8 @@ int HttpsUpstream::send_reply(Downstream *downstream, const uint8_t *body, if (!resp.fs.header(http2::HD_SERVER)) { output->append("Server: "); - output->append(get_config()->server_name, - strlen(get_config()->server_name)); + const auto &server_name = get_config()->server_name; + output->append(server_name.c_str(), server_name.size()); output->append("\r\n"); } @@ -840,7 +840,8 @@ void HttpsUpstream::error_reply(unsigned int status_code) { auto status_str = http2::get_status_string(status_code); output->append(status_str.c_str(), status_str.size()); output->append("\r\nServer: "); - output->append(get_config()->server_name, strlen(get_config()->server_name)); + const auto &server_name = get_config()->server_name; + output->append(server_name.c_str(), server_name.size()); output->append("\r\nContent-Length: "); auto cl = util::utos(html.size()); output->append(cl.c_str(), cl.size()); @@ -999,7 +1000,8 @@ int HttpsUpstream::on_downstream_header_complete(Downstream *downstream) { if (!get_config()->http2_proxy && !get_config()->client_proxy) { buf->append("Server: "); - buf->append(get_config()->server_name, strlen(get_config()->server_name)); + const auto &server_name = get_config()->server_name; + buf->append(server_name.c_str(), server_name.size()); buf->append("\r\n"); } else { auto server = resp.fs.header(http2::HD_SERVER); diff --git a/src/shrpx_spdy_upstream.cc b/src/shrpx_spdy_upstream.cc index b41bee1d..a9dc4a04 100644 --- a/src/shrpx_spdy_upstream.cc +++ b/src/shrpx_spdy_upstream.cc @@ -875,7 +875,7 @@ int SpdyUpstream::send_reply(Downstream *downstream, const uint8_t *body, if (!resp.fs.header(http2::HD_SERVER)) { nva.push_back("server"); - nva.push_back(get_config()->server_name); + nva.push_back(get_config()->server_name.c_str()); } nva.push_back(nullptr); @@ -919,7 +919,7 @@ int SpdyUpstream::error_reply(Downstream *downstream, std::string status_string = http2::get_status_string(status_code); const char *nv[] = {":status", status_string.c_str(), ":version", "http/1.1", "content-type", "text/html; charset=UTF-8", "server", - get_config()->server_name, "content-length", + get_config()->server_name.c_str(), "content-length", content_length.c_str(), "date", lgconf->time_http_str.c_str(), nullptr}; @@ -1034,7 +1034,7 @@ int SpdyUpstream::on_downstream_header_complete(Downstream *downstream) { if (!get_config()->http2_proxy && !get_config()->client_proxy) { nv[hdidx++] = "server"; - nv[hdidx++] = get_config()->server_name; + nv[hdidx++] = get_config()->server_name.c_str(); } else { auto server = resp.fs.header(http2::HD_SERVER); if (server) { diff --git a/src/template.h b/src/template.h index 5ce17bae..a5249b23 100644 --- a/src/template.h +++ b/src/template.h @@ -227,9 +227,11 @@ struct VString { }; struct StringAdaptor { + StringAdaptor() : base(""), len(0) {} template StringAdaptor(const T &s) : base(s.c_str()), len(s.size()) {} + StringAdaptor(const char *s) : base(s), len(strlen(s)) {} const char *c_str() const { return base; } size_t size() const { return len; }