changeset 170:1481cbc12553

metadata: only store hostname if we did a successful lookup if lookup fails or is disallowed, do not store fake data. get_hostname: simplify, return None if it is disallowed or fails
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Wed, 06 Apr 2011 02:52:21 +0200
parents 0ce973659f45
children 0574bae2bf7c
files MoinMoin/items/__init__.py MoinMoin/util/edit_lock.py MoinMoin/wikiutil.py
diffstat 3 files changed, 12 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/items/__init__.py	Wed Apr 06 01:50:42 2011 +0200
+++ b/MoinMoin/items/__init__.py	Wed Apr 06 02:52:21 2011 +0200
@@ -446,8 +446,9 @@
         if remote_addr:
             if app.cfg.log_remote_addr:
                 newrev[ADDRESS] = unicode(remote_addr)
-                if app.cfg.log_reverse_dns_lookups:
-                    newrev[HOSTNAME] = unicode(wikiutil.get_hostname(remote_addr))
+                hostname = wikiutil.get_hostname(remote_addr)
+                if hostname:
+                    newrev[HOSTNAME] = hostname
         if flaskg.user.valid:
             newrev[USERID] = unicode(flaskg.user.id)
 
--- a/MoinMoin/util/edit_lock.py	Wed Apr 06 01:50:42 2011 +0200
+++ b/MoinMoin/util/edit_lock.py	Wed Apr 06 02:52:21 2011 +0200
@@ -19,7 +19,7 @@
     """
     for key in EDIT_LOCK:
         if not key in item:
-            return (False, 0.0, "", "", "")
+            return (False, 0.0, "", u"", "")
         else:
             return (True, float(item[EDIT_LOCK_TIMESTAMP]), item[EDIT_LOCK_ADDR],
                     item[EDIT_LOCK_HOSTNAME], item[EDIT_LOCK_USERID])
@@ -30,7 +30,7 @@
     """
     timestamp = time.time()
     addr = request.remote_addr
-    hostname = wikiutil.get_hostname(addr)
+    hostname = wikiutil.get_hostname(addr) or u''
     userid = flaskg.user.valid and flaskg.user.id or ''
 
     item.change_metadata()
--- a/MoinMoin/wikiutil.py	Wed Apr 06 01:50:42 2011 +0200
+++ b/MoinMoin/wikiutil.py	Wed Apr 06 02:52:21 2011 +0200
@@ -557,18 +557,18 @@
 
 def get_hostname(addr):
     """
-    Looks up the hostname depending on the configuration.
+    Looks up the DNS hostname for some IP address.
+
+    :param addr: IP address to look up (str)
+    :returns: host dns name (unicode) or
+              None (if lookup is disallowed or failed)
     """
     if app.cfg.log_reverse_dns_lookups:
         import socket
         try:
-            hostname = socket.gethostbyaddr(addr)[0]
-            hostname = unicode(hostname, config.charset)
+            return unicode(socket.gethostbyaddr(addr)[0], config.charset)
         except (socket.error, UnicodeError):
-            hostname = addr
-    else:
-        hostname = addr
-    return hostname
+            pass
 
 
 def file_headers(filename=None,