HTTP::request

Description

Returns the raw HTTP request headers as a string. You can access the request payload (e.g. POST data) by triggering payload collection with the HTTP::collect command and then using HTTP::payload in the HTTP_REQUEST_DATA event.

Syntax

HTTP::request

HTTP::request

  • Returns the raw HTTP request headers as a string. You can access the request payload (e.g. POST data) by triggering payload collection with the HTTP::collect command and then using HTTP::payload in the HTTP_REQUEST_DATA event.

Examples

# Note: this example would not work if the client sends a POST request with a payload, as HTTP::request does not return the payload.
when HTTP_REQUEST {

  # Exit this event if the request isn't a GET
  if {[HTTP::method] ne "GET"}{return}

  # save original request headers
  set req [HTTP::request]

  # flag as new request needing lookup
  set lookup 1

  # inject lookup URI in place of original request
  HTTP::uri "/page.aspx?ip=[IP::client_addr]"

  # set pool to lookup server pool
  pool lookup_server
}

when HTTP_RESPONSE {
  if {$lookup == 1 }{
    # collect first response (from lookup server) only
    HTTP::collect 1
  }
}

when HTTP_RESPONSE_DATA {

  # Get poolname from server response
  # Response would ideally be in the form of a pool name only.
  # Otherwise parse or derive the poolname here
  set myPool [HTTP::payload]

  # re-set flag so that subsequent response to re-tried
  # request from real server is not processed as a lookup
  set lookup 0

  # verify pool exists and has members
  if { ![catch [pool $myPool]] }{

    # Retry the request with the request headers set
    HTTP::retry $req

  } else {

    #
    # insert dead/non-existent pool logic here
    #
  }
}

when HTTP_REQUEST {

    # llength uses the spaces as well as carriage returns and line feeds
    log local0. "list length: [llength [HTTP::request]], all headers: [HTTP::request]"

    # Log the request headers in hex
    binary scan [HTTP::request] H* request_hex
    log local0. "hex: $request_hex"

    # Replace the main whitespace characters with their hex equivalent (ie, " " -> \x20)
    log local0. "escaped: [string map {\x20 \\x20 \x0d \\x0d \x0a \\x0a} [HTTP::request]]"

    # Use string map to remove the \x0d carriage return
    #    then split the string on the \x0a linefeeds into a list
    set headers [split [string map {\x0d ""} [HTTP::request]] \x0a]
    set len [llength $headers]

    # Loop through each request header line and log it
    for { set i 0 } { $i <  $len } { incr i } {
        log local0. "$i = [lindex $headers $i]"
    }
}

# /var/log/ltm output:

<HTTP_REQUEST>: list length: 9, all headers: GET /test.html HTTP/1.1  User-Agent: curl/7.41.0  Host: 11.3.0.10  Accept: */*

<HTTP_REQUEST>: hex: 474554202f746573742e68746d6c20485454502f312e310d0a557365722d4167656e743a206375726c2f372e34312e300d0a486f73743a2031312e332e302e31300d0a4163636570743a202a2f2a0d0a0d0a

<HTTP_REQUEST>: escaped: GET\x20/test.html\x20HTTP/1.1\x0d\x0aUser-Agent:\x20curl/7.41.0\x0d\x0aHost:\x2011.3.0.10\x0d\x0aAccept:\x20*/*\x0d\x0a\x0d\x0a

<HTTP_REQUEST>: 0 = GET /test.html HTTP/1.1
<HTTP_REQUEST>: 1 = User-Agent: curl/7.41.0
<HTTP_REQUEST>: 2 = Host: 11.3.0.10
<HTTP_REQUEST>: 3 = Accept: */*
<HTTP_REQUEST>: 4 =
<HTTP_REQUEST>: 5 =