Http Response Based On Requested Uri

Problem this snippet solves:

This iRule sends an HTTP response based on the requested path. It can be used as a pool member on another LTM for testing. The iRule parses the first three characters in the path for a status code to use in the response. If a valid three digit code (100-599) isn't found, an HTTP 200 response is sent. The second example allows the client to specify a redirect location for requests made to a URI of /301 or /302.

How to use this snippet:

Dependencies

The virtual server must have an HTTP profile. No pool is required as the iRule sends a response for every request.

Code :

when HTTP_REQUEST {

   # Data to send to the client
   set data {Some data or HTML to send to the client.  If you want to use variables in here, replace the curly braces with quotes.}

   # Look for a URI starting with a forward slash and then 3 digits (the first digit being 1-5)
   # If one is found, set it as the HTTP response code.  Else, send back an HTTP 200
   if {[string match {/[1-5][0-9][0-9]*} [HTTP::path]]}{

      # Send the response with the status code from the requested path and data
      HTTP::respond [string range [HTTP::path] 1 3] content $data "a_header_name" "a_header_value"
      log local0. "Sending [string range [HTTP::path] 1 3] response with $data"

   } else {

      # Send a 200 response with data
      HTTP::respond 200 content $data "a_header_name" "a_header_value"
      log local0. "Sending default 200 response with $data"
   }
}

# Sample request and response

# curl -v 10.42.2.102/503

* About to connect() to 10.42.2.102 port 80
*   Trying 10.42.2.102... connected
* Connected to 10.42.2.102 (10.42.2.102) port 80
> GET /503 HTTP/1.1
> User-Agent: curl/7.15.3 (i686-redhat-linux-gnu) libcurl/7.15.3 OpenSSL/0.9.7l zlib/1.1.4
> Host: 10.42.2.102
> Accept: '''/'''
>

< HTTP/1.0 503 Service Unavailable
< a_header_name: a_header_value
* HTTP/1.0 connection set to keep alive!
< Connection: Keep-Alive
< Content-Length: 109
Connection #0 to host 10.42.2.102 left intact
* Closing connection #0
Some data to send to the client.  If you want to use variables in here, replace the curly braces with quotes.

# iRule Source for Example 2 with 301/302 redirects

when HTTP_REQUEST {

set data {Some data to send to the client.  If you want to use variables or commands here, replace the curly braces with quotes.}

# Look for a URI starting with a forward slash and then 3 digits (the first digit being 1-5)
# If one is found, set it as the HTTP response code.  Else, send back an HTTP 200
switch -glob [HTTP::path] {
"/30[12]*" {
# Send a 30x redirect
if {[set location [URI::query [HTTP::uri] location]] ne ""}{
HTTP::respond [string range [HTTP::path] 1 3] location $location
log local0. "Sending [string range [HTTP::path] 1 3] response with location $location"
} else {
HTTP::respond [string range [HTTP::path] 1 3] content $data "a_header_name" "a_header_value" location "/redirected"
log local0. "Sending [string range [HTTP::path] 1 3] response with $data to default location /redirected"
}
}
{/[1-5][0-9][0-9]*} {
# Send the response with the status code from the requested path and data
HTTP::respond [string range [HTTP::path] 1 3] content $data "a_header_name" "a_header_value"
log local0. "Sending [string range [HTTP::path] 1 3] response with $data"
}
default {
# Send a 200 response with data
HTTP::respond 200 content $data "a_header_name" "a_header_value"
log local0. "Sending default 200 response with $data"
}
}
}
Published Mar 18, 2015
Version 1.0

Was this article helpful?

No CommentsBe the first to comment