diff options
author | Vincent Douillet <vincent@vdouillet.fr> | 2024-01-12 18:14:17 +0100 |
---|---|---|
committer | Vincent Douillet <vincent@vdouillet.fr> | 2024-01-12 18:26:22 +0100 |
commit | 760031b4a74d70bdeaab4ce9aedfe772acd05bd3 (patch) | |
tree | 443a456770d5db052a3dfe3d2f1a8e5f66604ccb /download.c | |
parent | 43f65c76152017420ce723b4d4ef4230ff072818 (diff) |
browse: build download url for files
Diffstat (limited to 'download.c')
-rw-r--r-- | download.c | 37 |
1 files changed, 36 insertions, 1 deletions
@@ -38,8 +38,40 @@ #include "cgi.h" #include "config.h" #include "download.h" +#include "file.h" #include "http.h" #include "url.h" +#include "util.h" + +/* + * download url = r->pname / DOWNLOAD_URL / file->path / file->name + */ +size_t +build_download_url(struct kreq * r, struct file * file) +{ + char action_url[PATH_MAX]; + size_t action_url_len; + + /* don't download directories */ + if (file->is_dir) + return 0; + + action_url_len = url_build(action_url, PATH_MAX, r->pname, DOWNLOAD_URL, + file->path, file->name, NULL); + if (action_url_len == 0 || action_url_len >= PATH_MAX) { + kutil_warn(r, NULL, + "download: action URL overflow: %s", action_url); + return 0; + } + file->action_url = v_strcpy(action_url, action_url_len); + if (file->action_url == NULL) { + kutil_warn(r, NULL, + "download: unable to allocate file url buffer: %s", + action_url); + return 0; + } + return action_url_len; +} void download(struct kreq * r) @@ -55,13 +87,16 @@ download(struct kreq * r) http_exit(r, KHTTP_400, "download: Invalid request path"); /* build requested file path */ - path_size = url_build(file_path, PATH_MAX, DATA_DIR, "File.txt", + path_size = url_build(file_path, PATH_MAX, DATA_DIR, r->path, NULL); if (path_size == 0) http_exit(r, KHTTP_404, "download: Unable to build file path"); if (path_size >= PATH_MAX) http_exit(r, KHTTP_414, NULL); + /* check that it is a file */ + + /* memory map the file */ fd = open(file_path, O_RDONLY); if (fd < 0) |