update README

This commit is contained in:
加里 2014-09-02 15:32:59 +08:00
parent 8ad15162fa
commit 4bffdf8224
2 changed files with 27 additions and 21 deletions

View File

@ -29,32 +29,28 @@ How to write your own rule file
* you may learn how it works by our samples: [https://github.com/alipay-ct-wd/anyproxy/tree/master/rule_sample](https://github.com/alipay-ct-wd/anyproxy/tree/master/rule_sample) * you may learn how it works by our samples: [https://github.com/alipay-ct-wd/anyproxy/tree/master/rule_sample](https://github.com/alipay-ct-wd/anyproxy/tree/master/rule_sample)
* rule file scheme * rule file scheme
```javascript ```javascript
module.exports = { module.exports = {
/* /*
these functions are required these functions will overwrite the default ones, write your own when necessary.
you may leave their bodies blank if necessary
*/ */
//whether to intercept this request by local logic //whether to intercept this request by local logic
//if the return value is true, anyproxy will call dealLocalResponse to get response data and will not send request to remote server anymore //if the return value is true, anyproxy will call dealLocalResponse to get response data and will not send request to remote server anymore
shouldUseLocalResponse : function(req){ shouldUseLocalResponse : function(req,reqBody){
return false; if(/hello/.test(reqBody.toString())){
return true;
}else{
return false;
}
}, },
//you may deal the response locally instead of sending it to server //you may deal the response locally instead of sending it to server
//this function be called when shouldUseLocalResponse returns true //this function be called when shouldUseLocalResponse returns true
//callback(statusCode,resHeader,responseData) //callback(statusCode,resHeader,responseData)
//e.g. callback(200,{"content-type":"text/html"},"hello world") //e.g. callback(200,{"content-type":"text/html"},"hello world")
dealLocalResponse : function(req,callback){ dealLocalResponse : function(req,reqBody,callback){
//callback(statusCode,resHeader,responseData) callback(statusCode,resHeader,responseData)
},
//req is user's request sent to the proxy server
// option is how the proxy server will send request to the real server. i.e. require("http").request(option,function(){...})
//you may return a customized option to replace the original option
replaceRequestOption : function(req,option){
var newOption = option;
return newOption;
}, },
//replace the request protocol when sending to the real server //replace the request protocol when sending to the real server
@ -64,6 +60,20 @@ module.exports = {
return newProtocol; return newProtocol;
}, },
//req is user's request sent to the proxy server
//option is how the proxy server will send request to the real server. i.e. require("http").request(option,function(){...})
//you may return a customized option to replace the original option
//you should not write content-length header in options, since anyproxy will handle it for you
replaceRequestOption : function(req,option){
var newOption = option;
return newOption;
},
//replace the request body
replaceRequestData: function(req,data){
return data;
},
//replace the statusCode before it's sent to the user //replace the statusCode before it's sent to the user
replaceResponseStatusCode: function(req,res,statusCode){ replaceResponseStatusCode: function(req,res,statusCode){
var newStatusCode = statusCode; var newStatusCode = statusCode;
@ -96,6 +106,7 @@ module.exports = {
} }
}; };
``` ```
Using https features Using https features

View File

@ -18,8 +18,7 @@ module.exports = {
//callback(statusCode,resHeader,responseData) //callback(statusCode,resHeader,responseData)
//e.g. callback(200,{"content-type":"text/html"},"hello world") //e.g. callback(200,{"content-type":"text/html"},"hello world")
dealLocalResponse : function(req,reqBody,callback){ dealLocalResponse : function(req,reqBody,callback){
callback(200,{"content-type":"text/html"},reqBody); callback(statusCode,resHeader,responseData)
//callback(statusCode,resHeader,responseData)
}, },
//replace the request protocol when sending to the real server //replace the request protocol when sending to the real server
@ -40,11 +39,7 @@ module.exports = {
//replace the request body //replace the request body
replaceRequestData: function(req,data){ replaceRequestData: function(req,data){
// console.log(data.toString().indexOf("alipay.acquire.order.precreate")); return data;
// if(data.toString().indexOf("alipay.acquire.order.precreate") >= 0){
// req.needReplaceResponse = true;
// }
// return text;
}, },
//replace the statusCode before it's sent to the user //replace the statusCode before it's sent to the user