diff options
Diffstat (limited to 'browse.c')
| -rw-r--r-- | browse.c | 67 |
1 files changed, 28 insertions, 39 deletions
@@ -41,6 +41,7 @@ #include "browse.h" #include "cgi.h" #include "config.h" +#include "create.h" #include "download.h" #include "file.h" #include "http.h" @@ -164,7 +165,7 @@ struct template_data { struct file *f; }; -static const char *const header_template_keys[] = {"upload_url"}; +static const char *const header_template_keys[] = {"upload_url", "create_url"}; static int header_template_callback(size_t index, void *arg) @@ -185,7 +186,21 @@ header_template_callback(size_t index, void *arg) switch (index) { case 0: /* upload_url */ - K_OK(khtml_puts(html, data->f->action_url), r); + if (build_upload_url(r, data->f) > 0) { + K_OK(khtml_puts(html, data->f->action_url), r); + } else + kutil_warnx(r, NULL, + "failed to build upload URL for path %s", + data->f->path); + break; + case 1: + /* create_url */ + if (build_create_url(r, data->f) > 0) { + K_OK(khtml_puts(html, data->f->action_url), r); + } else + kutil_warnx(r, NULL, + "failed to build create URL for path %s", + data->f->path); break; default: kutil_warnx(r, NULL, @@ -241,7 +256,11 @@ file_template_callback(size_t index, void *arg) K_OK(khtml_puts(html, data->f->action_url), r); break; case 3: - /* size, print as human readable */ + /* size, only for files, print as human readable */ + if (data->f->is_dir) { + break; + } + file_size = data->f->size; i = 0; while (file_size > 1024 && i < 3) { @@ -275,12 +294,11 @@ file_template_callback(size_t index, void *arg) return 1; } -struct http_ret +void browse(struct kreq * r) { struct file *file; struct page_template *tmpl; - struct http_ret ret; struct khtmlreq html; struct ktemplate template; struct template_data data; @@ -289,53 +307,26 @@ browse(struct kreq * r) file = NULL; tmpl = NULL; - /* initialize return structure for success */ - ret = (struct http_ret) { - KHTTP_200, "" - }; - /* list requested directory content */ file = file_new(r->path); if (file == NULL) { - ret = (struct http_ret) { - KHTTP_404, - "browse: Unable to build data file" - }; + http_exit(r, KHTTP_404, "browse: Unable to build data file"); goto end; } if (!file->is_dir) { - ret = (struct http_ret) { - KHTTP_404, - "browse: Invalid data file" - }; - goto end; - } - if (build_upload_url(r, file) == 0) { - ret = (struct http_ret) { - KHTTP_500, - "browse: Can't build upload url" - }; + http_exit(r, KHTTP_404, "browse: Invalid data file"); goto end; } - if (build_file_list(r, file) < 0) { - ret = (struct http_ret) { - KHTTP_500, - "browse: Unable to build file list" - }; + http_exit(r, KHTTP_500, "browse: Unable to build file list"); goto end; } - /* read template */ tmpl = page_template_new(BROWSE_URL); if (tmpl == NULL) { - ret = (struct http_ret) { - KHTTP_500, - "browse: Unable to read template" - }; + http_exit(r, KHTTP_500, "browse: Unable to read template"); goto end; } - /* we have all the data we need, we can start to write output page */ http_open(r, KHTTP_200, KMIME_TEXT_HTML); @@ -345,7 +336,7 @@ browse(struct kreq * r) /* print browse header with action buttons for current dir */ template.key = header_template_keys; - template.keysz = 1; + template.keysz = 2; template.cb = header_template_callback; template.arg = &data; data.r = r; @@ -383,6 +374,4 @@ end: file_free(file); file = NULL; } - - return ret; } |
