Let's say I'm sending a multipart request (or response). I need to choose a multipart boundary which does not appear in any of my payloads. However, my payloads are large binary files and I am streaming them to the destination. I want to avoid streaming them twice - once to scan for the boundary and one to stream out.
So my question is: is it possible to escape the boundary if it appears in the payload? If so, how?
No, it's not possible; you need to either scan, or live with potential failures.
Don't Panic. Your boundary can be up to 70 characters long. If you go with that maximum and randomly generate it out of characters and numbers you'll have 62⁷⁰ possible combinations for each position in a file. Chance of having the same sequence of bytes in your binary files is so infinitesimal that it shouldn't bother your sleep at all