Skip to content

Commit 68b3ab3

Browse files
authoredDec 24, 2021
Merge pull request #55 from e2dk4r/master
fix received and size same issue when using HTTP/2
2 parents afb2d31 + 1ed1825 commit 68b3ab3

File tree

1 file changed

+38
-33
lines changed

1 file changed

+38
-33
lines changed
 

‎ngx_http_uploadprogress_module.c

+38-33
Original file line numberDiff line numberDiff line change
@@ -371,36 +371,32 @@ find_node(ngx_str_t * id, ngx_http_uploadprogress_ctx_t * ctx, ngx_log_t * log)
371371
sentinel = ctx->rbtree->sentinel;
372372

373373
while (node != sentinel) {
374-
375-
if (hash < node->key) {
376-
node = node->left;
377-
continue;
378-
}
379-
380-
if (hash > node->key) {
381-
node = node->right;
374+
if (hash != node->key) {
375+
node = (hash < node->key) ? node->left : node->right;
382376
continue;
383377
}
384378

385379
/* hash == node->key */
380+
up = (ngx_http_uploadprogress_node_t *) node;
386381

387-
do {
388-
up = (ngx_http_uploadprogress_node_t *) node;
389-
390-
rc = ngx_memn2cmp(id->data, up->data, id->len, (size_t) up->len);
391-
392-
if (rc == 0) {
393-
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0,
394-
"upload-progress: found node");
395-
return up;
396-
}
382+
rc = ngx_memn2cmp(id->data, up->data, id->len, up->len);
397383

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

400-
} while (node != sentinel && hash == node->key);
390+
if (rc > 0) {
391+
node = node->right;
392+
continue;
393+
}
401394

402-
/* found a key with unmatching hash (and value), let's keep comparing hashes then */
395+
/* found the hash */
396+
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0, "upload-progress: found node");
397+
return up;
403398
}
399+
404400
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0, "upload-progress: can't find node");
405401
return NULL;
406402
}
@@ -458,6 +454,7 @@ static void ngx_http_uploadprogress_event_handler(ngx_http_request_t *r)
458454
ngx_str_t *id, *oldid;
459455
ngx_slab_pool_t *shpool;
460456
ngx_shm_zone_t *shm_zone;
457+
ngx_http_request_body_t *rb;
461458
ngx_http_uploadprogress_ctx_t *ctx;
462459
ngx_http_uploadprogress_node_t *up;
463460
ngx_http_uploadprogress_conf_t *upcf;
@@ -466,6 +463,8 @@ static void ngx_http_uploadprogress_event_handler(ngx_http_request_t *r)
466463
off_t rest;
467464

468465

466+
rb = r->request_body;
467+
469468
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "upload-progress: ngx_http_uploadprogress_event_handler");
470469

471470
/* find node, update rest */
@@ -520,15 +519,25 @@ static void ngx_http_uploadprogress_event_handler(ngx_http_request_t *r)
520519
if (up != NULL && !up->done) {
521520
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
522521
"upload-progress: read_event_handler found node: %V", id);
523-
rest = r->request_body->rest;
524-
size = r->request_body->buf->last - r->request_body->buf->pos;
525-
if ((off_t) size < rest) {
526-
rest -= size;
527-
} else {
528-
rest = 0;
522+
523+
#if (NGX_HTTP_V2)
524+
if (r->http_connection->addr_conf->http2) { /* http/2 */
525+
up->rest = up->length - r->request_length;
526+
} else { /* http/1 */
527+
#endif
528+
rest = rb->rest;
529+
size = rb->buf->last - rb->buf->pos;
530+
if ((off_t) size < rest) {
531+
rest -= size;
532+
} else {
533+
rest = 0;
534+
}
535+
up->rest = rest;
536+
537+
#if (NGX_HTTP_V2)
529538
}
539+
#endif
530540

531-
up->rest = rest;
532541
if(up->length == 0)
533542
up->length = r->headers_in.content_length_n;
534543
ngx_log_debug3(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
@@ -1091,11 +1100,7 @@ ngx_http_uploadprogress_init_zone(ngx_shm_zone_t * shm_zone, void *data)
10911100
return NGX_ERROR;
10921101
}
10931102

1094-
ngx_rbtree_sentinel_init(sentinel);
1095-
1096-
ctx->rbtree->root = sentinel;
1097-
ctx->rbtree->sentinel = sentinel;
1098-
ctx->rbtree->insert = ngx_http_uploadprogress_rbtree_insert_value;
1103+
ngx_rbtree_init(ctx->rbtree, sentinel, ngx_http_uploadprogress_rbtree_insert_value);
10991104

11001105
return NGX_OK;
11011106
}

0 commit comments

Comments
 (0)
Please sign in to comment.