Fiddlerscript Learning 1: Modify request or response

Source: Internet
Author: User

Two days ago, due to project requirements, I briefly read fiddlerscript, which has a strong function. Today I have time to take a closer look and take notes.

Modify request or response

Modify the request and response by adding rules to the onbeforerequest and onbeforeresponse functions in fiddlerscript. The onbeforerequest function is called before each request. The onbeforeresponse function is called before each response.

1. Add Request Header

 oSession.oRequest["NewHeaderName"] = "New header value";

2. Delete the Response Header

 oSession.oResponse.headers.Remove("Set-Cookie");

3. forward requests from one page to another on the same server

if (oSession.PathAndQuery=="/hello/hello.html") {      oSession.PathAndQuery="/hello/index.html";    }
Note: The value of osession. pathandquery is the URL in the session List in Fiddler:


That is, the red part in the figure. The yellow part in the figure is a bit special. The host is tunnel to, and the URL is another host. View the request header as follows:


In this special case, there are examples below.

In the preceding example, the request address/Hello/hello.html is intercepted and forwarded to/Hello/index.html of the same server.


4. forward the request to different servers with the same port number (modify the requested host)

<pre name="code" class="javascript"> if(oSession.HostnameIs("www.baidu.com")){      oSession.hostname = "www.sina.com.cn";}

 

In this example, if a request sent to Baidu is forwarded to Sina, the system prompts that the page does not exist. Here, the host is changed and the subsequent address is not changed. Therefore, if there is no corresponding page on Sina. As shown in the following figure:




If I access the following address: http://www.baidu.com/link? Url = CQuVpjo9u9UQADcstwECPEmrziPMk5u5H9PlRN2TbWLkKZaxafVER2X8OEYzovr-yasX2Fwcgj0NANBtKVj0gN78jNJ3bXTmIsTeBk7hXem

The result is as follows: (this page actually exists. It is the result page searched by Baidu and is forwarded to Sina by Fiddler, but this page is not saved on Sina)


5. forward requests to different port numbers and different servers

    if (oSession.host=="192.168.0.70:8080") {      oSession.host="192.168.0.69:8020";    }
In this example, the request sent to 192.168.0.70: 8080 is forwarded to 192.168.0.69: 8020. Here, only the host is changed and the subsequent request address is not changed. For example, after the above rules are implemented, what I request is:

Http: // 192.168.0.70: 8080/Hello/hello.html


Actually, my project is deployed to: 192.168.0.69: 8020.

6. forward all requests from one server to another, including https

    // Redirect traffic, including HTTPS tunnels    if (oSession.HTTPMethodIs("CONNECT") && (oSession.PathAndQuery == "www.example.com:443")) {         oSession.PathAndQuery = "beta.example.com:443";     }    if (oSession.HostnameIs("www.example.com")) oSession.hostname = "beta.example.com"; 

7. Simulate the Windows Hosts file, by pointing one hostname to a different IP address. (retargets without changing the request's host header)

    // All requests for subdomain.example.com should be directed to the development server at 128.123.133.123    if (oSession.HostnameIs("subdomain.example.com")){    oSession.bypassGateway = true;                   // Prevent this request from going through an upstream proxy    oSession["x-overrideHost"] = "128.123.133.123";  // DNS name or IP address of target server    }

8. retarget requests for a single page to a different page, potentially on a different server. (retargets by changing the request's host header)

    if (oSession.url=="www.example.com/live.js") {      oSession.url = "dev.example.com/workinprogress.js";    }
9. Prevent upload of HTTP cookies

 oSession.oRequest.headers.Remove("Cookie");

10. decompress and unchunk a HTTP response, updating headers if needed

    // Remove any compression or chunking from the response in order to make it easier to manipulate    oSession.utilDecodeResponse();

11. search and replace in HTML.

    if (oSession.HostnameIs("www.bayden.com") && oSession.oResponse.headers.ExistsAndContains("Content-Type","text/html")){      oSession.utilDecodeResponse();      oSession.utilReplaceInResponse('<b>','<u>');    }

12. Case Insensitive search of response html.

    if (oSession.oResponse.headers.ExistsAndContains("Content-Type", "text/html") && oSession.utilFindInResponse("searchfor", false)>-1){      oSession["ui-color"] = "red";    }

13. Remove all Div tags (and content inside the DIV tag)

    // If content-type is HTML, then remove all DIV tags    if (oSession.oResponse.headers.ExistsAndContains("Content-Type", "html")){      // Remove any compression or chunking      oSession.utilDecodeResponse();      var oBody = System.Text.Encoding.UTF8.GetString(oSession.responseBodyBytes);      // Replace all instances of the DIV tag with an empty string      var oRegEx = /<div[^>]*>(.*?)<\/div>/gi;      oBody = oBody.replace(oRegEx, "");      // Set the response body to the div-less string      oSession.utilSetResponseBody(oBody);     }

14. Pretend your browser is the googlebot webcrawler

oSession.oRequest["User-Agent"]="Googlebot/2.X (+http://www.googlebot.com/bot.html)";

15. Request Hebrew content

    oSession.oRequest["Accept-Language"]="he";

16. Deny. CSS requests

    if (oSession.uriContains(".css")){     oSession["ui-color"]="orange";      oSession["ui-bold"]="true";     oSession.oRequest.FailSession(404, "Blocked", "Fiddler blocked CSS file");    }

17. Simulate HTTP Basic Authentication (requires user to enter a password before displaying web content .)

    if ((oSession.HostnameIs("www.example.com")) &&      !oSession.oRequest.headers.Exists("Authorization"))     {    // Prevent IE's "Friendly Errors Messages" from hiding the error message by making response body longer than 512 chars.    var oBody = "18. respond to a request with a file loaded from the \ captures \ responses folder (can be placed in onbeforerequest or onbeforeresponse function)

    if (oSession.PathAndQuery=="/version1.css") {      oSession["x-replywithfile"] ="version2.css";    }


I have not used all of the above examples. I only used the forwarding of several addresses in the middle because I need to use it now. For the rest, please do what you need.



Fiddlerscript Learning 1: Modify request or response

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.