improve file mapping

This commit is contained in:
加里 2014-08-18 19:51:15 +08:00
parent a0fd07fbc8
commit e1053b15d4
3 changed files with 21 additions and 10 deletions

View File

@ -27,7 +27,7 @@ var handleRule = {
function userRequestHandler(req,userRes){ function userRequestHandler(req,userRes){
var host = req.headers.host, var host = req.headers.host,
path = url.parse(req.url).path, path = url.parse(req.url).pathname,
ifLocalruleMatched = false; ifLocalruleMatched = false;
console.log(color.green("\nreceived request to : " + host + path)); console.log(color.green("\nreceived request to : " + host + path));
@ -47,10 +47,11 @@ function userRequestHandler(req,userRes){
console.log("==>meet the rules, will map to local file"); console.log("==>meet the rules, will map to local file");
var targetLocalfile = rule.localFile; var targetLocalfile = rule.localFile;
if(!targetLocalfile){ //find file in dir //TODO : /a/b/c -> b/c
var basename = pathUtil.basename(path); //localfile not set, map to dir
basename = basename.slice(0,basename.indexOf("?")); //remove chars after question mark if(!targetLocalfile){ //find file in dir, /a/b/file.html -> dir + b/file.html
targetLocalfile = pathUtil.join(rule.localDir,basename); var remotePathWithoutPrefix = path.replace(new RegExp(rule.path),""); //remove prefix
targetLocalfile = pathUtil.join(rule.localDir,remotePathWithoutPrefix);
} }
console.log("==>local file: " + targetLocalfile); console.log("==>local file: " + targetLocalfile);

View File

@ -26,9 +26,13 @@ function proxyServer(type, port, hostname,ruleFile){
self.httpProxyServer = null; self.httpProxyServer = null;
if(ruleFile){ //TODO : fs.join if(ruleFile){
if(fs.existsSync(ruleFile)){ if(fs.existsSync(ruleFile)){
requestHandler.setRules(require(ruleFile)); try{ //for abs path
requestHandler.setRules(require(ruleFile)); //todo : require path
}catch(e){ //for relative path
requestHandler.setRules(require("./" + ruleFile));
}
console.log(color.green("rule file loaded")); console.log(color.green("rule file loaded"));
}else{ }else{
console.log(color.red("can not find rule file")); console.log(color.red("can not find rule file"));

View File

@ -6,11 +6,17 @@ var rules = {
"localFile" :"", //this file will be returned to user when host and path pattern both meets the request "localFile" :"", //this file will be returned to user when host and path pattern both meets the request
"localDir" :"~/" //find the file of same name in localdir. anyproxy will not read localDir settings unless localFile is falsy "localDir" :"~/" //find the file of same name in localdir. anyproxy will not read localDir settings unless localFile is falsy
} }
// ,{
// "host" :/./,
// "path" :/\.(png|gif|jpg|jpeg)/,
// "localFile" :"/Users/Stella/tmp/test.png",
// "localDir" :"~/"
// }
,{ ,{
"host" :/./, "host" :/./,
"path" :/\.(png|gif|jpg|jpeg)/, "path" :/tps/,
"localFile" :"/Users/Stella/tmp/test.png", "localFile" :"",
"localDir" :"~/" "localDir" :"/Users/Stella/tmp/"
} }
] ]
,"httpsConfig":{ ,"httpsConfig":{