Forum Discussion
Ken_Wong_48678
Sep 20, 2005Historic F5 Account
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
}
}
5 Replies
- 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 - Brian_Gupta_115
Nimbostratus
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 - drteeth_127330Historic F5 AccountYes, they are truly global.
- unRuleY_95363Historic F5 AccountI 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... ?
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)Recent Discussions
Related Content
DevCentral Quicklinks
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com
Discover DevCentral Connects