Skip to Content

Topics

For a long time I was using Membrane Monitor to monitor and trace the client-server communication between my (mobile) devices and a server (usually servers like SAP Mobile Platform, HCPms, MobileDocs, Netweaver Gateway, …). I described the way for Android devices in an article some time ago: Debugging and Developing Android applications in enterprise environments . The described way is still working, but recently I prefer Fiddler to Membrane Monitor as reverse proxy.

By default Fiddler serves as a web proxy. Daniel van Leeuwen described in his blog how to use this to trace http/https communication on SMP server as well as on mobile devices (see Getting Started with Kapsel – Appendix F — Tips (SP09+) ). Anyway I prefer using a “real” reverse proxy, thus I will describe here steps how to use Fiddler as a reverse proxy.

Using Fiddler as Reverse Proxy

1. If you do not have Fiddler yet, download it for free Download Fiddler Web Debugging Tool for Free by Telerik

2. Install it

3. (Optional) Install the Syntax-Highlighting Add-On which you can download from here http://www.telerik.com/fiddler/add-ons

1_Fiddler_Syntax_Extension.png

This will simplify changing the reverse proxy configurations later.

4. (Optional) Go to Tools > Fiddler Options and choose Tab Connections. Activate the option “Allow remote computers to connect”. Restart Fiddler after that.

This will allow other clients to connect to your reverse proxy

2_AllowRemoteComputersToConnect.png

5. Close/Exit Fiddler

6. Open the registry editor to set a registry key which will define the (local) port Fiddler will forward the traffic. Actually we will not use this port, but we need to specify it, so that Fiddler knows it is running in reverse-proxy mode.

(Run > regedit ). Navigate in regedit to “Computer > HKEY_CURRENT_USER > Software > Microsoft > Fiddler2 and create a new “DWORD (32-bit) Value” (right click and choose “New”) named “ReverseProxyForPort”. Choose Decimal representation and provide any port. In my case I choose port 8000.

3_ReverseProxyPort_Regedit.png

7. Start Fiddler

By default requests going to http://localhost:8888 or http://127.0.0.1:8888 (default Fiddler listen port) will get redirected to localhost port 8000. Because we want our reverse proxy to route all traffic to a specific other server, we have to use some Fiddler scripting.


8. Switch to tab “FiddlerScript” (you will only have this tab if you installed the AddOn as suggested in step 3). Fiddler provides you a quite powerful way of manipulating requests and responses by using JScript.NET language. The most important methods are “OnBeforeRequest” which is executed by Fiddler before a concrete request is sent out and “OnBeforeResponse” which is called before the response from the backend server is delivered to the client.

4_FiddlerScript.png

9. Go to method “OnBeforeRequest” and add following coding at the beginning of this function. Replace the variable serverAddress with your target server host url.


        /*  
         *  Custom reverse proxy mode
         *  Forward requests to localhost:8888 to the specified server address
         *
         */
    
        var serverAddress = "http://dewdfwssp2011.dhcp.wdf.sap.corp:8100";
    
        oSession.fullUrl = oSession.fullUrl.Replace("http://localhost:8888", serverAddress);
        oSession.fullUrl = oSession.fullUrl.Replace("http://127.0.0.1:8888", serverAddress);
 


I tried several possibilities here to forward a request, e.g. by using the x-overrideHost parameter or by directly changing oSession.host. In some scenarios, e.g. when a protocol switch (http->https) occured or if a different port were used I was running into some problems… so I ended up with above coding which is working for me very fine. 

In this example the reverse proxy (which is running on port 8888) will forward all related traffic to the system dewdfwssp2011.dhcp.wdf.sap.corp on port 8100. If you also want to allow other urls (e.g. your machine’s host name to reach it over network) you can add another replace rule.

.5_OnBeforeRequest_routing.png

After that do not forget to click on “Save Script”.


10. Now the reverse proxy configuration is finished and it should work. You can try it directly inside Fiddler when using the “Composer” to create a request. In my case I am calling a sample odata service, because I connected Fiddler to a Netweaver Gateway: http://127.0.0.1:8888/sap/opu/odata/IWBEP/GWDEMO/

6_TestReverseProxy.png

11. Test inside a webbrowser: Call address http://localhost:8888/sap/opu/odata/IWBEP/GWDEMO/

6_z_TestinBrowser.png

Using FiddlerScript to rewrite HTTP Responses

We can now even go one step ahead. Let’s say we want to replace some content of a http reponse. I had this case some days ago, where an url did not get rewritten (because it was part of the message body), so I configured Fiddler as a reverse proxy rewriting these urls inside the response message bodys.

In the following example I will only replace a sample string, but you can use the same procedure for complex Regex based search and replace operations.

1. In the request I sent in step 10 (above) I was getting the service document of an odata service, which looked like this.

7_RewriteSalesOrderCollection.png

We want to rewrite now the term “SalesOrderCollection”

2. Switch again to tab “FiddlerScript”, now go to function “OnBeforeResponse” and add the following coding into the function



       /*
        *
        * Search and replace content in Response
        *
        */
        if (oSession.HostnameIs("127.0.0.1")){
            oSession.utilDecodeResponse(); //decode the response first
  
            var oBody = System.Text.Encoding.UTF8.GetString(oSession.responseBodyBytes);
            // Replace actions
            var oRegEx = /SalesOrderCollection/gi;
            oBody = oBody.replace(oRegEx, "NameChangedCollection");
            // Set the response body to the div-less string
            oSession.utilSetResponseBody(oBody);  
  
        }








8_FiddlerScript_Replacingrewriting.png

3. After that “Save Script”


4. Switch to the “Composer” tag and execute the same request again. In the response body you can see that the string “SalesOrderCollection” got replaced by “NameChangedCollection”

9_TestReplace.png


Fiddler and FiddlerScript is quite powerful. If you want to learn more, check out the documentation:

http://docs.telerik.com/fiddler/KnowledgeBase/FiddlerScript/ModifyRequestOrResponse



To report this post you need to login first.

2 Comments

You must be Logged on to comment or reply to a post.

  1. Jitendra Kansal

    Hey Marvin Hoffmann,

    I have below OData service URL  http://jk:8080/gateway/odata/SAP/ARTICLESERVICE;v=1

    step 4: done as mentioned

    step 6: i have given port as 8080

    step 9: script (jk is host name of my computer FQDN)

    1.   if ((oSession.HostnameIs(“127.0.0.1”)) || (oSession.HostnameIs(“localhost”))) { 
    2.            // oSession.bypassGateway = true; // use webproxy? 
    3.             oSession[“x-overrideHost”] = “jk:8080”
    4.         }

    As per step #11, when i access this url in a browser , i get below response  http://localhost:8888/gateway/odata/SAP/ARTICLESERVICE;v=1

    Capture.PNG

    What i have missed?

    Regards,

    JK

    (0) 
    1. Marvin Hoffmann Post author

      HI Jitendra,

      hm, I changed the blog post (step 9) a bit. Because sometimes dependent on the chosen backend I was also running into some problems. I ended up in replacing the full server target url which is working fine for me:

      var serverAddress = “http://dewdfwssp2011.dhcp.wdf.sap.corp:8100“; 

      oSession.fullUrl = oSession.fullUrl.Replace(“http://localhost:8888“, serverAddress);  

      oSession.fullUrl = oSession.fullUrl.Replace(“http://127.0.0.1:8888“, serverAddress);  

      (0) 

Leave a Reply