HTTP::payload replace

I have 2 needs…1 to replace/hide the server in the http redirects that are being sent by the node. 2) to replace/hide the server in the actual content.

when HTTP_RESPONSE {

if { [LB::server addr] eq “10.1.1.1” } {

HTTP::header replace Location [string map { } [HTTP::header Location]] }

elseif { [LB::server addr] eq “10.1.1.2” } {

HTTP::header replace Location [string map { } } [HTTP::header Location]] }

}

the irule above works great for the redirects but not for the content / payload. When I read up on the HTTP::payload command though it’s specifying offset or begin point and I won’t know that because the servername will appear different on different pages.

Any suggestions ? I’d love to do a HTTP::payload replace with a findstr if possible but they way I read the wiki for HTTP::payload replace I can’t.

Oh, and by the way, using stream profile isn’t working. I have a separate support case open for that. I would have preferred to use stream but for some reason it ain’t working whereas the irules are.

thanks!!!

There is an example showing how to replace a string in a payload on the HTTP::payload page (Click here) which uses regsub -all. I suppose you could use string map for a more efficient replacement if don’t need a regex to describe the search string(s).

Something like this…?

when HTTP_RESPONSE {
  if { [HTTP::header exists "Content-Length"] } {
     set content_length [HTTP::header "Content-Length"]
  } else {
     set content_length 1000000
  }
  if { $content_length > 0 } {
     HTTP::collect $content_length
  }
}
when HTTP_RESPONSE_DATA { 
     regsub -all "oursite" [HTTP::payload] "oursitedev" newdata  
    log "Replacing payload with new data."  
    HTTP::payload replace 0 $content_length [string map {oursite oursitedev} [HTTP::payload]}]
    HTTP::release
}

Out of curiosity, do you have any idea of why the stream profile isn’t working? What version are you running?

Aaron

they’re running 9.4.1 HF1…let me try your solution, Thanks by the way!!!..I’ll post back if it works…I do have a support case open on the stream profile..so far the feedback I’ve gotten after they checked the tcpdumps, qkview, and httpwatch captures is the stream profile should have worked and it’s being escalated to see why it isn’t…

so irule now looks like

when HTTP_RESPONSE {

if { [LB::server addr] eq “10.0.19.50” } {

HTTP::header replace Location [string map {nwte1jas7:8882 bob} [HTTP::header Location]]

log “replacing nwte1jas7:8882 in header with bob” }

elseif { [LB::server addr] eq “10.0.19.42” } {

HTTP::header replace Location [string map {nwte1jas6:8882 bob} [HTTP::header Location]]

log “replacing nwte1jas7:8882 in header with bob” }

if { [HTTP::header exists “Content-Length”] } {

set content_length [HTTP::header “Content-Length”] }

else {

set content_length 1000000 }

if { $content_length > 0 } {

HTTP::collect $content_length }

}

when HTTP_RESPONSE_DATA {

if { [LB::server addr] eq “10.0.19.50” } {

regsub -all “nwte1jas7:8882” [HTTP::payload] “bob” newdata

log “Replacing nwtejas7:8882 with bob in the payload.”

HTTP::payload replace 0 $content_length [string map {nwte1jas7:8882 bob} [HTTP::payload]]

HTTP::release }

elseif { [LB::server addr] eq “10.0.19.42” } {

regsub -all “nwte1jas6:8882” [HTTP::payload] “bob” newdata

log “Replacing nwtejas6:8882 with bob in the payload.”

HTTP::payload replace 0 $content_length [string map {nwte1jas7:8882 bob} [HTTP::payload]]

HTTP::release }

}

I’m seeing error in log shown below

TCL error: Rule jas_redirect2 HTTP_RESPONSE_DATA - Out of bounds line 1 invoked from within HTTP::payload replace 0 $content_length [string map {nwte1jas7:8882 bob} [HTTP::payload]]

From the wiki page and Deb’s post (Click here), it looks like you need to use the original content length (what’s been collected so far). LTM should then recalculate the new length and update the Content-Length header it sends (in 9.4.0 and later).

As $content_length should be the amount of bytes collected so far, I’m not sure why you’re seeing the out of bounds error.

Anyone have ideas on this?

Thanks,

Aaron

I think I’m getting the out of bounds because there is no content length set w/in the original http header…so the irule is using 10000 when the content may only be 100,200, etc…

Ah. Can you try this then?

HTTP::payload replace 0 [HTTP::payload length] [string map {nwte1jas7:8882 bob} [HTTP::payload]]

Aaron

I get wrong of args for those lines when I do that..

line 28: [wrong args] [HTTP::payload replace 0 [string map {nwte1jas7:8882 bob} [HTTP::payload]]]

line 33: [wrong args] [HTTP::payload replace 0 [string map {nwte1jas7:8882 bob} [HTTP::payload]]]

There should be ‘[HTTP::payload length]’ after the 0. Also, I think nwte1jas7 should be nwteljas6 is the second HTTP::payload replace command.

Aaron

Aaron, I’m not getting errors anymore but it appears like none of the HTTP_RESPONSE_DATA section is even running. I put a log entry at the beginning of it to see if I got anything and I didn’t. Here’s what I have in teh irule now…

when HTTP_RESPONSE {

if { [HTTP::status] starts_with “301” } {

if { [LB::server addr] eq “10.0.19.50” } {

HTTP::header replace Location [string map {nwte1jas7:8882 E1NP.crocs.com} [HTTP::header Location]]

log “replacing nwte1jas7:8882 in header with E1NP” }

elseif { [LB::server addr] eq “10.0.19.42” } {

HTTP::header replace Location [string map {nwte1jas6:8882 E1NP.crocs.com} [HTTP::header Location]]

log “replacing nwte1jas7:8882 in header with E1NP” }

}

}

when HTTP_RESPONSE_DATA {

if { [LB::server addr] eq “10.0.19.50” } {

regsub -all “nwte1jas7:8882” [HTTP::payload] “bob” newdata

log “Replacing nwtejas7:8882 with bob in the payload.”

HTTP::payload replace 0 [HTTP::payload length] [string map {nwte1jas7:8882 E1NP.crocs.com} [HTTP::payload]]

HTTP::release }

elseif { [LB::server addr] eq “10.0.19.42” } {

regsub -all “nwte1jas6:8882” [HTTP::payload] “bob” newdata

log “Replacing nwtejas6:8882 with bob in the payload.”

HTTP::payload replace 0 [HTTP::payload length] [string map {nwte1jas6:8882 E1NP.crocs.com} [HTTP::payload]]

HTTP::release }

}

It looks like you removed the HTTP::collect command from the HTTP_RESPONSE event. This is what dictates that LTM should buffer the HTTP data in the response. The HTTP_RESPONSE_DATA event is triggered when the amount of data specified in the HTTP::collect command has been collected.

If there isn’t a Content-Length header (like when the server is sending chunked responses) to use in the collect command, you could specify something smaller than one million bytes. Can you add the collect command back in and try with 100?

Does anyone else have ideas on how best to collect response data when there isn’t a content length header? If you specify a smaller number of bytes than what is sent, does the HTTP::payload command only return what you’ve collect? If so, don’t you miss the change to perform the replacement in any subsequent response data that wasn’t collected?

Aaron

Here’s the irule now..with the HTTP::collect in the first event, I get page can’t be displayed so that now the 301 redirect isn’t even working. If I move the HTTP::collect to the 2nd event, the 301 redirect logic works properly but nothing in the 2nd event works.

when HTTP_RESPONSE {

if { [HTTP::status] starts_with “301” } {

if { [LB::server addr] eq “10.0.19.50” } {

HTTP::header replace Location [string map {nwte1jas7:8882 E1NP.crocs.com} [HTTP::header Location]]

log “replacing nwte1jas7:8882 in header with E1NP” }

elseif { [LB::server addr] eq “10.0.19.42” } {

HTTP::header replace Location [string map {nwte1jas6:8882 E1NP.crocs.com} [HTTP::header Location]]

log “replacing nwte1jas7:8882 in header with E1NP” }

}

}

when HTTP_RESPONSE_DATA {

HTTP::collect

log “HTTP payload length is [HTTP::payload length]”

if { [LB::server addr] eq “10.0.19.50” } {

regsub -all “nwte1jas7:8882” [HTTP::payload] “bob” newdata

log “Replacing nwtejas7:8882 with bob in the payload.”

HTTP::payload replace 0 [HTTP::payload length] [string map {nwte1jas7:8882 E1NP.crocs.com} [HTTP::payload]]

HTTP::release }

elseif { [LB::server addr] eq “10.0.19.42” } {

regsub -all “nwte1jas6:8882” [HTTP::payload] “bob” newdata

log “Replacing nwtejas6:8882 with bob in the payload.”

HTTP::payload replace 0 [HTTP::payload length] [string map {nwte1jas6:8882 E1NP.crocs.com} [HTTP::payload]]

HTTP::release }

}

OK..update…after checking this in HTTPwatch the Content-length is in the HTTP header but I’m not getting it in the irule..Every time this irule runs, the content_length variable gets set to 1000000

when HTTP_RESPONSE {

if {([HTTP::header exists “Content-Length”]) && ([HTTP::header “Content-Length”] <= 1000000)}{

set content_length [HTTP::header “Content-Length”] }

else {

set content_length 1000000 }

current irule looks like below…issues, the http::collect is killing performance.

when HTTP_RESPONSE {

grab the response

if { [HTTP::header exists “Content-Length”] } {

set content_length [HTTP::header “Content-Length”]

} else {

set content_length 20000

}

if { $content_length > 0 } {

HTTP::collect $content_length

}

}

when HTTP_RESPONSE_DATA {

if { [HTTP::status] starts_with “30” } {

if { [LB::server addr] eq “10.0.19.50” } {

HTTP::header replace Location [string map {nwte1jas7:8882 e1np.crocs.com} [HTTP::header Location]]

log “replacing nwte1jas7:8882 in header with E1NP”

} elseif { [LB::server addr] eq “10.0.19.42” } {

HTTP::header replace Location [string map {nwte1jas6:8882 e1np.crocs.com} [HTTP::header Location]]

log “replacing nwte1jas7:8882 in header with E1NP”

}

}

log “HTTP payload length is [HTTP::payload length]”

if { [LB::server addr] eq “10.0.19.50” } {

regsub -all “nwte1jas7:8882” [HTTP::payload] “bob” newdata

log “Replacing nwtejas7:8882 with bob in the payload.”

HTTP::payload replace 0 [HTTP::payload length] [string map {nwte1jas7:8882 e1np.crocs.com} [HTTP::payload]]

HTTP::release

} elseif { [LB::server addr] eq “10.0.19.42” } {

regsub -all “nwte1jas6:8882” [HTTP::payload] “bob” newdata

log “Replacing nwtejas6:8882 with bob in the payload.”

HTTP::payload replace 0 [HTTP::payload length] [string map {nwte1jas6:8882 e1np.crocs.com} [HTTP::payload]]

HTTP::release

}

}

Are you testing a single connection and seeing the response times go very high, or are you doing load testing with multiple clients. If the former, I’d try capturing a tcpdump on interface 0.0 which includes the TCP traffic and UDP514 traffic (syslog). You can then compare the packets in the connection with the log statements from the rule to see what’s happening. If you’re doing load testing, I would expect some additional latency even if the rule was working correctly as LTM is having to buffer the response content.

Aaron

I wanted to update / close this out. We were never able to get the irule to rewrite all of the HTTP_RESPONSE_DATA being passed back. well, correction. We were able to rewrite data up to the amount being collected w/ the HTTP::collect. When we set a length to the HTTP::collect of 1 MB we had issues w/ the pages not even displaying plus the latency introduced was not acceptable to the customer. They made changes to their application so that a simple http redirect irule and a simple source <=> target substitution stream profile were able to address the problem.

The final irule that this post was about is below. This seemed to provide the best mix of least delay and getting “most” of the data rewritten. The reason the app was changed is that none of the gifs would display properly to the customer since the applicaiton server in question wanted it’s server name in the the headers it was seeing and we never were able to completely rewrite the http_request data properly.

when HTTP_REQUEST {

switch [HTTP::header Host] {

nwte1jas7:8882 {

HTTP::header replace Host “nwte1jas7:8882”

}

nwte1jas7:8882 {

HTTP::header replace Host “nwte1jas6:8882”

}

}

}

when HTTP_RESPONSE {

if { [HTTP::header exists “Content-Length”] } {

set content_length [HTTP::header value “Content-Length”]

} else {

set content_length 5000

}

if { $content_length > 0 } {

HTTP::collect $content_length

}

if { [HTTP::status] starts_with “3” } {

switch [LB::server addr] {

10.0.19.50 {

HTTP::header replace Location [string map {nwte1jas7:8882 e1np.crocs.com} [HTTP::header Location]]

log local0.info “Replacing nwte1jas7:8882 with e1np.crocs.com in the header.”

}

10.0.19.42 {

HTTP::header replace Location [string map {nwte1jas6:8882 e1np.crocs.com} [HTTP::header Location]]

log local0.info “Replacing nwte1jas6:8882 with e1np.crocs.com in the header.”

}

}

}

}

when HTTP_RESPONSE_DATA {

log “running http_reponse_data event”

grab the response

switch [LB::server addr] {

10.0.19.50 {

regsub -all “nwte1jas7:8882” [HTTP::payload] “e1np.crocs.com” newpayload

log “Replacing nwtejas7:8882 with e1np.crocs.com in the payload.”

HTTP::payload replace 0 [HTTP::payload length] $newpayload

HTTP::release

}

10.0.19.42 {

regsub -all “nwte1jas6:8882” [HTTP::payload] “e1np.crocs.com” newpayload

log “Replacing nwtejas6:8882 with e1np.crocs.com in the payload.”

HTTP::payload replace 0 [HTTP::payload length] $newpayload

HTTP::release

}

}

}

Thanks for posting the additional info. What was the issue/fix for using a stream profile?

Also, for anyone:

Does anyone else have ideas on how best to collect response data when there isn’t a content length header? If you specify a smaller number of bytes than what is sent, does the HTTP::payload command only return what you’ve collect? If so, don’t you miss the change to perform the replacement in any subsequent response data that wasn’t collected?

Thanks,

Aaron

initially each app server was putting in it’s name and non-standart port - ie appserver1:8882, appserver2:8882. the stream profile for a multi-string search / replace was to put @appserver1:8882@dnsname.vip.com@ @appserver2:8882@dnsname.vip.com@ . That multi-search didn’t work. They changed their appservers to put in dnsname.vip.com but couldn’t stop the non-standard port. So either app server would had all the content / headers dnsname.vip.com:8882. I wrote an irule to handle the redirects and then used a simple stream profile like source “dnsname.vip.com:8882” target “dnsname.vip.com” .

I would also like to know how to limit the HTTP::response. What I was seeing w/ their content is that some of the HTTP headers had the Content-Length field and some didn’t. I can also accurately say that when I was using the irule above, w/ the default content_length set to 5k we were repeatedly seeing a 16 second delay in hitting the initial long page. Normally, browsing directly to the page they would see 3-4 seconds to bring it up. With the original HTTP::response irule they would take 16-18 seconds to display the page.