From 2ba20e3451ca5f31ad7a9811081b85d92472082c Mon Sep 17 00:00:00 2001 From: Roman Arutyunyan Date: Thu, 13 Jan 2022 11:34:42 +0300 Subject: [PATCH] QUIC: return written size from ngx_quic_write_chain(). This allows to escape calculating it before calling the function. --- src/event/quic/ngx_event_quic_frames.c | 10 +++++++++- src/event/quic/ngx_event_quic_frames.h | 2 +- src/event/quic/ngx_event_quic_ssl.c | 2 +- src/event/quic/ngx_event_quic_streams.c | 19 +++++-------------- 4 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/event/quic/ngx_event_quic_frames.c b/src/event/quic/ngx_event_quic_frames.c index 89bd6d236..55e58d329 100644 --- a/src/event/quic/ngx_event_quic_frames.c +++ b/src/event/quic/ngx_event_quic_frames.c @@ -478,13 +478,17 @@ ngx_quic_copy_buf(ngx_connection_t *c, u_char *data, size_t len) ngx_chain_t * ngx_quic_write_chain(ngx_connection_t *c, ngx_chain_t **chain, ngx_chain_t *in, - off_t limit, off_t offset) + off_t limit, off_t offset, size_t *size) { off_t n; u_char *p; ngx_buf_t *b; ngx_chain_t *cl, *sl; + if (size) { + *size = 0; + } + while (in && limit) { cl = *chain; @@ -549,6 +553,10 @@ ngx_quic_write_chain(ngx_connection_t *c, ngx_chain_t **chain, ngx_chain_t *in, in->buf->pos += n; offset += n; limit -= n; + + if (size) { + *size += n; + } } if (b->sync && p == b->last) { diff --git a/src/event/quic/ngx_event_quic_frames.h b/src/event/quic/ngx_event_quic_frames.h index 45505601f..b06575d4e 100644 --- a/src/event/quic/ngx_event_quic_frames.h +++ b/src/event/quic/ngx_event_quic_frames.h @@ -31,7 +31,7 @@ void ngx_quic_free_chain(ngx_connection_t *c, ngx_chain_t *in); ngx_chain_t *ngx_quic_read_chain(ngx_connection_t *c, ngx_chain_t **chain, off_t limit); ngx_chain_t *ngx_quic_write_chain(ngx_connection_t *c, ngx_chain_t **chain, - ngx_chain_t *in, off_t limit, off_t offset); + ngx_chain_t *in, off_t limit, off_t offset, size_t *size); #if (NGX_DEBUG) void ngx_quic_log_frame(ngx_log_t *log, ngx_quic_frame_t *f, ngx_uint_t tx); diff --git a/src/event/quic/ngx_event_quic_ssl.c b/src/event/quic/ngx_event_quic_ssl.c index 5cf579cb1..e5e3ffcab 100644 --- a/src/event/quic/ngx_event_quic_ssl.c +++ b/src/event/quic/ngx_event_quic_ssl.c @@ -370,7 +370,7 @@ ngx_quic_handle_crypto_frame(ngx_connection_t *c, ngx_quic_header_t *pkt, if (f->offset > ctx->crypto_received) { if (ngx_quic_write_chain(c, &ctx->crypto, frame->data, f->length, - f->offset - ctx->crypto_received) + f->offset - ctx->crypto_received, NULL) == NGX_CHAIN_ERROR) { return NGX_ERROR; diff --git a/src/event/quic/ngx_event_quic_streams.c b/src/event/quic/ngx_event_quic_streams.c index 6f6ab5f9e..5863265a7 100644 --- a/src/event/quic/ngx_event_quic_streams.c +++ b/src/event/quic/ngx_event_quic_streams.c @@ -824,9 +824,10 @@ ngx_quic_stream_send(ngx_connection_t *c, u_char *buf, size_t size) static ngx_chain_t * ngx_quic_stream_send_chain(ngx_connection_t *c, ngx_chain_t *in, off_t limit) { - off_t n, flow; + off_t flow; + size_t n; ngx_event_t *wev; - ngx_chain_t *out, *cl; + ngx_chain_t *out; ngx_connection_t *pc; ngx_quic_frame_t *frame; ngx_quic_stream_t *qs; @@ -851,17 +852,7 @@ ngx_quic_stream_send_chain(ngx_connection_t *c, ngx_chain_t *in, off_t limit) limit = flow; } - n = 0; - - for (cl = in; cl; cl = cl->next) { - n += cl->buf->last - cl->buf->pos; - if (n >= limit) { - n = limit; - break; - } - } - - in = ngx_quic_write_chain(pc, &qs->out, in, limit, 0); + in = ngx_quic_write_chain(pc, &qs->out, in, limit, 0, &n); if (in == NGX_CHAIN_ERROR) { return NGX_CHAIN_ERROR; } @@ -1099,7 +1090,7 @@ ngx_quic_handle_stream_frame(ngx_connection_t *c, ngx_quic_header_t *pkt, } if (ngx_quic_write_chain(c, &qs->in, frame->data, f->length, - f->offset - qs->recv_offset) + f->offset - qs->recv_offset, NULL) == NGX_CHAIN_ERROR) { return NGX_ERROR;