simonwittber / uniwebserver

An embedded HTTP server for Unity3D.
MIT License
76 stars 30 forks source link

Request body mangling #11

Open whfung opened 3 years ago

whfung commented 3 years ago

https://github.com/simonwittber/uniwebserver/blob/5b0a566ae88c8a0618d0050ca18c908185d5cce7/UniWebServer/Assets/UniWebServer/Lib/WebServer.cs#L123-L126

Apparently some of the forks already fix this, but we ran into POST requests being mangled when running this headless in a virtualized environment. After investigation, we found this block, which sets the read offset to the # of bytes read every iteration.

This doesn't happen on localhost because Read() fully empties the stream in one iteration but as soon as we deployed it on a VM Read() gets called multiple times and it started chopping up our uploaded data.

To fix the issue we corrected the usage of Read():

while (count > 0) {
    var bytesRead = stream.Read (bytes, offset, count);
        offset += bytesRead;
    count -= bytesRead;
}

Hope this saves someone some time. That's all.