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,
- You can use the HTTP::header command to get the User-Agent header.
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 } }
- 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. - 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 } }
- 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 - Colin_Walker_12Historic F5 AccountHmm, that sounds a bit odd. Have you tried using string match? The syntax looks like this:
when HTTP_REQUEST { if {[string match -nocase "test" [HTTP::header "User-Agent"]]} { pool testpool } }
- 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: - 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.
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