From ff9c276c144bc24c70f1d5b531a8fd412c34c3d9 Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Fri, 13 Oct 2023 12:17:45 +0000 Subject: Improved logging system Signed-off-by: Laurent Bercot --- src/tipideed/responses.c | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) (limited to 'src/tipideed/responses.c') diff --git a/src/tipideed/responses.c b/src/tipideed/responses.c index 8c6f1f5..e5706e2 100644 --- a/src/tipideed/responses.c +++ b/src/tipideed/responses.c @@ -9,63 +9,64 @@ #include #include +#include #include #include "tipideed-internal.h" -void response_error (tipidee_rql const *rql, char const *rsl, char const *text, int doclose) +void response_error (tipidee_rql const *rql, unsigned int status, char const *rsl, char const *text, uint32_t options) { tain deadline ; - char ans[4] = "???" ; - memcpy(ans, rsl, 3) ; - tipidee_response_error(buffer_1, rql, rsl, text, doclose || !g.cont) ; + tipidee_response_error(buffer_1, rql, status, rsl, text, options & 1 || !g.cont) ; tain_add_g(&deadline, &g.writetto) ; - log_response(ans, 0) ; + if (!(options & 2)) tipidee_log_answer(g.logv, rql, status, 0) ; if (!buffer_timed_flush_g(buffer_1, &deadline)) strerr_diefu1sys(111, "write to stdout") ; } -void response_error_and_exit (tipidee_rql const *rql, char const *rsl, char const *text) +void response_error_and_exit (tipidee_rql const *rql, unsigned int status, char const *rsl, char const *text, uint32_t options) { - response_error(rql, rsl, text, 1) ; - log_and_exit(0) ; + response_error(rql, status, rsl, text, options | 1) ; + tipidee_log_exit(g.logv, 0) ; + _exit(0) ; } -void response_error_and_die (tipidee_rql const *rql, int e, char const *rsl, char const *text, char const *const *v, unsigned int n, int dosys) +void response_error_and_die (tipidee_rql const *rql, int e, unsigned int status, char const *rsl, char const *text, char const *const *v, unsigned int n, uint32_t options) { - response_error(rql, rsl, text, 1) ; - if (dosys) strerr_dievsys(e, v, n) ; + response_error(rql, status, rsl, text, options | 1) ; + if (options & 1) strerr_dievsys(e, v, n) ; else strerr_diev(e, v, n) ; } void exit_405 (tipidee_rql const *rql, uint32_t options) { tain deadline ; - tipidee_response_status_line(buffer_1, rql, "405 Method Not Allowed") ; + tipidee_response_status(buffer_1, rql, 405, "Method Not Allowed") ; tipidee_response_header_common_put_g(buffer_1, 1) ; buffer_putsnoflush(buffer_1, "Allow: GET, HEAD") ; if (options & 1) buffer_putsnoflush(buffer_1, ", POST") ; buffer_putnoflush(buffer_1, "\r\n\r\n", 4) ; + if (!(options & 2)) tipidee_log_answer(g.logv, rql, 405, 0) ; tain_add_g(&deadline, &g.writetto) ; if (!buffer_timed_flush_g(buffer_1, &deadline)) strerr_diefu1sys(111, "write to stdout") ; - log_and_exit(0) ; + tipidee_log_exit(g.logv, 0) ; + _exit(0) ; } void respond_30x (tipidee_rql const *rql, tipidee_redirection const *rd) { - static char const *rsl[4] = { "307 Temporary Redirect", "308 Permanent Redirect", "302 Found", "301 Moved Permanently" } ; + static unsigned int const status[4] = { 307, 308, 302, 301 } ; + static char const *const reason[4] = { "Temporary Redirect", "Permanent Redirect", "Found", "Moved Permanently" } ; tain deadline ; - char ans[4] = "30x" ; - memcpy(ans, rsl[rd->type], 3) ; - tipidee_response_status_line(buffer_1, rql, rsl[rd->type]) ; + tipidee_response_status(buffer_1, rql, status[rd->type], reason[rd->type]) ; tipidee_response_header_common_put_g(buffer_1, 0) ; buffer_putsnoflush(buffer_1, "Content-Length: 0\r\nLocation: ") ; buffer_putsnoflush(buffer_1, rd->location) ; if (rd->sub) buffer_putsnoflush(buffer_1, rd->sub) ; buffer_putnoflush(buffer_1, "\r\n\r\n", 4) ; + tipidee_log_answer(g.logv, rql, status[rd->type], 0) ; tain_add_g(&deadline, &g.writetto) ; - log_response(ans, 0) ; if (!buffer_timed_flush_g(buffer_1, &deadline)) strerr_diefu1sys(111, "write to stdout") ; } -- cgit v1.3.1