[nginx] Implemented the "server_tokens build" option.

Ruslan Ermilov ru at nginx.com
Tue Jan 31 09:16:36 UTC 2017


details:   http://hg.nginx.org/nginx/rev/25203fc377fb
branches:  
changeset: 6885:25203fc377fb
user:      Ruslan Ermilov <ru at nginx.com>
date:      Tue Jan 31 12:09:40 2017 +0300
description:
Implemented the "server_tokens build" option.

Based on a patch by Tom Thorogood.

diffstat:

 src/http/ngx_http_core_module.c          |  20 ++++++++++---
 src/http/ngx_http_core_module.h          |   7 ++++-
 src/http/ngx_http_header_filter_module.c |  26 +++++++++++++----
 src/http/ngx_http_special_response.c     |  13 ++++++++-
 src/http/v2/ngx_http_v2_filter_module.c  |  45 ++++++++++++++++++++++++++++---
 5 files changed, 92 insertions(+), 19 deletions(-)

diffs (248 lines):

diff -r 640f03529395 -r 25203fc377fb src/http/ngx_http_core_module.c
--- a/src/http/ngx_http_core_module.c	Fri Jan 27 19:06:35 2017 +0300
+++ b/src/http/ngx_http_core_module.c	Tue Jan 31 12:09:40 2017 +0300
@@ -120,6 +120,14 @@ static ngx_conf_enum_t  ngx_http_core_li
 };
 
 
+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_comm
       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
     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 
     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);
 
diff -r 640f03529395 -r 25203fc377fb src/http/ngx_http_core_module.h
--- a/src/http/ngx_http_core_module.h	Fri Jan 27 19:06:35 2017 +0300
+++ b/src/http/ngx_http_core_module.h	Tue Jan 31 12:09:40 2017 +0300
@@ -55,6 +55,11 @@ typedef struct ngx_thread_pool_s  ngx_th
 #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 */
 
diff -r 640f03529395 -r 25203fc377fb src/http/ngx_http_header_filter_module.c
--- a/src/http/ngx_http_header_filter_module.c	Fri Jan 27 19:06:35 2017 +0300
+++ b/src/http/ngx_http_header_filter_module.c	Tue Jan 31 12:09:40 2017 +0300
@@ -46,8 +46,9 @@ ngx_module_t  ngx_http_header_filter_mod
 };
 
 
-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_
     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_
     *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;
         }
 
diff -r 640f03529395 -r 25203fc377fb src/http/ngx_http_special_response.c
--- a/src/http/ngx_http_special_response.c	Fri Jan 27 19:06:35 2017 +0300
+++ b/src/http/ngx_http_special_response.c	Tue Jan 31 12:09:40 2017 +0300
@@ -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_
     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;
diff -r 640f03529395 -r 25203fc377fb src/http/v2/ngx_http_v2_filter_module.c
--- a/src/http/v2/ngx_http_v2_filter_module.c	Fri Jan 27 19:06:35 2017 +0300
+++ b/src/http/v2/ngx_http_v2_filter_module.c	Tue Jan 31 12:09:40 2017 +0300
@@ -148,6 +148,10 @@ ngx_http_v2_header_filter(ngx_http_reque
     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_reque
     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_reque
     }
 
     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_reque
 
             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));
         }


More information about the nginx-devel mailing list