mirror of
https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake.git
synced 2025-10-13 20:11:19 -04:00
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.
This commit is contained in:
parent
76b81bcf77
commit
fdc5563f87
4 changed files with 11 additions and 117 deletions
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue