Forum Discussion
Using switch statements on HTTP::headers
Hi Sam,
when comparing just a few conditions (like you do), then [if] outperforms [switch] very much.
The very simplyfied guideline would be to use [if] for less that 10 contions, to use use [switch] for more than 10 but less than 50 condition and to use [class] for more than 50 conditions. But only individual [timing on] tests would provide reliable data.
Your code looks already very clean and straightforward. The only optimizations I could spot are within your [foreach] cookie encryption and [HTTP::header remove] syntax.
[Foreach] Optimization:
foreach cookie_name [lsearch -all -inline [HTTP::cookie names] $static::ck_pattern] {
Decrypt the cookie value and check if the decryption failed (null return value)
if {[HTTP::cookie decrypt $cookie_name $static::ck_pass] eq ""}{
HTTP::cookie remove $cookie_name
}
}
foreach cookie_name [lsearch -all -inline [HTTP::cookie names] $static::ck_pattern] {
Encrypt the cookie value
HTTP::cookie encrypt $cookie_name $static::ck_pass
}
Background: [foreach] has a build-in ne "" check.
[HTTP::header remove] Optimization:
foreach header $static::InfoHeaders {
HTTP::header remove $header
}
Background: The required cpu cycles for remove and exists are almost identiacal and remove wouldn't trow an error if the header is not in place.
Additional Note: You could free up some memory by unsetting the variable after usage. I would recommend to migrate your code to $temp(your_var_name) variables and then just issue an unset -nocomplain temp at the of HTTP_RESPONSE to be able to flush every variable in one step.
Cheers, Kai
Recent Discussions
Related Content
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com