From 13f96054f6fb68aae464363e283e32f01d2da1a3 Mon Sep 17 00:00:00 2001 From: Xavier Del Campo Romero Date: Thu, 9 Mar 2023 01:23:02 +0100 Subject: page.c: Provide meaningful description on HTTP 404 --- page.c | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/page.c b/page.c index 03671d6..9130147 100644 --- a/page.c +++ b/page.c @@ -837,6 +837,33 @@ static int serve_file(struct http_response *const r, return 0; } +static int page_not_found(struct http_response *const r) +{ + static const char body[] = + DOCTYPE_TAG + "\n" + " " + " " PROJECT_TAG "\n" + " " + "File or directory not found\n" + ""; + + *r = (const struct http_response) + { + .status = HTTP_STATUS_NOT_FOUND, + .buf.ro = body, + .n = sizeof body - 1 + }; + + if (http_response_add_header(r, "Content-Type", "text/html")) + { + fprintf(stderr, "%s: http_response_add_header failed\n", __func__); + return -1; + } + + return 0; +} + int page_resource(struct http_response *const r, const char *const dir, const char *const root, const char *const res, const struct page_quota *const q) @@ -849,14 +876,7 @@ int page_resource(struct http_response *const r, const char *const dir, __func__, res, strerror(errno)); if (errno == ENOENT) - { - *r = (const struct http_response) - { - .status = HTTP_STATUS_NOT_FOUND - }; - - return 0; - } + return page_not_found(r); else return -1; } -- cgit v1.2.3