Skip to content

Commit

Permalink
[api] Allow adding custom headers to API responses
Browse files Browse the repository at this point in the history
  • Loading branch information
ar2rsawseen committed Aug 18, 2016
1 parent 3e5d620 commit be13a30
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
3 changes: 2 additions & 1 deletion api/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ plugins.setConfigs("api", {
event_segmentation_value_limit:1000,
sync_plugins: false,
session_cooldown: 15,
total_users: true
total_users: true,
additional_headers: ""
});

plugins.setConfigs("apps", {
Expand Down
28 changes: 26 additions & 2 deletions api/utils/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -432,8 +432,20 @@ var common = {},
};

common.returnMessage = function (params, returnCode, message) {
//set provided in configuration headers
var headers = {'Content-Type': 'application/json; charset=utf-8', 'Access-Control-Allow-Origin':'*'};
var add_headers = plugins.getConfig("api").additional_headers.replace(/\r\n|\r|\n|\/n/g, "\n").split("\n");
var parts;
for(var i = 0; i < add_headers.length; i++){
if(add_headers[i] && add_headers[i].length){
parts = add_headers[i].split(/:(.+)?/);
if(parts.length == 3){
headers[parts[0]] = parts[1];
}
}
}
if (params && params.res && !params.blockResponses) {
params.res.writeHead(returnCode, {'Content-Type': 'application/json; charset=utf-8', 'Access-Control-Allow-Origin':'*'});
params.res.writeHead(returnCode, headers);
if (params.qstring.callback) {
params.res.write(params.qstring.callback + '(' + JSON.stringify({result: message}, escape_html_entities) + ')');
} else {
Expand All @@ -445,8 +457,20 @@ var common = {},
};

common.returnOutput = function (params, output) {
//set provided in configuration headers
var headers = {'Content-Type': 'application/json; charset=utf-8', 'Access-Control-Allow-Origin':'*'};
var add_headers = plugins.getConfig("api").additional_headers.replace(/\r\n|\r|\n|\/n/g, "\n").split("\n");
var parts;
for(var i = 0; i < add_headers.length; i++){
if(add_headers[i] && add_headers[i].length){
parts = add_headers[i].split(/:(.+)?/);
if(parts.length == 3){
headers[parts[0]] = parts[1];
}
}
}
if (params && params.res && !params.blockResponses) {
params.res.writeHead(200, {'Content-Type': 'application/json; charset=utf-8', 'Access-Control-Allow-Origin':'*'});
params.res.writeHead(200, headers);
if (params.qstring.callback) {
params.res.write(params.qstring.callback + '(' + JSON.stringify(output, escape_html_entities) + ')');
} else {
Expand Down
5 changes: 5 additions & 0 deletions plugins/plugins/frontend/public/javascripts/countly.views.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ window.ConfigurationsView = countlyView.extend({
"api-sync_plugins":jQuery.i18n.map["configs.api-sync_plugins"],
"api-session_cooldown":jQuery.i18n.map["configs.api-session_cooldown"],
"api-total_users":jQuery.i18n.map["configs.api-total_users"],
"api-additional_headers":jQuery.i18n.map["configs.api-additional_headers"],
"apps-country":jQuery.i18n.map["configs.apps-country"],
"apps-category":jQuery.i18n.map["configs.apps-category"]
};
Expand Down Expand Up @@ -244,6 +245,10 @@ window.ConfigurationsView = countlyView.extend({
return '<textarea rows="5" style="width:100%" id="frontend-additional_headers">'+(value || "")+'</textarea>';
});

this.registerInput("api-additional_headers", function(value){
return '<textarea rows="5" style="width:100%" id="api-additional_headers">'+(value || "")+'</textarea>';
});

this.registerInput("apps-timezone", function(value){
return null;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ configs.api-event_segmentation_value_limit = Max unique values in each segmentat
configs.api-sync_plugins = Sync plugin states
configs.api-session_cooldown = Session cooldown (seconds)
configs.api-total_users = Total users
configs.api-additional_headers = Additional HTTP Response headers
configs.apps-country = Default Country
configs.apps-category = Default Category
configs.account-settings = Account Settings
Expand Down Expand Up @@ -92,3 +93,4 @@ configs.help.crashes-report_limit = Amount of reports to display in each crash g
configs.help.frontend-use_google = Disable this, if you are in the country where Google services are blocked
configs.help.frontend-code = Display links to Countly Code generator
configs.help.frontend-additional_headers = Add headers for Countly to use on Dashboard responses by default (one header per new line)
configs.help.api-additional_headers = Add headers for Countly to use on API responses by default (one header per new line)

0 comments on commit be13a30

Please sign in to comment.