Skip to content

Commit

Permalink
looser encoded parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
chjj committed Nov 23, 2011
1 parent 5bd2053 commit 97da149
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
31 changes: 20 additions & 11 deletions lib/encoded.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@
*/

var EventEmitter = require('events').EventEmitter
, StringDecoder = require('string_decoder').StringDecoder
, set = require('./qs').set;
, StringDecoder = require('string_decoder').StringDecoder;

var qs = require('./qs')
, unescape = qs.unescape
, set = qs.set;

/**
* Character Constants
*/

var AMP = '&'.charCodeAt(0)
, EQUAL = '='.charCodeAt(0);
Expand Down Expand Up @@ -84,7 +91,16 @@ Parser.prototype._parse = function(data) {
j = i + 1;
break;
case AMP:
return this._error('Unexpected AMP.');
// they did this: ...&key&...
// no state change
// this.state = 'key';
this.buff += this.decode.write(data.slice(j, i));
this.key = unescape(this.buff);
this.emit('value', this.key, '');
this.buff = '';
this.key = '';
j = i + 1;
break;
}
break;
case 'value':
Expand All @@ -98,6 +114,7 @@ Parser.prototype._parse = function(data) {
j = i + 1;
break;
case EQUAL:
// this should be encoded
return this._error('Unexpected EQUAL.');
}
break;
Expand All @@ -121,14 +138,6 @@ Parser.prototype.destroy = function() {
this.readable = false;
};

var unescape = function(str) {
try {
str = decodeURIComponent(str.replace(/\+/g, ' '));
} finally {
return str.replace(/\0/g, '');
}
};

/**
* Middleware
*/
Expand Down
10 changes: 10 additions & 0 deletions lib/qs.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ var unescape = function(str) {
}
};

/**
* Nested Fields
*/

var set = function(field, part, parts) {
var obj = parts
, name = field.split('[')
Expand Down Expand Up @@ -64,8 +68,14 @@ var set = function(field, part, parts) {
}
};

/**
* Expose
*/

exports = qs;

exports.parse = qs;
exports.set = set;
exports.unescape = unescape;

module.exports = exports;

0 comments on commit 97da149

Please sign in to comment.