Forum Discussion

Bret_McGinnis_1's avatar
Bret_McGinnis_1
Icon for Nimbostratus rankNimbostratus
Aug 22, 2005

user_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.

    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
  • 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_12's avatar
    Colin_Walker_12
    Historic F5 Account
    For 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

     

     

     

  • 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_12's avatar
    Colin_Walker_12
    Historic F5 Account
    Hmm, 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_95363's avatar
    unRuleY_95363
    Historic F5 Account
    The 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
       }
    }

  • 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_95363's avatar
    unRuleY_95363
    Historic F5 Account
    No, 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.