summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authorVincent Douillet <vincent@vdouillet.fr>2023-08-01 23:09:57 +0200
committerVincent Douillet <vincent@vdouillet.fr>2023-08-07 14:57:21 +0200
commitf172ffca292594147939280dab20cb03a4921fb6 (patch)
treebc63edcd5fbd474cdfdcced0d89b659c3bcc1cb4 /main.c
parent62b01654da4f1ae1a9aaeab106cea5e7bb9d77e3 (diff)
add log messages
Diffstat (limited to 'main.c')
-rw-r--r--main.c51
1 files changed, 24 insertions, 27 deletions
diff --git a/main.c b/main.c
index 6b54a5e..eab7bca 100644
--- a/main.c
+++ b/main.c
@@ -28,11 +28,14 @@
* POSSIBILITY OF SUCH DAMAGE.
*/
+#include <errno.h>
#include <stdlib.h>
+#include <string.h>
#include <unistd.h>
#include "browse.h"
#include "cgi.h"
+#include "config.h"
#include "http.h"
enum page {
@@ -47,46 +50,40 @@ static const char *const pages[PAGE__MAX] = {
int
main(void)
{
- int page_ret_val;
+ enum kcgi_err parse_err;
struct kreq r;
- K_OK(khttp_parse(&r, NULL, 0, pages, PAGE__MAX, PAGE_BROWSE));
+ if (kutil_openlog(LOG_FILE) == 0)
+ http_exit(NULL, KHTTP_500, "Unable to open %s", LOG_FILE);
+
+ parse_err = khttp_parse(&r, NULL, 0, pages, PAGE__MAX, PAGE_BROWSE);
+ if (parse_err != KCGI_OK)
+ http_exit(NULL, KHTTP_500, "Unable to parse request: %s",
+ kcgi_strerror(parse_err));
+
+ /* A bit of security cannot hurt */
+ if (-1 == pledge("stdio rpath wpath cpath", NULL))
+ http_exit(&r, KHTTP_500, "Pledge failed: %s", strerror(errno));
/*
* Make sure basic request parameters are as expected : GET or POST,
* valid page and HTML document
*/
-
- if (r.method != KMETHOD_GET && r.method != KMETHOD_POST) {
- http_open(&r, KHTTP_405);
- khttp_free(&r);
- return EXIT_SUCCESS;
- } else if (r.mime != KMIME_TEXT_HTML) {
- http_open(&r, KHTTP_406); /* Not Acceptable */
- khttp_free(&r);
- return EXIT_SUCCESS;
- } else if (r.page == PAGE__MAX) {
- http_open(&r, KHTTP_404);
- khttp_free(&r);
- return EXIT_SUCCESS;
- }
- if (-1 == pledge("stdio rpath", NULL)) {
- khttp_free(&r);
- return EXIT_FAILURE;
- }
- /*
- * TODO error happens here if(-1 == unveil(DATA_DIR, "r")) { ret_val
- * = EXIT_FAILURE; goto FAIL_HTML; }
- */
+ if (r.method != KMETHOD_GET && r.method != KMETHOD_POST)
+ http_exit(&r, KHTTP_405, NULL);
+ if (r.mime != KMIME_TEXT_HTML)
+ http_exit(&r, KHTTP_406, NULL); /* Not Acceptable */
+ if (r.page == PAGE__MAX)
+ http_exit(&r, KHTTP_404, NULL);
switch (r.page) {
case PAGE_BROWSE:
- page_ret_val = browse(&r);
+ browse(&r);
break;
default:
- abort();
+ http_exit(&r, KHTTP_404, NULL);
}
khttp_free(&r);
- return page_ret_val;
+ return EXIT_SUCCESS;
}