Skip to content

Commit

Permalink
fix received and size same issue when using HTTP/2
Browse files Browse the repository at this point in the history
bug mentioned at masterzen#45
  • Loading branch information
e2dk4r committed Dec 5, 2021
1 parent afb2d31 commit 1ed1825
Showing 1 changed file with 38 additions and 33 deletions.
71 changes: 38 additions & 33 deletions ngx_http_uploadprogress_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -371,36 +371,32 @@ find_node(ngx_str_t * id, ngx_http_uploadprogress_ctx_t * ctx, ngx_log_t * log)
sentinel = ctx->rbtree->sentinel;

while (node != sentinel) {

if (hash < node->key) {
node = node->left;
continue;
}

if (hash > node->key) {
node = node->right;
if (hash != node->key) {
node = (hash < node->key) ? node->left : node->right;
continue;
}

/* hash == node->key */
up = (ngx_http_uploadprogress_node_t *) node;

do {
up = (ngx_http_uploadprogress_node_t *) node;

rc = ngx_memn2cmp(id->data, up->data, id->len, (size_t) up->len);

if (rc == 0) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0,
"upload-progress: found node");
return up;
}
rc = ngx_memn2cmp(id->data, up->data, id->len, up->len);

node = (rc < 0) ? node->left : node->right;
/* found a key with unmatching hash (and value), let's keep comparing hashes then */
if (rc < 0) {
node = node->left;
continue;
}

} while (node != sentinel && hash == node->key);
if (rc > 0) {
node = node->right;
continue;
}

/* found a key with unmatching hash (and value), let's keep comparing hashes then */
/* found the hash */
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0, "upload-progress: found node");
return up;
}

ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0, "upload-progress: can't find node");
return NULL;
}
Expand Down Expand Up @@ -458,6 +454,7 @@ static void ngx_http_uploadprogress_event_handler(ngx_http_request_t *r)
ngx_str_t *id, *oldid;
ngx_slab_pool_t *shpool;
ngx_shm_zone_t *shm_zone;
ngx_http_request_body_t *rb;
ngx_http_uploadprogress_ctx_t *ctx;
ngx_http_uploadprogress_node_t *up;
ngx_http_uploadprogress_conf_t *upcf;
Expand All @@ -466,6 +463,8 @@ static void ngx_http_uploadprogress_event_handler(ngx_http_request_t *r)
off_t rest;


rb = r->request_body;

ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "upload-progress: ngx_http_uploadprogress_event_handler");

/* find node, update rest */
Expand Down Expand Up @@ -520,15 +519,25 @@ static void ngx_http_uploadprogress_event_handler(ngx_http_request_t *r)
if (up != NULL && !up->done) {
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
"upload-progress: read_event_handler found node: %V", id);
rest = r->request_body->rest;
size = r->request_body->buf->last - r->request_body->buf->pos;
if ((off_t) size < rest) {
rest -= size;
} else {
rest = 0;

#if (NGX_HTTP_V2)
if (r->http_connection->addr_conf->http2) { /* http/2 */
up->rest = up->length - r->request_length;
} else { /* http/1 */
#endif
rest = rb->rest;
size = rb->buf->last - rb->buf->pos;
if ((off_t) size < rest) {
rest -= size;
} else {
rest = 0;
}
up->rest = rest;

#if (NGX_HTTP_V2)
}
#endif

up->rest = rest;
if(up->length == 0)
up->length = r->headers_in.content_length_n;
ngx_log_debug3(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
Expand Down Expand Up @@ -1091,11 +1100,7 @@ ngx_http_uploadprogress_init_zone(ngx_shm_zone_t * shm_zone, void *data)
return NGX_ERROR;
}

ngx_rbtree_sentinel_init(sentinel);

ctx->rbtree->root = sentinel;
ctx->rbtree->sentinel = sentinel;
ctx->rbtree->insert = ngx_http_uploadprogress_rbtree_insert_value;
ngx_rbtree_init(ctx->rbtree, sentinel, ngx_http_uploadprogress_rbtree_insert_value);

return NGX_OK;
}
Expand Down

0 comments on commit 1ed1825

Please sign in to comment.