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

[tor-commits] [flashproxy/master] Patch for ticket #7110, adding a get_query_param_boolean() function.



commit 3001f18b435ee9192bb4538acbf646f3ae1e063b
Author: Alexandre Allaire <aallai@xxxxxxxxxxxxx>
Date:   Wed Oct 17 13:03:44 2012 -0400

    Patch for ticket #7110, adding a get_query_param_boolean() function.
---
 proxy/flashproxy-test.js |   37 +++++++++++++++++++++++++++++++++++++
 proxy/flashproxy.js      |   32 +++++++++++++++++++++++++++-----
 2 files changed, 64 insertions(+), 5 deletions(-)

diff --git a/proxy/flashproxy-test.js b/proxy/flashproxy-test.js
index 2dc185a..f38182e 100755
--- a/proxy/flashproxy-test.js
+++ b/proxy/flashproxy-test.js
@@ -159,6 +159,42 @@ function test_parse_query_string()
     }
 }
 
+function test_get_query_param_boolean()
+{
+    var TESTS = [
+        {qs: "param=true",
+         expected: true},
+        {qs: "param",
+         expected: true},
+        {qs: "param=",
+         expected: true},
+        {qs: "param=1",
+         expected: true},
+        {qs: "param=0",
+         expected: false},
+        {qs: "param=false",
+         expected: false},
+        {qs: "param=unexpected",
+         expected: null},
+        {qs: "pram=true",
+         expected: false},
+    ];
+
+    announce("test_get_query_param_boolean");
+    for (var i = 0; i < TESTS.length; i++) {
+        var test = TESTS[i];
+        var actual;
+        var query;
+
+        query = parse_query_string(test.qs);
+        actual = get_query_param_boolean(query, "param", false);
+        if (objects_equal(actual, test.expected))
+            pass(test.qs);
+        else
+            fail(test.qs, test.expected, actual);
+    }
+}
+
 function test_parse_addr_spec()
 {
     var TESTS = [
@@ -230,6 +266,7 @@ function test_get_query_param_addr()
 
 test_build_url();
 test_parse_query_string();
+test_get_query_param_boolean();
 test_parse_addr_spec();
 test_get_query_param_addr();
 
diff --git a/proxy/flashproxy.js b/proxy/flashproxy.js
index 327c9a5..ec7d550 100644
--- a/proxy/flashproxy.js
+++ b/proxy/flashproxy.js
@@ -7,9 +7,10 @@
  * information from the facilitator. When this option is used, the facilitator
  * query is not done. The "relay" parameter must be given as well.
  *
- * debug=1
- * If set (to any value), show verbose terminal-like output instead of the
- * badge.
+ * debug=<value>
+ * If set to true, 1, or present in the query string with no value, show
+ * verbose terminal-like output instead of the badge. To disable debug output,
+ * omit the debug parameter, or set it to false or 0.
  *
  * facilitator=https://host:port/
  * The URL of the facilitator CGI script. By default it is
@@ -67,9 +68,10 @@ var RATE_LIMIT_HISTORY = 5.0;
 var WebSocket = window.WebSocket || window.MozWebSocket;
 
 var query = parse_query_string(window.location.search.substr(1));
+var DEBUG = get_query_param_boolean(query, "debug", false);
 var debug_div;
 
-if (query.debug) {
+if (DEBUG) {
     debug_div = document.createElement("pre");
     debug_div.className = "debug";
 }
@@ -178,6 +180,26 @@ function build_url(scheme, host, port, path, params) {
     return parts.join("");
 }
 
+/* Get a query string parameter and return it as a boolean, or return
+    default_val if param is not present in the query string.
+    Parameter values of "true", "1" and "" evalutate to true.
+    "false" and "0" evalutate to false.
+    Any other value evaluates to null.
+*/
+function get_query_param_boolean(query, param, default_val) {
+    var val;
+
+    val = query[param];
+    if (val === undefined)
+        return default_val;
+    else if (val === "true" || val === "1" || val === "")
+        return true;
+    else if (val === "false" || val === "0")
+        return false;
+    else
+        return null;
+}
+
 /* Get a query string parameter and return it as a string. Returns default_val
    if param is not defined in the query string. */
 function get_query_param_string(query, param, default_val) {
@@ -358,7 +380,7 @@ function make_websocket(addr) {
 }
 
 function FlashProxy() {
-    if (query.debug) {
+    if (DEBUG) {
         this.badge_elem = debug_div;
     } else {
         this.badge = new Badge();



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