refactor(ponse) rm unneeded conditions

This commit is contained in:
coderaiser 2014-11-18 04:15:19 -05:00
parent a29cdd64d0
commit 813da6a0f7

View file

@ -41,49 +41,47 @@
* @param notLog
*/
function send(data, params, notLog) {
var p, isGzip, head,
ret = checkParams(params);
var isGzip, head,
p = params;
if (ret) {
p = params;
data = data;
isGzip = p.gzip && isGZIP(p.request);
head = generateHeaders({
name : p.name,
cache : p.cache,
gzip : isGzip,
query : p.query
});
fillHeader(head, p.response);
if (!notLog)
Util.log(data);
/* если браузер поддерживает gzip-сжатие - сжимаем данные*/
Util.exec.if(!isGzip,
function() {
if (!p.data)
p.data = data;
p.response.statusCode = p.status || OK;
p.response.end(p.data);
},
checkParams(params);
isGzip = p.gzip && isGZIP(p.request);
head = generateHeaders({
name : p.name,
cache : p.cache,
gzip : isGzip,
query : p.query
});
fillHeader(head, p.response);
if (!notLog)
Util.log(data);
/* если браузер поддерживает gzip-сжатие - сжимаем данные*/
Util.exec.if(!isGzip,
function() {
if (!p.data)
p.data = data;
function(callback) {
zlib.gzip(data, function(error, data) {
if (!error)
p.data = data;
else {
p.status = FILE_NOT_FOUND;
p.data = error.message;
}
callback();
});
p.response.statusCode = p.status || OK;
p.response.end(p.data);
},
function(callback) {
zlib.gzip(data, function(error, data) {
if (!error)
p.data = data;
else {
p.status = FILE_NOT_FOUND;
p.data = error.message;
}
callback();
});
}
});
}
/**
@ -152,27 +150,26 @@
return header;
}
function setHeader(pParams) {
var p, header, gzip,
lRet = checkParams(pParams);
function setHeader(params) {
var header, gzip,
p = params;
if (lRet) {
p = pParams;
gzip = isGZIP(p.request) && p.gzip;
header = generateHeaders({
name : p.name,
time : p.time,
range : p.range,
length : p.length,
cache : p.cache,
gzip : gzip,
query : getQuery(p.request)
});
fillHeader(header, p.response);
p.response.statusCode = p.status || OK;
}
checkParams(params);
gzip = isGZIP(p.request) && p.gzip;
header = generateHeaders({
name : p.name,
time : p.time,
range : p.range,
length : p.length,
cache : p.cache,
gzip : gzip,
query : getQuery(p.request)
});
fillHeader(header, p.response);
p.response.statusCode = p.status || OK;
}
function fillHeader(header, response) {
@ -191,77 +188,75 @@
*
*/
function sendFile(params) {
var p = params,
ret = checkParams(params);
var p = params;
if (ret)
fs.lstat(p.name, function(error, stat) {
var time, length, range, isGzip,
options = {};
checkParams(params);
fs.lstat(p.name, function(error, stat) {
var time, length, range, isGzip,
options = {};
if (error) {
sendError(error, params);
} else {
isGzip = isGZIP(p.request) && p.gzip;
time = stat.mtime,
length = stat.size,
range = getRange(p.request, length);
if (error) {
sendError(error, params);
} else {
isGzip = isGZIP(p.request) && p.gzip;
time = stat.mtime,
length = stat.size,
range = getRange(p.request, length);
if (range)
Util.copyObj(p, {
range : range,
status : RANGE
});
if (range)
Util.copyObj(p, {
time : time
range : range,
status : RANGE
});
if (!isGzip)
p.length = length;
setHeader(params);
options = {
gzip : isGzip,
range : range
};
files.pipe(p.name, p.response, options, function(error) {
if (error)
sendError(error, params);
});
}
});
Util.copyObj(p, {
time : time
});
if (!isGzip)
p.length = length;
setHeader(params);
options = {
gzip : isGzip,
range : range
};
files.pipe(p.name, p.response, options, function(error) {
if (error)
sendError(error, params);
});
}
});
}
/**
* send error response
*/
function sendError(error, params) {
var data, ret = checkParams(params);
var data;
if (ret) {
params.status = FILE_NOT_FOUND;
data = error.message || '' + error;
send(data, params);
}
checkParams(params);
params.status = FILE_NOT_FOUND;
data = error.message || '' + error;
send(data, params);
}
function checkParams(params) {
var ret = true;
var p = params;
Util.check(arguments, ['params']);
Util.check([params.name, params.request, params.response], ['name', 'requst', 'response']);
return ret;
Util.check([params], ['params'])
.check([p.name, p.request, p.response], ['name', 'requst', 'response']);
}
function getQuery(req) {
var query, parsed;
Util.check(arguments, ['req']);
Util.check([req], ['req']);
parsed = url.parse(req.url);
query = parsed.query;
@ -272,7 +267,7 @@
function getPathName(req) {
var pathname, parsed;
Util.check(arguments, ['req']);
Util.check([req], ['req']);
parsed = url.parse(req.url);
pathname = parsed.pathname;
@ -322,7 +317,7 @@
'Location': url
};
Util.check(arguments, ['url', 'response']);
Util.check([url, response], ['url', 'response']);
fillHeader(header, response);
response.statusCode = MOVED_PERMANENTLY;