[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [flashproxy/master] Move get_reg and put_reg into fac.py.



commit 9c570dd3a0d8c9c86c92b9bab7a0543dfa92e4c9
Author: David Fifield <david@xxxxxxxxxxxxxxx>
Date:   Wed Sep 19 15:21:29 2012 -0700

    Move get_reg and put_reg into fac.py.
---
 facilitator/fac.py          |   53 +++++++++++++++++++++++++++++++++++++++
 facilitator/facilitator.cgi |   58 +-----------------------------------------
 2 files changed, 55 insertions(+), 56 deletions(-)

diff --git a/facilitator/fac.py b/facilitator/fac.py
index 494b378..cb77ccc 100644
--- a/facilitator/fac.py
+++ b/facilitator/fac.py
@@ -181,3 +181,56 @@ def render_transaction(command, *params):
     for key, value in params:
         parts.append("%s=%s" % (key, quote_string(value)))
     return " ".join(parts)
+
+def fac_socket(facilitator_addr):
+    return socket.create_connection(facilitator_addr, 1.0).makefile()
+
+def transact(f, command, *params):
+    transaction = render_transaction(command, *params)
+    print >> f, transaction
+    f.flush()
+    line = f.readline()
+    if not (len(line) > 0 and line[-1] == '\n'):
+        raise ValueError("No newline at end of string returned by facilitator")
+    return parse_transaction(line[:-1])
+
+def put_reg(facilitator_addr, client_addr, registrant_addr):
+    """Send a registration to the facilitator using a one-time socket. Returns
+    true iff the command was successful."""
+    f = fac_socket(facilitator_addr)
+    try:
+        command, params = transact(f, "PUT", ("CLIENT", format_addr(client_addr)), ("FROM", format_addr(registrant_addr)))
+    finally:
+        f.close()
+    return command == "OK"
+
+def get_reg(facilitator_addr, proxy_addr):
+    """Get a registration from the facilitator using a one-time socket. Returns
+    a dict with keys "client" and "relay" if successful, or a dict with the key
+    "client" mapped to the value "" if there are no registrations available for
+    proxy_addr. Raises an exception otherwise."""
+    f = fac_socket(facilitator_addr)
+    try:
+        command, params = transact(f, "GET", ("FROM", format_addr(proxy_addr)))
+    finally:
+        f.close()
+    if command == "NONE":
+        return {
+            "client": ""
+        }
+    elif command == "OK":
+        client_spec = param_first("CLIENT", params)
+        relay_spec = param_first("RELAY", params)
+        if not client_spec:
+            raise ValueError("Facilitator did not return CLIENT")
+        if not relay_spec:
+            raise ValueError("Facilitator did not return RELAY")
+        # Check the syntax returned by the facilitator.
+        client = parse_addr_spec(client_spec)
+        relay = parse_addr_spec(relay_spec)
+        return {
+            "client": format_addr(client),
+            "relay": format_addr(relay),
+        }
+    else:
+        raise ValueError("Facilitator response was not \"OK\"")
diff --git a/facilitator/facilitator.cgi b/facilitator/facilitator.cgi
index 6ccb479..2ef4cfa 100755
--- a/facilitator/facilitator.cgi
+++ b/facilitator/facilitator.cgi
@@ -17,57 +17,6 @@ Status: %d\r
 \r""" % status
     sys.exit()
 
-def fac_socket():
-    return socket.create_connection(FACILITATOR_ADDR, 1.0).makefile()
-
-def transact(f, command, *params):
-    transaction = fac.render_transaction(command, *params)
-    print >> f, transaction
-    f.flush()
-    line = f.readline()
-    if not (len(line) > 0 and line[-1] == '\n'):
-        raise ValueError("No newline at end of string returned by facilitator")
-    return fac.parse_transaction(line[:-1])
-
-def put_reg(client_addr, registrant_addr):
-    f = fac_socket()
-    try:
-        command, params = transact(f, "PUT", ("CLIENT", fac.format_addr(client_addr)), ("FROM", fac.format_addr(registrant_addr)))
-    finally:
-        f.close()
-    if command == "OK":
-        pass
-    else:
-        exit_error(500)
-
-def get_reg(proxy_addr):
-    f = fac_socket()
-    try:
-        command, params = transact(f, "GET", ("FROM", fac.format_addr(proxy_addr)))
-    finally:
-        f.close()
-    if command == "NONE":
-        return {
-            "client": ""
-        }
-    elif command == "OK":
-        client_spec = fac.param_first("CLIENT", params)
-        relay_spec = fac.param_first("RELAY", params)
-        if not client_spec or not relay_spec:
-            exit_error(500)
-        try:
-            # Check the syntax returned by the backend.
-            client = fac.parse_addr_spec(client_spec)
-            relay = fac.parse_addr_spec(relay_spec)
-        except ValueError:
-            exit_error(500)
-        return {
-            "client": fac.format_addr(client),
-            "relay": fac.format_addr(relay),
-        }
-    else:
-        exit_error(500)
-
 method = os.environ.get("REQUEST_METHOD")
 path_info = os.environ.get("PATH_INFO")
 proxy_addr = (os.environ.get("REMOTE_ADDR"), None)
@@ -83,7 +32,7 @@ def do_get():
     if path != "/":
         exit_error(400)
     try:
-        reg = get_reg(proxy_addr) or ""
+        reg = fac.get_reg(FACILITATOR_ADDR, proxy_addr) or ""
     except:
         exit_error(500)
     # Allow XMLHttpRequest from any domain. http://www.w3.org/TR/cors/.
@@ -106,10 +55,7 @@ def do_post():
         client_addr = fac.parse_addr_spec(client_spec, defhost=proxy_addr[0])
     except ValueError:
         exit_error(400)
-    try:
-        put_reg(client_addr, proxy_addr)
-    except:
-        raise
+    if not fac.put_reg(FACILITATOR_ADDR, client_addr, proxy_addr):
         exit_error(500)
     print """\
 Status: 200\r



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits