From ba247cbf0b6a5b88f309cfb32585bd8f3eb3abb0 Mon Sep 17 00:00:00 2001 From: Aleksey Veresov Date: Sat, 28 Jan 2023 18:24:00 +0300 Subject: Fix content-length Content-length in response was in reverse order. --- src/response.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'src/response.c') diff --git a/src/response.c b/src/response.c index 8809837..ed9d8ae 100644 --- a/src/response.c +++ b/src/response.c @@ -167,15 +167,19 @@ void magi_response_header(struct magi_response *r, void magi_response_content_length(struct magi_response *r, int length) { struct magi_param addon; - int len = 1; - addon.name = magi_str_create_copy("Content-Length", 14); - addon.data = malloc(len + 1); - *addon.data = '0' + length % 10; - while (length /= 10) { - addon.data = realloc(addon.data, len + 2); - addon.data[len] = '0' + length % 10; + int len = 1; + int tmp = length; + addon.name = magi_str_create_copy("Content-Length", 14); + while (tmp /= 10) { ++len; } + addon.data = malloc(len + 1); + addon.data[len - 1] = '0' + length % 10; + length /= 10; + for (tmp = len - 2; tmp >= 0; --tmp) { + addon.data[tmp] = '0' + length % 10; + length /= 10; + } addon.data[len] = 0; magi_params_set(&r->head_entity, &addon); } -- cgit v1.2.3