For more information regarding the security incident at F5, the actions we are taking to address it, and our ongoing efforts to protect our customers, click here.

offloading content with ifiles

Problem this snippet solves:

This irule allows to serve parts of a website directly from BIG-IP LTM. Only URIs configured in the data class my_to_serve_files will be served via this irule

Have a look here how to manage ifiles : https://devcentral.f5.com/Tutorials/TechTips/tabid/63/articleType/ArticleView/articleId/1086514/v111ndashExternal-File-Access-from-iRules-via-iFiles.aspx

Code :

when RULE_INIT {
# Debug Logging
set static::debug 1
# this irule is serving ifiles to specific URIs provisioned in the dataclass my_to_serve_files
# v 0.1 christian@f5.com
#
# Current limitations:
# - the unique identifier is the file is its name, no similar named files are allowed 
}

when HTTP_REQUEST { 
# provision uri to served in the data class my_to_serve_files  
if { [class match [HTTP::uri] equals my_to_serve_files ] } {

#extract filename from request
set my_ifile  "/Common/[URI::basename [HTTP::uri]]"
if { $static::debug }  {log local0. "URI match, opening file is $my_ifile" }

# try to open ifile with the name of $my_ifile
if { [ catch {set ifileContent [ifile get $my_ifile] } fid] } {

# ifile could not be opened, sending a http 404 status code 
set ifileContent "File not found"
if { $static::debug }  {log local0. "ifile $my_ifile not found" }
HTTP::respond 404 content $ifileContent
} else {

# ifile could be opened, sending content in a http response with a 200 status code
HTTP::respond 200 content $ifileContent
}

unset my_ifile
unset ifileContent

} else {
#this code only gets called when the URI is not provisioned in the data class, so the request will sent through to the webserver
if { $static::debug }  {log local0. "no match for [HTTP::uri], passing to webserver" }
}
}

# Data Group

ltm data-group internal /Common/my_to_serve_files {
    records {
        /my/path/Sample.png { }
    }
   type string
}
Published Mar 18, 2015
Version 1.0

2 Comments

  • Hi, Code works great except situation when URI is just "/" - so no basename exist. I resolved it by adding entry to data group /:=name of default html iFile to serve. Then modified code like that: if { [HTTP::uri] eq "/" } { set my_ifile [class match -value "/" equals my_to_serve_files_dg] } else { set my_ifile "/Common/[URI::basename [HTTP::uri]] } What do you think? Any better simpler way? Piotr
  • I added a version that just presents the iFile path and allows browsing all the iFiles.

     

    https://devcentral.f5.com/s/articles/Serving-or-browsing-iFiles