Forum Discussion

Tarik_B_'s avatar
Tarik_B_
Icon for Nimbostratus rankNimbostratus
Jun 07, 2017

iRule removes second value of header cache-control

Hi all,

I'm trying to remove all values after the comma of cache-control header

cache-control: no-cache, max-age=0

For instance here I want to keep

cache-control: no-cache,
it is not necessarily
no-cache,
I want to keep the first value and remove the rest.

Via an iRule I should be able to do that but as I am quite inexperienced in this field, I need your help.

This is what I already did:

when HTTP_RESPONSE {
      if {[string tolower [HTTP::header]] matches_regex cache-control\s*:\s*(public|private|no-cache|no-store|no-transform|max-age\s*[=]{1}\s*[0-9]{1,8}|max-stale\s*[=]{1}\s*[0-9]{1,8}|min-fresh\s*[=]{1}\s*[0-9]{1,8}|min-vers\s*[=]{1}\s*[0-9]{1,8}|must-revalidate|proxy-revalidate|only-if-cached)\s*[\,]{1}
      }
}

I don't know if I'm on the right way, so do not hesitate to propose a solution.

Regards,

Tarik

2 Replies

  • Hi,

     

    Maybe try getfield for that:

     

    if { [HTTP::header "Cache-Control" exists] } {
        set new_hdr set new [getfield [HTTP::header "Cache-Control"] "," 1]
    
        HTTP::header replace "Cache-Control" $new_header
    }

    Not the best code but should work. Probably it would be good to check if there are no multiple instances of Cache-Control header because above code will replace value only in last instance of header.

     

    pro

     

  • Regarding Piotr's comment on checking for multiple instances of "Cache-Control", you could use HTTP::remove and then HTTP::insert, like this:

    if { [HTTP::header "Cache-Control" exists] } {
        set new_header [getfield [HTTP::header "Cache-Control"] "," 1]
    
        HTTP::header remove "Cache-Control"
        HTTP::header insert "Cache-Control" $new_header
    }