Forum Discussion

dubdub's avatar
dubdub
Icon for Nimbostratus rankNimbostratus
Oct 26, 2011

HTTP::payload replace length

Hi all,

I used Colin's excellent example of replacing outbound payload content at http://devcentral.f5.com/Tutorials/...Rules.aspx - but was curious, that last line where it says:


HTTP::payload replace 0 [string length $newPayload] $newPayload 

Should it be:


HTTP::payload replace 0 $originalPayloadLength $newPayload 

where $originalPayloadLength is calculated right before the "set newPayload" line in HTTP_RESPONSE_DATA? Won't the content length in the header be off otherwise?

Thanks,

Jen
  • If you edit the payload and do not correct the length then you can get mismatch errors which would tell the client that the data has been tampered with. Some systems like the ASM will flag or even block this.

     

     

    Colin followed the formula: HTTP::payload replace

     

    http://devcentral.f5.com/wiki/iRules.HTTP__payload.ashx

     

     

    HTTP::payload replace 0 [string length $newPayload] $newPayload

     

     

    HTTP::payload

     

    replace - Offset 0

     

    string length $newPayload - Returns a decimal string giving the number of characters in string

     

    $newPayload - Payload

     

     

    Hope this helps.
  • dubdub's avatar
    dubdub
    Icon for Nimbostratus rankNimbostratus
    Hi Michael,

     

     

    I apologize, I really am not trying to be obtuse, I'm just confused :( Checking the link you sent - it says:

     

     

    ----

     

    HTTP::payload replace offset length string

     

     

    Replaces the amount of content that you specified with the length argument, starting at offset with string, adjusting the Content-Length header appropriately.

     

     

    To clarify, the length argument should be the length of original content to replace. In order to replace the entire payload, the offset should be 0 and the length should be the original size in bytes of the payload. The original content length can typically be retrieved using [HTTP::header value Content-Length].

     

    ----

     

     

    If I am trying to replace the entire original payload, don't I need to send the length of the original payload to the replace command, not the length of the new payload? I'm not following :(

     

     

    Thanks,

     

    Jen
  • It is the old length. From the Wiki for HTTP:payload replace: To clarify, the length argument should be the length of original content to replace. In order to replace the entire payload, the offset should be 0 and the length should be the original size in bytes of the payload. The original content length can typically be retrieved using [HTTP::header value Content-Length].

     

     

     

    It would have to work this way otherwise you could never replace a string shorter than the original length. There would always be something left in the end. Payload is NOT a register-type location where it has an atomic value. Since the engine is perfectly capable of determining the length of the new string, there would be no need for you to tell it how much of the new string to replace. Granted, if the documentation is wrong, then this has seriously changed, but as I said, how could it work any other way.

     

     

    Note if you did want to only replace the payload starting at 0 with a part of the new string, then you would have to use a function to take only the desired number of bytes from the new string.

     

     

    Regards,

     

     

    Tom
  • One more thought...This is actually clearer in the example of scrubbing a credit card number somewhere in the middle of the string where the first number is not 0. If you find a credit card at position 1300, then it seems pretty clear that you replace starting at position 1300 for 16 (length of the credit card--except AMEX). Perhaps the confusion comes in the fact that in this case the length you want to replace is 16 which also happens to be the length of the new string as well. If I have the statement
    HTTP::payload replace 1300 16 $scrubbedCard 
    it is not obvious which is which, but in fact, it is the size of the content you are replacing.
  • Colin_Walker_12's avatar
    Colin_Walker_12
    Historic F5 Account
    Now that I'm sitting in a room with dubdub, and have been pointed at this post (Thanks!!) I want to weigh in here. It is definitely a typo in my example, which I'll go fix once I'm back at my desk. It should be the old length. Sorry for any confusion folks, and thanks for the heads up! Any and all feedback is most welcome.

     

     

    Colin