Forum Discussion

Aurel's avatar
Aurel
Icon for Cirrus rankCirrus
Jan 04, 2019

iRule : matching 2 patterns in the query string but in order

Hello, I would like to match 2 different patterns in the query string, but one before the other. Example : http://www.example.com/test?pattern1=value1&pattern2=value2

 

The "pattern1" have to be before the "pattern2" within the query string, to match.

 

I am not sure this is possible, and stil searching for it. Many thanks for any help.

 

1 Reply

  • of course it's possible. following lines are from tclsh for demo. you can use same if expression in irule.

    % set uri_list {"/test?pattern1=value1&pattern2=value2" "/test?pattern2=value2" "/test?pattern1=value1" "/test?pattern2=value2&pattern1=value1"}
    
    "/test?pattern1=value1&pattern2=value2" "/test?pattern2=value2" "/test?pattern1=value1" "/test?pattern2=value2&pattern1=value1"
    
    % foreach uri $uri_list {
        unset -nocomplain p1pos p2pos
         For logging only because p2pos is not set if first condition fails in next line.
        set p2pos [string first "pattern2=" $uri]
        if {([set p1pos [string first "pattern1=" $uri]] != -1) && ([set p2pos [string first "pattern2=" $uri]] != -1) && ($p1pos < $p2pos)} { 
            puts "ok : $p1pos / $p2pos / [expr {$p1pos < $p2pos}]"
        } else {
            puts "Not ok : $p1pos / $p2pos / [expr {$p1pos < $p2pos}]"
        }
    }
    % 
    ok : 6 / 22 / 1
    Not ok : -1 / 6 / 1
    Not ok : 6 / -1 / 0
    Not ok : 22 / 6 / 0
    %