[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[tor-commits] [snowflake/master] Bug 31126: Use URLSearchParams instead of Query.parse.
commit fdc5563f87e8b9af80995c39081e3026f1e87915
Author: David Fifield <david@xxxxxxxxxxxxxxx>
Date: Thu Jul 18 15:41:16 2019 -0600
Bug 31126: Use URLSearchParams instead of Query.parse.
This standard interface does the same thing as Query.parse did, with a
different API on the return value. It doesn't have the problems with
keys inherited from Object.prototype that Query.parse did.
---
proxy/init-badge.js | 6 ++---
proxy/shims.js | 1 +
proxy/spec/util.spec.js | 70 ++-----------------------------------------------
proxy/util.js | 51 ++++-------------------------------
4 files changed, 11 insertions(+), 117 deletions(-)
diff --git a/proxy/init-badge.js b/proxy/init-badge.js
index 1a887a1..8646bc4 100644
--- a/proxy/init-badge.js
+++ b/proxy/init-badge.js
@@ -1,4 +1,4 @@
-/* global TESTING, Util, Query, Params, Config, DebugUI, BadgeUI, UI, Broker, Snowflake */
+/* global TESTING, Util, Params, Config, DebugUI, BadgeUI, UI, Broker, Snowflake */
/*
Entry point.
@@ -15,7 +15,7 @@ var snowflake, query, debug, silenceNotifications, log, dbg, init;
snowflake = null;
- query = Query.parse(location.search.substr(1));
+ query = new URLSearchParams(location.search);
debug = Params.getBool(query, 'debug', false);
@@ -38,7 +38,7 @@ var snowflake, query, debug, silenceNotifications, log, dbg, init;
init = function() {
var broker, config, ui;
config = new Config;
- if ('off' !== query['ratelimit']) {
+ if ('off' !== query.get('ratelimit')) {
config.rateLimitBytes = Params.getByteCount(query, 'ratelimit', config.rateLimitBytes);
}
ui = null;
diff --git a/proxy/shims.js b/proxy/shims.js
index 8c88eae..5d93183 100644
--- a/proxy/shims.js
+++ b/proxy/shims.js
@@ -13,6 +13,7 @@ if (typeof module !== "undefined" && module !== null ? module.exports : void 0)
};
chrome = {};
location = { search: '' };
+ ({ URLSearchParams } = require('url'));
if ((typeof TESTING === "undefined" || TESTING === null) || !TESTING) {
webrtc = require('wrtc');
PeerConnection = webrtc.RTCPeerConnection;
diff --git a/proxy/spec/util.spec.js b/proxy/spec/util.spec.js
index d592517..9552d56 100644
--- a/proxy/spec/util.spec.js
+++ b/proxy/spec/util.spec.js
@@ -1,4 +1,4 @@
-/* global expect, it, describe, Parse, Query, Params */
+/* global expect, it, describe, Parse, Params */
/*
jasmine tests for Snowflake utils
@@ -157,78 +157,12 @@ describe('Parse', function() {
});
-describe('query string', function() {
-
- it('should parse correctly', function() {
- expect(Query.parse('')).toEqual({});
- expect(Query.parse('a=b')).toEqual({
- a: 'b'
- });
- expect(Query.parse('a=b=c')).toEqual({
- a: 'b=c'
- });
- expect(Query.parse('a=b&c=d')).toEqual({
- a: 'b',
- c: 'd'
- });
- expect(Query.parse('client=&relay=1.2.3.4%3A9001')).toEqual({
- client: '',
- relay: '1.2.3.4:9001'
- });
- expect(Query.parse('a=b%26c=d')).toEqual({
- a: 'b&c=d'
- });
- expect(Query.parse('a%3db=d')).toEqual({
- 'a=b': 'd'
- });
- expect(Query.parse('a=b+c%20d')).toEqual({
- 'a': 'b c d'
- });
- expect(Query.parse('a=b+c%2bd')).toEqual({
- 'a': 'b c+d'
- });
- expect(Query.parse('a+b=c')).toEqual({
- 'a b': 'c'
- });
- expect(Query.parse('a=b+c+d')).toEqual({
- a: 'b c d'
- });
- });
-
- it('uses the first appearance of duplicate key', function() {
- expect(Query.parse('a=b&c=d&a=e')).toEqual({
- a: 'b',
- c: 'd'
- });
- expect(Query.parse('a')).toEqual({
- a: ''
- });
- expect(Query.parse('=b')).toEqual({
- '': 'b'
- });
- expect(Query.parse('&a=b')).toEqual({
- '': '',
- a: 'b'
- });
- expect(Query.parse('a=b&')).toEqual({
- a: 'b',
- '': ''
- });
- expect(Query.parse('a=b&&c=d')).toEqual({
- a: 'b',
- '': '',
- c: 'd'
- });
- });
-
-});
-
describe('Params', function() {
describe('bool', function() {
var getBool = function(query) {
- return Params.getBool(Query.parse(query), 'param', false);
+ return Params.getBool(new URLSearchParams(query), 'param', false);
};
it('parses correctly', function() {
diff --git a/proxy/util.js b/proxy/util.js
index 3235f79..0310899 100644
--- a/proxy/util.js
+++ b/proxy/util.js
@@ -52,45 +52,6 @@ Util.TBB_UAS = [
class Query {
- /*
- 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
- */
- static parse(qs) {
- var i, j, len, name, result, string, strings, value;
- result = {};
- strings = [];
- if (qs) {
- strings = qs.split('&');
- }
- if (0 === strings.length) {
- return result;
- }
- for (i = 0, len = strings.length; i < len; i++) {
- string = strings[i];
- j = string.indexOf('=');
- if (j === -1) {
- name = string;
- value = '';
- } else {
- name = string.substr(0, j);
- value = string.substr(j + 1);
- }
- name = decodeURIComponent(name.replace(/\+/g, ' '));
- value = decodeURIComponent(value.replace(/\+/g, ' '));
- if (!(name in result)) {
- result[name] = value;
- }
- }
- return result;
- }
-
// params is a list of (key, value) 2-tuples.
static buildString(params) {
var i, len, param, parts;
@@ -212,11 +173,11 @@ class Parse {
class Params {
static getBool(query, param, defaultValue) {
- var val;
- val = query[param];
- if (void 0 === val) {
+ if (!query.has(param)) {
return defaultValue;
}
+ var val;
+ val = query.get(param);
if ('true' === val || '1' === val || '' === val) {
return true;
}
@@ -230,12 +191,10 @@ class Params {
// '100' and '1.3m'. Returns |defaultValue| if param is not a key. Return null
// on a parsing error.
static getByteCount(query, param, defaultValue) {
- var spec;
- spec = query[param];
- if (void 0 === spec) {
+ if (!query.has(param)) {
return defaultValue;
}
- return Parse.byteCount(spec);
+ return Parse.byteCount(query.get(param));
}
}
_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits