Forum Discussion

jaskel_106221's avatar
jaskel_106221
Icon for Nimbostratus rankNimbostratus
Aug 27, 2007

Header insertion (newbie alert)

Hey there,

 

 

So, I'm really new to iRules, and I question about a HTTP::header insert

 

 

Basically, I am looking to 'replace' the value of a custom(?) header if it exists in the current connection, or insert the header. Here is what I have with some comments:

 

 

when HTTP_REQUEST {

 

Check to see if the header already exists

 

if { [ string tolower HTTP::header] starts_with "client-"} {

 

If it does, remove the current header element

 

HTTP::header remove Client-IP

 

The insert our own assuring that only one element exists in the

 

header

 

HTTP::header insert Client-IP:[IP::remote_addr]

 

}

 

else {

 

If it doesn't already exist, insert it.

 

HTTP::header insert Client-IP:[IP::remote_addr]

 

}

 

}

 

 

I created a test virtual server as well to just insert the header no matter what with this code:

 

 

when HTTP_REQUEST {

 

Insert Client-IP:[IP::remote_addr] into

 

the header for each http request

 

HTTP::header insert Client-IP:[IP::remote_addr]

 

}

 

 

But, sadly, it appears that neither of these two options are actually inserting a header with:

 

 

Client-IP: ip.address.here.

 

 

into my HTTP request.

 

 

I'm sorry for the lame newbie post....but thanks for looking!

 

 

-James

3 Replies

  • Ok, ok....this always happens to me. The minute I post a question, I figure out what I was doing wrong. So, this is what I ended up with:

     

     

    when HTTP_REQUEST {

     

    Check to see if the header already exists

     

    if { [ string tolower HTTP::header] starts_with "client-ip"} {

     

    If it does, remove the current header element

     

    HTTP::header remove "Client-IP"

     

    The insert our own assuring that only one element exists in the

     

    header

     

    HTTP::header insert "Client-IP" [IP::remote_addr]

     

    }

     

    else {

     

    If it doesn't already exist, insert it.

     

    HTTP::header insert "Client-IP" [IP::remote_addr]

     

    }

     

    }

     

     

     

    And it appears to work. I get the inserted header. Now I just have to test if it cleans out the header if it finds the Client-IP portion already in the header on the initial request.

     

     

    Thanks for anyone who looked and was thinking about replying!

     

     

    -James
  • I'm not clear on what the "HTTP::header" command returns with no arguments? The complete header string? In that case this would only work if client-ip was the first header, correct?

     

     

    You may want to use "HTTP::header exists" as follows:

     

     

    if { [HTTP::header exists {"Client-IP"}] }
  • Otherwise, you can use '[HTTP::header replace "Client-IP" [IP::client_addr]' (Click here), to replace the header with the new value if the header is set already or insert if it's not present already. If you wanted to be doubly sure that no header exists already, you could use this to remove all instances and then insert a new header:

    
    when HTTP_REQUEST {
       while {[HTTP::header exists "Client-IP"]} {
          HTTP::header remove "Client-IP"
       }
       HTTP::header insert "Client-IP" [IP::client_addr]
    }

    Aaron