summaryrefslogtreecommitdiff
path: root/browse.c
diff options
context:
space:
mode:
Diffstat (limited to 'browse.c')
-rw-r--r--browse.c106
1 files changed, 44 insertions, 62 deletions
diff --git a/browse.c b/browse.c
index 23a1001..79b3f9c 100644
--- a/browse.c
+++ b/browse.c
@@ -41,81 +41,52 @@
#include "browse.h"
#include "cgi.h"
#include "config.h"
+#include "download.h"
+#include "file.h"
#include "http.h"
#include "url.h"
#include "util.h"
/*
- * list of files for current directory
+ * a list of files
*/
SLIST_HEAD(, file) list;
-struct file {
- char *name;
- char *url;
- SLIST_ENTRY(file) files;
-};
/*
- * should be able to free incomplete files as per file_new usage
+ * browse url = r->pname / BROWSE_URL / file->path / file->name
*/
-static void
-file_free(struct file * file)
+static size_t
+build_browse_url(struct kreq * r, struct file * file)
{
- if (file == NULL)
- return;
-
- if (file->name != NULL) {
- free(file->name);
- file->name = NULL;
- }
- if (file->url != NULL) {
- free(file->url);
- file->url = NULL;
- }
- free(file);
-}
+ char action_url[PATH_MAX];
+ size_t action_url_len;
-static struct file *
-file_new(char *name, size_t name_len, char *url, size_t url_len)
-{
- struct file *file;
+ if (!file->is_dir)
+ return 0;
- file = calloc(1, sizeof(struct file));
- if (file == NULL)
- return NULL;
- file->name = malloc(sizeof(char) * (name_len + 1));
- if (strlcpy(file->name, name, name_len + 1) >= name_len + 1) {
- free(file);
- return NULL;
+ action_url_len = url_build(action_url, PATH_MAX, r->pname, BROWSE_URL,
+ file->path, file->name, NULL);
+ if (action_url_len == 0 || action_url_len >= PATH_MAX) {
+ kutil_warn(r, NULL,
+ "browse: action URL overflow: %s", action_url);
+ return 0;
}
- file->url = malloc(sizeof(char) * (url_len + 1));
- if (strlcpy(file->url, url, url_len + 1) >= url_len + 1) {
- free(file);
- return NULL;
+ file->action_url = v_strcpy(action_url, action_url_len);
+ if (file->action_url == NULL) {
+ kutil_warn(r, NULL,
+ "browse: unable to allocate file url buffer: %s",
+ action_url);
+ return 0;
}
- return file;
-}
-
-/*
- * file url = r->pname / r->pagename / r->path / file_name
- */
-static size_t
-build_browse_url(struct kreq * r, char *url, size_t url_len, const char *file)
-{
- char *page_name;
-
- page_name = strlen(r->pagename) == 0 ? BROWSE_URL : r->pagename;
- return url_build(url, url_len, r->pname, page_name, r->path, file,
- NULL);
+ return action_url_len;
}
static int
-build_file_list(struct kreq * r, char *request_dir)
+build_file_list(struct kreq * r, const char *request_dir)
{
+ bool action_url_ok;
char *file_name;
- char url[PATH_MAX];
DIR *data_dir;
- size_t url_len;
struct dirent *dir;
struct file *file;
struct file *last_file;
@@ -131,17 +102,28 @@ build_file_list(struct kreq * r, char *request_dir)
file_name = dir->d_name;
if (strcmp(".", file_name) == 0 || strcmp("..", file_name) == 0)
continue;
- url_len = build_browse_url(r, url, PATH_MAX, file_name);
- if (url_len == 0 || url_len >= PATH_MAX) {
- kutil_warn(r, NULL, "browse: Detected URL overflow: %s", url);
+ /* build file */
+ file = file_new(r->path, strlen(r->path), dir->d_name,
+ dir->d_namlen);
+ if (file == NULL) {
+ kutil_warn(r, NULL,
+ "browse: unable to allocate file, skipping %s",
+ file_name);
continue;
}
- /* build file and add to list */
- file = file_new(dir->d_name, dir->d_namlen, url, url_len);
- if (file == NULL) {
- kutil_warn(r, NULL, "browse: unable to build file info");
+ /* build action url */
+ if (file->is_dir)
+ action_url_ok = build_browse_url(r, file) > 0;
+ else
+ action_url_ok = build_download_url(r, file) > 0;
+ if (!action_url_ok) {
+ kutil_warn(r, NULL,
+ "browse: unable to build action url, skipping %s",
+ file_name);
+ file_free(file);
continue;
}
+ /* add file to list */
if (last_file == NULL)
SLIST_INSERT_HEAD(&list, file, files);
else
@@ -183,7 +165,7 @@ template_callback(size_t index, void *arg)
break;
case 1:
/* url */
- K_OK(khttp_puts(r, data->f->url), r);
+ K_OK(khttp_puts(r, data->f->action_url), r);
break;
default:
kutil_warnx(r, NULL, "Invalid key index for browse template: %zd", index);