add web interface

This commit is contained in:
加里
2014-08-27 17:42:42 +08:00
parent 5c2fac9352
commit 385a4a8fe6
50 changed files with 533 additions and 24688 deletions

View File

@@ -29,10 +29,24 @@ var handleRule = {
function userRequestHandler(req,userRes){
var host = req.headers.host,
urlPattern = url.parse(req.url),
path = urlPattern.path,
urlPattern = url.parse(req.url),
path = urlPattern.path,
ifLocalruleMatched = false,
callback = null;
callback = null,
ifHttps = !!req.connection.encrypted && !/http:/.test(req.url),
resourceInfo = {},
resourceInfoId = -1;
resourceInfo.host = host;
resourceInfo.method = req.method;
resourceInfo.path = path;
resourceInfo.url = (ifHttps ? "https://" :"http://") + host + path;
resourceInfo.req = req;
resourceInfo.startTime = new Date().getTime();
try{
resourceInfoId = GLOBAL.recorder.appendRecord(resourceInfo);
}catch(e){}
console.log(color.green("\nreceived request to : " + host + path));
/*
@@ -41,6 +55,7 @@ function userRequestHandler(req,userRes){
in https server : /work/alibaba
*/
//handle OPTIONS request
if(req.method == "OPTIONS"){
console.log("==>OPTIONS req for CROS, will allow all");
userRes.writeHead(200,mergeCORSHeader(req.headers)); //remove any cache related header, add crossdomain headers
@@ -83,8 +98,6 @@ function userRequestHandler(req,userRes){
}
}
//sleep for seconds if configed in the rule file
//see rule_sample.js
if(hostTest && pathTest && !!rule.sleep){
@@ -102,7 +115,6 @@ function userRequestHandler(req,userRes){
}else{
console.log("==>will forward to real server by proxy");
var ifHttps = !!req.connection.encrypted && !/http:/.test(req.url);
var options = {
hostname : urlPattern.hostname || req.headers.host,
@@ -114,17 +126,31 @@ function userRequestHandler(req,userRes){
var proxyReq = (ifHttps ? https : http).request(options, function(res) {
userRes.writeHead(res.statusCode,mergeCORSHeader(req.headers,res.headers));
if(callback){
res.on('data',function(chunk){
userRes.write(chunk);
});
res.on('end',function(){
callback(userRes);
userRes.end();
});
}else{
res.pipe(userRes);
}
var resData = [],
length = 0;
res.on("data",function(chunk){
resData.push(chunk);
length += chunk.length;
userRes.write(chunk);
});
res.on("end",function(){
callback && callback.call(null,userRes);
userRes.end();
//update record info
resourceInfo.endTime = new Date().getTime();
resourceInfo.res = res;
resourceInfo.resBody = Buffer.concat(resData);
resourceInfo.length = length;
try{
GLOBAL.recorder.updateRecord(resourceInfoId,resourceInfo);
}catch(e){}
});
});
proxyReq.on("error",function(e){