From 1d7d4176c868995ada6ddb55db19b802af6429f4 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Sat, 23 Feb 2013 06:36:00 -0500 Subject: [PATCH] minor changes --- lib/server/commander.js | 35 ++++++-------- lib/server/main.js | 101 +++++++++++++++++++++++----------------- shell/deploy.sh | 1 + 3 files changed, 73 insertions(+), 64 deletions(-) diff --git a/lib/server/commander.js b/lib/server/commander.js index 9c24a027..91ed0348 100644 --- a/lib/server/commander.js +++ b/lib/server/commander.js @@ -219,9 +219,10 @@ ''; fs.readFile(INDEX, Util.call(readFile, { - list : lList, - request : lReq, - response : lRes + name : getCleanPath(lReq), + list : lList, + request : lReq, + response : lRes }) ); @@ -257,7 +258,7 @@ if(lRet) lRet = Util.checkObjTrue(pParams.params, [REQUEST, RESPONSE, 'list']); - + if(lRet){ var p = pParams, c = pParams.params; @@ -303,12 +304,15 @@ if(lRet){ var p = pParams; - var lPath = p.name || getCleanPath(p.request), - lQuery = main.getQuery(p.request), + var lQuery = main.getQuery(p.request), /* download, json */ - lGzip = isGZIP(p.request), - lHead = main.generateHeaders(lPath, lGzip, lQuery); - + lGzip = main.isGZIP(p.request), + lHead = main.generateHeaders({ + name : p.name, + gzip : lGzip, + query : lQuery + }); + /* если браузер поддерживает gzip-сжатие - сжимаем данные*/ Util.ifExec(!lGzip, function(pParams){ @@ -322,7 +326,7 @@ p.response.writeHead(p.status || OK, lHead); p.response.end(p.data); - Util.log(lPath + ' sended'); + Util.log( p.name + ' sended'); Util.log( p.status === FILE_NOT_FOUND && p.data ); }, @@ -392,15 +396,4 @@ return lRet; } - - function isGZIP(pReq){ - var lEnc, lGZIP; - if(pReq){ - lEnc = pReq.headers['accept-encoding'] || '', - lGZIP = lEnc.match(/\bgzip\b/); - } - - return lGZIP; - } - })(); \ No newline at end of file diff --git a/lib/server/main.js b/lib/server/main.js index 279711a6..ac6c9a78 100644 --- a/lib/server/main.js +++ b/lib/server/main.js @@ -49,6 +49,7 @@ exports.rootrequire = rootrequire, exports.generateHeaders = generateHeaders, exports.getQuery = getQuery, + exports.isGZIP = isGZIP, exports.sendFile = sendFile, /* compitability with old versions of node */ @@ -150,47 +151,49 @@ * Функция создаёт заголовки файлов * в зависимости от расширения файла * перед отправкой их клиенту - * @param pName - имя файла - * @param pGzip - данные сжаты gzip'ом + * @param pParams + * name - имя файла + * gzip - данные сжаты gzip'ом + * query + * cacheControl + * https://developers.google.com/speed/docs/best-practices/caching?hl=ru#LeverageProxyCaching */ - function generateHeaders(pName, pGzip, pQuery){ - var lRet, - lType = '', - lContentEncoding = '', - lCacheControl = 0, + function generateHeaders(pParams){ + var lRet = Util.checkObjTrue(pParams, 'name'), + p = pParams; + Util.log(pParams); + if(lRet){ + var lExt = Util.getExtension(p.name), + lType = ext[lExt] || 'text/plain', + lContentEncoding = ''; - lExt = Util.getExtension(pName); - - if( Util.strCmp(lExt, '.appcache') ) - lCacheControl = 1; - - lType = ext[lExt] || 'text/plain'; - - /* if type of file any, but img - - * then we shoud specify charset - */ - if( !Util.isContainStr(lType, 'img') ) - lContentEncoding = '; charset=UTF-8'; - - if(Util.strCmp(pQuery, 'download') ) - lType = 'application/octet-stream'; - - - if(!lCacheControl) - lCacheControl = 31337 * 21; - - lRet = { - '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'; + if( !Util.checkObjTrue(pParams, 'cacheControl') ) + p.cacheControl = 1; + + /* if type of file any, but img - then we shoud specify charset */ + if( !Util.isContainStr(lType, 'img') ) + lContentEncoding = '; charset=UTF-8'; + if( Util.strCmp(p.query, 'download') ) + lType = 'application/octet-stream'; + + + if( !Util.strCmp(lExt, '.appcache') ) + p.cacheControl = 31337 * 21; + + lRet = { + 'Content-Type': lType + lContentEncoding, + 'last-modified': new Date().toString(), + 'Vary': 'Accept-Encoding' + }; + + if(p.cacheControl) + lRet['cache-control'] = 'max-age=' + p.cacheControl; + + if(p.gzip) + lRet['content-encoding'] = 'gzip'; + } + return lRet; } @@ -203,19 +206,17 @@ */ function sendFile(pParams){ var lRet, - lName, lReq, lRes, lQuery, lGziped; + lName, lReq, lRes, lGziped; if(pParams){ lName = pParams.name, lReq = pParams.request, lRes = pParams.response; lGziped = pParams.gziped; - lQuery = getQuery(lReq); } if(lName && lRes && lReq){ - var lEnc = lReq.headers['accept-encoding'] || '', - lGzip = lEnc.match(/\bgzip\b/), + var lGzip = isGZIP(lReq), lReadStream = fs.createReadStream(lName, { 'bufferSize': 4 * 1024 @@ -226,7 +227,11 @@ lRes.end(String(pError)); }); - lRes.writeHead(OK, generateHeaders(lName, lGzip, lQuery ) ); + lRes.writeHead(OK, generateHeaders({ + name : lName, + gzip : lGzip, + query : getQuery(lReq) + }) ); if (lGzip && !lGziped) lReadStream = lReadStream.pipe( zlib.createGzip() ); @@ -250,6 +255,16 @@ return lQuery; } + function isGZIP(pReq){ + var lEnc, lGZIP; + if(pReq){ + lEnc = pReq.headers['accept-encoding'] || ''; + lGZIP = lEnc.match(/\bgzip\b/); + } + + return lGZIP; + } + function linuxWatch(pFile, pCallBack){ fs.watchFile(pFile, function(pCurr, pPrev){ diff --git a/shell/deploy.sh b/shell/deploy.sh index 798ac07e..316dbf69 100755 --- a/shell/deploy.sh +++ b/shell/deploy.sh @@ -1,3 +1,4 @@ +cd .. echo 'appfog' af update echo 'http://cloudcmd.aws.af.cm/'