Forum Discussion

Jamey_Price_105's avatar
Jamey_Price_105
Icon for Nimbostratus rankNimbostratus
Jan 25, 2005

Problem rewriting header attribute on HTTP code 302

I'm new to TCL as of yesterday, so if I'm doing something monumentally dumb, please pardon me.

 

 

Our application runs on a server on port 9080, clients access it via port 80 or 443. When the server needs to issue a 302 Found to instruct the client to go elsewhere (say to log in first) it throws in the port that the internal server is running on. We need to strip that out and return the rest to the client. My rule is below, and something is just not right. The string does not seem to be stored in variable result. Any time I try to reference it in any way I can come up with that I ought, I get the string "result" instead.

 

 

when HTTP_RESPONSE {

 

if { [HTTP::header Location] starts_with "http://dev.oursite.com:9080" } {

 

variable result

 

regsub :9080 [HTTP::header Location] {} result

 

log {Result is [set result] or $result or [result]}

 

[HTTP::redirect ${result}]

 

[HTTP::redirect [regsub :9080 [HTTP::header Location] {}]]

 

}

 

}

 

 

Also, there seems to be some difference between TCL regsub as implemented on the Big-IP and in the official documentation. According to the official documentation, the variable is optional, and if excluded the modified string will be returned. If I try that (such as in the last, commented-out line) I get an error stating that I have the wrong number of arguments for regsub.

 

 

Please, can someone help me out here?

 

 

Thanks,

 

 

Jamey
  • Replying to my own post here...

     

     

    I've modified the way it works, and like this, things are fine-ish. What I don't understand is if I try to use the commented line below to just fix the header instead of issuing my own redirect, I get the following in the logs.

     

     

    TCL error: Rule redirectwrongport - Illegal argument. Illegal argument. Illegal argument. Illegal argument. Ille (line 1) invoked from within "HTTP::header Location $thingy"

     

     

    when HTTP_RESPONSE {

     

    if { [HTTP::header Location] starts_with "http://dev.oursite.com:9080" } {

     

    regexp .+:9080/(.*) [HTTP::header Location] garbage whatiwant

     

    variable thingy

     

    set thingy "http://dev.oursite.com/"

     

    append thingy $whatiwant

     

    log "Thingy is $thingy"

     

    HTTP::header Location $thingy

     

    HTTP::redirect $thingy

     

    }

     

    }

     

     

    Any ideas now?

     

     

    Thanks,

     

     

    Jamey
  • unRuleY_95363's avatar
    unRuleY_95363
    Historic F5 Account
    A couple of notes pertaining to our Tcl implementation.

    a) You don't really need to declare local variables with the "variable" command. On first initialization, variables are automatically scoped into a per-connection, local scope.

    b) As for the regexp command, you may have misinterpreted the optional nature of the matchVar and subMatchVar's. The matchVar is the complete matching string, where subMatchVar is the part within the parenthesis. In order to have a subMatchVar, you must have a matchVar (Tcl otherwise doesn't know whether you meant matchVar or subMatchVar). I often see the use of the variable '->' to make this appear more readable, however it is still just a garbage variable.

    Eg: regexp .+:9080/(.*) [HTTP::header Location] -> whatiwant

    c) In order to replace the header, you need to specify "replace" on the HTTP::header command. I am looking into why this ended up resulting in the bizarre error message you received, but it's probably some kind of bug.

    d) Thanks for continuing to try!

    Here is an updated example of your rule:

     
     when HTTP_RESPONSE {  
        if { [HTTP::header Location] starts_with "http://dev.oursite.com:9080" } {  
           regexp .+:9080/(.*) [HTTP::header Location] garbage whatiwant  
           set thingy "http://dev.oursite.com/$whatiwant"  
           log "Thingy is $thingy"  
           HTTP::header replace Location $thingy  
        }  
     }  
     
  • rapmaster_c_127's avatar
    rapmaster_c_127
    Historic F5 Account
    One thing I'll add to unruley's well-written response is that you may want to condition your check on the server's response actually being a 302. Otherwise you'll be needlessly scanning non-redirects for Location: headers.
  • unRuleY_95363's avatar
    unRuleY_95363
    Historic F5 Account
    Oh, sorry, I confused myself regarding regexp and regsub.

     

     

    The problem is with the Tcl syntax validator that we incorporated which is based on an earlier version of Tcl. It erroneously requires 4 arguments, not 3 arguments with an optional 4th argument. This will be fixed in an upcoming release.