nghttpx: Add --server-name option to change server response header field

This commit is contained in:
Tatsuhiro Tsujikawa 2016-08-26 22:28:09 +09:00
parent 833cd962a1
commit 0d4d1a63d4
7 changed files with 33 additions and 7 deletions

View File

@ -134,6 +134,7 @@ OPTIONS = [
"backend-http2-settings-timeout",
"api-max-request-body",
"backend-max-backoff",
"server-name",
]
LOGVARS = [

View File

@ -178,6 +178,9 @@ template <typename Memchunk> struct Memchunks {
}
size_t append(const std::string &s) { return append(s.c_str(), s.size()); }
size_t append(const StringRef &s) { return append(s.c_str(), s.size()); }
size_t append(const ImmutableString &s) {
return append(s.c_str(), s.size());
}
size_t remove(void *dest, size_t count) {
if (!tail || count == 0) {
return 0;

View File

@ -1317,7 +1317,7 @@ void fill_default_config(Config *config) {
auto &httpconf = config->http;
httpconf.server_name =
StringRef::from_lit("nghttpx nghttp2/" NGHTTP2_VERSION);
ImmutableString::from_lit("nghttpx nghttp2/" NGHTTP2_VERSION);
httpconf.no_host_rewrite = true;
httpconf.request_header_field_buffer = 64_k;
httpconf.max_request_header_fields = 100;
@ -2203,6 +2203,9 @@ HTTP:
599. If "*" is used instead of <CODE>, it matches all
HTTP status code. If error status code comes from
backend server, the custom error pages are not used.
--server-name=<NAME>
Change server response header field value to <NAME>.
Default: )" << get_config()->http.server_name << R"(
API:
--api-max-request-body=<SIZE>
@ -2831,6 +2834,7 @@ int main(int argc, char **argv) {
&flag, 125},
{SHRPX_OPT_API_MAX_REQUEST_BODY.c_str(), required_argument, &flag, 126},
{SHRPX_OPT_BACKEND_MAX_BACKOFF.c_str(), required_argument, &flag, 127},
{SHRPX_OPT_SERVER_NAME.c_str(), required_argument, &flag, 128},
{nullptr, 0, nullptr, 0}};
int option_index = 0;
@ -3428,6 +3432,10 @@ int main(int argc, char **argv) {
// --backend-max-backoff
cmdcfgs.emplace_back(SHRPX_OPT_BACKEND_MAX_BACKOFF, StringRef{optarg});
break;
case 128:
// --server-name
cmdcfgs.emplace_back(SHRPX_OPT_SERVER_NAME, StringRef{optarg});
break;
default:
break;
}

View File

@ -1127,6 +1127,11 @@ int option_lookup_token(const char *name, size_t namelen) {
break;
case 11:
switch (name[10]) {
case 'e':
if (util::strieq_l("server-nam", name, 10)) {
return SHRPX_OPTID_SERVER_NAME;
}
break;
case 's':
if (util::strieq_l("backend-tl", name, 10)) {
return SHRPX_OPTID_BACKEND_TLS;
@ -2673,6 +2678,11 @@ int parse_config(Config *config, int optid, const StringRef &opt,
case SHRPX_OPTID_BACKEND_MAX_BACKOFF:
return parse_duration(&config->conn.downstream->timeout.max_backoff, opt,
optarg);
case SHRPX_OPTID_SERVER_NAME:
config->http.server_name =
ImmutableString{std::begin(optarg), std::end(optarg)};
return 0;
case SHRPX_OPTID_CONF:
LOG(WARN) << "conf: ignored";

View File

@ -284,6 +284,7 @@ constexpr auto SHRPX_OPT_API_MAX_REQUEST_BODY =
StringRef::from_lit("api-max-request-body");
constexpr auto SHRPX_OPT_BACKEND_MAX_BACKOFF =
StringRef::from_lit("backend-max-backoff");
constexpr auto SHRPX_OPT_SERVER_NAME = StringRef::from_lit("server-name");
constexpr size_t SHRPX_OBFUSCATED_NODE_LENGTH = 8;
@ -552,7 +553,7 @@ struct HttpConfig {
std::vector<ErrorPage> error_pages;
Headers add_request_headers;
Headers add_response_headers;
StringRef server_name;
ImmutableString server_name;
size_t request_header_field_buffer;
size_t max_request_header_fields;
size_t response_header_field_buffer;
@ -843,6 +844,7 @@ enum {
SHRPX_OPTID_REQUEST_HEADER_FIELD_BUFFER,
SHRPX_OPTID_RESPONSE_HEADER_FIELD_BUFFER,
SHRPX_OPTID_RLIMIT_NOFILE,
SHRPX_OPTID_SERVER_NAME,
SHRPX_OPTID_STREAM_READ_TIMEOUT,
SHRPX_OPTID_STREAM_WRITE_TIMEOUT,
SHRPX_OPTID_STRIP_INCOMING_FORWARDED,

View File

@ -51,7 +51,7 @@ StringRef create_error_html(BlockAllocator &balloc, unsigned int http_status) {
return concat_string_ref(
balloc, StringRef::from_lit(R"(<!DOCTYPE html><html lang="en"><title>)"),
status_string, StringRef::from_lit("</title><body><h1>"), status_string,
StringRef::from_lit("</h1><footer>"), server_name,
StringRef::from_lit("</h1><footer>"), StringRef{server_name},
StringRef::from_lit("</footer></body></html>"));
}

View File

@ -1307,8 +1307,8 @@ int Http2Upstream::send_reply(Downstream *downstream, const uint8_t *body,
}
if (!resp.fs.header(http2::HD_SERVER)) {
nva.push_back(
http2::make_nv_ls_nocopy("server", get_config()->http.server_name));
nva.push_back(http2::make_nv_ls_nocopy(
"server", StringRef{get_config()->http.server_name}));
}
for (auto &p : httpconf.add_response_headers) {
@ -1359,7 +1359,8 @@ int Http2Upstream::error_reply(Downstream *downstream,
auto nva = std::array<nghttp2_nv, 5>{
{http2::make_nv_ls_nocopy(":status", response_status),
http2::make_nv_ll("content-type", "text/html; charset=UTF-8"),
http2::make_nv_ls_nocopy("server", get_config()->http.server_name),
http2::make_nv_ls_nocopy("server",
StringRef{get_config()->http.server_name}),
http2::make_nv_ls_nocopy("content-length", content_length),
http2::make_nv_ls_nocopy("date", date)}};
@ -1506,7 +1507,8 @@ int Http2Upstream::on_downstream_header_complete(Downstream *downstream) {
http2::copy_headers_to_nva_nocopy(nva, resp.fs.headers());
if (!get_config()->http2_proxy) {
nva.push_back(http2::make_nv_ls_nocopy("server", httpconf.server_name));
nva.push_back(
http2::make_nv_ls_nocopy("server", StringRef{httpconf.server_name}));
} else {
auto server = resp.fs.header(http2::HD_SERVER);
if (server) {