Forum Discussion

OTS02's avatar
OTS02
Icon for Cirrus rankCirrus
Feb 09, 2016

Desire to view contents of If-Modified-Since & If-None-Match http headers

Using iRule below:

 

when HTTP_REQUEST {

 

set rqst_headers [HTTP::header names]

 

set hsl [HSL::open -proto UDP -pool HIGH_SPEED_LOGGING]

 

HSL::send $hsl "REQUEST HEADERS = $rqst_headers"

 

if { [HTTP::header exists If-Modified-Since] }{

 

set since_modded [HTTP::header value If-Modified-Since]

 

HSL::send $hsl "If-Modified-Since exists: [HTTP::header If-Modified-Since] CACHE DISCOVERY $since_modded "

 

}

 

if { [HTTP::header exists If-None-Match] }{

 

set none_match [HTTP::header value If-None-Match]

 

HSL::send $hsl "If-None-Match exists: [HTTP::header If-None-Match] CACHE DISCOVERY $none_match "

 

}

 

}

 

Those headers show up in the request headers list:

 

REQUEST HEADERS = Accept Referer Accept-Language User-Agent Host If-Modified-Since If-None-Match DNT Connection Cookie

 

But never catch in the conditional statements. Does anyone see my error?

 

6 Replies

  • I just tried the following on 11.5.3:

     

    when HTTP_REQUEST {
        log local0. "headers = [HTTP::header names]"
    
        if { [HTTP::header exists If-None-Match] } {
            log local0. "If-None-Match"
        }
    }
    

     

    and with the following request:

     

    GET / HTTP/1.1
    Host:
    If-None-Match: foo
    

     

    the following is logged:

     

     Rule /Common/test : headers = Host If-None-Match
     Rule /Common/test : If-None-Match
    

     

    just as I'd expect.

    Can you switch the HSL to local logging (just for debugging; in general, HSL logging is a better production choice)? Can you also verify you see no errors in the log otherwise?

    • OTS02's avatar
      OTS02
      Icon for Cirrus rankCirrus
      Thanks Vernon. As it turned out, my original code was working, but got missed by the syslog server(because I forgot to put the text string in the log line that the syslog server uses to direct to the correct file). As always, the Devcentral community leads me on to cool discoveries. So when you did the GET/ http/1.1 etc - was that from Curl?
    • VernonWells's avatar
      VernonWells
      Icon for Employee rankEmployee
      I used telnet, but you can use curl to add headers (using the -H flag). It's also sometimes helpful to add -V to see all headers in the transaction.
  • Jordan_Self_149's avatar
    Jordan_Self_149
    Historic F5 Account

    For logging/modifying specific headers, I find that using a loop is usually the best way to parse through them:

     

    when HTTP_REQUEST {
        set rqst_headers [HTTP::header names]
        set hsl [HSL::open -proto UDP -pool HIGH_SPEED_LOGGING]
        HSL::send $hsl "REQUEST HEADERS = $rqst_headers"
        foreach request_header [HTTP::header names] {
            if { $request_header eq "If-None-Match" } {
                HSL::send $hsl " $request_header = [HTTP::header values $request_header] "
            }
        }
    }
    

     

    The result should be that the If-None-Match header is logged along with the value using this format:

    HEADER NAME = HEADER VALUE

    Hope this helps!

  • Hi OTS02,

    in the past I've seen some really strage behavior, when using unquoted conditions containing "-" signs. Well, it should work, but somehow its doesn't work well everytime...?

     

    when CLIENT_ACCEPTED {
        set hsl [HSL::open -proto UDP -pool HIGH_SPEED_LOGGING]
    }
    when HTTP_REQUEST {
        HSL::send $hsl "REQUEST HEADERS = [HTTP::header names]"
        if { [set since_modded [HTTP::header value "If-Modified-Since"]] ne "" } {
            HSL::send $hsl "If-Modified-Since exists: CACHE DISCOVERY $since_modded "
        }
        if { [set none_match [HTTP::header value "If-None-Match"]] ne "" } {
            HSL::send $hsl "If-None-Match exists: CACHE DISCOVERY $none_match "
        }
    }
    

     

    Note: I've tweaked your iRule a little bit to increase its performance. Its now using the CLIENT_ACCEPTED event to define the HSL connection and using a [set since_modded [HTTP::header value "If-Modified-Since"]] ne "" syntax to check for existence ( header ne "" is faster then header exists) and store the results in a single step. Well, the code could be further optimized by skipping the $variable creation at all, but I assume you need to process the results further...

    Cheers, Kai