Create an iRule that can search and replace

Looking for some irule help please this is a search and replace rule…

Step one in the message payload

Find soapenv:Header/ and replace with

soapenv:Header
job:roleIdentifierSERVER1</job:roleIdentifier>
</soapenv:Header>

or if the message is from Server2

Find soapenv:Header/ and replace with

soapenv:Header
job:roleIdentifierSERVER2</job:roleIdentifier>
</soapenv:Header>

Assuming that you can identify server1 and 2 based on IP you can use below exmaple

when HTTP_REQUEST 
{
	switch [IP::client_addr] 
	{
        "server1_IP" 
		{
            set server "SERVER1"
			HTTP::collect
        }
        "server2_IP" 
		{
            set server "SERVER2"
			HTTP::collect
        }
	}
}

when HTTP_REQUEST_DATA 
{
	if {[info exists server]} 
	{
		set payload [HTTP::payload]
		set new_header "<soapenv:Header><job:roleIdentifier>$server</job:roleIdentifier></soapenv:Header>"
		regsub {<soapenv:Header/>} $payload $new_header payload
		HTTP::payload replace 0 [string length [HTTP::payload]] $payload
	}
}

thank you for your message, when you say identify the server, how can identify the server with irule code?

what if i just wanted to replace the below and then assign the irule to the virtual server, then i would have identified which server the message came from.

Step one in the message payload

Find soapenv:Header/ and replace with

soapenv:Header
job:roleIdentifierSERVER1</job:roleIdentifier>
</soapenv:Header>

It depends how is your topology.

If you you a specific VS fo each server then you don’t need to identify it.

But if you have one VS then you somehow need to know which server it is.

You can use IPs, headers, cookies, or whatever unique application may has.

Another one is that you don’t specify if this is a request or a response.

So my example is for request.

If you need it for a response it needs to be adjusted accordingly.

The message will come through from an application to the Virtual Server, i’m not sure if this is a response or a request. The message will not have a role identifier. Hence I have to add in the following irule.

Let’s start with it being one virtual server and we want a basic example to get the irule started to search and replace.

This is a message sent from the application, and an irule to replace soapenv:Header/ with soapenv:Header
job:roleIdentifierSERVER1</job:roleIdentifier>
</soapenv:Header>

Step one in the message payload

Find soapenv:Header/ and replace with

soapenv:Header
job:roleIdentifierSERVER1</job:roleIdentifier>
</soapenv:Header>

Server 1 and Server 2 are both separate. I thought the irule can be attached to each server.

Oh when you say server you mean virtual server?

If so yes you can attach same irule to both virtual servers.

But again you somehow need to identify which virtual server was used.

You need to modify the request or the response?

So for virtual servers you can use [IP::local_addr clientside] instead of [IP::client_addr]

it’s a request. And they will hit individual VIP’s (Virtual Servers).

then you can try to apply this irule to both Virtual Servers

when HTTP_REQUEST 
{
	switch [IP::local_addr clientside] 
	{
        "VIP_1" 
		{
            set server "SERVER1"
        }
        "VIP_2" 
		{
            set server "SERVER2"
        }
	}
	if {[info exists server]} 
	{
        if {[HTTP::header "Content-Length"] ne "" && [HTTP::header "Content-Length"] <= 1048576}
        {
            set content_length [HTTP::header "Content-Length"]
	    } 
	    else 
	    {
		    set content_length 1048576
	    }
    }
    if { $content_length > 0} 
    {
        HTTP::collect $content_length
    }
}

when HTTP_REQUEST_DATA 
{
	if {[info exists server]} 
	{
		set payload [HTTP::payload]
	    set new_header "<soapenv:Header><job:roleIdentifier>$server</job:roleIdentifier></soapenv:Header>"
	    regsub {<soapenv:Header/>} $payload $new_header payload
	    HTTP::payload replace 0 [string length [HTTP::payload]] $payload
	}
}

Thank you, what fields would i edit for server ip or name? I’m thinking Server1 and Server2.

I’ll give this a test on the irule.

What can i do if the message sent over doesn’t have a soapheader and i need to insert one in?

VIP_1 and VIP_2 should be change with virtual servers IPs
Server1 and Server2 can be changed to whatever you need to add in payload