Skip to content

Commit

Permalink
Merge pull request socketio#943 from nus-fboa2016-si/queryStringFix
Browse files Browse the repository at this point in the history
Fix for Issue socketio#331
  • Loading branch information
rauchg committed Apr 12, 2016
2 parents 2f7c930 + 4fff66d commit 2802463
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 8 deletions.
23 changes: 20 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,27 @@ function lookup (uri, opts) {
}
io = cache[id];
}

return io.socket(parsed.path);
if (parsed.query && !opts.query) {
opts.query = parsed.query;
} else if (opts && 'object' === typeof opts.query) {
opts.query = encodeQueryString(opts.query);
}
return io.socket(parsed.path, opts);
}
/**
* Helper method to parse query objects to string.
* @param {object} query
* @returns {string}
*/
function encodeQueryString (obj) {
var str = [];
for (var p in obj) {
if (obj.hasOwnProperty(p)) {
str.push(encodeURIComponent(p) + '=' + encodeURIComponent(obj[p]));
}
}
return str.join('&');
}

/**
* Protocol version.
*
Expand Down
7 changes: 4 additions & 3 deletions lib/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ Manager.prototype.maybeReconnectOnOpen = function () {
*/

Manager.prototype.open =
Manager.prototype.connect = function (fn) {
Manager.prototype.connect = function (fn, opts) {
debug('readyState %s', this.readyState);
if (~this.readyState.indexOf('open')) return this;

Expand Down Expand Up @@ -350,10 +350,10 @@ Manager.prototype.onerror = function (err) {
* @api public
*/

Manager.prototype.socket = function (nsp) {
Manager.prototype.socket = function (nsp, opts) {
var socket = this.nsps[nsp];
if (!socket) {
socket = new Socket(this, nsp);
socket = new Socket(this, nsp, opts);
this.nsps[nsp] = socket;
var self = this;
socket.on('connecting', onConnecting);
Expand Down Expand Up @@ -400,6 +400,7 @@ Manager.prototype.destroy = function (socket) {
Manager.prototype.packet = function (packet) {
debug('writing packet %j', packet);
var self = this;
if (packet.query && packet.type === 0) packet.nsp += '?' + packet.query;

if (!self.encoding) {
// encode, then write to engine with result
Expand Down
11 changes: 9 additions & 2 deletions lib/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var emit = Emitter.prototype.emit;
* @api public
*/

function Socket (io, nsp) {
function Socket (io, nsp, opts) {
this.io = io;
this.nsp = nsp;
this.json = this; // compat
Expand All @@ -62,6 +62,9 @@ function Socket (io, nsp) {
this.sendBuffer = [];
this.connected = false;
this.disconnected = true;
if (opts && opts.query) {
this.query = opts.query;
}
if (this.io.autoConnect) this.open();
}

Expand Down Expand Up @@ -183,7 +186,11 @@ Socket.prototype.onopen = function () {

// write connect packet if necessary
if ('/' !== this.nsp) {
this.packet({ type: parser.CONNECT });
if (this.query) {
this.packet({type: parser.CONNECT, query: this.query});
} else {
this.packet({type: parser.CONNECT});
}
}
};

Expand Down
13 changes: 13 additions & 0 deletions test/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,17 @@ describe('socket', function () {
socket.compress(false).emit('hi');
});
});

it('should store query string as a property', function (done) {
var socket = io('/abc', {query: {a: 'b'}}); // passes in as a query obj
var socket2 = io('/abcd?b=c&d=e'); // passes in as a query string
var socket3 = io('/abc', {query: {'&a': '&=?a'}}); // checks that it encodes a string
expect(socket.query).to.be('a=b');
expect(socket2.query).to.be('b=c&d=e');
expect(socket3.query).to.be('%26a=%26%3D%3Fa');
socket.disconnect();
socket2.disconnect();
socket3.disconnect();
done();
});
});

0 comments on commit 2802463

Please sign in to comment.