HTTP::respond

Description

Generates a response to the client as if it came from the server. If the command runs on the client side, it sends the response to the client without any load balancing taking place. If the command runs on the server side, the content from the actual server is discarded and replaced with the information provided.
The BIG-IP will send the response as soon as the current iRule event completes, so you cannot alter the response in other HTTP iRule events. Also, this command will not work if another response has already been sent to the client (for example, by invoking HTTP::redirect). If the client’s HTTP request is not using http keep-alive (eg, it sent ‘Connection: Close’ in the request, or is using HTTP/1.0), then the HTTP profile will close the clientside connection after sending the response.
By default, the response generated is an HTTP/1.0 response; use the “-version” flag (introduced in 11.2.0) to explicitly set the response to HTTP/1.0 or HTTP/1.1. The HTTP status code is determined by the supplied parameter. Unless you add the “noserver” option, a header of the form “Server: BIG-IP” will be inserted to distinguish replies generated by the BIG-IP from those supplied by remote servers. A suitable “Content-Length” header is inserted automatically (replacing any you supply; you cannot force an arbitrary Content-Length header).
A word of warning: when you return a 401 HTTP status code, the BIGIP will inject a WWW-Authenticate: Basic, Realm=”” according to the RFC.

Syntax

HTTP::respond <status code> [-version [1.0|1.1|auto]] [content <content Value>] [noserver] [<Header name> <Header Value>]+

v11.5+
HTTP::respond [-reset] <status code> [-version [1.0|1.1|auto]] [content <content Value>] [noserver] [<Header name> <Header Value>]+

HTTP::respond <status code> [-version [1.0|1.1|auto] ] [content <content Value>] [noserver] [<Header name> <Header Value>]+

  • Generates a response to the client as if it came from the server, with the supplied content and header values.

HTTP::respond [-reset] <status code> [-version [1.0|1.1|auto] ] [content <content Value>] [noserver] [<Header name> <Header Value>]+

  • Generates a response to the client as if it came from the server, with the supplied content and header values and resets serverside connection instead of sinking the data.

Note: The noserver flag was added in 9.4.2. It suppresses the insertion of the ‘Server: BigIP’ header. The noserver flag must be included after the content (if any).

Note: The -version flag was added in 11.2.0. It must immediately follow the status code and precede the content (if any) and any other flags.

Note: An ‘auto’ option was added for the ‘-version’ flag in 11.4.0.

Examples

To send a redirect with a cookie set.
when HTTP_REQUEST {
    set ckname "app"
    set ckvalue "893"
    set cookie [format "%s=%s; path=/; domain=%s" $ckname $ckvalue ".domain.org"]
    HTTP::respond 302 Location "http://www.domain.org" "Set-Cookie" $cookie
}

Or you can generate an apology page from within the iRule. The curly braces prevent variable expansion and should eliminate the need to escape meta-characters within the response content.
when HTTP_REQUEST {
   HTTP::respond 200 content {
      <html>
         <head>
            <title>Apology Page</title>
         </head>
         <body>
            We are sorry, but the site you are looking for is temporarily out of service<br>
            If you feel you have reached this page in error, please try again.
         </body>
      </html>
   }
}

Here is an example which rewrites a redirect with the original cookies from the HTTP response of the server:
when HTTP_RESPONSE {
   if { [HTTP::is_redirect] } {
      foreach aCookieName [HTTP::cookie names] {
         set currentCookie "$aCookieName=[HTTP::cookie value  $aCookieName]"
         set cookies "$cookies\r\nSet-Cookie: $currentCookie"
      }
      HTTP::respond 200 content "<HTML><HEAD><TITLE>Forbidden Redirect From Remote Server</TITLE></HEAD>\
<BODY>The server is trying to redirect the client to an external site, but it is forbidden</BODY></HTML>" Set-Cookie $cookies
   }
}

Another example with multiple cookies, but without excessive empty cookie - returns original cookies to the client:
when HTTP_REQUEST {
   set cookies ""
   foreach next_cookie [HTTP::cookie names] {
      set cookies "$cookies\"Set-Cookie\" \"$next_cookie=[HTTP::cookie value $next_cookie]\" "
   }
   log local0. "Sending back $cookies"
   eval HTTP::respond 200 "$cookies"
}

An example of a plain redirect - equivalent to using HTTP::redirect, but with the Server BigIP header suppressed. In this example we’re redirecting our http:// request to the https:// version.
when HTTP_REQUEST {
   HTTP::respond 302 noserver Location "https://[HTTP::host][HTTP::uri]"
}