anyproxy- modify the returned content (beforeSendResponse)

Foreword

Fiddler can capture the break point, the content is returned, to facilitate simulation of various return results. anyproxy rule may be written by the rules module, the analog return a status code, the head, body

beforeSendResponse

beforeSendResponse(requestDetail, responseDetail)

  • AnyProxy before sending a request to the client calls beforeSendResponse, and bring the parameterrequestDetail responseDetail
  • requestDetail With beforeSendRequestthe parameters
  • responseDetail
    Response Object {} return information server, comprising three fields statusCode header body
    _res {object} primitive objects returned from the server

For example, the request  http://httpbin.org/get time, responseDetail along the following parameters

{
  response: {
    statusCode: 200,
    header: {
      'Content-Type': 'image/gif', Connection: 'close', 'Cache-Control': '...' }, body: '...' }, _res: { /* ... */ } }

Modify the returned content

The following are legitimate return

Without any treatment, return null

return null;

Modified status code returned

var newResponse = Object.assign({}, responseDetail.response);
newResponse.statusCode = 404;
return {
  response: newResponse };

Modify the content returned

var newResponse = Object.assign({}, responseDetail.response);
newResponse.body += '--from anyproxy--';
return {
  response: newResponse };

rule Case

Here are some examples provided to explain the rules of the common use of the module
you can --rule anyproxy  HTTP: // .... JS to load modules and experience

The method of requesting a curl test.

Returns the local json format body

Using local data, is sent to intercept  http://httpbin.org  request, instead of using the local data returned from the server, sample_use_local_response.js rules

/* 
  sample: 
    intercept all requests toward httpbin.org, use a local response
  test:
    curl http://httpbin.org/user-agent --proxy http://127.0.0.1:8001
*/
module.exports = {
  *beforeSendRequest(requestDetail) {
    const localResponse = {
      statusCode: 200, header: { 'Content-Type': 'application/json' }, body: '{"hello": "this is local response"}' }; if (requestDetail.url.indexOf('http://httpbin.org') === 0) { return { response: localResponse }; } }, };

Js file loaded rule rules

anyproxy -i --rule sample_use_local_response.js

Use curl test http://httpbin.org

C:\Users\dell>curl http://httpbin.org/user-agent --proxy http://127.0.0.1:8001
{"hello": "this is local response"}

Analog return status code

Analog return status code 404, sample_modify_response_statuscode.js following rules

/* 
  sample: 
    modify all status code of http://httpbin.org/ to 404
  test:
    curl -I 'http://httpbin.org/user-agent' --proxy http://127.0.0.1:8001
  expected response:
    HTTP/1.1 404 Not Found
*/
module.exports = {
  *beforeSendResponse(requestDetail, responseDetail) {
    if (requestDetail.url.indexOf('http://httpbin.org') === 0) { const newResponse = responseDetail.response; newResponse.statusCode = 404; return { response: newResponse }; } } };

Analog return head

Modify the header, sample_modify_response_header.js rule returns as follows

/* 
  sample: 
    modify response header of http://httpbin.org/user-agent
  test:
    curl -I 'http://httpbin.org/user-agent' --proxy http://127.0.0.1:8001
  expected response:
    X-Proxy-By: AnyProxy
*/
module.exports = {
  *beforeSendResponse(requestDetail, responseDetail) {
    if (requestDetail.url.indexOf('http://httpbin.org/user-agent') === 0) { const newResponse = responseDetail.response; newResponse.header['X-Proxy-By'] = 'AnyProxy'; return { response: newResponse }; } } };

Modify the return body

Modifying the returned inside the body part, sample_modify_response_data.js following rules

/* 
  sample: 
    modify response data of http://httpbin.org/user-agent
  test:
    curl 'http://httpbin.org/user-agent' --proxy http://127.0.0.1:8001
  expected response:
    { "user-agent": "curl/7.43.0" } -- AnyProxy Hacked! --
*/

module.exports = {
  *beforeSendResponse(requestDetail, responseDetail) {
    if (requestDetail.url === 'http://httpbin.org/user-agent') {
      const newResponse = responseDetail.response; newResponse.body += '-- AnyProxy Hacked! --'; return new Promise((resolve, reject) => { setTimeout(() => { // delay the response for 5s resolve({ response: newResponse }); }, 5000); }); } }, };

More learning materials reference https://github.com/alibaba/anyproxy/blob/master/docs/cn/src_doc.md

 

 
 Transfer: https://www.cnblogs.com/yoyoketang/p/10878193.html

Foreword

Fiddler can capture the break point, the content is returned, to facilitate simulation of various return results. anyproxy rule may be written by the rules module, the analog return a status code, the head, body

beforeSendResponse

beforeSendResponse(requestDetail, responseDetail)

  • AnyProxy before sending a request to the client calls beforeSendResponse, and bring the parameterrequestDetail responseDetail
  • requestDetail With beforeSendRequestthe parameters
  • responseDetail
    Response Object {} return information server, comprising three fields statusCode header body
    _res {object} primitive objects returned from the server

For example, the request  http://httpbin.org/get time, responseDetail along the following parameters

{
  response: {
    statusCode: 200,
    header: {
      'Content-Type': 'image/gif', Connection: 'close', 'Cache-Control': '...' }, body: '...' }, _res: { /* ... */ } }

Modify the returned content

The following are legitimate return

Without any treatment, return null

return null;

Modified status code returned

var newResponse = Object.assign({}, responseDetail.response);
newResponse.statusCode = 404;
return {
  response: newResponse };

Modify the content returned

var newResponse = Object.assign({}, responseDetail.response);
newResponse.body += '--from anyproxy--';
return {
  response: newResponse };

rule Case

Here are some examples provided to explain the rules of the common use of the module
you can --rule anyproxy  HTTP: // .... JS to load modules and experience

The method of requesting a curl test.

Returns the local json format body

Using local data, is sent to intercept  http://httpbin.org  request, instead of using the local data returned from the server, sample_use_local_response.js rules

/* 
  sample: 
    intercept all requests toward httpbin.org, use a local response
  test:
    curl http://httpbin.org/user-agent --proxy http://127.0.0.1:8001
*/
module.exports = {
  *beforeSendRequest(requestDetail) {
    const localResponse = {
      statusCode: 200, header: { 'Content-Type': 'application/json' }, body: '{"hello": "this is local response"}' }; if (requestDetail.url.indexOf('http://httpbin.org') === 0) { return { response: localResponse }; } }, };

Js file loaded rule rules

anyproxy -i --rule sample_use_local_response.js

Use curl test http://httpbin.org

C:\Users\dell>curl http://httpbin.org/user-agent --proxy http://127.0.0.1:8001
{"hello": "this is local response"}

Analog return status code

Analog return status code 404, sample_modify_response_statuscode.js following rules

/* 
  sample: 
    modify all status code of http://httpbin.org/ to 404
  test:
    curl -I 'http://httpbin.org/user-agent' --proxy http://127.0.0.1:8001
  expected response:
    HTTP/1.1 404 Not Found
*/
module.exports = {
  *beforeSendResponse(requestDetail, responseDetail) {
    if (requestDetail.url.indexOf('http://httpbin.org') === 0) { const newResponse = responseDetail.response; newResponse.statusCode = 404; return { response: newResponse }; } } };

Analog return head

Modify the header, sample_modify_response_header.js rule returns as follows

/* 
  sample: 
    modify response header of http://httpbin.org/user-agent
  test:
    curl -I 'http://httpbin.org/user-agent' --proxy http://127.0.0.1:8001
  expected response:
    X-Proxy-By: AnyProxy
*/
module.exports = {
  *beforeSendResponse(requestDetail, responseDetail) {
    if (requestDetail.url.indexOf('http://httpbin.org/user-agent') === 0) { const newResponse = responseDetail.response; newResponse.header['X-Proxy-By'] = 'AnyProxy'; return { response: newResponse }; } } };

Modify the return body

Modifying the returned inside the body part, sample_modify_response_data.js following rules

/* 
  sample: 
    modify response data of http://httpbin.org/user-agent
  test:
    curl 'http://httpbin.org/user-agent' --proxy http://127.0.0.1:8001
  expected response:
    { "user-agent": "curl/7.43.0" } -- AnyProxy Hacked! --
*/

module.exports = {
  *beforeSendResponse(requestDetail, responseDetail) {
    if (requestDetail.url === 'http://httpbin.org/user-agent') {
      const newResponse = responseDetail.response; newResponse.body += '-- AnyProxy Hacked! --'; return new Promise((resolve, reject) => { setTimeout(() => { // delay the response for 5s resolve({ response: newResponse }); }, 5000); }); } }, };

More learning materials reference https://github.com/alibaba/anyproxy/blob/master/docs/cn/src_doc.md

 

Guess you like

Origin www.cnblogs.com/dreamhighqiu/p/10990047.html