Forum Discussion
hooleylist
Jul 07, 2008Cirrostratus
Using getfield to split the path wouldn't work if there is a period anywhere else in the path (like /path/to/my.image.jpg).
You could add a check to see if the URI is /. It would be a one-off though as you don't want to check if the URI ends with /, it would be if the URI is /.
To enable use of CMP you can reference the class without the $:: (Click here).
Any variable set in RULE_INIT is implicitly created as a global variable. To avoid this for expire_content_timeout you could depend on it being set in HTTP_REQUEST.
Aaron
class class_cacheable_file_types {
"htm 300"
"html 300"
"css 3600"
"js 3600"
"bmp 86400"
"gif 86400"
"jpeg 86400"
"jpg 86400"
"png 86400"
"pdf 604800"
"swf 604800"
"ico 604800"
}
rule irule_manipulate_client_cache {
when HTTP_REQUEST {
Set the cache timeout for root requests. Check that the request is a GET and that there is not a query string.
if {[HTTP::method] eq "GET" and [HTTP::uri] eq "/"}{
set expire_content_timeout 300
} else {
Set the tiemout based on the class entry if it exists for this request
set expire_content_timeout [findclass [getfield [string tolower [HTTP::uri]] "." 2] class_cacheable_file_types " "]
}
}
when HTTP_RESPONSE {
if { $expire_content_timeout ne "" } {
HTTP::header replace "Cache-Control" "max-age=$expire_content_timeout, public"
HTTP::header replace "Expires" "[clock format [expr ([clock seconds]+$expire_content_timeout)] -format "%a, %d %h %Y %T GMT" -gmt true]"
} else {
HTTP::header replace "Cache-Control" "private"
HTTP::header replace Expires {-1}
}
}
}
Here's one method for handling files with more than one period:
class class_cacheable_file_types {
".htm 300"
".html 300"
".css 3600"
".js 3600"
".bmp 86400"
".gif 86400"
".jpeg 86400"
".jpg 86400"
".png 86400"
".pdf 604800"
".swf 604800"
".ico 604800"
}
rule irule_manipulate_client_cache {
when HTTP_REQUEST {
Set the cache timeout for root requests. Check that the request is a GET and that there is not a query string.
if {[HTTP::method] eq "GET" and [HTTP::uri] eq "/"}{
set expire_content_timeout 300
} else {
Set the tiemout based on the class entry if it exists for this request.
set expire_content_timeout [findclass [string tolower [string range [HTTP::path] [string last . [HTTP::path]] end]] class_cacheable_file_types " "]
}
}
when HTTP_RESPONSE {
if { $expire_content_timeout ne "" } {
HTTP::header replace "Cache-Control" "max-age=$expire_content_timeout, public"
HTTP::header replace "Expires" "[clock format [expr ([clock seconds]+$expire_content_timeout)] -format "%a, %d %h %Y %T GMT" -gmt true]"
} else {
HTTP::header replace "Cache-Control" "private"
HTTP::header replace Expires {-1}
}
}
}