summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
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;
}