mg_read() does not block on content_len=0. mg_read() reads until socket is closed if content-length is not provided

This commit is contained in:
Sergey Lyubka committed 2013-10-01 17:47:32 +01:00
1 parent 70154f6c23
commit 5153eebc9b
4 files changed
+43 -42

No files matched your search

+6 -10
View File
@@ -734,10 +734,8 @@ int mg_read(struct mg_connection *conn, void *buf, int len) {
int n, buffered_len, nread = 0;
int64_t left;
// If Content-Length is not set, read until socket is closed
if (conn->content_len <= 0) {
conn->content_len = INT64_MAX;
conn->must_close = 1;
return 0;
}
// conn->buf body
@@ -1790,7 +1788,7 @@ static int forward_body_data(struct mg_connection *conn, FILE *fp,
expect = mg_get_header(conn, "Expect");
assert(fp != NULL);
if (conn->content_len == -1) {
if (conn->content_len == INT64_MAX) {
send_http_error(conn, 411, "Length Required", "%s", "");
} else if (expect != NULL && mg_strcasecmp(expect, "100-continue")) {
send_http_error(conn, 417, "Expectation Failed", "%s", "");
@@ -3644,14 +3642,12 @@ static int getreq(struct mg_connection *conn, char *ebuf, size_t ebuf_len) {
&conn->request_info) <= 0) {
snprintf(ebuf, ebuf_len, "Bad request: [%.*s]", conn->data_len, conn->buf);
} else {
// Request is valid
// Request is valid. Set content_len attribute by parsing Content-Length
// If Content-Length is absent, instruct mg_read() to read from the socket
// until socket is closed.
conn->content_len = INT64_MAX;
if ((cl = get_header(&conn->request_info, "Content-Length")) != NULL) {
conn->content_len = strtoll(cl, NULL, 10);
} else if (!mg_strcasecmp(conn->request_info.request_method, "POST") ||
!mg_strcasecmp(conn->request_info.request_method, "PUT")) {
conn->content_len = -1;
} else {
conn->content_len = 0;
}
conn->birth_time = time(NULL);
}