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. --- LICENSE | 2 +- src/response.c | 18 +++++++++++------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/LICENSE b/LICENSE index 30573e9..08bdde0 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright 2019-2021 Aleksey Veresov +Copyright 2019-2023 Aleksey Veresov This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from 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