regexp to parse querystring parameters

I am trying to get a list of querystring parameters from the URI. There’s an example iRule that does this here:

http://devcentral.f5.com/wiki/default.aspx/iRules/URIInterrogation.html

The relevant code is:

set namevals [split [HTTP::query] "&"] 
 for {set i 0} {$i < [llength $namevals]} {incr i} { 
 set params [split [lindex $namevals $i] "="] 
 set pnum [expr $i+1] 
 log local0. "Param\[$pnum\]: [lindex $params 0]" 
 log local0. "Value\[$pnum\]: [URI::query [HTTP::uri] [lindex $params 0]]" 
 } 
 }

That code works fine, but it seems like this would be more efficient as a regexp, or even a foreach. Can anyone think of a better way to implement this?

I’d think this looping with string commands would be more efficient than a regex. A foreach loop would probably be about the same. You could test these options using the timing command (Click here).

If you are trying to retrieve the value for a specific parameter, you don’t have to loop through each parameter name. You can just use URI::query:

URI::query [HTTP::uri] “param_name”

Aaron

Makes sense. This is related to my other post - I’m stuck in the TCP stream without an HTTP profile since I am tunneling non HTTP traffic over HTTP, so I’m forced to do all my HTTP parsing manually (joy of joys).

Jim

If you have any string in the format of ?name1=value1&name2=value2, you can use URI::query to parse the value of any of the parameters:

set my_uri “?action=login&pref=2”

log local0. “[URI::query $my_uri “action”]”

This would output “login”. If you’ve already removed the question mark when parsing the URI string, you could prepend it:

set my_uri “action=login&pref=2”

log local0. “[URI::query “?$my_uri” “action”]”

Aaron