问题
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?
回答1:
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 😀. The probability of collision in a 1GB file is roughly 1-((1-(1/(62^70)))^(10^9)) ~= 3.4*10⁻¹¹⁸. Human brain can't really fathom how small that number is. For comparison the number of atoms in our universe is estimated to be ~ 10⁸⁰.
回答2:
No, it's not possible; you need to either scan, or live with potential failures.
来源:https://stackoverflow.com/questions/37706955/how-to-escape-multipart-http-boundary