[tor-commits] [flashproxy/master] Change find_client_addr to read_client_registrations.

infinity0 at torproject.org infinity0 at torproject.org
Mon Oct 28 14:47:41 UTC 2013


commit 856b6573dec23b6296a5facbd3b71ae9aa1f2fa0
Author: David Fifield <david at bamsoftware.com>
Date:   Thu Oct 17 02:55:36 2013 -0700

    Change find_client_addr to read_client_registrations.
    
    This new function yields all the registrations that exist in a message,
    and parses "client-transport".
---
 facilitator/fac.py                 |   34 +++++++++++++++++++---------------
 facilitator/facilitator-reg-daemon |   11 ++++++-----
 2 files changed, 25 insertions(+), 20 deletions(-)

diff --git a/facilitator/fac.py b/facilitator/fac.py
index 1ad1abd..8daa73c 100644
--- a/facilitator/fac.py
+++ b/facilitator/fac.py
@@ -8,6 +8,8 @@ import pwd
 import urlparse
 from collections import namedtuple
 
+DEFAULT_CLIENT_TRANSPORT = "websocket"
+
 # Return true iff the given fd is readable, writable, and executable only by its
 # owner.
 def check_perms(fd):
@@ -148,22 +150,24 @@ def format_addr(addr):
         raise ValueError("host and port may not both be None")
     return u"%s%s" % (host_str, port_str)
 
-def find_client_addr(body):
-    """Find and parse the first client line of the form
-        client=...
-    Returns None if no client line was found."""
+def get_single(qs, key, default=None):
+    try:
+        vals = qs[key]
+    except KeyError:
+        if default is None:
+            raise ValueError("missing %r key" % key)
+        vals = (default,)
+    if len(vals) != 1:
+        raise ValueError("more than one %r key" % key)
+    return vals[0]
+
+def read_client_registrations(body):
+    """Parse the lines of body and yield an Endpoint for each."""
     for line in body.splitlines():
-        try:
-            qs = urlparse.parse_qs(line, keep_blank_values=True, strict_parsing=True)
-        except ValueError:
-            continue
-        client_specs = qs["client"]
-        if len(client_specs) != 1:
-            continue
-        addr = parse_addr_spec(client_specs[0])
-        transport = "websocket"
-        return Endpoint(addr, transport)
-    return None
+        qs = urlparse.parse_qs(line, keep_blank_values=True, strict_parsing=True)
+        addr = parse_addr_spec(get_single(qs, "client"))
+        transport = get_single(qs, "client-transport", DEFAULT_CLIENT_TRANSPORT)
+        yield Endpoint(addr, transport)
 
 
 class Transport(namedtuple("Transport", "inner outer")):
diff --git a/facilitator/facilitator-reg-daemon b/facilitator/facilitator-reg-daemon
index 33921dd..04f2102 100755
--- a/facilitator/facilitator-reg-daemon
+++ b/facilitator/facilitator-reg-daemon
@@ -107,12 +107,13 @@ class Handler(SocketServer.StreamRequestHandler):
         try:
             ciphertext = b64_ciphertext.decode("base64")
             plaintext = rsa.private_decrypt(ciphertext, RSA.pkcs1_oaep_padding)
-            client_reg = fac.find_client_addr(plaintext)
-            log(u"registering %s" % safe_str(fac.format_addr(client_reg.addr)))
-            if fac.put_reg(FACILITATOR_ADDR, client_reg.addr, client_reg.transport):
-                print >> self.wfile, "OK"
+            for client_reg in fac.read_client_registrations(plaintext):
+                log(u"registering %s" % safe_str(fac.format_addr(client_reg.addr)))
+                if not fac.put_reg(FACILITATOR_ADDR, client_reg.addr, client_reg.transport):
+                    print >> self.wfile, "FAIL"
+                    break
             else:
-                print >> self.wfile, "FAIL"
+                print >> self.wfile, "OK"
         except Exception, e:
             log("error registering: %s" % str(e))
             print >> self.wfile, "FAIL"





More information about the tor-commits mailing list