diff options
-rw-r--r-- | LICENSE | 2 | ||||
-rw-r--r-- | src/response.c | 18 |
2 files changed, 12 insertions, 8 deletions
@@ -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); } |