Forum Discussion

dc_18986's avatar
dc_18986
Historic F5 Account
Aug 30, 2007

My 1st iRule - How can I improve this?

Hello all,

 

 

Below is my 1st attempt at an iRule. A client of ours is replacing a Java servlet with an ASPX page. They want to the ability to process requests for the new service, while still handling requests for the old. They are phasing in this change gradually. I believe they need an iRule that detects a client connection, and 1) inspects tcp payload for a specific string (call to new service), and if found, redirects that traffic to the new service, url B) if upon inspection, the payload does not contain that string, redirect to old service, url A, which is an existing pool, providing the original service. Will something like the below work?

 

 

 

when CLIENT_ACCEPTED {

 

set port [80]

 

if { [TCP::local_port] == $port } {

 

TCP::collect 300

 

}

 

}

 

 

when CLIENT_DATA {

 

if { [TCP::payload] contains ""} {

 

log "Request For New XML Service Found. Sending to ASPX page."

 

HTTP::redirect urlB

 

}else {

 

log "Request Was for Old XML Service. Sending to Default xml Pool."

 

pool xml_pool

 

} else

 

}client_data

2 Replies

  • Is this data going over HTTP? If so, do you have an HTTP profile associated with your virtual?

    The reason why I ask is that the HTTP::redirect command is only available in the HTTP_REQUEST and HTTP_REQUEST_DATA events, not the CLIENT_DATA event. If you are working at the TCP data level (CLIENT_DATA, etc), you'll need to form your own HTTP 302 response (including correct HTTP response headers/etc), that the HTTP::respond command does for you, and send it back to the client with the TCP::respond command. That's going to be a bit tricky, but it's possible.

    If you do have a HTTP profile, then things are much easier for you. Something like this should work:

    when HTTP_REQUEST {
      if { [TCP::local_port] == 80 } {
        HTTP::collect [HTTP::header Content-Length]
      }
    }
    when HTTP_REQUEST_DATA {
      if { [HTTP::payload] contains "" } {
        log local0. "Request for new XML Service Found.  Sending ASPX page."
        HTTP::redirect "http://otherdomain/otheruri"
      } else {
        log local0. "Request was for Old XML Service.  Sending to Default xml Pool."
        pool xml_pool
      }
    }

    Better yet, I'd assign "xml_pool" as the default pool for the virtual and you can leave the else clause out altogether.

    Hope this helps...

    -Joe
  • dc_18986's avatar
    dc_18986
    Historic F5 Account
    Yes, it helps indeed. I'm virtually 100% positive that this will be done over HTTP. I think your suggestions will make an excellent starting point. I'm curious, can you point me to an example on how to do this if I were working at a TCP data level? For sure, it'll come up some time.