From fd3f0d8e6e7bd7325d9149d0ebe1f254f5aa2473 Mon Sep 17 00:00:00 2001 From: Yosuke Furukawa Date: Fri, 13 Nov 2015 02:43:48 +0900 Subject: [PATCH] querystring: Parse multiple separator characters Fix querystring.parse to handle multiple separator characters PR-URL: https://github.com/nodejs/node/pull/3807 Reviewed-By: Colin Ihrig Reviewed-By: Brian White Reviewed-By: James M Snell --- lib/querystring.js | 3 ++- .../test-querystring-multichar-separator.js | 24 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-querystring-multichar-separator.js diff --git a/lib/querystring.js b/lib/querystring.js index 8735d2814bc307..40677ffb0f0859 100644 --- a/lib/querystring.js +++ b/lib/querystring.js @@ -202,6 +202,7 @@ QueryString.stringify = QueryString.encode = function(obj, sep, eq, options) { QueryString.parse = QueryString.decode = function(qs, sep, eq, options) { sep = sep || '&'; eq = eq || '='; + const eqLen = eq.length; var obj = {}; if (typeof qs !== 'string' || qs.length === 0) { @@ -235,7 +236,7 @@ QueryString.parse = QueryString.decode = function(qs, sep, eq, options) { if (idx >= 0) { k = decodeStr(x.substring(0, idx), decode); - v = decodeStr(x.substring(idx + 1), decode); + v = decodeStr(x.substring(idx + eqLen), decode); } else { k = decodeStr(x, decode); v = ''; diff --git a/test/parallel/test-querystring-multichar-separator.js b/test/parallel/test-querystring-multichar-separator.js new file mode 100644 index 00000000000000..257492659bdfde --- /dev/null +++ b/test/parallel/test-querystring-multichar-separator.js @@ -0,0 +1,24 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const qs = require('querystring'); + +assert.deepEqual( + qs.parse('foo=>bar&&bar=>baz', '&&', '=>'), + {foo: 'bar', bar: 'baz'} +); + +assert.strictEqual( + qs.stringify({foo: 'bar', bar: 'baz'}, '&&', '=>'), + 'foo=>bar&&bar=>baz' +); + +assert.deepEqual( + qs.parse('foo==>bar, bar==>baz', ', ', '==>'), + {foo: 'bar', bar: 'baz'} +); + +assert.strictEqual( + qs.stringify({foo: 'bar', bar: 'baz'}, ', ', '==>'), + 'foo==>bar, bar==>baz' +);