Forum Discussion
yves_werniers_1
Nimbostratus
Jun 08, 2010irule stream::exporession and data group list
Hello,
I would like to rewrite responses from webservers to add a part to the url (in certain cases). Is it possible to use the stream::expression with data group lists? Like that, I could make a list for the source, and one for the target. The content of those lists would include things like
href="/asia/
so with only one double quote
If I just use a stream profile with those entries, it works. But the traget field would probably become too long if I had to put all strings in there.
I have tried something like
when HTTP_RESPONSE {
set stream_expression "@$::test_in@$::test_out@"
STREAM::expression $stream_expression
STREAM::enable
}
where test_in and test_out are data group lists
that does not work
any suggestions?
11 Replies
- hoolio
Cirrostratus
Hi Yves,
You can set multiple find/replace strings in a stream expression using the format from the STREAM::expression wiki page:
STREAM::expression {@IE@Apache@ @Windows@Linux@}
So you could get the contents of the class and create a stream expression, but just referencing the datagroups by name won't work.
Which LTM version are you running? The way to reference datagroups has changed from v9 to v10.
Aaron - yves_werniers_1
Nimbostratus
Hi Aaron,
I just read about the change in V 10.1.
I am running 10.1.
I have tried changing the irule to
when HTTP_RESPONSE {
STREAM::expression "@[class get test_in]@[class get test_out]@"
STREAM::enable
}
but that does not yet do the trick. - yves_werniers_1
Nimbostratus
Would it be an option to loop through the list crteated by the get class list_in ?
If so, could someone get me started on a loop through a list?
thanks,
yves - The_Bhattman
Nimbostratus
Hi yves,
You can use a for loop
I.E
foreach { select_test_in } [class get test_in] {
scan $select_test_in { parse } variable1 Variable 2 etc..
}
Here is a blurb on for and for each loops
http://devcentral.f5.com/Tutorials/TechTips/tabid/63/articleType/ArticleView/articleId/118/iRules-Optimization-101--03--for-vs-foreach.aspx
I hope this helps
Bhattman - yves_werniers_1
Nimbostratus
For the moment, I have this:
when HTTP_REQUEST {
Don't allow data to be chunked
if { [HTTP::version] eq "1.1" } {
if { [HTTP::header is_keepalive] } {
HTTP::header replace "Connection" "Keep-Alive"
}
HTTP::version "1.0"
}
}
when HTTP_RESPONSE {
foreach stukje1 [class get test_in] stukje2 [class get test_out])
{
log local0. "stukje1: $stukje1"
STREAM::expression "@$stukje1@$stukje2@"
STREAM::enable
}
}
and my logging looks lik ethis:
TCL error: response_rewrite - list element in braces followed by ")" instead of space while executing "foreach stukje1 [class get test_in] stukje2 [class get test_out]) { log local0. "stukje1: $stukje1" STREAM::expression "@$stukje1@$stukje2@" ..."
so it looks like my "class get" is not even working. - Another option would be to store the expressions each as a single entry in the string data group
class stream_expressions { { "@five@six@" "@nine@ten@" "@one@two@" "@seven@eight@" "@three@four@" } }
Then you could do something like this to pull each one out and append it to a string and use that for the expressionset expressions [class get stream_expressions] set se "" foreach {re} $expressions { append se [lindex $re 0] } if { [llength se] > 0 } { STREAM::expression $se; } else { STREAM::disable; } }
Of course, you could just store the entire concatenated string as a single entry as well and that would save on some string allocations. The class would look something like thisclass stream_expressions { "@one@two@@three@four@@five@six@@seven@eight@@nine@ten@" }
And the iRule could be likeset se [lindex [class element 0 stream_expressions] 0]; if { [llength se] > 0 } { STREAM::expression $se; } else { STREAM::disable; } }
Hope this helps...
-Joe - yves_werniers_1
Nimbostratus
Joe,
since there are "s (double quotes) in my list entries, a data group list seems to be the only decent option to describe the list. - hoolio
Cirrostratus
You should be able to escape the double quotes with backslashes, or just use curly braces to force literal interpretation of the double quotes:
STREAM::expression "@\"find1\"@\"replace1\"@@\"find2\"@\"replace2\"@"
or maybe something like this:
STREAM::expression {@"find1"@"replace1"@@"find2"@"replace2"@}
Aaron - yves_werniers_1
Nimbostratus
Hello all,
thanks for all the input. What I finally put together:
I created 2 data group lists testin and testout
I linked the default stream profile to my virtual server, as well as the below irule.when HTTP_RESPONSE { set listlengte [llength [class get testin]] set streamexpression "" foreach {stringin} [class get testin] {stringout} [class get testout] { set streamexpression [concat $streamexpression "@[lindex $stringin 1]@[lindex $stringout 1]@"] } set streamexpression [string map {"@ @" "@@"} $streamexpression] STREAM::expression $streamexpression STREAM::enable }
All I need to do is to add the part that disabls chunking:when HTTP_REQUEST { Don't allow data to be chunked if { [HTTP::version] eq "1.1" } { if { [HTTP::header is_keepalive] } { HTTP::header replace "Connection" "Keep-Alive" } HTTP::version "1.0" } }
Is there anyone that can estimate the impact (performance wise) of an iRule like this? I used the foreach instead of the 'heavier' for loop, and I used string map instead of string replace, so I would expect it to be 'as light as possible'. If anyone can think of ways to make it even lighter, please let me know.
Yves - hoolio
Cirrostratus
The most efficient option would be to hard code the stream expression in the iRule. Are you expecting to need to change the search/replace strings often enough to justify the overhead in creating it on every TCP connection or HTTP request/response?
Aaron
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)Recent Discussions
Related Content
DevCentral Quicklinks
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com
Discover DevCentral Connects
