diff --git a/lib/engine.js b/lib/engine.js index 327b8f9..a69f1a6 100644 --- a/lib/engine.js +++ b/lib/engine.js @@ -68,7 +68,31 @@ var sendResponse = function(jDoc, myRes, ip, bid, callback, gzip) { var responseHeaders = {'Content-Type': responseType, 'vary': 'Accept-Encoding', 'max-age': jDoc.secleft, 'cache-control': 'public, max-age='+jDoc.secleft+', no-transform', "Expires": jDoc.expires, "Last-Modified": jDoc.lastModified}; - doc = JSON.stringify(jDoc); + var ext = bid.match(/\.[\w]+$/); + if (ext && (ext = ext[0])) { + // When the bid has an extension, we only return the first api. + var first = _.find(jDoc, function(api, key) { + return api && api.cname; + }); + doc = first.result; + + switch (ext) { + case ".kml": + case ".xml": + // When expecting response is KML or XML, we set the correct Content-Type + // and wrap it with quotes if it's a JSONP request. + responseHeaders['Content-Type'] = 'text/xml'; + callback && (doc = "'"+ doc+"'"); + break; + case ".json": + // Content-Type for JSON is already set above. + doc = JSON.stringify(doc); + default: + break; + } + } else { + doc = JSON.stringify(jDoc); + } if (callback) { doc = callback + '(' + doc + ');';