Forum Discussion

Vedio_315956's avatar
Vedio_315956
Icon for Nimbostratus rankNimbostratus
Mar 31, 2017

Substring syntax in iRules

if { [string length [substr [HTTP::uri] 0 "?"]] > 0 }

 

IN an above statement I can't able to understand what is the purpose of [ Substr 0 "?" ].

 

give some suggestion or basic idea about that

 

thanks

 

  • David_Scott_104's avatar
    David_Scott_104
    Historic F5 Account

    substr will get at part of a string, the first argument is the string to look at, the second argument specifies how many characters to skip over (0 meaning don't skip any) and the last argument is the terminator or where to stop the substring.

     

    For an example of the parameters, say you have the following url

     

    http://f5.com/downloads?v13

     

    So the HTTP::uri would be "/downloads?v13"

     

    If I put this in an iRule:

     

    log local0. "[substr [HTTP::uri] 0 "?"] "

     

    Will return everything up to the ? so "/downloads" would be returned.

     

    So in /var/log/ltm you would see something similar to this:

     

    Mar 31 09:59:34 ltm2 info tmm1[11931]: Rule /Common/test : /downloads

     

     

    Hope that helps you a bit,

     

    Dave

     

  • looking at your rule if{[string length [substr {HTTP::uri] 0 "?"]]>0} this rule is always going to be true since the first character on a HTTP::uri is always "/" therefore the string lengh will always be greater than 0 may be you want to do if{[string length [substr {HTTP::uri] 1 "?"]]>0} this will start the string length after the first character of HTTP::uri which is always "/"

     

    set VAR1 "/downloads/hector/file1?V12" set VAR2 [substr [VAR1] 0 "?"]

     

    VAR2 will be equal to /downloads/hector/file1 the substr command will start at the beginning and will end when the "?" is matched another example

    set VAR3 "/downloads/hector/fil?v13?hello" set VAR4 [substr [VAR1] 0 "?"]

     

    VAR3 is equal to /downloads/hector/fil

    set VAR5 [[substr [VAR1] 5 "?"]

     

    VAR5 is equal loads/hector/fil the substr
  • In this context, wouldn't

    HTTP::path
    return the same thing and be more efficient?

  • Hi Charles,

    as already outlined by David, a if { [string length [substr [HTTP::uri] 0 "?"]] > 0 } will make no sense at all, since the used

    [substr]
    command will extract portion from the beginning of the
    [HTTP::uri]
    until the first ocourence of a question mark. This will be always at least a
    /
    char, which would then resolve to a
    [string length]
    value of at least
    1
    .

    To check if a HTTP query string (the portion after the first

    ?
    character) is present you may use one of the iRule snippets below...

    Example1: Using a rather simple

    if { X contain Y } then { }
    syntax.

    if { [HTTP::uri] contains "?" } then {
         A query string is present
    } else {
         A query string is NOT present
    }
    

    Example2: Using F5s

    [HTTP::query]
    command to extract the URI query string.

    if { [HTTP::query] ne "" } then {
         A query string is present
    } else {
         A query string is NOT present
    }
    

    Note: The difference of the two syntaxes is, that the first example will also identify empty query strings (e.g.

    /somepath?
    ) and the seconds example will only identify query strings with valid parameters (e.g.
    /somepath?param=1
    ).

    Cheers, Kai