diff --git a/mercurial/commands.py b/mercurial/commands.py --- a/mercurial/commands.py +++ b/mercurial/commands.py @@ -2461,10 +2461,7 @@ def serve(ui, repo, **opts): class service: def init(self): util.set_signal_handler() - try: - self.httpd = hgweb.server.create_server(parentui, repo) - except socket.error, inst: - raise util.Abort(_('cannot start server: ') + inst.args[1]) + self.httpd = hgweb.server.create_server(parentui, repo) if not ui.verbose: return @@ -2473,12 +2470,12 @@ def serve(ui, repo, **opts): else: prefix = '' - if self.httpd.port != 80: - ui.status(_('listening at http://%s:%d/%s\n') % - (self.httpd.addr, self.httpd.port, prefix)) - else: - ui.status(_('listening at http://%s/%s\n') % - (self.httpd.addr, prefix)) + port = ':%d' % self.httpd.port + if port == ':80': + port = '' + + ui.status(_('listening at http://%s%s/%s (%s:%d)\n') % + (self.httpd.fqaddr, port, prefix, self.httpd.addr, self.httpd.port)) def run(self): self.httpd.serve_forever() @@ -3115,8 +3112,8 @@ table = { ('d', 'daemon', None, _('run server in background')), ('', 'daemon-pipefds', '', _('used internally by daemon mode')), ('E', 'errorlog', '', _('name of error log file to write to')), - ('p', 'port', 0, _('port to use (default: 8000)')), - ('a', 'address', '', _('address to use')), + ('p', 'port', 0, _('port to listen on (default: 8000)')), + ('a', 'address', '', _('address to listen on (default: all interfaces)')), ('', 'prefix', '', _('prefix path to serve from (default: server root)')), ('n', 'name', '', _('name to show in web pages (default: working dir)')), diff --git a/mercurial/hgweb/server.py b/mercurial/hgweb/server.py --- a/mercurial/hgweb/server.py +++ b/mercurial/hgweb/server.py @@ -253,13 +253,6 @@ def create_server(ui, repo): return hgwebobj self.application = make_handler() - addr = address - if addr in ('', '::'): - addr = socket.gethostname() - - self.addr, self.port = addr, port - self.prefix = prefix - if ssl_cert: try: from OpenSSL import SSL @@ -273,6 +266,15 @@ def create_server(ui, repo): self.server_bind() self.server_activate() + self.addr, self.port = self.socket.getsockname()[0:2] + self.prefix = prefix + + self.fqaddr = socket.getfqdn(address) + try: + socket.getaddrbyhost(self.fqaddr) + except: + fqaddr = address + class IPv6HTTPServer(MercurialHTTPServer): address_family = getattr(socket, 'AF_INET6', None) @@ -292,4 +294,5 @@ def create_server(ui, repo): else: return MercurialHTTPServer((address, port), handler) except socket.error, inst: - raise util.Abort(_('cannot start server: %s') % inst.args[1]) + raise util.Abort(_("cannot start server at '%s:%d': %s") + % (address, port, inst.args[1])) diff --git a/tests/test-http b/tests/test-http --- a/tests/test-http +++ b/tests/test-http @@ -9,7 +9,7 @@ hg commit -A -d '0 0' -m 1 hg --config server.uncompressed=True serve -p $HGPORT -d --pid-file=../hg1.pid hg serve -p $HGPORT1 -d --pid-file=../hg2.pid # Test server address cannot be reused -hg serve -p $HGPORT1 2>&1 | sed -e 's/abort: cannot start server:.*/abort: cannot start server:/' +hg serve -p $HGPORT1 2>&1 | sed -e "s/abort: cannot start server at ':$HGPORT1':.*/abort: cannot start server at ':$HGPORT1':/" cd .. cat hg1.pid hg2.pid >> $DAEMON_PIDS diff --git a/tests/test-http.out b/tests/test-http.out --- a/tests/test-http.out +++ b/tests/test-http.out @@ -1,5 +1,5 @@ adding foo -abort: cannot start server: +abort: cannot start server at ':20060': % clone via stream streaming all changes XXX files to transfer, XXX bytes of data diff --git a/tests/test-serve b/tests/test-serve --- a/tests/test-serve +++ b/tests/test-serve @@ -14,33 +14,38 @@ if [ -f access.log ]; then fi echo % With -v -hg serve -a localhost -p $HGPORT1 -d --pid-file=hg.pid -v | sed -e 's,:[0-9][0-9]*/,/,' +hg serve -a localhost -p $HGPORT1 -d --pid-file=hg.pid -v \ + | sed -e 's/:[0-9][0-9]*//g' -e 's/localhost\.localdomain/localhost/' cat hg.pid >> "$DAEMON_PIDS" sleep 1 kill `cat hg.pid` sleep 1 echo % With --prefix foo -hg serve -a localhost -p $HGPORT1 -d --pid-file=hg.pid -v --prefix foo | sed -e 's,:[0-9][0-9]*/,/,' +hg serve -a localhost -p $HGPORT1 -d --pid-file=hg.pid -v --prefix foo \ + | sed -e 's/:[0-9][0-9]*//g' -e 's/localhost\.localdomain/localhost/' cat hg.pid >> "$DAEMON_PIDS" sleep 1 kill `cat hg.pid` sleep 1 echo % With --prefix /foo -hg serve -a localhost -p $HGPORT1 -d --pid-file=hg.pid -v --prefix /foo | sed -e 's,:[0-9][0-9]*/,/,' +hg serve -a localhost -p $HGPORT1 -d --pid-file=hg.pid -v --prefix /foo \ + | sed -e 's/:[0-9][0-9]*//g' -e 's/localhost\.localdomain/localhost/' cat hg.pid >> "$DAEMON_PIDS" sleep 1 kill `cat hg.pid` sleep 1 echo % With --prefix foo/ -hg serve -a localhost -p $HGPORT1 -d --pid-file=hg.pid -v --prefix foo/ | sed -e 's,:[0-9][0-9]*/,/,' +hg serve -a localhost -p $HGPORT1 -d --pid-file=hg.pid -v --prefix foo/ \ + | sed -e 's/:[0-9][0-9]*//g' -e 's/localhost\.localdomain/localhost/' cat hg.pid >> "$DAEMON_PIDS" sleep 1 kill `cat hg.pid` sleep 1 echo % With --prefix /foo/ -hg serve -a localhost -p $HGPORT1 -d --pid-file=hg.pid -v --prefix /foo/ | sed -e 's,:[0-9][0-9]*/,/,' +hg serve -a localhost -p $HGPORT1 -d --pid-file=hg.pid -v --prefix /foo/ \ + | sed -e 's/:[0-9][0-9]*//g' -e 's/localhost\.localdomain/localhost/' cat hg.pid >> "$DAEMON_PIDS" diff --git a/tests/test-serve.out b/tests/test-serve.out --- a/tests/test-serve.out +++ b/tests/test-serve.out @@ -1,12 +1,12 @@ % Without -v access log created - .hg/hgrc respected % With -v -listening at http://localhost/ +listening at http://localhost/ (127.0.0.1) % With --prefix foo -listening at http://localhost/foo/ +listening at http://localhost/foo/ (127.0.0.1) % With --prefix /foo -listening at http://localhost/foo/ +listening at http://localhost/foo/ (127.0.0.1) % With --prefix foo/ -listening at http://localhost/foo/ +listening at http://localhost/foo/ (127.0.0.1) % With --prefix /foo/ -listening at http://localhost/foo/ +listening at http://localhost/foo/ (127.0.0.1)