Forum Discussion

Lemaire_Frédéri's avatar
Sep 15, 2023

Is it possible to use the following Irule syntac with TCL in a policy ?

Hello,

I've setup some code in an Irule .
This concerns a code that will take the URI, within this uri, search for first directory in the path and put it tolower before sending it to the server.


set uri [HTTP::uri]
  
    set block [lindex [split $uri /] 1]
   
    if { $block ne [string tolower $block]}
        {
        set block2 [string tolower $block]
        HTTP::uri [string map [list $block $block2] $uri]
        #log local0. "Rewrited part of the URI : $block2"
        #log local0. "URI Send to Back-end application : https://[HTTP::host][HTTP::uri]"
        }
 
 
Is it possible to put this code in a TCL within a policy rule ?
I need to replace the first directory of the URI  (ie: "/APPLICATION/dir1/DIR2/index.html") to lowercase /application/dir1/DIR2/index.html ( the rest of the URI must stay intact, only /Application/ part must be set to lowercase.

Thanks in advance.
Regards
Frédéric
 

2 Replies

  • HI Lemaire_Frédéri , conditions in policies don't accept Tcl expressions, only actions. If we could land on a single expression to accomplish this it might be possible, but anything requiring anything more than a cached value lookup in a policy sends me to an iRule outright by preference...your mileage may vary. To your block of code, you can simplify that to not set any variables at all (not as readable), and if you're always going to rewrite if it "isn't" lowercase, then you don't really need the condition and can just rewrite always, right? Anway...IF that's the case for this particular virtual server, THEN, you might yet be able to use a policy with this:

    rewrite_first_path_segment {
        actions {
            0 {
                http-uri
                replace
                value "tcl:[string map [list [getfield [HTTP::uri] / 2] [string tolower [getfield [HTTP::uri] / 2]]] [HTTP::uri]]"
            }
        }
        ordinal 5
    }

     Again, I'd go the iRule route here, but you can investigate from here with the policy.  A couple other solution options I was noodling on for fun:

    # option 1 - close to your solution
    set pathobj [getfield [HTTP::uri] "/" 2]
    if { ![string equal $pathobj [string tolower $pathobj]] } {
        HTTP::uri [string map [list $pathobj [string tolower $pathobj]] [HTTP::uri]]
    }
    
    # option 2 - regex rewrite 
    if { [regexp {^(/[^/]+)(/.*)?$} [HTTP::uri] -> pathobj remaining_uri] } {
        HTTP::uri [join [list [string tolower $pathobj] $remaining_uri] "/"]
    }
  • Lemaire_Frédéri I don't see any issue with the block of code if it were added to an iRule with the correct event. I would convert all your policy rules to one iRule though instead of using both simultaniously because it's much easier to go to one location rather than multiple when troubleshooting.