Forum Discussion

t-roy's avatar
t-roy
Icon for Nimbostratus rankNimbostratus
Nov 22, 2011

inserting X-Origin-Host heade when using catch command

iRule's purpose:

 

rewrite some URIs to /

 

rewrite some hosts to start with www

 

send some URIs to my non-default pool but putting them in a data-group like "foo" := "foopool",

 

My problem: If my last if statement matches I would like to insert an X-Origin-Host header like so:

 

 

HTTP::header insert lws X-Origin-Host "[HTTP::host]"I know how to do this if I set a variable then add once my pool is selected, but I can't figure out how to do it using catch. Any help is appreciated!

 

 

when HTTP_REQUEST {

 

if {[ class match -value [string toupper [HTTP::uri]] starts_with WWW-PREIMP.FOO.CA-URIREDIR] ne "" } {

 

HTTP::uri "/"

 

}

 

if {

 

[catch {[class match -value [string toupper [HTTP::host]] starts_with WWW-PREIMP.FOO.CA-PATHREDIR] } ]

 

} {

 

HTTP::respond 301 Location "]"

 

}

 

if {

 

[catch { pool [class match -value [string toupper [HTTP::uri]] starts_with FOO.CA-REDIR] } ]

 

} {

 

pool WWW-PREIMP.FOO.CA

 

}

 

}

 

 

Thanks!

 

Troy

4 Replies

  • Hi Troy,

    So you're trying to use catch to handle errors when inserting the header? How about something like this:

    
    if {[HTTP::host] ne ""}{
       catch {HTTP::header insert lws X-Origin-Host "[HTTP::host]"}
    }
    

    Aaron
  • e.g.

    [root@ve1023:Active] config  b virtual bar list
    virtual bar {
       snat automap
       pool foo
       destination 172.28.19.79:80
       ip protocol 6
       rules myrule
       profiles {
          http {}
          tcp {}
       }
    }
    [root@ve1023:Active] config  b pool foo list
    pool foo {
       members 200.200.200.101:80 {}
    }
    [root@ve1023:Active] config  b pool foopool list
    pool foopool {
       members 200.200.200.102:80 {}
    }
    [root@ve1023:Active] config  b rule myrule list
    rule myrule {
       when HTTP_REQUEST {
            if {[class match -- [string toupper [HTTP::host]] starts_with WWW-PREIMP.FOO.CA-PATHREDIR]} {
                    HTTP::respond 301 Location "http://www.[HTTP::host]"
    
            } elseif {[class match -- [string toupper [HTTP::uri]] starts_with WWW-PREIMP.FOO.CA-URIREDIR]} {
                    HTTP::uri "/"
            }
    
            if {[class match -- [string toupper [HTTP::uri]] starts_with FOO.CA-REDIR]} {
                    catch {HTTP::header insert lws X-Origin-Host [HTTP::host]}
                    catch {pool [class match -value [string toupper [HTTP::uri]] starts_with FOO.CA-REDIR]}
            }
    }
    }
    [root@ve1023:Active] config  b class WWW-PREIMP.FOO.CA-PATHREDIR list
    class WWW-PREIMP.FOO.CA-PATHREDIR {
       "ABC.COM"
    }
    [root@ve1023:Active] config  b class WWW-PREIMP.FOO.CA-URIREDIR list
    bclass WWW-PREIMP.FOO.CA-URIREDIR {
       "/TEST"
    }
    [root@ve1023:Active] config  b class FOO.CA-REDIR list
    class FOO.CA-REDIR {
       "/FOO" { "foopool" }
    }
    
    1
    
    [root@ve1023:Active] config  curl -I http://abc.com/abc
    HTTP/1.0 301 Moved Permanently
    Location: http://www.abc.com
    Server: BigIP
    Connection: Keep-Alive
    Content-Length: 0
    
    2
    
    [root@ve1023:Active] config  ssldump -Aed -nni 0.0 port 80
    New TCP connection 1: 172.28.19.253(45755) <-> 172.28.19.79(80)
    1322048483.6894 (0.0031)  C>S
    ---------------------------------------------------------------
    HEAD /test/abc HTTP/1.1
    User-Agent: curl/7.15.5 (i686-redhat-linux-gnu) libcurl/7.15.5 OpenSSL/0.9.8r zlib/1.2.3 libidn/0.6.5
    Host: www.abc.com
    Accept: */*
    ---------------------------------------------------------------
    
    New TCP connection 2: 200.200.200.10(45755) <-> 200.200.200.101(80)
    1322048483.6904 (0.0009)  C>S
    ---------------------------------------------------------------
    HEAD / HTTP/1.1
    User-Agent: curl/7.15.5 (i686-redhat-linux-gnu) libcurl/7.15.5 OpenSSL/0.9.8r zlib/1.2.3 libidn/0.6.5
    Host: www.abc.com
    Accept: */*
    ---------------------------------------------------------------
    
    3
    
    [root@ve1023:Active] config  ssldump -Aed -nni 0.0 port 80
    New TCP connection 1: 172.28.19.253(47562) <-> 172.28.19.79(80)
    1322048588.1531 (0.0019)  C>S
    ---------------------------------------------------------------
    HEAD /foo/abc HTTP/1.1
    User-Agent: curl/7.15.5 (i686-redhat-linux-gnu) libcurl/7.15.5 OpenSSL/0.9.8r zlib/1.2.3 libidn/0.6.5
    Host: www.abc.com
    Accept: */*
    ---------------------------------------------------------------
    
    New TCP connection 2: 200.200.200.10(47562) <-> 200.200.200.102(80)
    1322048588.1553 (0.0010)  C>S
    ---------------------------------------------------------------
    HEAD /foo/abc HTTP/1.1
    User-Agent: curl/7.15.5 (i686-redhat-linux-gnu) libcurl/7.15.5 OpenSSL/0.9.8r zlib/1.2.3 libidn/0.6.5
    Host: www.abc.com
    Accept: */*
    X-Origin-Host: www.abc.com
    ---------------------------------------------------------------
    
    
  • t-roy's avatar
    t-roy
    Icon for Nimbostratus rankNimbostratus
    awesome!!! nitass your solution is exactly what I had in mind. Going to test in my lab but I think that should work perfectly.

     

    thanks guys!!!