diff --git a/src/lib/proxy.c b/src/lib/proxy.c index ae24938..f213328 100644 --- a/src/lib/proxy.c +++ b/src/lib/proxy.c @@ -656,11 +656,12 @@ int proxy_peek_response(proxy_ctx_t *proxy, http_res *res, http_status_ctx *ctx, 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]; - 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 0; + return ret; } int proxy_dump(proxy_ctx_t *proxy, char *buf, long len) { diff --git a/src/lib/proxy.h b/src/lib/proxy.h index f66b7df..1b177af 100644 --- a/src/lib/proxy.h +++ b/src/lib/proxy.h @@ -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_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); diff --git a/src/worker/chunk_handler.c b/src/worker/chunk_handler.c index 44820b2..8d70fde 100644 --- a/src/worker/chunk_handler.c +++ b/src/worker/chunk_handler.c @@ -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); 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) { // error error("Unable to splice chunk"); @@ -28,6 +28,7 @@ void chunk_handler_func(chunk_ctx_t *ctx) { ctx->next_cb(ctx); } else { // next chunk + ctx->client->transferred_length += sent; handle_chunk(ctx); return; } diff --git a/src/worker/func.h b/src/worker/func.h index f8ef533..d268e8a 100644 --- a/src/worker/func.h +++ b/src/worker/func.h @@ -33,7 +33,7 @@ typedef struct { http_status custom_status; host_config_t *conf; FILE *file; - long content_length; + long content_length, transferred_length; char *msg_buf, *msg_buf_ptr, msg_content[1024]; proxy_ctx_t *proxy; void *fcgi_ctx; diff --git a/src/worker/proxy_handler.c b/src/worker/proxy_handler.c index 1a7b571..d7a8f49 100644 --- a/src/worker/proxy_handler.c +++ b/src/worker/proxy_handler.c @@ -138,19 +138,21 @@ static void proxy_chunk_err_cb(chunk_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"); - 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"); - 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) { handle_chunks(ctx, &ctx->proxy->proxy, SOCK_CHUNKED, proxy_chunk_next_cb, proxy_chunk_err_cb); return 1; } - int ret; + long ret; if ((ret = proxy_send(ctx->proxy, &ctx->socket, len_to_send, 0)) == -1) { ctx->c_keep_alive = 0; + } else if (ret > 0) { + ctx->transferred_length += ret; } return ret; diff --git a/src/worker/request_handler.c b/src/worker/request_handler.c index 2ea475c..29203f8 100644 --- a/src/worker/request_handler.c +++ b/src/worker/request_handler.c @@ -399,10 +399,15 @@ void request_complete(client_ctx_t *ctx) { } 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 *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", - 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, + 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->host[0] == 0 ? "-" : ctx->host, user[0] != 0 ? user : "-", buf, + 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 ? "\"" : "", ua != NULL ? "\"" : "", ua != NULL ? ua : "-", ua != NULL ? "\"" : ""); fclose(log);