From 4bffdf82246cc37b2ca97113aecddce00bef363d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8A=A0=E9=87=8C?= Date: Tue, 2 Sep 2014 15:32:59 +0800 Subject: [PATCH] update README --- README.md | 39 ++++++++++++++++++++++++-------------- rule_sample/rule__blank.js | 9 ++------- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 93787a2..66f8365 100644 --- a/README.md +++ b/README.md @@ -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) * rule file scheme ```javascript + module.exports = { /* - these functions are required - you may leave their bodies blank if necessary + these functions will overwrite the default ones, write your own when necessary. */ //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 - shouldUseLocalResponse : function(req){ - return false; + shouldUseLocalResponse : function(req,reqBody){ + if(/hello/.test(reqBody.toString())){ + return true; + }else{ + return false; + } }, //you may deal the response locally instead of sending it to server //this function be called when shouldUseLocalResponse returns true //callback(statusCode,resHeader,responseData) //e.g. callback(200,{"content-type":"text/html"},"hello world") - dealLocalResponse : function(req,callback){ - //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; + dealLocalResponse : function(req,reqBody,callback){ + callback(statusCode,resHeader,responseData) }, //replace the request protocol when sending to the real server @@ -64,6 +60,20 @@ module.exports = { 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 replaceResponseStatusCode: function(req,res,statusCode){ var newStatusCode = statusCode; @@ -96,6 +106,7 @@ module.exports = { } }; + ``` Using https features diff --git a/rule_sample/rule__blank.js b/rule_sample/rule__blank.js index 84af8f0..eb42e3f 100644 --- a/rule_sample/rule__blank.js +++ b/rule_sample/rule__blank.js @@ -18,8 +18,7 @@ module.exports = { //callback(statusCode,resHeader,responseData) //e.g. callback(200,{"content-type":"text/html"},"hello world") 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 @@ -40,11 +39,7 @@ module.exports = { //replace the request body replaceRequestData: function(req,data){ - // console.log(data.toString().indexOf("alipay.acquire.order.precreate")); - // if(data.toString().indexOf("alipay.acquire.order.precreate") >= 0){ - // req.needReplaceResponse = true; - // } - // return text; + return data; }, //replace the statusCode before it's sent to the user