libmicrohttpd mingw gotcha

Let’s say you want to build a nice tiny cross-platform web server using libmicrohttpd and mingw.  So for static content you’ll probably convert the URI to a path (safely! watch out for nasty clients playing tricks with “../”) and call open(3) on the path, and call MHD_create_response_from_fd with the file descriptor.

Sounds good, except on Windows you’ll see some very crazy errors whereby you actually read past the end of the file.  This is because Windows may decide to open the file as a text document, which will convert all your newlines to newlines + carriage returns.

The fix is to add the “O_BINARY” option to the open(3) call.  So,

Could not embed GitHub Gist 2030342: Not Found