Implemented the "server_tokens build" option.

Based on a patch by Tom Thorogood.
This commit is contained in:
Ruslan Ermilov 2017-01-31 12:09:40 +03:00
parent 10c43d8e81
commit 7e080678d6
5 changed files with 92 additions and 19 deletions

View File

@ -120,6 +120,14 @@ static ngx_conf_enum_t ngx_http_core_lingering_close[] = {
};
static ngx_conf_enum_t ngx_http_core_server_tokens[] = {
{ ngx_string("off"), NGX_HTTP_SERVER_TOKENS_OFF },
{ ngx_string("on"), NGX_HTTP_SERVER_TOKENS_ON },
{ ngx_string("build"), NGX_HTTP_SERVER_TOKENS_BUILD },
{ ngx_null_string, 0 }
};
static ngx_conf_enum_t ngx_http_core_if_modified_since[] = {
{ ngx_string("off"), NGX_HTTP_IMS_OFF },
{ ngx_string("exact"), NGX_HTTP_IMS_EXACT },
@ -599,11 +607,11 @@ static ngx_command_t ngx_http_core_commands[] = {
NULL },
{ ngx_string("server_tokens"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_enum_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_core_loc_conf_t, server_tokens),
NULL },
&ngx_http_core_server_tokens },
{ ngx_string("if_modified_since"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
@ -3576,9 +3584,9 @@ ngx_http_core_create_loc_conf(ngx_conf_t *cf)
clcf->log_not_found = NGX_CONF_UNSET;
clcf->log_subrequest = NGX_CONF_UNSET;
clcf->recursive_error_pages = NGX_CONF_UNSET;
clcf->server_tokens = NGX_CONF_UNSET;
clcf->chunked_transfer_encoding = NGX_CONF_UNSET;
clcf->etag = NGX_CONF_UNSET;
clcf->server_tokens = NGX_CONF_UNSET_UINT;
clcf->types_hash_max_size = NGX_CONF_UNSET_UINT;
clcf->types_hash_bucket_size = NGX_CONF_UNSET_UINT;
@ -3842,11 +3850,13 @@ ngx_http_core_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
ngx_conf_merge_value(conf->log_subrequest, prev->log_subrequest, 0);
ngx_conf_merge_value(conf->recursive_error_pages,
prev->recursive_error_pages, 0);
ngx_conf_merge_value(conf->server_tokens, prev->server_tokens, 1);
ngx_conf_merge_value(conf->chunked_transfer_encoding,
prev->chunked_transfer_encoding, 1);
ngx_conf_merge_value(conf->etag, prev->etag, 1);
ngx_conf_merge_uint_value(conf->server_tokens, prev->server_tokens,
NGX_HTTP_SERVER_TOKENS_ON);
ngx_conf_merge_ptr_value(conf->open_file_cache,
prev->open_file_cache, NULL);

View File

@ -55,6 +55,11 @@ typedef struct ngx_thread_pool_s ngx_thread_pool_t;
#define NGX_HTTP_KEEPALIVE_DISABLE_SAFARI 0x0008
#define NGX_HTTP_SERVER_TOKENS_OFF 0
#define NGX_HTTP_SERVER_TOKENS_ON 1
#define NGX_HTTP_SERVER_TOKENS_BUILD 2
typedef struct ngx_http_location_tree_node_s ngx_http_location_tree_node_t;
typedef struct ngx_http_core_loc_conf_s ngx_http_core_loc_conf_t;
@ -393,7 +398,7 @@ struct ngx_http_core_loc_conf_s {
ngx_flag_t log_not_found; /* log_not_found */
ngx_flag_t log_subrequest; /* log_subrequest */
ngx_flag_t recursive_error_pages; /* recursive_error_pages */
ngx_flag_t server_tokens; /* server_tokens */
ngx_uint_t server_tokens; /* server_tokens */
ngx_flag_t chunked_transfer_encoding; /* chunked_transfer_encoding */
ngx_flag_t etag; /* etag */

View File

@ -46,8 +46,9 @@ ngx_module_t ngx_http_header_filter_module = {
};
static char ngx_http_server_string[] = "Server: nginx" CRLF;
static char ngx_http_server_full_string[] = "Server: " NGINX_VER CRLF;
static u_char ngx_http_server_string[] = "Server: nginx" CRLF;
static u_char ngx_http_server_full_string[] = "Server: " NGINX_VER CRLF;
static u_char ngx_http_server_build_string[] = "Server: " NGINX_VER_BUILD CRLF;
static ngx_str_t ngx_http_status_lines[] = {
@ -274,8 +275,15 @@ ngx_http_header_filter(ngx_http_request_t *r)
clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
if (r->headers_out.server == NULL) {
len += clcf->server_tokens ? sizeof(ngx_http_server_full_string) - 1:
sizeof(ngx_http_server_string) - 1;
if (clcf->server_tokens == NGX_HTTP_SERVER_TOKENS_ON) {
len += sizeof(ngx_http_server_full_string) - 1;
} else if (clcf->server_tokens == NGX_HTTP_SERVER_TOKENS_BUILD) {
len += sizeof(ngx_http_server_build_string) - 1;
} else {
len += sizeof(ngx_http_server_string) - 1;
}
}
if (r->headers_out.date == NULL) {
@ -436,12 +444,16 @@ ngx_http_header_filter(ngx_http_request_t *r)
*b->last++ = CR; *b->last++ = LF;
if (r->headers_out.server == NULL) {
if (clcf->server_tokens) {
p = (u_char *) ngx_http_server_full_string;
if (clcf->server_tokens == NGX_HTTP_SERVER_TOKENS_ON) {
p = ngx_http_server_full_string;
len = sizeof(ngx_http_server_full_string) - 1;
} else if (clcf->server_tokens == NGX_HTTP_SERVER_TOKENS_BUILD) {
p = ngx_http_server_build_string;
len = sizeof(ngx_http_server_build_string) - 1;
} else {
p = (u_char *) ngx_http_server_string;
p = ngx_http_server_string;
len = sizeof(ngx_http_server_string) - 1;
}

View File

@ -25,6 +25,13 @@ static u_char ngx_http_error_full_tail[] =
;
static u_char ngx_http_error_build_tail[] =
"<hr><center>" NGINX_VER_BUILD "</center>" CRLF
"</body>" CRLF
"</html>" CRLF
;
static u_char ngx_http_error_tail[] =
"<hr><center>nginx</center>" CRLF
"</body>" CRLF
@ -628,10 +635,14 @@ ngx_http_send_special_response(ngx_http_request_t *r,
ngx_uint_t msie_padding;
ngx_chain_t out[3];
if (clcf->server_tokens) {
if (clcf->server_tokens == NGX_HTTP_SERVER_TOKENS_ON) {
len = sizeof(ngx_http_error_full_tail) - 1;
tail = ngx_http_error_full_tail;
} else if (clcf->server_tokens == NGX_HTTP_SERVER_TOKENS_BUILD) {
len = sizeof(ngx_http_error_build_tail) - 1;
tail = ngx_http_error_build_tail;
} else {
len = sizeof(ngx_http_error_tail) - 1;
tail = ngx_http_error_tail;

View File

@ -148,6 +148,10 @@ ngx_http_v2_header_filter(ngx_http_request_t *r)
static size_t nginx_ver_len = ngx_http_v2_literal_size(NGINX_VER);
static u_char nginx_ver[ngx_http_v2_literal_size(NGINX_VER)];
static size_t nginx_ver_build_len =
ngx_http_v2_literal_size(NGINX_VER_BUILD);
static u_char nginx_ver_build[ngx_http_v2_literal_size(NGINX_VER_BUILD)];
if (!r->stream) {
return ngx_http_next_header_filter(r);
}
@ -232,7 +236,16 @@ ngx_http_v2_header_filter(ngx_http_request_t *r)
clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
if (r->headers_out.server == NULL) {
len += 1 + (clcf->server_tokens ? nginx_ver_len : sizeof(nginx));
if (clcf->server_tokens == NGX_HTTP_SERVER_TOKENS_ON) {
len += 1 + nginx_ver_len;
} else if (clcf->server_tokens == NGX_HTTP_SERVER_TOKENS_BUILD) {
len += 1 + nginx_ver_build_len;
} else {
len += 1 + sizeof(nginx);
}
}
if (r->headers_out.date == NULL) {
@ -420,13 +433,25 @@ ngx_http_v2_header_filter(ngx_http_request_t *r)
}
if (r->headers_out.server == NULL) {
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, fc->log, 0,
"http2 output header: \"server: %s\"",
clcf->server_tokens ? NGINX_VER : "nginx");
if (clcf->server_tokens == NGX_HTTP_SERVER_TOKENS_ON) {
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, fc->log, 0,
"http2 output header: \"server: %s\"",
NGINX_VER);
} else if (clcf->server_tokens == NGX_HTTP_SERVER_TOKENS_BUILD) {
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, fc->log, 0,
"http2 output header: \"server: %s\"",
NGINX_VER_BUILD);
} else {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, fc->log, 0,
"http2 output header: \"server: nginx\"");
}
*pos++ = ngx_http_v2_inc_indexed(NGX_HTTP_V2_SERVER_INDEX);
if (clcf->server_tokens) {
if (clcf->server_tokens == NGX_HTTP_SERVER_TOKENS_ON) {
if (nginx_ver[0] == '\0') {
p = ngx_http_v2_write_value(nginx_ver, (u_char *) NGINX_VER,
sizeof(NGINX_VER) - 1, tmp);
@ -435,6 +460,16 @@ ngx_http_v2_header_filter(ngx_http_request_t *r)
pos = ngx_cpymem(pos, nginx_ver, nginx_ver_len);
} else if (clcf->server_tokens == NGX_HTTP_SERVER_TOKENS_BUILD) {
if (nginx_ver_build[0] == '\0') {
p = ngx_http_v2_write_value(nginx_ver_build,
(u_char *) NGINX_VER_BUILD,
sizeof(NGINX_VER_BUILD) - 1, tmp);
nginx_ver_build_len = p - nginx_ver_build;
}
pos = ngx_cpymem(pos, nginx_ver_build, nginx_ver_build_len);
} else {
pos = ngx_cpymem(pos, nginx, sizeof(nginx));
}