diff options
author | Vincent Douillet <vincent@vdouillet.fr> | 2023-07-31 17:34:24 +0200 |
---|---|---|
committer | Vincent Douillet <vincent@vdouillet.fr> | 2023-08-01 18:14:47 +0200 |
commit | 62b01654da4f1ae1a9aaeab106cea5e7bb9d77e3 (patch) | |
tree | 275b99f1fe4d12777f389045d6cd8411d5e6f324 | |
parent | 89a76cdd3fa168902d83f4c043d2cc6629b40024 (diff) |
handle kcgi errors
-rw-r--r-- | browse.c | 36 | ||||
-rw-r--r-- | cgi.h | 41 | ||||
-rw-r--r-- | main.c | 12 |
3 files changed, 63 insertions, 26 deletions
@@ -35,15 +35,16 @@ #include <string.h> #include "browse.h" +#include "cgi.h" #include "config.h" -#include "error.h" #include "http.h" #include "url.h" #define URL_LENGTH_MAX 8192 /* - * Checks that the path can be safely processed. Namely, it should not contain "..", which denotes an attempt to get out of the DATA_DIR root folder. + * Checks that the path can be safely processed. Namely, it should not contain + * "..", which denotes an attempt to get out of the DATA_DIR root folder. */ static bool check_request_path(char *path) @@ -105,20 +106,19 @@ browse(struct kreq * r) } http_open(r, KHTTP_200); - if (KCGI_OK != khtml_open(&html, r, 0)) - return VAULT_CGI_ERROR; + K_OK(khtml_open(&html, r, 0)); /* build basic html page */ - khtml_elem(&html, KELEM_DOCTYPE); - khtml_elem(&html, KELEM_HEAD); - khtml_attr(&html, KELEM_META, KATTR_CHARSET, "utf-8", KATTR__MAX); - khtml_elem(&html, KELEM_HTML); - khtml_elem(&html, KELEM_BODY); - khtml_elem(&html, KELEM_P); - khtml_puts(&html, "/"); - khtml_closeelem(&html, 1); + 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)); - khtml_elem(&html, KELEM_UL); + K_OK(khtml_elem(&html, KELEM_UL)); while ((dir = readdir(data_dir)) != NULL) { /* ignore special . and .. folders */ file_name = dir->d_name; @@ -127,12 +127,12 @@ browse(struct kreq * r) url_size = build_browse_url(r, url, URL_LENGTH_MAX, file_name); if (url_size == 0 || url_size >= URL_LENGTH_MAX) continue; - khtml_elem(&html, KELEM_LI); - khtml_attr(&html, KELEM_A, KATTR_HREF, url, KATTR__MAX); - khtml_puts(&html, dir->d_name); - khtml_closeelem(&html, 2); + 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)); } - khtml_close(&html); + K_OK(khtml_close(&html)); closedir(data_dir); return VAULT_SUCCESS; @@ -0,0 +1,41 @@ +/* + * Copyright 2023, Vincent Douillet <vincent@vdouillet.fr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors + * may be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef VAULT_CGI_H +#define VAULT_CGI_H + +#include <kcgi.h> + +#include "error.h" + +#define K_OK(kcgi_call) if(kcgi_call != KCGI_OK) \ + return VAULT_CGI_ERROR + +#endif /* VAULT_CGI_H */ @@ -29,10 +29,10 @@ */ #include <stdlib.h> -#include <kcgi.h> #include <unistd.h> #include "browse.h" +#include "cgi.h" #include "http.h" enum page { @@ -47,14 +47,10 @@ static const char *const pages[PAGE__MAX] = { int main(void) { - enum kcgi_err err; - int page_ret_val; - struct kreq r; + int page_ret_val; + struct kreq r; - err = khttp_parse(&r, NULL, 0, pages, PAGE__MAX, PAGE_BROWSE); - - if (KCGI_OK != err) - return EXIT_FAILURE; + K_OK(khttp_parse(&r, NULL, 0, pages, PAGE__MAX, PAGE_BROWSE)); /* * Make sure basic request parameters are as expected : GET or POST, |