Forum Discussion

wlepkin_98758's avatar
wlepkin_98758
Icon for Nimbostratus rankNimbostratus
Jan 30, 2012

string trimleft, and assigning HTTP::uri

Hi --

 

 

I'm trying to use the 'string trimleft' function to change the URI, and must be doing something wrong. Here's what I've got, with lots of log statements so I can see what's going on:

 

 

when HTTP_REQUEST {

 

set loweruri [string tolower [HTTP::uri]]

 

log local0. "String-to-lower URI is $loweruri"

 

if { $loweruri starts_with "/chart" } then

 

{

 

log local0. "Original URI is [HTTP::uri]"

 

log local0. "string trimleft = [string trimleft [HTTP::uri] /chart]"

 

HTTP::uri [string trimleft [HTTP::uri] /chart]

 

log local0. "New URI is [HTTP::uri]"

 

log local0. "Redirecting to http://ifp-vip:8080[HTTP::uri]"

 

HTTP::redirect ]

 

unset loweruri

 

}

 

}

 

 

The point being that if the URI starts with '/chart' then you want to strip that off, and send what's left to a different host:port.

 

 

If I send request: http://denfpot-vip/chart/abc

 

 

This is what shows up in the log:

 

 

: String-to-lower URI is /chart/abc

 

: Original URI is /chart/abc

 

: string trimleft = bc

 

: New URI is /chart/abc

 

: Redirecting to http://ifp-vip:8080/chart/abc

 

 

So, this prompts two question:

 

- why is trimleft returning 'bc' instead of '/abc'?

 

- why is the URI not getting reassigned (even to the wrong thing)?

 

 

Software version is 9.3.1, perhaps that's relevant.

 

 

As always, thanks much for your help.

 

 

WDL

 

  • string trimleft operates by removing characters not words. So a is part of the characters you have in your "trim" list so it's removed from the source string. If you want to chop off /chart, you could use string range or scan:

    string range [HTTP::uri] 7 end

    scan [HTTP::uri] {/charts%s}

    string range should be more efficient:

    % time {string range $uri 7 end} 1000

    1 microseconds per iteration

    %

    % time {scan $uri {/charts%s}} 1000

    2 microseconds per iteration

    Also, the value for HTTP::uri, HTTP::path and other HTTP:: commands was cached in the same iRule and event in 9.x and 10.x. This was fixed in v11. Until you upgrade to v11, you'll see the original value when logging, modifying and then logging the values for the HTTP:: commands. The value is actually changed though.

    
    when HTTP_REQUEST {
    if {[string tolower [HTTP::uri]] starts_with "/charts"}{
    HTTP::redirect "http://ifp-vip:8080[string range [HTTP::uri] 7 end]"
    log local0. "redirecting to http://ifp-vip:8080[string range [HTTP::uri] 7 end]"
    }
    }
    

    Aaron
  • Hoolio/Aaron --

     

     

    Gotcha, that's great, thanks! I was misled by the example at the bottom of 'iRules 101 - 14' -- I should have just paid more attention to the 'string range' discussion above that.

     

     

    Thanks again,

     

     

    Wayne