[tor-commits] [flashproxy/master] Allow format_addr to have a None host or port.

dcf at torproject.org dcf at torproject.org
Fri Aug 31 11:39:36 UTC 2012


commit 853a4fddff28710b893e08f6f807fac25b4efb42
Author: David Fifield <david at bamsoftware.com>
Date:   Sat Jul 28 09:15:47 2012 -0700

    Allow format_addr to have a None host or port.
---
 fac.py |   31 +++++++++++++++++++------------
 1 files changed, 19 insertions(+), 12 deletions(-)

diff --git a/fac.py b/fac.py
index ab8157c..8cf030d 100644
--- a/fac.py
+++ b/fac.py
@@ -38,18 +38,25 @@ def parse_addr_spec(spec, defhost = None, defport = None):
 
 def format_addr(addr):
     host, port = addr
-    if not host:
-        return u":%d" % port
-    # Numeric IPv6 address?
-    try:
-        addrs = socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM, socket.IPPROTO_TCP, socket.AI_NUMERICHOST)
-        af = addrs[0][0]
-    except socket.gaierror, e:
-        af = 0
-    if af == socket.AF_INET6:
-        return u"[%s]:%d" % (host, port)
-    else:
-        return u"%s:%d" % (host, port)
+    host_str = u""
+    port_str = u""
+    if host is not None:
+        # Numeric IPv6 address?
+        try:
+            addrs = socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM, socket.IPPROTO_TCP, socket.AI_NUMERICHOST)
+            af = addrs[0][0]
+        except socket.gaierror, e:
+            af = 0
+        if af == socket.AF_INET6:
+            host_str = u"[%s]" % host
+        else:
+            host_str = u"%s" % host
+    if port is not None:
+        port_str = u":%d" % port
+
+    if not host_str and not port_str:
+        raise ValueError("host and port may not both be None")
+    return u"%s%s" % (host_str, port_str)
 
 def skip_space(pos, line):
     """Skip a (possibly empty) sequence of space characters (the ASCII character





More information about the tor-commits mailing list