From d0b1e2dd7127f5262a541e8cb5339c090a7243df Mon Sep 17 00:00:00 2001 From: coderaiser Date: Sun, 2 Mar 2014 08:12:23 -0500 Subject: [PATCH] refactor(main) generateHeaders --- lib/server/main.js | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/lib/server/main.js b/lib/server/main.js index 030c4663..99703cff 100644 --- a/lib/server/main.js +++ b/lib/server/main.js @@ -197,37 +197,40 @@ * query * https://developers.google.com/speed/docs/best-practices/caching?hl=ru#LeverageProxyCaching */ - function generateHeaders(pParams) { - var lRet = Util.checkObjTrue(pParams, ['name']); + function generateHeaders(params) { + var header, p, extension, type, encoding, isContain, cmp; - if (lRet) { - var p = pParams, - lExt = Util.getExtension(p.name), - lType = ext[lExt] || 'text/plain', - lContentEncoding = ''; + if (params.name) { + p = params, + extension = Util.getExtension(p.name), + type = ext[extension] || 'text/plain', + encoding = ''; /* if type of file any, but img - then we shoud specify charset */ - if (!Util.isContainStr(lType, 'img')) - lContentEncoding = '; charset=UTF-8'; + isContain = Util.isContainStr(type, ['img', 'image']); + if (!isContain) + encoding = '; charset=UTF-8'; - if (Util.isContainStr(p.query, 'download')) - lType = 'application/octet-stream'; + isContain = Util.isContainStr(p.query, 'download'); + if (isContain) + type = 'application/octet-stream'; - lRet = { + header = { 'Access-Control-Allow-Origin' : '*', - 'Content-Type' : lType + lContentEncoding, - 'last-modified' : new Date().toString(), + 'Content-Type' : type + encoding, + 'last-modified' : '' + new Date(), 'Vary' : 'Accept-Encoding' }; - if (!Util.strCmp(lExt, '.appcache') && p.cache) - lRet['cache-control'] = 'max-age=' + 31337 * 21; + cmp = Util.strCmp(ext, '.appcache'); + if (!cmp && p.cache) + header['cache-control'] = 'max-age=' + 31337 * 21; if (p.gzip) - lRet['content-encoding'] = 'gzip'; + header['content-encoding'] = 'gzip'; } - return lRet; + return header; } function mainSetHeader(pParams) {