Forum Discussion

Ken_Wong_48678's avatar
Ken_Wong_48678
Historic F5 Account
Sep 20, 2005

change content type by irule

Hi guys,

 

 

I have written a test irule to change the content type of a specific page. I would like to change the content type of main.css from text/css to application/x-javascript.

 

 

In HTTP_REQUEST part, I can successfully to check the existence of the page and set a parameter.

 

But, in the HTTP_RESPONSE part, the parameter seems cannot maintain. Therefore, the content type cannot changed. Anyone can point out any problems in my rule? Thanks!

 

 

Regards,

 

Ken

 

 

------------------------------------------------------

 

when HTTP_REQUEST {

 

if {[HTTP::uri] contains "main.css"} {

 

set par 1

 

log local0. "par is $par"

 

}

 

set par 0

 

pool test_pool

 

}

 

 

when HTTP_RESPONSE {

 

set ct1 "application/x-javascript"

 

set ct [HTTP::header "Content-Type"]

 

log local0. $ct

 

if { $par == 1 } {

 

HTTP::header replace "Content-Type" $ct1

 

}

 

}
  • When creating variables that you want to persist across multiple events, you'll need to access them in the global namespace (with the "::" prefix).

    Here's an example:

    when HTTP_REQUEST {
      set this_variable_is_local 1
      set ::this_variable_is_global 1
    }
    when HTTP_RESPONSE {
       This will fail as this_variable_is_local isn't defined in this scope
      if { [info exists this_variable_is_local] } {
        if { $this_variable_is_local == 1 } {
        }
      }
       This should succeed.
      if { [info exists ::this_variable_is_global] } {
        if { $::this_variable_is_global == 1 } {
          log local0. "Found this_variable_is_global"
        }
      }
    }

    Note, You should always use the "info exists" command before you access a variable that you are unsure exists to avoid runtime errors.

    -Joe
  • 1) Are these truly global? IE: Can I access them from different VIPS/rules/etc? (vs. multiple iterations of the same irule)

     

    2) Is there a way to release a declared variable?

     

    3) I haven't make much use of variables to date. (Just simple strings and small integers) Where is the best place to find out more about variable types and ranges?

     

    4) Also would the TCL docs be the best place to find out about variable operations?

     

     

    Thanks,

     

    Brian
  • I'll try to answer these one by one:

     

     

    1) Are these truly global? IE: Can I access them from different VIPS/rules/etc? (vs. multiple iterations of the same irule)

     

     

    I'm going to have to defer to unRuleY to answer this one. I know they are global within the given rule within a given Virtual Server's resource instance, but I am unsure as to the extent beyond that.

     

     

    2) Is there a way to release a declared variable?

     

     

    Use the TCL "unset" command to release and undefine a variable.

     

     

    3) I haven't make much use of variables to date. (Just simple strings and small integers) Where is the best place to find out more about variable types and ranges?

     

     

    The best place to look is the TCL Reference Manual on SourceForge

     

     

    http://tmml.sourceforge.net/doc/tcl/

     

    Click here

     

     

    We have removed support for procedures and external IO calls but most of the other functions are available in our implementation.

     

     

    4) Also would the TCL docs be the best place to find out about variable operations?

     

     

    See 3. As for internal commands, you should see the BIG-IP Product Documentation on ask.f5.com and look in the iRules section.

     

     

    -Joe
  • unRuleY_95363's avatar
    unRuleY_95363
    Historic F5 Account
    I think the original problem was simply that your HTTP_REQUEST event is always resetting par back to 0.

     

    I think you want to have an "else { set par 0 }" in your HTTP_REQUEST event.

     

     

    I'm not sure why Joe thought you needed a global variable... ?