Created
June 14, 2025 03:45
-
-
Save ammarfaizi2/bfb2660b4fbf4686a870de954cb51e0b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/http_logger_old.c b/http_logger.c | |
index 7d75a0ee5a2f..a60c4b98c9f7 100644 | |
--- a/http_logger_old.c | |
+++ b/http_logger.c | |
@@ -1687,6 +1687,7 @@ struct buf { | |
uint64_t cap; | |
uint64_t len; | |
char *buf; | |
+ char *orig_buf; | |
}; | |
struct http_res { | |
@@ -1783,8 +1784,53 @@ static void http_req_free_all(struct http_req *req) | |
} | |
} | |
+static void buf_free(struct buf *b) | |
+{ | |
+ if (!b->orig_buf) | |
+ return; | |
+ | |
+ free(b->orig_buf); | |
+ b->orig_buf = b->buf = NULL; | |
+ b->cap = 0; | |
+ b->len = 0; | |
+} | |
+ | |
+static void buf_sync(struct buf *b) | |
+{ | |
+ if (b->buf == b->orig_buf) | |
+ return; | |
+ | |
+ if (!b->len) { | |
+ buf_free(b); | |
+ return; | |
+ } | |
+ | |
+ memmove(b->orig_buf, b->buf, b->len); | |
+ b->buf = b->orig_buf; | |
+} | |
+ | |
+__attribute__((__unused__)) | |
+static void buf_advance(struct buf *b, size_t len) | |
+{ | |
+ if (len >= b->len) { | |
+ buf_free(b); | |
+ return; | |
+ } | |
+ | |
+ b->len -= len; | |
+ memmove(b->buf, &b->buf[len], b->len); | |
+} | |
+ | |
+static void buf_soft_advance(struct buf *b, size_t len) | |
+{ | |
+ b->buf += len; | |
+ b->len -= len; | |
+} | |
+ | |
static int buf_append(struct buf *b, const char *data, size_t len) | |
{ | |
+ buf_sync(b); | |
+ | |
if (b->len + len > b->cap) { | |
size_t new_cap = b->cap ? b->cap * 2 : 1024; | |
char *new_buf; | |
@@ -1796,7 +1842,7 @@ static int buf_append(struct buf *b, const char *data, size_t len) | |
if (!new_buf) | |
return -ENOMEM; | |
- b->buf = new_buf; | |
+ b->orig_buf = b->buf = new_buf; | |
b->cap = new_cap; | |
} | |
@@ -1805,28 +1851,6 @@ static int buf_append(struct buf *b, const char *data, size_t len) | |
return 0; | |
} | |
-static void buf_free(struct buf *b) | |
-{ | |
- if (!b->buf) | |
- return; | |
- | |
- free(b->buf); | |
- b->buf = NULL; | |
- b->cap = 0; | |
- b->len = 0; | |
-} | |
- | |
-static void buf_advance(struct buf *b, size_t len) | |
-{ | |
- if (len >= b->len) { | |
- buf_free(b); | |
- return; | |
- } | |
- | |
- b->len -= len; | |
- memmove(b->buf, &b->buf[len], b->len); | |
-} | |
- | |
static int sock_slots_init(struct sock_slots *ss) | |
{ | |
int r; | |
@@ -2135,10 +2159,10 @@ static int consume_body(struct gwnet_http_body_pctx *c, struct buf *b, | |
c->max_len = MAX_BODY_SIZE; | |
r = gwnet_http_body_parse_chunked(c, NULL, 0); | |
if (c->off) | |
- buf_advance(b, c->off); | |
+ buf_soft_advance(b, c->off); | |
} else { | |
uint64_t sub = min_st(*con_len, b->len); | |
- buf_advance(b, sub); | |
+ buf_soft_advance(b, sub); | |
*con_len -= sub; | |
r = (*con_len == 0) ? 0 : -EAGAIN; | |
} | |
@@ -2161,7 +2185,7 @@ static int handle_tx_hdr(struct sock *sk) | |
p->max_len = MAX_HEADER_SIZE; | |
r = gwnet_http_req_hdr_parse(p, &req->hdr); | |
if (p->off) | |
- buf_advance(b, p->off); | |
+ buf_soft_advance(b, p->off); | |
if (r) | |
return r; | |
req->con_len = probe_body_len(ff, &req->is_chunked, &inv); | |
@@ -2200,7 +2224,7 @@ static int handle_rx_hdr(struct sock *sk) | |
p->max_len = MAX_HEADER_SIZE; | |
r = gwnet_http_res_hdr_parse(p, &res->hdr); | |
if (p->off) | |
- buf_advance(b, p->off); | |
+ buf_soft_advance(b, p->off); | |
if (r < 0) | |
return r; | |
res->con_len = probe_body_len(ff, &res->is_chunked, &inv); | |
@@ -2415,6 +2439,7 @@ static void trace_recv(int fd, const void *buf, size_t len) | |
if (r && r != -EAGAIN) | |
goto out_del; | |
+ buf_sync(&sk->rx_buf); | |
return; | |
out_del: | |
@@ -2441,6 +2466,7 @@ static void trace_send(int fd, const void *buf, size_t len) | |
if (r && r != -EAGAIN) | |
goto out_del; | |
+ buf_sync(&sk->tx_buf); | |
sock_put(&tctx.ss, sk); | |
return; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment