mirror of
https://github.com/alibaba/anyproxy.git
synced 2025-07-29 00:59:10 +00:00
update post body recorder
This commit is contained in:
@@ -1,31 +1,27 @@
|
||||
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){
|
||||
dealLocalResponse : function(req,reqBody,callback){
|
||||
callback(200,{"content-type":"text/html"},reqBody);
|
||||
//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
|
||||
//protocol : "http" or "https"
|
||||
replaceRequestProtocol:function(req,protocol){
|
||||
@@ -33,6 +29,24 @@ 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){
|
||||
// console.log(data.toString().indexOf("alipay.acquire.order.precreate"));
|
||||
// if(data.toString().indexOf("alipay.acquire.order.precreate") >= 0){
|
||||
// req.needReplaceResponse = true;
|
||||
// }
|
||||
// return text;
|
||||
},
|
||||
|
||||
//replace the statusCode before it's sent to the user
|
||||
replaceResponseStatusCode: function(req,res,statusCode){
|
||||
var newStatusCode = statusCode;
|
||||
|
@@ -1,34 +1,10 @@
|
||||
//rule scheme :
|
||||
|
||||
module.exports = {
|
||||
shouldUseLocalResponse : function(req){
|
||||
},
|
||||
|
||||
dealLocalResponse : function(req,callback){
|
||||
},
|
||||
|
||||
replaceRequestOption : function(req,option){
|
||||
|
||||
},
|
||||
|
||||
replaceRequestProtocol:function(req,protocol){
|
||||
},
|
||||
|
||||
replaceResponseStatusCode: function(req,res,statusCode){
|
||||
},
|
||||
|
||||
replaceResponseHeader: function(req,res,header){
|
||||
},
|
||||
|
||||
replaceServerResData: function(req,res,serverResData){
|
||||
|
||||
},
|
||||
|
||||
pauseBeforeSendingResponse : function(req,res){
|
||||
//delay all the response for 1500ms
|
||||
return 1500;
|
||||
},
|
||||
|
||||
shouldInterceptHttpsReq :function(req){
|
||||
}
|
||||
|
||||
};
|
@@ -2,7 +2,7 @@
|
||||
// Ref: https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
|
||||
|
||||
module.exports = {
|
||||
shouldUseLocalResponse : function(req){
|
||||
shouldUseLocalResponse : function(req,reqBody){
|
||||
//intercept all options request
|
||||
if(req.method == "OPTIONS"){
|
||||
return true;
|
||||
@@ -11,33 +11,16 @@ module.exports = {
|
||||
}
|
||||
},
|
||||
|
||||
dealLocalResponse : function(req,callback){
|
||||
dealLocalResponse : function(req,reqBody,callback){
|
||||
if(req.method == "OPTIONS"){
|
||||
callback(200,mergeCORSHeader(req.headers),"");
|
||||
}
|
||||
},
|
||||
|
||||
replaceRequestOption : function(req,option){
|
||||
},
|
||||
|
||||
replaceRequestProtocol:function(req,protocol){
|
||||
},
|
||||
|
||||
replaceResponseStatusCode: function(req,res,statusCode){
|
||||
},
|
||||
|
||||
replaceResponseHeader: function(req,res,header){
|
||||
return mergeCORSHeader(req.headers, header);
|
||||
},
|
||||
|
||||
replaceServerResData: function(req,res,serverResData){
|
||||
},
|
||||
|
||||
pauseBeforeSendingResponse : function(req,res){
|
||||
},
|
||||
|
||||
shouldInterceptHttpsReq :function(req){
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
function mergeCORSHeader(reqHeader,originHeader){
|
||||
|
@@ -1,24 +1,7 @@
|
||||
//rule scheme :
|
||||
|
||||
module.exports = {
|
||||
shouldUseLocalResponse : function(req){
|
||||
},
|
||||
|
||||
dealLocalResponse : function(req,callback){
|
||||
},
|
||||
|
||||
replaceRequestOption : function(req,option){
|
||||
|
||||
},
|
||||
|
||||
replaceRequestProtocol:function(req,protocol){
|
||||
},
|
||||
|
||||
replaceResponseStatusCode: function(req,res,statusCode){
|
||||
},
|
||||
|
||||
replaceResponseHeader: function(req,res,header){
|
||||
},
|
||||
|
||||
replaceServerResData: function(req,res,serverResData){
|
||||
//add "hello github" to all github pages
|
||||
@@ -28,9 +11,6 @@ module.exports = {
|
||||
return serverResData;
|
||||
},
|
||||
|
||||
pauseBeforeSendingResponse : function(req,res){
|
||||
},
|
||||
|
||||
shouldInterceptHttpsReq :function(req){
|
||||
//intercept https://github.com/
|
||||
//otherwise, all the https traffic will not go through this proxy
|
||||
|
@@ -1,21 +1,6 @@
|
||||
//rule scheme :
|
||||
|
||||
module.exports = {
|
||||
shouldUseLocalResponse : function(req){
|
||||
},
|
||||
|
||||
dealLocalResponse : function(req,callback){
|
||||
},
|
||||
|
||||
replaceRequestOption : function(req,option){
|
||||
},
|
||||
|
||||
replaceRequestProtocol:function(req,protocol){
|
||||
},
|
||||
|
||||
replaceResponseStatusCode: function(req,res,statusCode){
|
||||
},
|
||||
|
||||
replaceResponseHeader: function(req,res,header){
|
||||
header = header || {};
|
||||
header["Cache-Control"] = "no-cache, no-store, must-revalidate";
|
||||
@@ -23,17 +8,7 @@ module.exports = {
|
||||
header["Expires"] = 0;
|
||||
|
||||
return header;
|
||||
},
|
||||
|
||||
replaceServerResData: function(req,res,serverResData){
|
||||
},
|
||||
|
||||
pauseBeforeSendingResponse : function(req,res){
|
||||
},
|
||||
|
||||
shouldInterceptHttpsReq :function(req){
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
function disableCacheHeader(header){
|
||||
|
@@ -1,11 +1,6 @@
|
||||
//rule scheme :
|
||||
|
||||
module.exports = {
|
||||
shouldUseLocalResponse : function(req){
|
||||
},
|
||||
|
||||
dealLocalResponse : function(req,callback){
|
||||
},
|
||||
|
||||
replaceRequestOption : function(req,option){
|
||||
//replace request towards http://www.taobao.com
|
||||
@@ -24,25 +19,5 @@ module.exports = {
|
||||
if(option.hostname == "www.taobao.com" && option.path == "/"){
|
||||
option.path = "/about/";
|
||||
}
|
||||
|
||||
console.log(option);
|
||||
},
|
||||
|
||||
replaceRequestProtocol:function(req,protocol){
|
||||
},
|
||||
|
||||
replaceResponseStatusCode: function(req,res,statusCode){
|
||||
},
|
||||
|
||||
replaceResponseHeader: function(req,res,header){
|
||||
},
|
||||
|
||||
replaceServerResData: function(req,res,serverResData){
|
||||
},
|
||||
|
||||
pauseBeforeSendingResponse : function(req,res){
|
||||
},
|
||||
|
||||
shouldInterceptHttpsReq :function(req){
|
||||
}
|
||||
};
|
@@ -1,24 +1,6 @@
|
||||
//rule scheme :
|
||||
|
||||
module.exports = {
|
||||
shouldUseLocalResponse : function(req){
|
||||
},
|
||||
|
||||
dealLocalResponse : function(req,callback){
|
||||
},
|
||||
|
||||
replaceRequestOption : function(req,option){
|
||||
|
||||
},
|
||||
|
||||
replaceRequestProtocol:function(req,protocol){
|
||||
},
|
||||
|
||||
replaceResponseStatusCode: function(req,res,statusCode){
|
||||
},
|
||||
|
||||
replaceResponseHeader: function(req,res,header){
|
||||
},
|
||||
|
||||
replaceServerResData: function(req,res,serverResData){
|
||||
|
||||
@@ -31,11 +13,5 @@ module.exports = {
|
||||
return serverResData;
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
pauseBeforeSendingResponse : function(req,res){
|
||||
},
|
||||
|
||||
shouldInterceptHttpsReq :function(req){
|
||||
}
|
||||
};
|
@@ -1,18 +1,6 @@
|
||||
//rule scheme :
|
||||
|
||||
module.exports = {
|
||||
shouldUseLocalResponse : function(req){
|
||||
},
|
||||
|
||||
dealLocalResponse : function(req,callback){
|
||||
},
|
||||
|
||||
replaceRequestOption : function(req,option){
|
||||
|
||||
},
|
||||
|
||||
replaceRequestProtocol:function(req,protocol){
|
||||
},
|
||||
|
||||
replaceResponseStatusCode: function(req,res,statusCode){
|
||||
//redirect requests toward http://www.taobao.com/*
|
||||
@@ -32,14 +20,5 @@ module.exports = {
|
||||
}
|
||||
|
||||
return header;
|
||||
},
|
||||
|
||||
replaceServerResData: function(req,res,serverResData){
|
||||
},
|
||||
|
||||
pauseBeforeSendingResponse : function(req,res){
|
||||
},
|
||||
|
||||
shouldInterceptHttpsReq :function(req){
|
||||
}
|
||||
};
|
@@ -1,9 +1,9 @@
|
||||
//replace all the images with local one
|
||||
|
||||
var url = require("url"),
|
||||
path = require("path"),
|
||||
fs = require("fs"),
|
||||
buffer = require("buffer");
|
||||
var url = require("url"),
|
||||
path = require("path"),
|
||||
fs = require("fs"),
|
||||
buffer = require("buffer");
|
||||
|
||||
var map = [
|
||||
{
|
||||
@@ -15,7 +15,7 @@ var map = [
|
||||
];
|
||||
|
||||
module.exports = {
|
||||
shouldUseLocalResponse : function(req){
|
||||
shouldUseLocalResponse : function(req,reqBody){
|
||||
var host = req.headers.host,
|
||||
urlPattern = url.parse(req.url),
|
||||
path = urlPattern.path;
|
||||
@@ -45,31 +45,10 @@ module.exports = {
|
||||
return false;
|
||||
},
|
||||
|
||||
dealLocalResponse : function(req,callback){
|
||||
dealLocalResponse : function(req,reqBody,callback){
|
||||
if(req.replaceLocalFile){
|
||||
callback(200, {"content-type":"image/png"}, fs.readFileSync(req.replaceLocalFile) );
|
||||
}
|
||||
},
|
||||
|
||||
replaceRequestOption : function(req,option){
|
||||
},
|
||||
|
||||
replaceRequestProtocol:function(req,protocol){
|
||||
},
|
||||
|
||||
replaceResponseStatusCode: function(req,res,statusCode){
|
||||
},
|
||||
|
||||
replaceResponseHeader: function(req,res,header){
|
||||
},
|
||||
|
||||
replaceServerResData: function(req,res,serverResData){
|
||||
},
|
||||
|
||||
pauseBeforeSendingResponse : function(req,res){
|
||||
},
|
||||
|
||||
shouldInterceptHttpsReq :function(req){
|
||||
}
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user