mg_read() returns 1, bug?

12 views
Skip to first unread message

Timo L.

unread,
Oct 23, 2021, 11:13:24 AM10/23/21
to civetweb
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;
}

Timo L.

unread,
Oct 23, 2021, 11:25:44 AM10/23/21
to civetweb
I am sorry, please ignore this.
When writing this message I saw my copy paste error, that I always overlooked.

Reply all
Reply to author
Forward
0 new messages