I modified the code of embedd_cpp slightly to understand how mg_read() works.
The documentation says it returns the number of read bytes, or indicating finish, error.
In my case however it always returns 1.
To verify whether it actually only reads 1 byte, I counted how often I looped until my amount of data has been transmitted. So I can clearly say, that it did not read only 1 byte, but exactly the buffer size.
Is my understanding wrong or is this a bug?
Here's the code:
bool
handlePost(CivetServer *server, struct mg_connection *conn) override
{
/* Handler may access the request info using mg_get_request_info */
const struct mg_request_info *req_info = mg_get_request_info(conn);
long long len = req_info->content_length;
char *buf[4096] = {0,};
int readBytes = 0;
int i = 0;
do {
readBytes = mg_read(conn, buf, sizeof(buf)) > 0;
printf("read: %d, i %d\n", readBytes, i);
i++;
} while (readBytes > 0);
printf("len %lld\n", len);
mg_printf(conn,
"HTTP/1.1 200 OK\r\nContent-Type: "
"text/html\r\nConnection: close\r\n\r\n");
return true;
}