Forum Discussion

Robert_47833's avatar
Robert_47833
Icon for Altostratus rankAltostratus
May 12, 2011

return and HTTP::redirect usage

Hi,I have a irule like this

 

if {$uri matches_regex "^/(help/)"}

 

{

 

 

HTTP::redirect ]

 

pool cjj1

 

 

return

 

}

 

else{

 

pool cjj2

 

}

 

 

1:don't know the true meaning of return

 

2:can I use

 

HTTP::redirect http://[HTTP::HOST][HTTP::uri]

 

pool cjj1

 

in this way?

9 Replies

  • Hi Jucao,

     

    Return immediately exits the current EVENT. In your case if there is a match for "cjj1" it exits the event and does not further process the statement. From your posting it appears that you do not need a return statement.

     

     

    I hope this helps

     

     

    Bhattman
  • I'd also replace the regex with a more efficient string command:

     

     

    if {$uri starts_with "/help/"}

     

     

    As Bhattman said, you don't need the return statement unless you have more code after the else statement.

     

     

    Aaron
  • Hi,Bhattman hoolio

     

    Thanks for your help

     

    if there exist 2 events in this irule ,

     

     

    when HTTP::request

     

    if {$uri matches_regex "^/(help/)"}

     

    {

     

    HTTP::redirect http://[HTTP::HOST][HTTP::uri]

     

    pool cjj1

     

    return

     

    }

     

    else{

     

    pool cjj2

     

    }

     

    when client::accpt

     

    {

     

    xxxxxx

     

    }

     

     

     

    1:if I want the traffic jump out of the irule when match help,it make sense to use return ,right?

     

    if only one event here,it is no need to use return ,right?

     

     

    2:I want to change the url to http://[HTTP::HOST][HTTP::uri] and send it to pool cjj1

     

    does it work in my irule ?

     

     

    if {$uri matches_regex "^/(help/)"}

     

    {

     

    HTTP::redirect http://[HTTP::HOST][HTTP::uri]

     

    pool cjj1

     

    return

     

    }

     

    else{

     

    pool cjj2

     

    }
  • The HTTP::redirect command immediately sends the redirect response to the client. You can't set a pool after issuing this command. You are sending the client back through your loadbalancer on a new request.

     

     

    Also, if $uri = [HTTP:uri], then you will be in a redirect loop since you are not altering the client request when you redirect them. If $uri != [HTTP::uri], then you need an "elseif" clause that catches the new request and uses pool cjj1.

     

     

    So, like this:

     

    when HTTP_REQUEST {

     

    if { $uri starts_with "/help/" } {

     

    HTTP::redirect "http://[HTTP::host][HTTP::uri]"

     

    }

     

    elseif { some new condition that matches your redirected request } {

     

    pool cjj1

     

    }

     

    else {

     

    pool cjj2

     

    }

     

    }

     

     

    I haven't used "return" so I won't advise on its use.

     

     

    Richard
  • Colin_Walker_12's avatar
    Colin_Walker_12
    Historic F5 Account
    Keep in mind that return does not exit the iRule, only the event context that it's fired in. This means in an iRule with multiple events, return does not stop the rest of the events from processing. It merely stops the processing of the current event.

     

     

    Colin
  • Colin,

     

     

    That's really good to know. So, if you wanted to bail on the rule altogether, would something like this work?

     

     

     

    when HTTP_REQUEST {

     

    if { some_condition } {

     

    set bail_on_rule "true"

     

    }

     

    }

     

     

     

    when HTTP_RESPONSE {

     

    if { $bail_on_rule equals "true" } {

     

    event disable

     

    }

     

    else {

     

    other_stuff

     

    }

     

    }

     

     

     

    Do variables live between events in the same rule? Is there an easier (maybe built in) way of doing this?

     

     

     

    Richard

     

     

     

    edit: lost my psuedo-code on posting

     

  • Colin_Walker_12's avatar
    Colin_Walker_12
    Historic F5 Account
    There isn't a way to stop the iRule processing as a whole, really. The closest you can come is "event disable all", but that's scoped to the connection as a context. That means that if you fire an event disable (or an event disable all) you're turning off that event for all iRules on that connection, not just the iRule it was fired within.

     

     

    There are some logic tricks you can play with things like an info exists at the top of every event in an iRule that will make it behave the way you want, but there isn't a specific command that exits the iRule.

     

     

    Colin