Forum Discussion
Adrian_Turcu_10
Nimbostratus
Oct 30, 2007Help With HTTP_RESPONSE
Hello Gurus
Could someone help me with a quick rule to replace a header in the response back to the client, please?
My problem is like follows: we have a poxi app (not proxy) and the damn thing is sending a redirection back to the client to localhost instead of maintaining the hostname that requested the initial page and the protocol
I was talking with the developers and they they don't have an estimate for a fix (I'm gonna kill them soon) and reading through the posts I believe my interim solution will be arounf HTTP_RESPONSE actions in the iRules.
How can I use iRules in the response to the client to replace the beging of the redirection (http://127.0.0.1) with the protocol and host that was made for that request (https://acme.com) and maintain the reamining of the redirectied URI (/some/redirected/path/to/the/client.jsp)
Thanks in advance,
Adrian
18 Replies
Sort By
- David_Homoney
Nimbostratus
How about like this..... - Adrian_Turcu_10
Nimbostratus
wow, i'm not that good on tcl (not good at all i should say) - hoolio
Cirrostratus
A couple of notes/suggestions:when RULE_INIT { save a sample string to the $::location_test variable set ::location_test "http://127.0.0.1/path/to/file.txt" save a sample HTTP host header value set ::host_test "www.example.com" log the value log local0. "Sample Location header value: $::location_test" replace the string "127.0.0.1" with the test host header value log local0. "Updated string 1: [string map "127.0.0.1 $::host_test" $::location_test]" replace the strings "http://" with "https://" and "127.0.0.1" with the test host header value log local0. "Updated string 2: [string map "http:// https:// 127.0.0.1 $::host_test" $::location_test]" }
Rule : Sample Location header value: http://127.0.0.1/path/to/file.txt Rule : Updated string 1: http://www.example.com/path/to/file.txt Rule : Updated string 2: https://www.example.com/path/to/file.txt
when HTTP_REQUEST { Save host (without the port if it's specified) set shost [getfield [HTTP::host] ":" 1] log local0. "Parsed Host header value: $shost" } when HTTP_RESPONSE { check if response is a redirect (HTTP status of 3xx) if { [HTTP::status] starts_with "3" } { Save original Location value set location_original [HTTP::header value Location] log local0. "Original Location header value: $location_original" Check if the port the request was made on was 443 if {[clientside {TCP::local_port}] == 443}{ Request was made to an SSL port, so replace 127.0.0.1 with the Host header value from the request and replace http:// with https:// in the Location header value set location_updated [string map "http:// https:// 127.0.0.1 $shost" $location_original] log local0. "Updated Location header value for HTTP request: $location_updated" Perform the actual header replacement HTTP::header replace Location $location_updated } else { Request was made to an HTTP port, so just replace 127.0.0.1 with the Host header value from the request set location_updated [string map "127.0.0.1 $shost" $location_original] log local0. "Updated Location header value for HTTPS request: $location_updated" Perform the actual header replacement HTTP::header replace Location $location_updated } } }
: Parsed Host header value: 192.168.101.42 : Original Location header value: http://127.0.0.1/test/file.txt : Updated Location header value for HTTP request: https://192.168.101.42/test/file.txt : Parsed Host header value: 192.168.101.42 : Original Location header value: http://127.0.0.1/test/file.txt : Updated Location header value for HTTPS request: http://192.168.101.42/test/file.txt
- Adrian_Turcu_10
Nimbostratus
Thanks Aaron. - hoolio
Cirrostratus
That's good to hear. Here is a version without the logging and intermediate variables:when HTTP_REQUEST { Save host (without the port if it's specified) set shost [getfield [HTTP::host] ":" 1] } when HTTP_RESPONSE { Check if response is a redirect (HTTP status of 3xx) if { [HTTP::status] starts_with "3" } { Check if the port the request was made on was 443 if {[clientside {TCP::local_port}] == 443}{ Request was made to an SSL port, so replace 127.0.0.1 with the Host header value from the request and replace http:// with https:// in the Location header value HTTP::header replace Location [string map "http:// https:// 127.0.0.1 $shost" [HTTP::header value Location]] } else { Request was made to an HTTP port, so just replace 127.0.0.1 with the Host header value from the request HTTP::header replace Location [string map "127.0.0.1 $shost" [HTTP::header value Location]] } } }
- t0_89784
Nimbostratus
Hi fellows, - kjc
Nimbostratus
Hey, this is VERY helpful. - kjc
Nimbostratus
This works... on the server that sends the last redirect to the original URL. Not intiutive, though, is it?
Recent Discussions
Related Content
DevCentral Quicklinks
* 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
Discover DevCentral Connects