[tor-commits] [flashproxy/js] Add parse_query_string.

dcf at torproject.org dcf at torproject.org
Tue Mar 13 17:58:28 UTC 2012


commit cc8507d12cfd162e29114e999ba94d84abcbd4c6
Author: David Fifield <david at bamsoftware.com>
Date:   Tue Mar 13 09:53:01 2012 -0700

    Add parse_query_string.
    
    It's strange that this is not built in. The Note at
    http://dev.w3.org/html5/spec/Overview.html#url-encoded-form-data is
    worth reading.
---
 flashproxy.js |   37 ++++++++++++++++++++++++++++++++++++-
 1 files changed, 36 insertions(+), 1 deletions(-)

diff --git a/flashproxy.js b/flashproxy.js
index e4c7319..b68da1f 100644
--- a/flashproxy.js
+++ b/flashproxy.js
@@ -3,9 +3,44 @@ var DEFAULT_FACILITATOR_ADDR = {
     port: 9002
 };
 
+/* Parse a URL query string or application/x-www-form-urlencoded body. The
+   return type is an object mapping string keys to string values. By design,
+   this function doesn't support multiple values for the same named parameter,
+   for example "a=1&a=2&a=3"; the first definition always wins. Returns null on
+   error.
+
+   Always decodes from UTF-8, not any other encoding.
+
+   http://dev.w3.org/html5/spec/Overview.html#url-encoded-form-data */
 function parse_query_string(qs)
 {
-    return {};
+    var strings;
+    var result;
+
+    result = {};
+    if (qs)
+        strings = qs.split("&");
+    else
+        strings = {}
+    for (var i = 0; i < strings.length; i++) {
+        var string = strings[i];
+        var j, name, value;
+
+        j = string.indexOf("=");
+        if (j == -1) {
+            name = string;
+            value = "";
+        } else {
+            name = string.substr(0, j);
+            value = string.substr(j + 1);
+        }
+        name = decodeURIComponent(name.replace(/\+/, " "));
+        value = decodeURIComponent(value.replace(/\+/, " "));
+        if (!(name in result))
+             result[name] = value;
+    }
+
+    return result;
 }
 
 function format_addr(addr)





More information about the tor-commits mailing list