vADC Docs

HowTo: Techniques to read HTTP headers

by on ‎02-24-2013 07:29 AM - edited on ‎06-09-2015 04:29 PM by PaulWallace (2,482 Views)

In early versions of Stingray Traffic Manager, you would use the function http.getHeaderNames() to obtain a space-separated list of HTTP header names in a request.  You could then iterate through them as follows:

 

$headers = http.getHeaderNames();  
  
while( $headers != "" ) {  
   $p = string.find( $headers, " " );  
   if( $p >= 0 ) {  
      $header = string.substring( $headers, 0, $p - 1 );   
      $headers = string.skip( $headers, $p + 1 );  
   } else {   
      $header = $headers; $headers = "";   
   }   
  
   # process the HTTP header named $header  
   # Get the header value using http.getHeader( $value )  
   ...  
}  

 

With the addition of arrays and hashes to TrafficScript, this rather unwieldy function has been replaced by a new http.listHeaderNames() function:

 

$headers = http.listHeaderNames();

foreach( $header in $headers ) {


   # process the HTTP header named $header

   # Get the header value using http.getHeader( $value )

}

 

Note that if you try to use the http.getHeaderNames() function, you will now recieve a 'deprecated function' warning:

 

Warning: line 1: http.getheadernames is deprecated, please use

http.listHeaderNames instead

   http.getHeaderNames();

   ^

 

Your rules will continue to work, but you may wish to update your code using the example above.

 

HTTP response headers

 

http.getHeaderNames() and http.listHeaderNames() return lists of the HTTP request headers; you can use these functions in both request and response rules.

 

In a response rule, you can also use the analogous http.getResponseHeaderNames() and http.listResponseHeaderNames() functions to inspect the HTTP response headers.

 

Read more

 

Contributors