Send Content-Length header in json_response

This commit is contained in:
Linus Groh
2019-12-14 22:07:24 +00:00
parent 4c096e58ed
commit 277b3cedf0

6
http.c
View File

@@ -364,9 +364,15 @@ static int json_response(struct mg_connection *conn, JsonNode *json)
mg_send_header(conn, "Access-Control-Allow-Origin", "*");
if (json == NULL) {
mg_send_header(conn, "Content-Length", "2");
mg_printf_data(conn, "{}");
} else {
if ((js = json_stringify(json, JSON_INDENT)) != NULL) {
size_t content_length = strlen(js);
int length = snprintf(NULL, 0, "%zu", content_length);
char buffer[length + 1];
snprintf(buffer, length, "%zu", content_length);
mg_send_header(conn, "Content-Length", buffer);
mg_printf_data(conn, js);
free(js);
}