LTM Image Hosting
Problem this snippet solves:
Host and Return images directly from LTM. First example is for favicon.ico, but could be more/different/etc.
- Save your image(s) in a file-based class (can be an internal class, but it makes the config file messy). The following script actually let’s you save multiple images in a file class. Copy them all to a directory (/var/images) and run the script. It will create base64 encoded versions of each image in the /var/class/images.class. NOTE: Must run "b load" anytime an external file-based class is created or updated!
-
Define the class as a Data Group List in the GUI: Local Traffic -> iRules -> Data Group List -> Create...
- Name: images_class
- Type: (External File)
- Path/Filename: /var/class/images.class
- File Contents: String
- Key/Value Pair Separator: :=
- Access Mode: Read Only
-
Now listen for and respond to favicon requests:
Code :
#!/bin/bash ## clear /var/class_build/images_build.class echo -n "" > /var/class/images.class ## loop through real images and create base64 data for images_build.class for i in $(ls /var/images); do echo "\"`echo \"$i\"|tr '[:upper:]' '[:lower:]'`\" := \"`base64 /var/images/$i|tr -d '\n'`\"," >> \ /var/class/images.class done when HTTP_REQUEST { if { [string tolower [HTTP::path]] equals "/favicon.ico" } { if { [class match [findstr [string tolower [HTTP::path]] "/" 1] equals images_class] } { HTTP::respond 200 content [b64decode [class lookup [findstr [HTTP::path] "/" 1] images_class]] \ "Content-Type" "image/x-icon" \ "Last-Modified" "Sun, 29 Mar 1970 18:53:56 GMT" } } } # Note:The “Last-Modified” header is there to allow it to cache. 4. And finally, if you want to serve up multiple images, not just favicon: ## is the request an image? Process images from the base64 encoded image class when HTTP_REQUEST { if { [string tolower [HTTP::path]] ends_with ".jpg" or [string tolower [HTTP::path]] ends_with ".png" or [string tolower [HTTP::path]] ends_with ".gif" } { ## assume image name is after the last forward slash character in the URI if { [class match [string range [HTTP::uri] [expr [string last "/" [HTTP::uri]] + 1] end] equals images_class] } { HTTP::respond 200 content [b64decode [class lookup [string range [HTTP::uri] [expr [string last "/" [HTTP::uri]] + 1] end] images_class]] \ "Content-Type" "image/png" \ "Last-Modified" "Sun, 29 Mar 1970 18:53:56 GMT" } } }
Published Mar 18, 2015
Version 1.0Kevin_Stewart
Employee
Joined March 16, 2006
Kevin_Stewart
Employee
Joined March 16, 2006
No CommentsBe the first to comment