diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2023-10-10 08:06:46 +0000 |
---|---|---|
committer | Laurent Bercot <ska@appnovation.com> | 2023-10-10 08:06:46 +0000 |
commit | 541325d00c7078d063ea97333d3c9c64d6b6c7b6 (patch) | |
tree | 86453b9f547359546d74988b04ab33ec10b4000c /src | |
parent | d9f5cb7a28ee79e299e41d3ce325171f01e1a3d7 (diff) | |
download | tipidee-541325d00c7078d063ea97333d3c9c64d6b6c7b6.tar.xz |
Add new logging functions, change method_conv for bsearch
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/include/tipidee/log.h | 41 | ||||
-rw-r--r-- | src/include/tipidee/method.h | 16 | ||||
-rw-r--r-- | src/libtipidee/deps-lib/tipidee | 9 | ||||
-rw-r--r-- | src/libtipidee/tipidee_log_answer.c | 24 | ||||
-rw-r--r-- | src/libtipidee/tipidee_log_exit.c | 14 | ||||
-rw-r--r-- | src/libtipidee/tipidee_log_request.c | 60 | ||||
-rw-r--r-- | src/libtipidee/tipidee_log_resource.c | 11 | ||||
-rw-r--r-- | src/libtipidee/tipidee_log_start.c | 25 | ||||
-rw-r--r-- | src/libtipidee/tipidee_method.c | 46 | ||||
-rw-r--r-- | src/libtipidee/tipidee_method_conv_table.c | 19 | ||||
-rw-r--r-- | src/libtipidee/tipidee_method_tonum.c | 12 | ||||
-rw-r--r-- | src/libtipidee/tipidee_method_tostr.c | 8 |
12 files changed, 231 insertions, 54 deletions
diff --git a/src/include/tipidee/log.h b/src/include/tipidee/log.h new file mode 100644 index 0000000..0fe44ce --- /dev/null +++ b/src/include/tipidee/log.h @@ -0,0 +1,41 @@ +/* ISC license. */ + +#ifndef TIPIDEE_LOG_H +#define TIPIDEE_LOG_H + +#include <sys/types.h> +#include <stdint.h> + +#include <skalibs/stralloc.h> + +#include <tipidee/rql.h> + +#define TIPIDEE_LOG_REQUEST 0x0001 +#define TIPIDEE_LOG_REFERRER 0x0002 +#define TIPIDEE_LOG_UA 0x0004 +#define TIPIDEE_LOG_RESOURCE 0x0008 +#define TIPIDEE_LOG_ANSWER 0x0010 +#define TIPIDEE_LOG_SIZE 0x0020 +#define TIPIDEE_LOG_START 0x100 +#define TIPIDEE_LOG_CLIENTIP 0x0200 +#define TIPIDEE_LOG_CLIENTHOST 0x0400 + +#define TIPIDEE_LOG_DEFAULT (TIPIDEE_LOG_REQUEST | TIPIDEE_LOG_ANSWER | TIPIDEE_LOG_SIZE) + +typedef struct tipidee_resattr_s tipidee_resattr, *tipidee_resattr_ref ; +struct tipidee_resattr_s +{ + char const *content_type ; + uint32_t iscgi : 1 ; + uint32_t isnph : 1 ; +} ; +#define TIPIDEE_RESATTR_ZERO { .content_type = 0, .iscgi = 0, .isnph = 0 } + +extern void tipidee_log_start (uint32_t, char const *, char const *) ; +extern void tipidee_log_exit (uint32_t, unsigned int) ; + +extern void tipidee_log_request (uint32_t, tipidee_rql const *, char const *, char const *, stralloc *) ; +extern void tipidee_log_resource (uint32_t, char const *, char const *, tipidee_resattr const *) ; +extern void tipidee_log_answer (uint32_t, unsigned int, off_t) ; + +#endif diff --git a/src/include/tipidee/method.h b/src/include/tipidee/method.h index 05123e8..b6efaa6 100644 --- a/src/include/tipidee/method.h +++ b/src/include/tipidee/method.h @@ -6,26 +6,18 @@ typedef enum tipidee_method_e tipidee_method, *tipidee_method_ref ; enum tipidee_method_e { - TIPIDEE_METHOD_GET = 0, + TIPIDEE_METHOD_CONNECT = 0, + TIPIDEE_METHOD_DELETE, + TIPIDEE_METHOD_GET, TIPIDEE_METHOD_HEAD, TIPIDEE_METHOD_OPTIONS, TIPIDEE_METHOD_POST, + TIPIDEE_METHOD_PRI, TIPIDEE_METHOD_PUT, - TIPIDEE_METHOD_DELETE, TIPIDEE_METHOD_TRACE, - TIPIDEE_METHOD_CONNECT, - TIPIDEE_METHOD_PRI, TIPIDEE_METHOD_UNKNOWN } ; -typedef struct tipidee_method_conv_s tipidee_method_conv, *tipidee_method_conv_ref ; -struct tipidee_method_conv_s -{ - tipidee_method num ; - char const *str ; -} ; - -extern tipidee_method_conv const *tipidee_method_conv_table ; extern char const *tipidee_method_tostr (tipidee_method) ; extern tipidee_method tipidee_method_tonum (char const *) ; diff --git a/src/libtipidee/deps-lib/tipidee b/src/libtipidee/deps-lib/tipidee index b7f88cc..cba7e43 100644 --- a/src/libtipidee/deps-lib/tipidee +++ b/src/libtipidee/deps-lib/tipidee @@ -10,9 +10,12 @@ tipidee_headers_get_content_length.o tipidee_headers_init.o tipidee_headers_parse.o tipidee_headers_search.o -tipidee_method_conv_table.o -tipidee_method_tonum.o -tipidee_method_tostr.o +tipidee_log_answer.o +tipidee_log_exit.o +tipidee_log_resource.o +tipidee_log_request.o +tipidee_log_start.o +tipidee_method.o tipidee_response_error.o tipidee_response_header_builtin.o tipidee_response_header_common_put.o diff --git a/src/libtipidee/tipidee_log_answer.c b/src/libtipidee/tipidee_log_answer.c new file mode 100644 index 0000000..beedb61 --- /dev/null +++ b/src/libtipidee/tipidee_log_answer.c @@ -0,0 +1,24 @@ +/* ISC license. */ + +#include <sys/types.h> + +#include <skalibs/uint64.h> +#include <skalibs/types.h> +#include <skalibs/strerr.h> + +#include <tipidee/log.h> + +void tipidee_log_answer (uint32_t v, unsigned int status, off_t size) +{ + char fmtstatus[UINT_FMT] ; + if (!(v & TIPIDEE_LOG_ANSWER)) return ; + fmtstatus[uint_fmt(fmtstatus, status)] = 0 ; + if (size) + { + char fmtsize[UINT64_FMT] ; + fmtsize[uint64_fmt(fmtsize, size)] = 0 ; + strerr_warni4x("answer ", fmtstatus, " size ", fmtsize) ; + } + else + strerr_warni2x("answer ", fmtstatus) ; +} diff --git a/src/libtipidee/tipidee_log_exit.c b/src/libtipidee/tipidee_log_exit.c new file mode 100644 index 0000000..7d7b3e0 --- /dev/null +++ b/src/libtipidee/tipidee_log_exit.c @@ -0,0 +1,14 @@ +/* ISC license. */ + +#include <skalibs/types.h> +#include <skalibs/strerr.h> + +#include <tipidee/log.h> + +void tipidee_log_exit (uint32_t v, unsigned int e) +{ + char fmt[UINT_FMT] ; + if (!(v & TIPIDEE_LOG_START)) return ; + fmt[uint_fmt(fmt, e)] = 0 ; + strerr_warni2x("exit ", fmt) ; +} diff --git a/src/libtipidee/tipidee_log_request.c b/src/libtipidee/tipidee_log_request.c new file mode 100644 index 0000000..666297d --- /dev/null +++ b/src/libtipidee/tipidee_log_request.c @@ -0,0 +1,60 @@ +/* ISC license. */ + +#include <stddef.h> + +#include <skalibs/strerr.h> +#include <skalibs/stralloc.h> +#include <skalibs/skamisc.h> + +#include <tipidee/method.h> +#include <tipidee/log.h> + +void tipidee_log_request (uint32_t v, tipidee_rql const *rql, char const *referrer, char const *ua, stralloc *sa) +{ + char const *a[14] = { "info: request " } ; + size_t m = 1 ; + size_t start = sa->len ; + size_t refpos = start, uapos = start ; + if (!(v & TIPIDEE_LOG_REQUEST)) return ; + if (!string_quotes(sa, rql->uri.path)) goto eerr ; + if (v & TIPIDEE_LOG_REFERRER) + { + refpos = sa->len ; + if (!string_quotes(sa, referrer) || !stralloc_0(sa)) goto err ; + } + if (v & TIPIDEE_LOG_UA) + { + uapos = sa->len ; + if (!string_quotes(sa, ua) || !stralloc_0(sa)) goto err ; + } + a[m++] = tipidee_method_tostr(rql->m) ; + a[m++] = " host " ; + a[m++] = rql->uri.host ; + a[m++] = " path " ; + a[m++] = sa->s + start ; + if (rql->uri.query) + { + a[m++] = " query " ; + a[m++] = rql->uri.query ; + } + a[m++] = " http 1." ; + a[m++] = rql->http_minor ? "1" : "0" ; + if (v & TIPIDEE_LOG_REFERRER) + { + a[m++] = " referrer " ; + a[m++] = sa->s + refpos ; + } + if (v & TIPIDEE_LOG_UA) + { + a[m++] = " user-agent " ; + a[m++] = sa->s + uapos ; + } + strerr_warnv(a, m) ; + sa->len = start ; + return ; + + err: + sa->len = start ; + eerr: + strerr_warnwu1sys("log request") ; +} diff --git a/src/libtipidee/tipidee_log_resource.c b/src/libtipidee/tipidee_log_resource.c new file mode 100644 index 0000000..dc525a5 --- /dev/null +++ b/src/libtipidee/tipidee_log_resource.c @@ -0,0 +1,11 @@ +/* ISC license. */ + +#include <skalibs/strerr.h> + +#include <tipidee/log.h> + +void tipidee_log_resource (uint32_t v, char const *docroot, char const *file, tipidee_resattr const *ra) +{ + if (!(v & TIPIDEE_LOG_RESOURCE)) return ; + strerr_warni6x("docroot ", docroot, " file ", file, " type ", ra->iscgi ? ra->isnph ? "nph" : "cgi" : ra->content_type) ; +} diff --git a/src/libtipidee/tipidee_log_start.c b/src/libtipidee/tipidee_log_start.c new file mode 100644 index 0000000..34284af --- /dev/null +++ b/src/libtipidee/tipidee_log_start.c @@ -0,0 +1,25 @@ +/* ISC license. */ + +#include <stddef.h> + +#include <skalibs/strerr.h> + +#include <tipidee/log.h> + +void tipidee_log_start (uint32_t v, char const *ip, char const *host) +{ + char const *a[5] = { "info: start" } ; + size_t m = 1 ; + if (!(v & TIPIDEE_LOG_START)) return ; + if (v & TIPIDEE_LOG_CLIENTIP) + { + a[m++] = " ip " ; + a[m++] = ip ; + } + if (v & TIPIDEE_LOG_CLIENTHOST) + { + a[m++] = " host " ; + a[m++] = host ; + } + strerr_warnv(a, m) ; +} diff --git a/src/libtipidee/tipidee_method.c b/src/libtipidee/tipidee_method.c new file mode 100644 index 0000000..c5e7195 --- /dev/null +++ b/src/libtipidee/tipidee_method.c @@ -0,0 +1,46 @@ +/* ISC license. */ + +#include <string.h> +#include <stdlib.h> + +#include <tipidee/method.h> + +struct blah_s +{ + tipidee_method num ; + char const *str ; +} ; + +static struct blah_s const table[] = +{ + { .num = TIPIDEE_METHOD_CONNECT, .str = "CONNECT" }, + { .num = TIPIDEE_METHOD_DELETE, .str = "DELETE" }, + { .num = TIPIDEE_METHOD_GET, .str = "GET" }, + { .num = TIPIDEE_METHOD_HEAD, .str = "HEAD" }, + { .num = TIPIDEE_METHOD_OPTIONS, .str = "OPTIONS" }, + { .num = TIPIDEE_METHOD_POST, .str = "POST" }, + { .num = TIPIDEE_METHOD_PRI, .str = "PRI" }, + { .num = TIPIDEE_METHOD_PUT, .str = "PUT" }, + { .num = TIPIDEE_METHOD_TRACE, .str = "TRACE" }, +} ; + +static int blah_cmp (void const *key, void const *el) +{ + return strcmp((char const *)key, ((struct blah_s const *)el)->str) ; +} + +tipidee_method tipidee_method_tonum (char const *s) +{ + struct blah_s const *p = bsearch( + s, + table, + sizeof(table) / sizeof(struct blah_s), + sizeof(struct blah_s), + &blah_cmp) ; + return p ? p->num : TIPIDEE_METHOD_UNKNOWN ; +} + +char const *tipidee_method_tostr (tipidee_method m) +{ + return m < TIPIDEE_METHOD_UNKNOWN ? table[m].str : 0 ; +} diff --git a/src/libtipidee/tipidee_method_conv_table.c b/src/libtipidee/tipidee_method_conv_table.c deleted file mode 100644 index 892e5b5..0000000 --- a/src/libtipidee/tipidee_method_conv_table.c +++ /dev/null @@ -1,19 +0,0 @@ -/* ISC license. */ - -#include <tipidee/method.h> - -static tipidee_method_conv const table[] = -{ - { .num = TIPIDEE_METHOD_GET, .str = "GET" }, - { .num = TIPIDEE_METHOD_HEAD, .str = "HEAD" }, - { .num = TIPIDEE_METHOD_OPTIONS, .str = "OPTIONS" }, - { .num = TIPIDEE_METHOD_POST, .str = "POST" }, - { .num = TIPIDEE_METHOD_PUT, .str = "PUT" }, - { .num = TIPIDEE_METHOD_DELETE, .str = "DELETE" }, - { .num = TIPIDEE_METHOD_TRACE, .str = "TRACE" }, - { .num = TIPIDEE_METHOD_CONNECT, .str = "CONNECT" }, - { .num = TIPIDEE_METHOD_PRI, .str = "PRI" }, - { .num = TIPIDEE_METHOD_UNKNOWN, .str = 0 } -} ; - -tipidee_method_conv const *tipidee_method_conv_table = table ; diff --git a/src/libtipidee/tipidee_method_tonum.c b/src/libtipidee/tipidee_method_tonum.c deleted file mode 100644 index 7c88e45..0000000 --- a/src/libtipidee/tipidee_method_tonum.c +++ /dev/null @@ -1,12 +0,0 @@ -/* ISC license. */ - -#include <string.h> - -#include <tipidee/method.h> - -tipidee_method tipidee_method_tonum (char const *s) -{ - tipidee_method_conv const *p = tipidee_method_conv_table ; - for (; p->str ; p++) if (!strcmp(s, p->str)) break ; - return p->num ; -} diff --git a/src/libtipidee/tipidee_method_tostr.c b/src/libtipidee/tipidee_method_tostr.c deleted file mode 100644 index 8a3831d..0000000 --- a/src/libtipidee/tipidee_method_tostr.c +++ /dev/null @@ -1,8 +0,0 @@ -/* ISC license. */ - -#include <tipidee/method.h> - -char const *tipidee_method_tostr (tipidee_method m) -{ - return m < TIPIDEE_METHOD_UNKNOWN ? tipidee_method_conv_table[m].str : 0 ; -} |