From 72904c3ba98c59634c4a098a0d38db5bc7c059e8 Mon Sep 17 00:00:00 2001 From: Lorenz Stechauner Date: Fri, 26 Sep 2025 15:04:37 +0200 Subject: [PATCH] Add log files --- src/lib/utils.c | 54 +++++++++++++++++++++++++++++++----- src/lib/utils.h | 2 ++ src/worker/request_handler.c | 35 ++++++++++++++++++++++- 3 files changed, 83 insertions(+), 8 deletions(-) diff --git a/src/lib/utils.c b/src/lib/utils.c index ec38d6c..984bc8d 100644 --- a/src/lib/utils.c +++ b/src/lib/utils.c @@ -20,6 +20,24 @@ static const char base64_encode_table[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; static const int base64_mod_table[3] = {0, 2, 1}; +static const char base64_decode_table[256] = { + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, + 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, 0, -1, -1, + -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, + 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, + -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, + 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, +}; char *format_duration(unsigned long micros, char *buf) { @@ -207,25 +225,25 @@ int strcontains(const char *restrict haystack, const char *restrict needle) { int strstarts(const char *restrict str, const char *restrict prefix) { if (str == NULL || prefix == NULL) return 0; - unsigned long l1 = strlen(str), l2 = strlen(prefix); + const unsigned long l1 = strlen(str), l2 = strlen(prefix); return l2 <= l1 && strncmp(str, prefix, l2) == 0; } int strends(const char *restrict str, const char *restrict suffix) { if (str == NULL || suffix == NULL) return 0; - unsigned long l1 = strlen(str), l2 = strlen(suffix); + const unsigned long l1 = strlen(str), l2 = strlen(suffix); return l2 <= l1 && strcmp(str + l1 - l2, suffix) == 0; } int base64_encode(void *data, unsigned long data_len, char *output, unsigned long *output_len) { - unsigned long out_len = 4 * ((data_len + 2) / 3); + const unsigned long out_len = 4 * ((data_len + 2) / 3); if (output_len != NULL) *output_len = out_len; for (int i = 0, j = 0; i < data_len;) { - unsigned int octet_a = (i < data_len) ? ((unsigned char *) data)[i++] : 0; - unsigned int octet_b = (i < data_len) ? ((unsigned char *) data)[i++] : 0; - unsigned int octet_c = (i < data_len) ? ((unsigned char *) data)[i++] : 0; - unsigned int triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c; + const unsigned int octet_a = (i < data_len) ? ((unsigned char *) data)[i++] : 0; + const unsigned int octet_b = (i < data_len) ? ((unsigned char *) data)[i++] : 0; + const unsigned int octet_c = (i < data_len) ? ((unsigned char *) data)[i++] : 0; + const unsigned int triple = (octet_a << 16) | (octet_b << 8) | octet_c; output[j++] = base64_encode_table[(triple >> 3 * 6) & 0x3F]; output[j++] = base64_encode_table[(triple >> 2 * 6) & 0x3F]; output[j++] = base64_encode_table[(triple >> 1 * 6) & 0x3F]; @@ -239,6 +257,28 @@ int base64_encode(void *data, unsigned long data_len, char *output, unsigned lon return 0; } +int base64_decode(const char *data, unsigned long data_len, void *output, unsigned long *output_len) { + const unsigned long out_len = 3 * ((data_len + 2) / 4); + if (output_len != NULL) *output_len = out_len; + + char *out = output; + for (int i = 0, j = 0; i < data_len;) { + const int octet_a = (i < data_len) ? base64_decode_table[((unsigned char *) data)[i++]] : 0; + const int octet_b = (i < data_len) ? base64_decode_table[((unsigned char *) data)[i++]] : 0; + const int octet_c = (i < data_len) ? base64_decode_table[((unsigned char *) data)[i++]] : 0; + const int octet_d = (i < data_len) ? base64_decode_table[((unsigned char *) data)[i++]] : 0; + if (octet_a < 0 || octet_b < 0 || octet_c < 0 || octet_d < 0) return -1; + const unsigned int triple = (octet_a << 3 * 6) | (octet_b << 2 * 6) | (octet_c << 6) | octet_d; + out[j++] = (char) (triple >> 16); + out[j++] = (char) ((triple >> 8) & 0xFF); + out[j++] = (char) (triple & 0xFF); + } + + out[out_len] = 0; + + return 0; +} + long clock_micros(void) { struct timespec time; clock_gettime(CLOCK_MONOTONIC, &time); diff --git a/src/lib/utils.h b/src/lib/utils.h index 162f2ee..0bc544e 100644 --- a/src/lib/utils.h +++ b/src/lib/utils.h @@ -47,6 +47,8 @@ int strends(const char *restrict str, const char *restrict suffix); int base64_encode(void *data, unsigned long data_len, char *output, unsigned long *output_len); +int base64_decode(const char *data, unsigned long data_len, void *output, unsigned long *output_len); + long clock_micros(void); long clock_cpu(void); diff --git a/src/worker/request_handler.c b/src/worker/request_handler.c index fe3d541..2ea475c 100644 --- a/src/worker/request_handler.c +++ b/src/worker/request_handler.c @@ -373,10 +373,43 @@ int respond(client_ctx_t *ctx) { } void request_complete(client_ctx_t *ctx) { - char buf[32]; + char buf[64]; ctx->req_e = clock_micros(); info("Transfer complete: %s", format_duration(ctx->req_e - ctx->req_s, buf)); + if (ctx->conf) { + char path[256]; + sprintf(path, "/var/log/sesimos/%s.access.log", ctx->req_host); + FILE *log = fopen(path, "a"); + if (log) { + struct timespec time1, time2; + clock_gettime(CLOCK_MONOTONIC, &time1); + clock_gettime(CLOCK_REALTIME, &time2); + const long diff = (time2.tv_sec - time1.tv_sec) * 1000000 + (time2.tv_nsec - time1.tv_nsec) / 1000; + struct tm time_info; + const long ts = (ctx->req_s + diff) / 1000000; + strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S%z", localtime_r(&ts, &time_info)); + + const char *auth = http_get_header_field(&ctx->req.hdr, "Authorization"); + char user[256] = {0}; + if (auth != NULL && strstarts(auth, "Basic ")) { + base64_decode(auth + 6, strlen(auth) - 6, user, NULL); + char *col = strchr(user, ':'); + if (col != NULL) col[0] = 0; + } + const char *ref = http_get_header_field(&ctx->req.hdr, "Referer"); + const char *ua = http_get_header_field(&ctx->req.hdr, "User-Agent"); + + fprintf(log, "%s %s %s [%s] \"%s %s HTTP/%s\" %i %li %s%s%s %s%s%s\n", + ctx->socket.addr, ctx->cc[0] == 0 ? "-" : ctx->cc, user[0] != 0 ? user : "-", buf, + ctx->req.method, ctx->req.uri, ctx->req.version, ctx->res.status->code, ctx->content_length, + ref != NULL ? "\"" : "", ref != NULL ? ref : "-", ref != NULL ? "\"" : "", + ua != NULL ? "\"" : "", ua != NULL ? ua : "-", ua != NULL ? "\"" : ""); + fclose(log); + } + errno = 0; + } + if (ctx->file) fclose(ctx->file); free(ctx->msg_buf_ptr); uri_free(&ctx->uri);