[PATCH] HTTP: factor out server name into a constant

Varun Varada varuncvarada at gmail.com
Sat Jun 6 21:08:40 UTC 2020


# HG changeset patch
# User Varun Varada <varuncvarada at gmail.com>
# Date 1591475111 18000
#      Sat Jun 06 15:25:11 2020 -0500
# Node ID f37aa29453e006bdc37cbe7d9f65eec0de27b731
# Parent  699f6e55bbb4672632e7def5c65b1dbae2960380
HTTP: factor out server name into a constant

This commit factors out the name of the server ("nginx") into a
constant to make the code DRY and so that modifying the server
name, if necessary, can be done in one place.

diff -r 699f6e55bbb4 -r f37aa29453e0 src/core/nginx.h
--- a/src/core/nginx.h	Wed Jun 03 19:11:32 2020 +0300
+++ b/src/core/nginx.h	Sat Jun 06 15:25:11 2020 -0500
@@ -10,8 +10,9 @@
 
 
 #define nginx_version      1019001
+#define NGINX_NAME         "nginx"
 #define NGINX_VERSION      "1.19.1"
-#define NGINX_VER          "nginx/" NGINX_VERSION
+#define NGINX_VER          NGINX_NAME "/" NGINX_VERSION
 
 #ifdef NGX_BUILD
 #define NGINX_VER_BUILD    NGINX_VER " (" NGX_BUILD ")"
diff -r 699f6e55bbb4 -r f37aa29453e0 src/http/ngx_http_header_filter_module.c
--- a/src/http/ngx_http_header_filter_module.c	Wed Jun 03 19:11:32 2020 +0300
+++ b/src/http/ngx_http_header_filter_module.c	Sat Jun 06 15:25:11 2020 -0500
@@ -46,7 +46,7 @@
 };
 
 
-static u_char ngx_http_server_string[] = "Server: nginx" CRLF;
+static u_char ngx_http_server_string[] = "Server: " NGINX_NAME 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;
 
diff -r 699f6e55bbb4 -r f37aa29453e0 src/http/v2/ngx_http_v2_filter_module.c
--- a/src/http/v2/ngx_http_v2_filter_module.c	Wed Jun 03 19:11:32 2020 +0300
+++ b/src/http/v2/ngx_http_v2_filter_module.c	Sat Jun 06 15:25:11 2020 -0500
@@ -148,12 +148,14 @@
     ngx_http_core_srv_conf_t  *cscf;
     u_char                     addr[NGX_SOCKADDR_STRLEN];
 
-    static const u_char nginx[5] = "\x84\xaa\x63\x55\xe7";
 #if (NGX_HTTP_GZIP)
     static const u_char accept_encoding[12] =
         "\x8b\x84\x84\x2d\x69\x5b\x05\x44\x3c\x86\xaa\x6f";
 #endif
 
+    static size_t nginx_len = ngx_http_v2_literal_size(NGINX_NAME);
+    static u_char nginx[ngx_http_v2_literal_size(NGINX_NAME)];
+
     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)];
 
@@ -268,7 +270,7 @@
             len += 1 + nginx_ver_build_len;
 
         } else {
-            len += 1 + sizeof(nginx);
+            len += 1 + nginx_len;
         }
     }
 
@@ -476,8 +478,9 @@
                            NGINX_VER_BUILD);
 
         } else {
-            ngx_log_debug0(NGX_LOG_DEBUG_HTTP, fc->log, 0,
-                           "http2 output header: \"server: nginx\"");
+            ngx_log_debug1(NGX_LOG_DEBUG_HTTP, fc->log, 0,
+                           "http2 output header: \"server: %s\"",
+                           NGINX_NAME);
         }
 
         *pos++ = ngx_http_v2_inc_indexed(NGX_HTTP_V2_SERVER_INDEX);
@@ -502,7 +505,14 @@
             pos = ngx_cpymem(pos, nginx_ver_build, nginx_ver_build_len);
 
         } else {
-            pos = ngx_cpymem(pos, nginx, sizeof(nginx));
+            if (nginx[0] == '\0') {
+                p = ngx_http_v2_write_value(nginx,
+                                            (u_char *) NGINX_NAME,
+                                            sizeof(NGINX_NAME) - 1, tmp);
+                nginx_len = p - nginx;
+            }
+
+            pos = ngx_cpymem(pos, nginx, nginx_len);
         }
     }
 


More information about the nginx-devel mailing list