mirror of
https://github.com/meteor/meteor.git
synced 2026-05-02 03:01:46 -04:00
allow semicolon in JSON Content-Type
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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)));
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user