File 6271-inets-Make-httpd-server_name-setting-optional.patch of Package erlang

From a412fc71fa836a7999e3332d0dd67a54b2d924e0 Mon Sep 17 00:00:00 2001
From: Johannes Christ <jc@jchri.st>
Date: Fri, 26 May 2023 12:22:30 +0200
Subject: [PATCH] inets: Make httpd server_name setting optional

By default, httpd will now use the system DNS name, if available. This
makes it easier to start httpd for cases where the server name isn't
important. Suggestion came from here:
https://erlangforums.com/t/httpc-httpd-improvements/2622/14
---
 lib/inets/doc/src/http_server.xml        |  8 +++-----
 lib/inets/doc/src/httpd.xml              | 11 ++++++-----
 lib/inets/src/http_server/httpd_conf.erl | 17 ++++++++++++++---
 lib/inets/test/inets_SUITE.erl           |  3 +--
 4 files changed, 24 insertions(+), 15 deletions(-)

diff --git a/lib/inets/doc/src/http_server.xml b/lib/inets/doc/src/http_server.xml
index 58671eaee6..bc7507f703 100644
--- a/lib/inets/doc/src/http_server.xml
+++ b/lib/inets/doc/src/http_server.xml
@@ -117,8 +117,7 @@
        number that was picked:</p>
 
     <code type="none">
-      2 > {ok, Pid} = inets:start(httpd, [{port, 0},
-      {server_name,"httpd_test"}, {server_root,"/tmp"},
+      2 > {ok, Pid} = inets:start(httpd, [{port, 0}, {server_root,"/tmp"},
       {document_root,"/tmp/htdocs"}, {bind_address, "localhost"}]).
       {ok, 0.79.0}      </code>
     
@@ -126,7 +125,7 @@
     <code type="none">
       3 >  httpd:info(Pid).
       [{mime_types,[{"html","text/html"},{"htm","text/html"}]},
-      {server_name,"httpd_test"},
+      {server_name,"machine.local"},
       {bind_address, {127,0,0,1}},
       {server_root,"/tmp"},
       {port,59408},
@@ -137,8 +136,7 @@
     </p>
     <code type="none">
     4 > httpd:reload_config([{port, 59408},
-      {server_name,"httpd_test"}, {server_root,"/tmp/www_test"},
-      {document_root,"/tmp/www_test/htdocs"},
+      {server_root,"/tmp/www_test"}, {document_root,"/tmp/www_test/htdocs"},
       {bind_address, "localhost"}], non_disturbing).
     ok.</code>
 
diff --git a/lib/inets/doc/src/httpd.xml b/lib/inets/doc/src/httpd.xml
index 118d38ba2a..27cac53aff 100644
--- a/lib/inets/doc/src/httpd.xml
+++ b/lib/inets/doc/src/httpd.xml
@@ -101,11 +101,6 @@
 	determine which port was picked.</p>
       </item>
 
-      <tag><marker id="prop_server_name"></marker>{server_name, string()}</tag>
-      <item>
-	<p>The name of your server, normally a fully qualified domain name.</p>
-      </item>
-
       <tag><marker id="prop_server_root"></marker>{server_root, path()}</tag>
       <item> 
 	<p>Defines the home directory of the server, where log files, and so on, 
@@ -128,6 +123,12 @@
 	<p>Default is <c>any</c></p>
       </item>
 
+      <tag><marker id="prop_server_name"></marker>{server_name, string()}</tag>
+      <item>
+	<p>The name of your server, normally a fully qualified domain name.</p>
+        <p>If not given, this defaults to <c>net_adm:localhost()</c>.</p>
+      </item>
+
       <tag><marker id="profile"></marker>{profile, atom()}</tag>
       <item>
         <p>Used together with <seeerl marker="#prop_bind_address"><c>bind_address</c></seeerl>
diff --git a/lib/inets/src/http_server/httpd_conf.erl b/lib/inets/src/http_server/httpd_conf.erl
index ffa9a23fec..3baad5c63b 100644
--- a/lib/inets/src/http_server/httpd_conf.erl
+++ b/lib/inets/src/http_server/httpd_conf.erl
@@ -75,12 +75,23 @@ load_mime_types(MimeTypesFile) ->
 	    {error, ?NICE("Can't open " ++ MimeTypesFile)}
     end.
 
+%% Add any optional properties if they are not present.
+optional_properties(Properties) ->
+    case proplists:get_value(server_name, Properties) of
+        undefined ->
+            Localhost = net_adm:localhost(),
+            [{server_name, Localhost} | Properties];
+        _Name ->
+            Properties
+    end.
 
-validate_properties(Properties) ->
-    %% First, check that all mandatory properties are present
+validate_properties(RawProperties) ->
+    %% First, prefill any optional properties
+    Properties = optional_properties(RawProperties),
+    %% Then check that all mandatory properties are present
     case mandatory_properties(Properties) of
 	ok -> 
-	    %% Second, check that property dependency are ok
+	    %% Finally, check that property dependency are ok
 	    {ok, check_minimum_bytes_per_second(validate_properties2(Properties))};
 	Error ->
 	    throw(Error)
diff --git a/lib/inets/test/inets_SUITE.erl b/lib/inets/test/inets_SUITE.erl
index 99184a266e..dddf5eaf04 100644
--- a/lib/inets/test/inets_SUITE.erl
+++ b/lib/inets/test/inets_SUITE.erl
@@ -202,8 +202,7 @@ start_httpd() ->
 start_httpd(Config) when is_list(Config) ->
     process_flag(trap_exit, true),
     PrivDir = proplists:get_value(priv_dir, Config),
-    HttpdConf = [{server_name, "httpd_test"}, {server_root, PrivDir},
-		 {document_root, PrivDir}, {bind_address, any}],
+    HttpdConf = [{server_root, PrivDir}, {document_root, PrivDir}, {bind_address, any}],
     
     ok = inets:start(),
     {ok, Pid0} = inets:start(httpd, [{port, 0}, {ipfamily, inet} | HttpdConf]),
-- 
2.35.3

openSUSE Build Service is sponsored by