Enhance logging

This commit is contained in:
2025-09-28 17:48:11 +02:00
parent e1a92729d2
commit 151c4804fe
6 changed files with 21 additions and 12 deletions

View File

@@ -656,11 +656,12 @@ int proxy_peek_response(proxy_ctx_t *proxy, http_res *res, http_status_ctx *ctx,
return header_len; return header_len;
} }
int proxy_send(proxy_ctx_t *proxy, sock *client, unsigned long len_to_send, int flags) { long proxy_send(proxy_ctx_t *proxy, sock *client, unsigned long len_to_send, int flags) {
long ret;
char buffer[CHUNK_SIZE]; char buffer[CHUNK_SIZE];
if (sock_splice(client, &proxy->proxy, buffer, sizeof(buffer), len_to_send) == -1) if ((ret = sock_splice(client, &proxy->proxy, buffer, sizeof(buffer), len_to_send)) == -1)
return -1; return -1;
return 0; return ret;
} }
int proxy_dump(proxy_ctx_t *proxy, char *buf, long len) { int proxy_dump(proxy_ctx_t *proxy, char *buf, long len) {

View File

@@ -45,7 +45,7 @@ int proxy_init(proxy_ctx_t **proxy, http_req *req, http_res *res, http_status_ct
int proxy_peek_response(proxy_ctx_t *proxy, http_res *res, http_status_ctx *ctx, http_status *custom_status, char *err_msg); int proxy_peek_response(proxy_ctx_t *proxy, http_res *res, http_status_ctx *ctx, http_status *custom_status, char *err_msg);
int proxy_send(proxy_ctx_t *proxy, sock *client, unsigned long len_to_send, int flags); long proxy_send(proxy_ctx_t *proxy, sock *client, unsigned long len_to_send, int flags);
int proxy_dump(proxy_ctx_t *proxy, char *buf, long len); int proxy_dump(proxy_ctx_t *proxy, char *buf, long len);

View File

@@ -16,7 +16,7 @@ void chunk_handler_func(chunk_ctx_t *ctx) {
logger_set_prefix("[%*s]%s", ADDRSTRLEN, ctx->client->socket.s_addr, ctx->client->log_prefix); logger_set_prefix("[%*s]%s", ADDRSTRLEN, ctx->client->socket.s_addr, ctx->client->log_prefix);
char buf[CHUNK_SIZE]; char buf[CHUNK_SIZE];
long sent = sock_splice_chunked(&ctx->client->socket, ctx->socket, buf, sizeof(buf), ctx->flags | SOCK_SINGLE_CHUNK); const long sent = sock_splice_chunked(&ctx->client->socket, ctx->socket, buf, sizeof(buf), ctx->flags | SOCK_SINGLE_CHUNK);
if (sent < 0) { if (sent < 0) {
// error // error
error("Unable to splice chunk"); error("Unable to splice chunk");
@@ -28,6 +28,7 @@ void chunk_handler_func(chunk_ctx_t *ctx) {
ctx->next_cb(ctx); ctx->next_cb(ctx);
} else { } else {
// next chunk // next chunk
ctx->client->transferred_length += sent;
handle_chunk(ctx); handle_chunk(ctx);
return; return;
} }

View File

@@ -33,7 +33,7 @@ typedef struct {
http_status custom_status; http_status custom_status;
host_config_t *conf; host_config_t *conf;
FILE *file; FILE *file;
long content_length; long content_length, transferred_length;
char *msg_buf, *msg_buf_ptr, msg_content[1024]; char *msg_buf, *msg_buf_ptr, msg_content[1024];
proxy_ctx_t *proxy; proxy_ctx_t *proxy;
void *fcgi_ctx; void *fcgi_ctx;

View File

@@ -138,19 +138,21 @@ static void proxy_chunk_err_cb(chunk_ctx_t *ctx) {
static int proxy_handler_2(client_ctx_t *ctx) { static int proxy_handler_2(client_ctx_t *ctx) {
const char *transfer_encoding = http_get_header_field(&ctx->res.hdr, "Transfer-Encoding"); const char *transfer_encoding = http_get_header_field(&ctx->res.hdr, "Transfer-Encoding");
int chunked = strcontains(transfer_encoding, "chunked"); const int chunked = strcontains(transfer_encoding, "chunked");
const char *content_len = http_get_header_field(&ctx->res.hdr, "Content-Length"); const char *content_len = http_get_header_field(&ctx->res.hdr, "Content-Length");
unsigned long len_to_send = (content_len != NULL) ? strtol(content_len, NULL, 10) : 0; const unsigned long len_to_send = (content_len != NULL) ? strtol(content_len, NULL, 10) : 0;
if (chunked) { if (chunked) {
handle_chunks(ctx, &ctx->proxy->proxy, SOCK_CHUNKED, proxy_chunk_next_cb, proxy_chunk_err_cb); handle_chunks(ctx, &ctx->proxy->proxy, SOCK_CHUNKED, proxy_chunk_next_cb, proxy_chunk_err_cb);
return 1; return 1;
} }
int ret; long ret;
if ((ret = proxy_send(ctx->proxy, &ctx->socket, len_to_send, 0)) == -1) { if ((ret = proxy_send(ctx->proxy, &ctx->socket, len_to_send, 0)) == -1) {
ctx->c_keep_alive = 0; ctx->c_keep_alive = 0;
} else if (ret > 0) {
ctx->transferred_length += ret;
} }
return ret; return ret;

View File

@@ -399,10 +399,15 @@ void request_complete(client_ctx_t *ctx) {
} }
const char *ref = http_get_header_field(&ctx->req.hdr, "Referer"); const char *ref = http_get_header_field(&ctx->req.hdr, "Referer");
const char *ua = http_get_header_field(&ctx->req.hdr, "User-Agent"); const char *ua = http_get_header_field(&ctx->req.hdr, "User-Agent");
const char *loc = http_get_header_field(&ctx->res.hdr, "Location");
const char *type = http_get_header_field(&ctx->res.hdr, "Content-Type");
const long len = ctx->content_length <= 0 ? ctx->transferred_length : ctx->content_length;
fprintf(log, "%s %s %s [%s] \"%s %s HTTP/%s\" %i %li %s%s%s %s%s%s\n", fprintf(log, "%s %s %s [%s] \"%s %s HTTP/%s\" %i %li %s%s%s %s%s%s %s%s%s %s%s%s\n",
ctx->socket.addr, ctx->cc[0] == 0 ? "-" : ctx->cc, user[0] != 0 ? user : "-", buf, ctx->socket.addr, ctx->host[0] == 0 ? "-" : ctx->host, user[0] != 0 ? user : "-", buf,
ctx->req.method, ctx->req.uri, ctx->req.version, ctx->res.status->code, ctx->content_length, ctx->req.method, ctx->req.uri, ctx->req.version, ctx->res.status->code, len,
loc != NULL ? "\"" : "", loc != NULL ? loc : "-", loc != NULL ? "\"" : "",
type != NULL ? "\"" : "", type != NULL ? type : "-", type != NULL ? "\"" : "",
ref != NULL ? "\"" : "", ref != NULL ? ref : "-", ref != NULL ? "\"" : "", ref != NULL ? "\"" : "", ref != NULL ? ref : "-", ref != NULL ? "\"" : "",
ua != NULL ? "\"" : "", ua != NULL ? ua : "-", ua != NULL ? "\"" : ""); ua != NULL ? "\"" : "", ua != NULL ? ua : "-", ua != NULL ? "\"" : "");
fclose(log); fclose(log);