added redirect function

This commit is contained in:
coderaiser 2013-03-10 06:48:23 -04:00
parent e8edb83d14
commit b21c2b15f4
4 changed files with 81 additions and 62 deletions

View file

@ -233,8 +233,10 @@
lRet = main.sendFile( pParams );
}
else if( Util.isContainStr(p.name, FS) || Util.strCmp( p.name, '/') ){
if( Util.isContainStr(p.name, 'no-js/') )
return noJSTMPRedirection(pParams);
if( Util.isContainStr(p.name, 'no-js/') ){
var lURL = Util.removeStr(pParams.name, 'no-js/');
return main.redirect(pParams, lURL);
}
lRet = sendCommanderContent(p);
}
@ -303,16 +305,6 @@
}
}
function noJSTMPRedirection(pParams){
var MOVED_PERMANENTLY = 301,
lPath = Util.removeStr(pParams.name, 'no-js/');
pParams.response.writeHead(MOVED_PERMANENTLY, {'Location': lPath});
pParams.response.end();
return true;
}
/* function sets stdout to file log.txt */
function writeLogsToFile(){

View file

@ -15,6 +15,6 @@
"port" : 80,
"sslPort" : 443,
"ip" : null,
"ssl" : false,
"ssl" : true,
"rest" : true
}

View file

@ -15,7 +15,7 @@
LIBDIR = main.LIBDIR,
SRVDIR = main.SRVDIR,
/* модуль для работы с путями */
URL = main.url,
Path = main.path,
Querystring = main.querystring,
@ -59,61 +59,70 @@
if(!pProcessing)
pProcessing = {};
Rest = pProcessing.rest;
Route = pProcessing.route;
Minimize = pProcessing.minimize;
Rest = pProcessing.rest;
Route = pProcessing.route;
Minimize = pProcessing.minimize;
init(pProcessing.appcache);
var lPort = process.env.PORT || /* c9 */
process.env.app_port || /* nodester */
process.env.VCAP_APP_PORT || /* cloudfoundry */
lConfig.port,
var lPort = process.env.PORT || /* c9 */
process.env.app_port || /* nodester */
process.env.VCAP_APP_PORT || /* cloudfoundry */
lConfig.port,
lIP = process.env.IP || /* c9 */
lConfig.ip ||
(main.WIN32 ? '127.0.0.1' : '0.0.0.0'),
lIP = process.env.IP || /* c9 */
lConfig.ip ||
(main.WIN32 ? '127.0.0.1' : '0.0.0.0'),
lSSL = pProcessing.ssl,
lSSLPort= lConfig.sslPort,
lHTTP = 'http',
lRet;
lSSL = pProcessing.ssl,
lSSLPort = lConfig.sslPort,
lSockets = function(pServer){
var lListen;
if(lConfig.socket && Socket)
lListen = Socket.listen(pServer);
Util.log('* Sockets ' + (lListen ? 'running' : 'disabled'));
},
lHTTPServer = function(){
Server = http.createServer( controller );
Server.listen(lPort, lIP);
lServerLog('http');
lSockets(Server);
},
lServerLog = function(pHTTP){
Util.log('* Server running at ' + pHTTP + '://' + lIP + ':' + lPort);
};
/* server mode or testing mode */
if (lConfig.server) {
if(lSSL){
lRet = Util.tryCatchLog(function(){
Server = https.createServer( lSSL, controller );
Server.listen(lSSLPort, lIP);
});
if(!lRet){
var lRedirectServer = http.createServer( function(){
var lRedirectServer = http.createServer( function(pReq, pRes){
main.redirect({
response: pRes,
url: pReq.url
});
});
lRedirectServer.listen(lPort, lIP);
lHTTP += 's';
}
lRedirectServer.listen(lPort, lIP);
Server = https.createServer( lSSL, controller );
Server.on('error', function (pError) {
Util.log('Could not you https port: ' + lSSLPort);
Util.log(pError);
lRedirectServer.close();
lHTTPServer();
});
lSockets();
Server.listen(lSSLPort, lIP);
lServerLog('https');
}
if(!lSSL || lRet){
Util.log(lRet);
Server = http.createServer( controller );
Server.listen(lPort, lIP);
}
var lListen;
if(lConfig.socket && Socket)
lListen = Socket.listen(Server);
Util.log('* Sockets ' + (lListen ? 'running' : 'disabled'));
Util.log('* Server running at ' + lHTTP + '://' + lIP + ':' + lPort);
if(lRet){
Util.log('Cloud Commander server could not started');
Util.log(lRet);
}
else
lHTTPServer();
}else
Util.log('Cloud Commander testing mode');
}
@ -130,8 +139,7 @@
/* Читаем содержимое папки, переданное в url */
var lRet,
lConfig = main.config,
lURL = main.url,
lParsedUrl = lURL.parse(pReq.url),
lParsedUrl = URL.parse(pReq.url),
lPath = lParsedUrl.pathname;
/* added supporting of Russian language in directory names */

View file

@ -12,7 +12,7 @@
ext,
path, fs, zlib, url,
OK, FILE_NOT_FOUND,
OK, FILE_NOT_FOUND, MOVED_PERMANENTLY,
Config = {
server : true,
@ -22,8 +22,10 @@
/* Consts */
exports.OK = OK = 200;
exports.FILE_NOT_FOUND = FILE_NOT_FOUND = 404;
exports.OK = OK = 200;
exports.FILE_NOT_FOUND = FILE_NOT_FOUND = 404;
exports.MOVED_PERMANENTLY = MOVED_PERMANENTLY = 301;
exports.REQUEST = 'request';
exports.RESPONSE = 'response';
@ -62,6 +64,7 @@
exports.sendFile = sendFile,
exports.sendResponse = sendResponse,
exports.sendError = sendError,
exports.redirect = redirect,
exports.checkParams = checkParams,
@ -300,6 +303,22 @@
}
}
/**
* redirect to another URL
*/
function redirect(pParams){
var lRet = Util.checkObjTrue(pParams,
['response']);
if(lRet){
var p = pParams;
p.response.writeHead(MOVED_PERMANENTLY, {'Location': p.url});
p.response.end();
}
}
function sendError(pParams, pError){
var lRet = Util.checkObjTrue(pParams,
['name', 'request', 'response']);