diff options
author | Vincent Douillet <vincent@vdouillet.fr> | 2024-12-26 17:40:36 +0100 |
---|---|---|
committer | Vincent Douillet <vincent@vdouillet.fr> | 2024-12-26 17:40:36 +0100 |
commit | 1bcf812973335b424655b272eb831d4eb8f059fc (patch) | |
tree | 766996e7fbf0ff66888c0093dbd3e461aa4ad3b3 /download.c | |
parent | a29738212841dcb699dc397ad66c3416324eccf8 (diff) |
download: use str_concat
Diffstat (limited to 'download.c')
-rw-r--r-- | download.c | 26 |
1 files changed, 11 insertions, 15 deletions
@@ -82,8 +82,9 @@ download(struct kreq * r) void *buffer; struct file *f; int fd; - char file_path[PATH_MAX], request_path[PATH_MAX]; - size_t path_size; + char file_path[PATH_MAX]; + char *path; + size_t path_size, suffix_len; struct http_ret ret; /* initialize empty file and return struct to success */ @@ -93,27 +94,20 @@ download(struct kreq * r) }; /* build requested file path, with suffix or without */ - if (strlen(r->suffix) > 0) { - if (snprintf(request_path, sizeof(request_path), "%s.%s", r->path, r->suffix) - >= (int) sizeof(request_path)) { + suffix_len = strlen(r->suffix); + if (suffix_len > 0) { + if (str_concat(&path, r->path, ".", r->suffix, NULL) <= 0) { ret = (struct http_ret) { KHTTP_414, - "download: request too long" + "download: unable to build requested file path" }; goto end; } } else { - if (snprintf(request_path, sizeof(request_path), "%s", r->path) - >= (int) sizeof(request_path)) { - ret = (struct http_ret) { - KHTTP_414, - "download: request too long" - }; - goto end; - } + path = r->path; } /* build file metadata */ - f = file_new(request_path); + f = file_new(path); if (f == NULL) { ret = (struct http_ret) { KHTTP_404, @@ -170,6 +164,8 @@ download(struct kreq * r) /* cleanup */ end: + if (suffix_len > 0) + free(path); if (f != NULL && f->size > 0) munmap(buffer, f->size); file_free(f); |