summaryrefslogtreecommitdiff
path: root/browse.c
diff options
context:
space:
mode:
Diffstat (limited to 'browse.c')
-rw-r--r--browse.c71
1 files changed, 33 insertions, 38 deletions
diff --git a/browse.c b/browse.c
index 577183f..f12ed70 100644
--- a/browse.c
+++ b/browse.c
@@ -71,7 +71,7 @@ build_browse_url(struct kreq * r, char *url, size_t url_size, const char *file)
return url_build(url, url_size, 4, r->pname, page_name, r->path, file);
}
-int
+void
browse(struct kreq * r)
{
struct dirent *dir;
@@ -82,58 +82,53 @@ browse(struct kreq * r)
struct khtmlreq html;
/* check that the requested URL can be safely processed */
- if (strlen(r->path) >= URL_LENGTH_MAX) {
- http_open(r, KHTTP_414);
- return VAULT_URI_ERROR;
- }
- if (!check_request_path(r->path)) {
- http_open(r, KHTTP_400);
- return VAULT_URI_ERROR;
- }
+ if (!check_request_path(r->path))
+ http_exit(r, KHTTP_400, "browse: Invalid request path");
+ if (strlen(r->path) >= URL_LENGTH_MAX)
+ http_exit(r, KHTTP_414, NULL);
+
/* list requested directory content */
url_size = url_build(current_dir, URL_LENGTH_MAX, 2, DATA_DIR, r->path);
- if (url_size == 0) {
- http_open(r, KHTTP_404);
- return VAULT_IO_ERROR;
- } else if (url_size >= URL_LENGTH_MAX) {
- http_open(r, KHTTP_414);
- return VAULT_URI_ERROR;
- }
+ if (url_size == 0)
+ http_exit(r, KHTTP_404, "browse: Unable to build data path");
+ if (url_size >= URL_LENGTH_MAX)
+ http_exit(r, KHTTP_414, NULL);
data_dir = opendir(current_dir);
- if (NULL == data_dir) {
- http_open(r, KHTTP_404);
- return VAULT_IO_ERROR;
- }
+ if (NULL == data_dir)
+ http_exit(r, KHTTP_404, NULL);
+
http_open(r, KHTTP_200);
- K_OK(khtml_open(&html, r, 0));
+ K_OK(khtml_open(&html, r, 0), r);
/* build basic html page */
- K_OK(khtml_elem(&html, KELEM_DOCTYPE));
- K_OK(khtml_elem(&html, KELEM_HEAD));
- K_OK(khtml_attr(&html, KELEM_META, KATTR_CHARSET, "utf-8", KATTR__MAX));
- K_OK(khtml_elem(&html, KELEM_HTML));
- K_OK(khtml_elem(&html, KELEM_BODY));
- K_OK(khtml_elem(&html, KELEM_P));
- K_OK(khtml_puts(&html, "/"));
- K_OK(khtml_closeelem(&html, 1));
+ K_OK(khtml_elem(&html, KELEM_DOCTYPE), r);
+ K_OK(khtml_elem(&html, KELEM_HEAD), r);
+ K_OK(khtml_attr(&html, KELEM_META, KATTR_CHARSET, "utf-8", KATTR__MAX),
+ r);
+ K_OK(khtml_elem(&html, KELEM_HTML), r);
+ K_OK(khtml_elem(&html, KELEM_BODY), r);
+ K_OK(khtml_elem(&html, KELEM_P), r);
+ K_OK(khtml_puts(&html, "/"), r);
+ K_OK(khtml_closeelem(&html, 1), r);
- K_OK(khtml_elem(&html, KELEM_UL));
+ K_OK(khtml_elem(&html, KELEM_UL), r);
while ((dir = readdir(data_dir)) != NULL) {
/* ignore special . and .. folders */
file_name = dir->d_name;
if (strcmp(".", file_name) == 0 || strcmp("..", file_name) == 0)
continue;
url_size = build_browse_url(r, url, URL_LENGTH_MAX, file_name);
- if (url_size == 0 || url_size >= URL_LENGTH_MAX)
+ if (url_size == 0 || url_size >= URL_LENGTH_MAX) {
+ kutil_warn(r, NULL, "browse: Detected URL overflow: %s", url);
continue;
- K_OK(khtml_elem(&html, KELEM_LI));
- K_OK(khtml_attr(&html, KELEM_A, KATTR_HREF, url, KATTR__MAX));
- K_OK(khtml_puts(&html, dir->d_name));
- K_OK(khtml_closeelem(&html, 2));
+ }
+ K_OK(khtml_elem(&html, KELEM_LI), r);
+ K_OK(khtml_attr(&html, KELEM_A, KATTR_HREF, url, KATTR__MAX),
+ r);
+ K_OK(khtml_puts(&html, dir->d_name), r);
+ K_OK(khtml_closeelem(&html, 2), r);
}
- K_OK(khtml_close(&html));
+ K_OK(khtml_close(&html), r);
closedir(data_dir);
-
- return VAULT_SUCCESS;
}