mercredi 30 septembre 2020

How do you choose a boundary for multipart/x-mixed-replace when serving MJPEG over HTTP?

When sending M-JPEG over HTTP, you first send a header with Content-Type: multipart/x-mixed-replace;boundary=<your boundary>, then each time the sequence \r\n--<your boundary>\r\n occurs, the browser treats it as the end of one part and the start of the next part.

The problem is that a JPEG can contain that sequence, either in a comment or just because that byte sequence happens to occur in the payload.

I get that if you know all your jpeg images when you send the initial HTTP response header, you can construct a boundary which doesn't occur in any of your jpegs. But most of the time when you use M-JPEG, the jpegs are generated on the fly. How can I choose a boundary which is guaranteed to not occur in the jpegs? Are there byte sequences which never occur in a valid jpeg? Or is the best strategy just to choose a long enough random boundary that the probability of collision is small and hope for the best..?




Aucun commentaire:

Enregistrer un commentaire