diff options
author | Vincent Douillet <vincent@vdouillet.fr> | 2024-05-13 20:44:07 +0200 |
---|---|---|
committer | Vincent Douillet <vincent@vdouillet.fr> | 2024-05-13 22:01:09 +0200 |
commit | 10eb77f9323110c55f88195c5b8207eb524baa73 (patch) | |
tree | 940c83fc095bad3e63045bc6fbe9ada8e4276135 /browse.c | |
parent | 0effd40bad7ae753102e7040159afd76be4e8732 (diff) |
add unit tests
Diffstat (limited to 'browse.c')
-rw-r--r-- | browse.c | 63 |
1 files changed, 48 insertions, 15 deletions
@@ -136,7 +136,7 @@ build_file_list(struct kreq * r, const char *request_dir) return 0; } -const char *const template_keys[] = {"icon", "name", "url", "size"}; +static const char *const template_keys[] = {"icon", "name", "url", "size"}; /* * data required in the template function @@ -203,36 +203,66 @@ template_callback(size_t index, void *arg) return 1; } -void +struct http_ret browse(struct kreq * r) { size_t url_len; char current_dir[PATH_MAX]; struct file *file; struct page_template *tmpl; + struct http_ret ret; struct khtmlreq html; struct ktemplate template; struct template_data data; - /* check that the requested URL can be safely processed */ - if (!check_request_path(r->path, r->suffix)) - http_exit(r, KHTTP_400, "browse: Invalid request path"); + /* initialize vars */ + tmpl = NULL; + + /* initialize return structure for success */ + ret = (struct http_ret) { + KHTTP_200, "" + }; + /* check that the requested URL can be safely processed */ + if (!check_request_path(r->path, r->suffix)) { + ret = (struct http_ret) { + KHTTP_400, + "browse: Invalid request path" + }; + goto end; + } /* list requested directory content */ url_len = url_build(current_dir, PATH_MAX, DATA_DIR, r->path, NULL); - if (url_len == 0) - http_exit(r, KHTTP_404, "browse: Unable to build data path"); - if (url_len >= PATH_MAX) - http_exit(r, KHTTP_414, NULL); - if (build_file_list(r, current_dir) < 0) - http_exit(r, KHTTP_500, "browse: Unable to build file list"); - + if (url_len == 0) { + ret = (struct http_ret) { + KHTTP_404, + "browse: Unable to build data path" + }; + goto end; + } + if (url_len >= PATH_MAX) { + ret = (struct http_ret) { + KHTTP_414, "" + }; + goto end; + } + if (build_file_list(r, current_dir) < 0) { + ret = (struct http_ret) { + KHTTP_500, + "browse: Unable to build file list" + }; + goto end; + } /* read template */ tmpl = page_template_new(BROWSE_URL); - if (tmpl == NULL) - http_exit(r, KHTTP_500, "browse: unable to read template"); - + if (tmpl == NULL) { + ret = (struct http_ret) { + 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, r->mime); @@ -261,6 +291,7 @@ browse(struct kreq * r) K_OK(khtml_close(&html), r); /* free page template and file list */ +end: page_template_free(tmpl); file = NULL; while (!SLIST_EMPTY(&list)) { @@ -269,4 +300,6 @@ browse(struct kreq * r) file_free(file); file = NULL; } + + return ret; } |