Forum Discussion

Deb_Allen_18's avatar
Deb_Allen_18
Historic F5 Account
May 09, 2006

calling a command from a class

I'm building lots of variations on a theme for redirects, and would like to be able to use dynamic request variables to build redirect responses.

I was hoping to use command strings stored in a class to build the redirect location dynamically, to avoid coding a separate condition/action for each location pattern:

class HostRedirects {
www.domain.com 301 https://www.domain.com[HTTP::uri]
host.domain.com 302 http://host2.domain.com/index.html
another.domain.com 301 https://www.domain.com/another[HTTP::uri]
...
}
rule HostRedirects {
when HTTP_REQUEST {
  set row [findclass [getfield [HTTP::host] ":" 1] $::HostRedirects]
  if { not ($row eq "")}{
    HTTP::respond [getfield $row " " 2] Location [getfield $row " " 3]
    unset row
    return
  }
}
}

but I can't seem to get the [HTTP::uri] command that is part of the class member to evaluate, so the redirect location comes back literally as "https://www.domain.com[HTTP::uri]", rather than "https://www.domain.com/my/favorite/website.html"

Any ideas?

/deb
  • Deb_Allen_18's avatar
    Deb_Allen_18
    Historic F5 Account
    (oops, had to clean that up a little.

     

    should make more sense now...)
  • Colin_Walker_12's avatar
    Colin_Walker_12
    Historic F5 Account
    Try adding an eval line in your rule.

    Like this:

    
    rule HostRedirects {
      when HTTP_REQUEST {
        set row [findclass [getfield [HTTP::host] ":" 1] $::HostRedirects]
        if { not ($row eq "")}{
          set tmpRow [eval $row]
          HTTP::respond [getfield $tmpRow " " 2] Location [getfield $tmpRow " " 3]
          unset row
          unset tmpRow
          return
        }
      }
    }

    That should get the HTTP::uri command to evaluate for you. You might also want to add some logging to see what each of your getfield commands are returning if you have further problems.

    Colin
  • Deb_Allen_18's avatar
    Deb_Allen_18
    Historic F5 Account
    getfield commands are definitely returning what's expected, problem is just that it's not being interpreted.

     

     

    When I use eval against any string that's not actually a command (the $row variable string retrieved by findclass), I get this runtime error:

     

    May 18 23:01:57 tmm tmm[788]: 01220001:3: TCL error: invalid command name "" while executing "" ("eval" body line 1) invoked from within "eval $row"

     

     

    I've been messing about with some other options, but no luck yet. The code works fine until I go to pull the redirect string out of the class. I'm wondering if the return from findclass is quoting the string such that evaluation is not taking place?

     

     

    /deb