We’ve covered pushing images from LTM before with Kirk’s excellent perl script work on sorry pages.  But that’s not the only thing you can host images for, and it’s not the only approach.  DevCentral community user kevin.stewart crafted up a nifty bash script to achieve the same ends, and gobbles up every image in /var/images, b64 encodes them, then drops them into a class.  The script is minimal in lines, but powerful in output:

#!/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

This results in a class with lines that look like this:

"favicon.ico" := "iVBORw0KGgoAAAANSUhEUgAAAJ8AAACfC…”

Create the class in the GUI, then the iRule is pretty simple as well:

when RULE_INIT {
  ## define the class names
  set static::IMAGES_CLASS “images.class”
}
when HTTP_REQUEST {
  ## is the request an image? Process images from the base64 encoded image class
  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 $static::IMAGES_CLASS] } {
        HTTP::respond 200 content [b64decode [class lookup [string range [HTTP::uri] [expr [string last “/” [HTTP::uri]] + 1] end] $static::IMAGES_CLASS]] \
          “Content-Type” “image/png” “Last-Modified” “Sun, 29 Mar 1970 18:53:56 GMT”
      }
  }
}

Pretty powerful stuff, huh?  Note you’ll need v10 for this implementation, it could, however, be rewritten for v9.  Full write-up is in the codeshare: http://devcentral.f5.com/wiki/default.aspx/iRules/LTMImageHosting.html

Related Articles