mirror of
https://github.com/alibaba/anyproxy.git
synced 2025-04-24 08:41:31 +00:00
improve file mapping
This commit is contained in:
parent
a0fd07fbc8
commit
e1053b15d4
@ -27,7 +27,7 @@ var handleRule = {
|
||||
|
||||
function userRequestHandler(req,userRes){
|
||||
var host = req.headers.host,
|
||||
path = url.parse(req.url).path,
|
||||
path = url.parse(req.url).pathname,
|
||||
ifLocalruleMatched = false;
|
||||
|
||||
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");
|
||||
|
||||
var targetLocalfile = rule.localFile;
|
||||
if(!targetLocalfile){ //find file in dir //TODO : /a/b/c -> b/c
|
||||
var basename = pathUtil.basename(path);
|
||||
basename = basename.slice(0,basename.indexOf("?")); //remove chars after question mark
|
||||
targetLocalfile = pathUtil.join(rule.localDir,basename);
|
||||
|
||||
//localfile not set, map to dir
|
||||
if(!targetLocalfile){ //find file in dir, /a/b/file.html -> dir + b/file.html
|
||||
var remotePathWithoutPrefix = path.replace(new RegExp(rule.path),""); //remove prefix
|
||||
targetLocalfile = pathUtil.join(rule.localDir,remotePathWithoutPrefix);
|
||||
}
|
||||
|
||||
console.log("==>local file: " + targetLocalfile);
|
||||
|
8
proxy.js
8
proxy.js
@ -26,9 +26,13 @@ function proxyServer(type, port, hostname,ruleFile){
|
||||
|
||||
self.httpProxyServer = null;
|
||||
|
||||
if(ruleFile){ //TODO : fs.join
|
||||
if(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"));
|
||||
}else{
|
||||
console.log(color.red("can not find rule file"));
|
||||
|
@ -6,11 +6,17 @@ var rules = {
|
||||
"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
|
||||
}
|
||||
// ,{
|
||||
// "host" :/./,
|
||||
// "path" :/\.(png|gif|jpg|jpeg)/,
|
||||
// "localFile" :"/Users/Stella/tmp/test.png",
|
||||
// "localDir" :"~/"
|
||||
// }
|
||||
,{
|
||||
"host" :/./,
|
||||
"path" :/\.(png|gif|jpg|jpeg)/,
|
||||
"localFile" :"/Users/Stella/tmp/test.png",
|
||||
"localDir" :"~/"
|
||||
"path" :/tps/,
|
||||
"localFile" :"",
|
||||
"localDir" :"/Users/Stella/tmp/"
|
||||
}
|
||||
]
|
||||
,"httpsConfig":{
|
||||
|
Loading…
x
Reference in New Issue
Block a user