Forum Discussion

Kunal_Borkar_11's avatar
Kunal_Borkar_11
Icon for Nimbostratus rankNimbostratus
Nov 24, 2017

HTTP Response Rewrite

I am new to iRule and Need your expertise solution on below requirement.

 

User is accessing the URL http://example.com/auth/v1.0/ from client computer via Load Balancer and LB forwards the request to the Backend Real Server.

 

now, server gives the response, Here, Server Inserts a Header "X-Storage-Url" in the Response (Mentioned Below).

 

X-Storage-Url: https://example.com/v1/AUTH_example

 

post receving the response from server, Client initiates a new connection to the URL mention on the "X-Storage-Url" Response header.

 

Now requirement is to rewrite the "X-Storage-Url" header response in Load Balancer as below and pass the response to client so that Client can initiate a connection on HTTP.

 

X-Storage-Url: http://example.com/v1/AUTH_example

 

Note: Load Balancer has to rewrite only Protocol Type from "HTTPS" to "HTTP" there is no chance to make any changes on the Application side and it has be made on Load Balancer only.

 

Hope you understand the requirement. Kindly help us, how we can achieve this using iRULE.

 

1 Reply

  • If the header is always the same you can simple do the following:

    when HTTP_RESPONSE {
        if {[HTTP::header exists "X-Storage-Url"]} {
            HTTP::header replace "X-Storage-Url" "http://example.com/v1/AUTH_example"
        }
    }
    

    This is quicker than trying to for a search and replace in the value of the header.

    If the header is dynamic then the following would take the header and split it into a list using "/" as a split character, replace the first element in the list with "http:" then replace the header after joining the list.

    when HTTP_RESPONSE {
        if {[HTTP::header exists "X-Storage-Url"]} {
            set xStorageUrl [split [HTTP::header "X-Storage-Url"] "/"]
            lreplace $xStorageUrl 0 0 "http:"
            HTTP::header replace "X-Storage-Url" [join $xStorageUrl "/"]
        }
    }
    

    You can also do this with a regular expression however as much more resource intensive recommend this only if the volume is going to be low and you do not have a lot of iRules running on the F5.

    when HTTP_RESPONSE {
        if {[HTTP::header exists "X-Storage-Url"]} {
            HTTP::header replace "X-Storage-Url" [regsub "^https" [HTTP::header "X-Storage-Url"] "http"]
        }
    }