Forum Discussion

hoolio's avatar
hoolio
Icon for Cirrostratus rankCirrostratus
Nov 24, 2008

Evaluate command only at runtime--not RULE_INIT?

Hi there,

I'd like to set a variable in RULE_INIT which contains the command HTTP::host and then have it evaluated at runtime in HTTP_REQUEST:

 
 when RULE_INIT { 
  
    set ::html_content "Requested host was [HTTP::host]" 
 } 
 when HTTP_RESQUEST { 
  
    if {[HTTP::path] starts_with "/test"}{ 
  
       HTTP::respond 200 content $::html_content 
    } 
 } 
 

This example doesn't work as HTTP::host is evaluated in RULE_INIT--where the command isn't valid. I tried using eval and subst, but couldn't figure out any combination that would allow me to set the contents in RULE_INIT and force evaluation in HTTP_REQUEST.

Is there a way to only evaluate the command in HTTP_REQUEST?

I'd like to have the variable defined in RULE_INIT along with all other variables that the customer would potentially need to modify. I realize I could use a placeholder and then use string map in HTTP_REQUEST to replace it with the actual HTTP::host value, but that doesn't seem very elegant.

Thanks,

Aaron
  • hoolio's avatar
    hoolio
    Icon for Cirrostratus rankCirrostratus
    Nevermind... escaping the braces of [HTTP::host] and [HTTP::uri] in RULE_INIT and then using subst in HTTP_REQUEST works:

        
     when RULE_INIT { 
      
         Trigger a meta-refresh every X seconds 
        set ::refresh_interval 30 
      
         HTML content containing meta-refresh to the same requested host/uri  
        set ::html_response_string " 
      
      
     Hi there 
      
      
      
     Meta-refresh to http://\[HTTP::host\]\[HTTP::uri\] in $::refresh_interval seconds 
      
      
        " 
     } 
     when HTTP_REQUEST { 
      
        if {[HTTP::path] starts_with "/meta"}{ 
      
           log local0. "response string: [subst $::html_response_string]" 
           HTTP::respond 200 content [subst $::html_response_string] 
      
        } else { 
           HTTP::respond 200 content "\r\n200\tClient IP:port: [IP::client_addr]:[TCP::client_port]\ 
              -> VIP IP:port: [IP::local_addr]:[TCP::local_port]\r\n" 
        } 
     } 
     

    Aaron