cancel
Showing results for 
Search instead for 
Did you mean: 
Login & Join the DevCentral Connects Group to watch the Recorded LiveStream (May 12) on Basic iControl Security - show notes included.
koenning_107182
Nimbostratus
Nimbostratus

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/v111...

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
}
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
TimRiker
Cirrostratus
Cirrostratus

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

Version history
Last update:
‎18-Mar-2015 13:05
Updated by:
Contributors