mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-07-28 18:23:35 +00:00
function generateHeaders moved to main module
This commit is contained in:
parent
d986d358bc
commit
894e8fc8bd
3 changed files with 94 additions and 88 deletions
|
|
@ -4,7 +4,18 @@
|
|||
var DIR,
|
||||
LIBDIR,
|
||||
SRVDIR,
|
||||
Util;
|
||||
Util,
|
||||
|
||||
Extensions = {
|
||||
'.css' : 'text/css',
|
||||
'.js' : 'text/javascript',
|
||||
'.png' : 'image/png',
|
||||
'.json' : 'application/json',
|
||||
'.html' : 'text/html',
|
||||
'.woff' : 'font/woff',
|
||||
'.appcache' : 'text/cache-manifest',
|
||||
'.mp3' : 'audio/mpeg'
|
||||
};
|
||||
|
||||
/* Constants */
|
||||
exports.DIR = DIR = process.cwd() + '/',
|
||||
|
|
@ -13,6 +24,7 @@
|
|||
exports.WIN32 = isWin32();
|
||||
|
||||
/* Functions */
|
||||
exports.generateHeaders = generateHeaders,
|
||||
exports.require = mrequire,
|
||||
exports.librequire = librequire,
|
||||
exports.srvrequire = srvrequire,
|
||||
|
|
@ -74,4 +86,53 @@
|
|||
*/
|
||||
function isWin32(){ return process.platform === 'win32'; }
|
||||
|
||||
/**
|
||||
* Функция создаёт заголовки файлов
|
||||
* в зависимости от расширения файла
|
||||
* перед отправкой их клиенту
|
||||
* @param pName - имя файла
|
||||
* @param pGzip - данные сжаты gzip'ом
|
||||
*/
|
||||
function generateHeaders(pName, pGzip, pQuery){
|
||||
var lType = '',
|
||||
lCacheControl = 0,
|
||||
lContentEncoding = '',
|
||||
lRet,
|
||||
|
||||
lDot = pName.lastIndexOf('.'),
|
||||
lExt = pName.substr(lDot);
|
||||
|
||||
if( Util.strCmp(lExt, '.appcache') )
|
||||
lCacheControl = 1;
|
||||
|
||||
lType = Extensions[lExt] || 'text/plain';
|
||||
|
||||
if( !Util.isContainStr(lType, 'img') )
|
||||
lContentEncoding = '; charset=UTF-8';
|
||||
|
||||
if(Util.strCmp(pQuery, 'download') )
|
||||
lType = 'application/octet-stream';
|
||||
|
||||
|
||||
if(!lCacheControl)
|
||||
lCacheControl = 31337 * 21;
|
||||
|
||||
lRet = {
|
||||
/* if type of file any, but img -
|
||||
* then we shoud specify charset
|
||||
*/
|
||||
'Content-Type': lType + lContentEncoding,
|
||||
'cache-control': 'max-age=' + lCacheControl,
|
||||
'last-modified': new Date().toString(),
|
||||
/* https://developers.google.com/speed/docs/best-practices
|
||||
/caching?hl=ru#LeverageProxyCaching */
|
||||
'Vary': 'Accept-Encoding'
|
||||
};
|
||||
|
||||
if(pGzip)
|
||||
lRet['content-encoding'] = 'gzip';
|
||||
|
||||
return lRet;
|
||||
}
|
||||
|
||||
})();
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@
|
|||
console.log(lUrl);
|
||||
console.log(lMethod);
|
||||
|
||||
if( Util.isContainStr(lUrl, APIURL) ){
|
||||
if( Util.isContainStr(lUrl, APIURL) ){
|
||||
lRes.end(APIURL);
|
||||
lRet = true;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue