Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove controls unicode #77

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion lib/urllib.js
Original file line number Diff line number Diff line change
Expand Up @@ -666,12 +666,26 @@ exports.requestWithCallback = function (url, args, callback) {
};

function parseJSON(data) {
var escMap = {
'"': '\\"', // \u0022
'\\': '\\\\', // \u005c
'\b': '\\b', // \u0008
'\f': '\\f', // \u000c
'\n': '\\n', // \u000a
'\r': '\\r', // \u000d
'\t': '\\t' // \u0009
};
var escFunc = function (m) {
return escMap[m] || '\\u' + (m.charCodeAt(0) + 0x10000).toString(16).substr(1);
};
var escRE = /[\u0000-\u001F\u005C]/g;

var result = {
error: null,
data: null
};
try {
result.data = JSON.parse(data);
result.data = JSON.parse(data.replace(escRE, escFunc));
} catch (err) {
if (err.name === 'SyntaxError') {
err.name = 'JSONResponseFormatError';
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ var server = http.createServer(function (req, res) {
} else if (req.url === '/errorcharset') {
res.setHeader('Content-Type', 'text/plain;charset=notfound');
return res.end('你好');
} else if (req.url === '/json_with_controls_unicode') {
res.writeHeader(200);
return res.end(new Buffer('{"foo":"\u000e"}'));
}

var url = req.url.split('?');
Expand Down
12 changes: 12 additions & 0 deletions test/urllib.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1161,4 +1161,16 @@ describe('urllib.test.js', function () {
});
});
});

describe('json string with controls unicode', function() {
it('should handle GET /json_with_controls_unicode with dataType=json', function (done) {
urllib.request(host + '/json_with_controls_unicode', {
dataType: 'json'
}, function (err, data, res) {
res.should.status(200);
data.should.eql({foo:"\u000e"});
done();
});
});
});
});