allow semicolon in JSON Content-Type

This commit is contained in:
David Greenspan
2012-06-05 16:46:50 -07:00
parent 0dd3992c94
commit 80bef5ea9b
4 changed files with 20 additions and 19 deletions

View File

@@ -125,15 +125,7 @@ Meteor.http = Meteor.http || {};
response.headers[m[1].toLowerCase()] = m[2];
});
// only parse data if correct content type.
if (_.include(['application/json', 'text/javascript'],
response.headers['content-type'])) {
try {
response.data = JSON.parse(response.content);
} catch (err) {
response.data = null;
}
};
Meteor.http._populateData(response);
var error = null;
if (xhr.status >= 400)

View File

@@ -35,6 +35,23 @@ Meteor.http = Meteor.http || {};
return url;
};
Meteor.http._populateData = function(response) {
// Read Content-Type header, up to a ';' if there is one.
// A typical header might be "application/json; charset=utf-8"
// or just "application/json".
var contentType = (response.headers['content-type'] || ';').split(';')[0];
// Only try to parse data as JSON if server sets correct content type.
if (_.include(['application/json', 'text/javascript'], contentType)) {
try {
response.data = JSON.parse(response.content);
} catch (err) {
response.data = null;
}
} else {
response.data = null;
}
};
Meteor.http.get = function (/* varargs */) {
return Meteor.http.call.apply(this, ["GET"].concat(_.toArray(arguments)));

View File

@@ -111,15 +111,7 @@ Meteor.http = Meteor.http || {};
response.content = body;
response.headers = res.headers;
// only parse data if correct content type.
if (_.include(['application/json', 'text/javascript'],
response.headers['content-type'])) {
try {
response.data = JSON.parse(response.content);
} catch (err) {
response.data = null;
}
};
Meteor.http._populateData(response);
if (res.statusCode >= 400)
error = new Error("failed");

View File

@@ -68,7 +68,7 @@ var respond = function(req, res) {
response_string = JSON.stringify(response_data);
res.statusCode = 200;
res.setHeader("Content-Type", "application/json");
res.setHeader("Content-Type", "application/json; charset=utf-8");
res.end(response_string);
});