Forum Discussion
Bret_McGinnis_1
Nimbostratus
Aug 22, 2005user_agent field in the http header
I would like to use the user_agent field to determine which pool a request goes to. Is there an easy or perfered way to access the user_angent field.
I looked at the doc and searched the forums. I didn't find what I was looking for.
Regards,
8 Replies
- You can use the HTTP::header command to get the User-Agent header.
Something like this:when HTTP_REQUEST { set ua [HTTP::header "User-Agent"] log "User Agent: $ua" if { $ua contains "MSIE" } { log "looks like Internet Explorer" pool pool_ie_users } elseif { $ua contains "Firefox" } { log "looks like FireFox" pool pool_firefox_users } else { log "looks like another browser!" pool pool_unknown_users } }
-Joe - Bret_McGinnis_1
Nimbostratus
Thanks, it works great. I have an additional request. Now that I can access the User-Agent I need to check it for and set the pool based on the results. I figured out how to code and if statement that is case insensitive but how do I also check for a for a specific string anywhere within the User-Agent field.
For example: If will be true when checking for the string "test" in User-Agent when the field contains the following sample data:
Test-image
test/v.2
msie Test/v2 m1
FireFox (Pre-Test)-M3 r2
thanks - Colin_Walker_12Historic F5 AccountFor this you would use the "contains" operator. This returns a positive on the match regardless of the positioning of the string you're searching for within the header.
when HTTP_REQUEST { if{ HTTP::header "User-Agent"] contains "test" } { pool testpool } }
This would match test anywhere inside the User-Agent header. So the code Joe provided would work just fine.
Hope this helps,
-Colin - Bret_McGinnis_1
Nimbostratus
I tried using contains before and it worked as long as test was preceded and followed by a space. I couldn't get it to work with something like "test-im". It also was not case insensitive. What
am I doing wrong?
thanks - Colin_Walker_12Historic F5 AccountHmm, that sounds a bit odd. Have you tried using string match? The syntax looks like this:
string match -nocase $string
So in your case it'd be something like:when HTTP_REQUEST { if {[string match -nocase "test" [HTTP::header "User-Agent"]]} { pool testpool } }
Give that a try...
-Colin - unRuleY_95363Historic F5 AccountThe other common way we solve this is to simply use string tolower on the result string before doing the conditional. So, IE:
when HTTP_REQUEST { set ua [string tolower [HTTP::header "User-Agent"]] if { $ua contains "test" } { pool testpool } } - Bret_McGinnis_1
Nimbostratus
I was trying to get regexp to work when I asked the question:
if { [HTTP::header exists User-Agent] and [regexp -nocase $User_Agent [HTTP::header User-Agent] ]}
I got it works minutes before I got your responses. Neither used the method I was trying to get working. It there any advantage to any of these methods. Do they all have the same performance?
Regards
Bret - unRuleY_95363Historic F5 AccountNo, regexp is pretty much the worst performance wise. string match and the contains operator should be about equivalent and they are at least twice as fast as the regexp.
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
