mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-07-21 02:29:23 +00:00
refactored
This commit is contained in:
parent
3d34f15f4d
commit
e3789e3eb8
7 changed files with 205 additions and 209 deletions
106
lib/server.js
106
lib/server.js
|
|
@ -31,15 +31,9 @@
|
|||
Socket = main.socket,
|
||||
|
||||
http = main.http,
|
||||
zlib = main.zlib,
|
||||
fs = main.fs,
|
||||
Util = main.util,
|
||||
ext = main.ext,
|
||||
|
||||
Server, Rest, Route, Minimize, Port, IP,
|
||||
|
||||
OK = 200,
|
||||
FILE_NOT_FOUND = 404;
|
||||
Server, Rest, Route, Minimize, Port, IP;
|
||||
|
||||
/* базовая инициализация */
|
||||
function init(pAppCachProcessing){
|
||||
|
|
@ -111,7 +105,7 @@
|
|||
}
|
||||
}else
|
||||
Util.log('Cloud Commander testing mode');
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
|
|
@ -168,7 +162,7 @@
|
|||
function(pParams){
|
||||
var lSendName = pParams && pParams.name || lName;
|
||||
|
||||
sendFile({
|
||||
main.sendFile({
|
||||
name : lSendName,
|
||||
request : pReq,
|
||||
response : pRes
|
||||
|
|
@ -183,99 +177,7 @@
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Функция создаёт заголовки файлов
|
||||
* в зависимости от расширения файла
|
||||
* перед отправкой их клиенту
|
||||
* @param pName - имя файла
|
||||
* @param pGzip - данные сжаты gzip'ом
|
||||
*/
|
||||
function generateHeaders(pName, pGzip, pQuery){
|
||||
var lRet,
|
||||
lType = '',
|
||||
lContentEncoding = '',
|
||||
lCacheControl = 0,
|
||||
|
||||
lExt = Util.getExtension(pName);
|
||||
|
||||
if( Util.strCmp(lExt, '.appcache') )
|
||||
lCacheControl = 1;
|
||||
|
||||
lType = ext[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;
|
||||
}
|
||||
|
||||
/**
|
||||
* send file to client thru pipe
|
||||
* and gzip it if client support
|
||||
*
|
||||
* @param pName - имя файла
|
||||
* @param pGzip - данные сжаты gzip'ом
|
||||
*/
|
||||
function sendFile(pParams){
|
||||
var lRet,
|
||||
lName, lReq, lRes;
|
||||
|
||||
if(pParams){
|
||||
lName = pParams.name,
|
||||
lReq = pParams.request,
|
||||
lRes = pParams.response;
|
||||
}
|
||||
|
||||
if(lName && lRes && lReq){
|
||||
var lEnc = lReq.headers['accept-encoding'] || '',
|
||||
lGzip = lEnc.match(/\bgzip\b/),
|
||||
|
||||
lReadStream = fs.createReadStream(lName, {
|
||||
'bufferSize': 4 * 1024
|
||||
});
|
||||
|
||||
lReadStream.on('error', function(pError){
|
||||
lRes.writeHead(FILE_NOT_FOUND, 'OK');
|
||||
lRes.end(String(pError));
|
||||
});
|
||||
|
||||
lRes.writeHead(OK, generateHeaders(lName, lGzip) );
|
||||
|
||||
if (lGzip)
|
||||
lReadStream = lReadStream.pipe( zlib.createGzip() );
|
||||
|
||||
lReadStream.pipe(lRes);
|
||||
|
||||
lRet = true;
|
||||
}
|
||||
|
||||
return lRet;
|
||||
}
|
||||
|
||||
exports.generateHeaders = generateHeaders;
|
||||
exports.sendFile = sendFile;
|
||||
|
||||
exports.start = start;
|
||||
|
||||
})();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue