Forum Discussion
If I may add, findstr is one of those crazy cool unsung commands that a lot of people don't know about (even though they probably see it all the time). It's not a native TCL command, but rather a highly optimize iRule-specific command. The syntax is basically like this:
findstr [source string] [find string] [skip-count] [terminator count or string]
The [skip count] value is the number of characters to skip once you've found the string, so given the URI:
http://xyzserver:8080/xxx/xxx.svc?cid=123-456-7895
And the findstr command:
log local0. [findstr [HTTP::uri] "?cid=" 5]
It will find the part of the string that starts with "?cid="
?cid=123-456-7895
And then skip 5 characters ahead
123-456-7895
The [terminator string or count] value basically tells you where to end. You can use a terminator count:
set uri "http://xyzserver:8080/xxx/xxx.svc?cid=123-456-7895"
log local0. [findstr $uri "?cid=" 5 5
123-4
You skip 5 characters (?cid=) and then grab the next 5. If you use a string terminator, then you're telling it to stop collecting when it gets to a specific string value. In the example above, Matin was terminating on the ";" semicolon. If none existed, then collection would continue to the end of the source string. One important note here, the skip count value is arbitrary, in which it won't skip any characters if it's not included, and you cannot have a terminator without a skip count.
Also, would I have to use cookie or cookie always-send as persistence with this iRule?
Now on to the second part. The fact that you're asking about cookie persistence leads me to assume that the cid value isn't always going to be in the URI. If that's true, and also assuming that the cid is at least in the FIRST request, then you need some other mechanism to maintain persistence after the first request. The easiest would probably be a cookie. So something like this:
when HTTP_REQUEST {
set cid [findstr [HTTP::uri] "cid" 4 ";"]
if { $cid != "" } {
persist uie $cid
set setcookie 1
} elseif { [HTTP::cookie exists CID] } {
persist uie [HTTP::cookie value CID]
}
}
when HTTP_RESPONSE {
if { [info exists setcookie] } {
unset setcookie
HTTP::cookie insert name CID value $cid
}
}