Forum Discussion

Rory_Hewitt_F5_'s avatar
Sep 30, 2015

Do I need to check HTTP::header exists before checking value?

I feel like this is a simple question, but I can't seem to find a definitive answer...

 

To check the value of a particular HTTP header, I can do this:

 

if { [HTTP::header Origin] ends_with "example.com" } {
    ...do stuff here...
}

My basic question is "What happens is the Origin header is not passed?"...

 

The spec says that HTTP::header will return null if the header called does not exist. What does this mean for my if statement - is null treated the same as false in an if statement?

 

If the above code example will be problematic if Origin is not passed, can I write the code as a single line, like this:

 

if { [HTTP::header exists Origin] and [HTTP::header Origin] ends_with "example.com" } {
    ...do stuff here...
}

or does it need to be two separate nested if statements:

 

if { [HTTP::header exists Origin] } {
    if {[HTTP::header Origin] ends_with "example.com" } {
        ...do stuff here...
    }
}

1 Reply

  • Hello,

    Use the first sample as it appears to be the most efficient and performance-friendly. If the header "Origin" is not found in the client's request, the block of code after the conditional statement will not be processed. There will be no TCL errors or any issues due.

    Personally, I would use

     

    if { [HTTP::header value "Origin"] ends_with "example.com" }

    as the conditional statement, just for clarity reasons.