From 277b3cedf00e9bc0fb45cf10720434fea542cd85 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Sat, 14 Dec 2019 22:07:24 +0000 Subject: [PATCH] Send Content-Length header in json_response --- http.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/http.c b/http.c index 35f9daf..e144d0a 100644 --- a/http.c +++ b/http.c @@ -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); }