changeset 45034:b1a1702262c9 stable

py3: fix crash when server address is 0.0.0.0 (issue6362) `socket.getfqdn()` assumes that the name is passed as `str` on Python 3 and always returns `str` in this case. Mercurial passed `bytes` (but still expected a `str` result), which worked by chance in many cases, except for e.g. b'0.0.0.0', which was returned unchanged, breaking later code. Instead of calling `socket.getfqdn()`, we can also use `self.server_name` from the base `HTTPServer` class, which already stores the FQDN of the locally-bound socket name (see `BaseHTTPServer.py` in the Python 2 stdlib and `http/server.py` in the Python 3 stdlib).
author Manuel Jacob <me@manueljacob.de>
date Fri, 03 Jul 2020 23:25:19 +0200
parents 383005aa9cdc
children dd3050227a84 40120de810ba
files mercurial/hgweb/server.py tests/test-serve.t
diffstat 2 files changed, 8 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/hgweb/server.py	Wed Jul 01 15:43:15 2020 +0200
+++ b/mercurial/hgweb/server.py	Fri Jul 03 23:25:19 2020 +0200
@@ -382,7 +382,7 @@
         self.errorlog = elog
 
         self.addr, self.port = self.socket.getsockname()[0:2]
-        self.fqaddr = socket.getfqdn(addr[0])
+        self.fqaddr = self.server_name
 
         self.serverheader = ui.config(b'web', b'server-header')
 
--- a/tests/test-serve.t	Wed Jul 01 15:43:15 2020 +0200
+++ b/tests/test-serve.t	Fri Jul 03 23:25:19 2020 +0200
@@ -99,4 +99,11 @@
   $LOCALIP - - [$LOGDATE$] "GET /some/dir7?cmd=capabilities HTTP/1.1" 404 - (glob)
   $LOCALIP - - [$LOGDATE$] "GET /some?cmd=capabilities HTTP/1.1" 404 - (glob)
 
+  $ "$PYTHON" $RUNTESTDIR/killdaemons.py $DAEMON_PIDS
+
+issue6362: Previously, this crashed on Python 3
+
+  $ hg serve -a 0.0.0.0 -d
+  listening at http://*:$HGPORT1/ (bound to *:$HGPORT1) (glob)
+
   $ cd ..