Переглянути джерело

support for progress status for multiple simultaneous uploads

Co-authored-by: devgs <devgs@ukr.net>
Co-authored-by: Benny Baumann <BenBE@geshi.org>
pull/62/head
devgs 13 роки тому
джерело
коміт
904343287c
1 змінених файлів з 497 додано та 75 видалено
  1. 497
    75
      ngx_http_uploadprogress_module.c

+ 497
- 75
ngx_http_uploadprogress_module.c Переглянути файл

@@ -65,7 +65,10 @@ typedef struct {
65 65
     ngx_str_t                        content_type;
66 66
     ngx_array_t                      templates;
67 67
     ngx_str_t                        header;
68
+    ngx_str_t                        header_mul;
69
+
68 70
     ngx_str_t                        jsonp_parameter;
71
+    ngx_uint_t                       json_multiple;
69 72
 } ngx_http_uploadprogress_conf_t;
70 73
 
71 74
 typedef struct {
@@ -87,6 +90,8 @@ static ngx_int_t ngx_http_uploadprogress_offset_variable(ngx_http_request_t *r,
87 90
     ngx_http_variable_value_t *v, uintptr_t data);
88 91
 static ngx_int_t ngx_http_uploadprogress_status_variable(ngx_http_request_t *r,
89 92
     ngx_http_variable_value_t *v, uintptr_t data);
93
+static ngx_int_t ngx_http_uploadprogress_id_variable(ngx_http_request_t *r,
94
+    ngx_http_variable_value_t *v, uintptr_t data);
90 95
 static ngx_int_t ngx_http_uploadprogress_callback_variable(ngx_http_request_t *r,
91 96
     ngx_http_variable_value_t *v, uintptr_t data);
92 97
 static char* ngx_http_upload_progress_set_template(ngx_conf_t * cf, ngx_http_uploadprogress_template_t *t, ngx_str_t *source);
@@ -97,6 +102,8 @@ static char* ngx_http_upload_progress_template(ngx_conf_t * cf, ngx_command_t *
97 102
 static char* ngx_http_upload_progress_java_output(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
98 103
 static char* ngx_http_upload_progress_json_output(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
99 104
 static char* ngx_http_upload_progress_jsonp_output(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
105
+static char* ngx_http_upload_progress_json_multiple_output(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
106
+static char* ngx_http_upload_progress_jsonp_multiple_output(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
100 107
 static void ngx_clean_old_connections(ngx_event_t * ev);
101 108
 static ngx_int_t ngx_http_uploadprogress_content_handler(ngx_http_request_t *r);
102 109
 
@@ -160,6 +167,20 @@ static ngx_command_t ngx_http_uploadprogress_commands[] = {
160 167
      0,
161 168
      NULL},
162 169
 
170
+    {ngx_string("upload_progress_json_multiple_output"),
171
+     NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_NOARGS,
172
+     ngx_http_upload_progress_json_multiple_output,
173
+     NGX_HTTP_LOC_CONF_OFFSET,
174
+     0,
175
+     NULL},
176
+
177
+    {ngx_string("upload_progress_jsonp_multiple_output"),
178
+     NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_NOARGS,
179
+     ngx_http_upload_progress_jsonp_multiple_output,
180
+     NGX_HTTP_LOC_CONF_OFFSET,
181
+     0,
182
+     NULL},
183
+
163 184
     {ngx_string("upload_progress_header"),
164 185
      NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
165 186
      ngx_conf_set_str_slot,
@@ -167,6 +188,13 @@ static ngx_command_t ngx_http_uploadprogress_commands[] = {
167 188
      offsetof(ngx_http_uploadprogress_conf_t, header),
168 189
      NULL},
169 190
 
191
+    {ngx_string("upload_progress_header_mul"),
192
+     NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
193
+     ngx_conf_set_str_slot,
194
+     NGX_HTTP_LOC_CONF_OFFSET,
195
+     offsetof(ngx_http_uploadprogress_conf_t, header_mul),
196
+     NULL},
197
+
170 198
     {ngx_string("upload_progress_jsonp_parameter"),
171 199
      NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
172 200
      ngx_conf_set_str_slot,
@@ -195,6 +223,10 @@ static ngx_http_variable_t  ngx_http_uploadprogress_variables[] = {
195 223
       (uintptr_t) offsetof(ngx_http_uploadprogress_node_t, err_status),
196 224
       NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
197 225
 
226
+    { ngx_string("uploadprogress_id"), NULL, ngx_http_uploadprogress_id_variable,
227
+      (uintptr_t) offsetof(ngx_http_uploadprogress_node_t, err_status),
228
+      NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
229
+
198 230
     { ngx_string("uploadprogress_callback"), NULL, ngx_http_uploadprogress_callback_variable,
199 231
       (uintptr_t) NULL,
200 232
       NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
@@ -261,6 +293,20 @@ static ngx_str_t ngx_http_uploadprogress_jsonp_defaults[] = {
261 293
     ngx_string("$uploadprogress_callback({ \"state\" : \"uploading\", \"received\" : $uploadprogress_received, \"size\" : $uploadprogress_length });\r\n")
262 294
 };
263 295
 
296
+static ngx_str_t ngx_http_uploadprogress_json_multiple_defaults[] = {
297
+    ngx_string("{ \"id\" : $uploadprogress_id, \"state\" : \"starting\" }"),
298
+    ngx_string("{ \"id\" : $uploadprogress_id, \"state\" : \"error\", \"status\" : $uploadprogress_status }"),
299
+    ngx_string("{ \"id\" : $uploadprogress_id, \"state\" : \"done\" }"),
300
+    ngx_string("{ \"id\" : $uploadprogress_id, \"state\" : \"uploading\", \"received\" : $uploadprogress_received, \"size\" : $uploadprogress_length }")
301
+};
302
+
303
+static ngx_str_t ngx_http_uploadprogress_jsonp_multiple_defaults[] = {
304
+    ngx_string("$uploadprogress_callback({ \"id\" : $uploadprogress_id, \"state\" : \"starting\" });\r\n"),
305
+    ngx_string("$uploadprogress_callback({ \"id\" : $uploadprogress_id, \"state\" : \"error\", \"status\" : $uploadprogress_status });\r\n"),
306
+    ngx_string("$uploadprogress_callback({ \"id\" : $uploadprogress_id, \"state\" : \"done\" });\r\n"),
307
+    ngx_string("$uploadprogress_callback({ \"id\" : $uploadprogress_id, \"state\" : \"uploading\", \"received\" : $uploadprogress_received, \"size\" : $uploadprogress_length });\r\n")
308
+};
309
+
264 310
 
265 311
 static ngx_array_t ngx_http_uploadprogress_global_templates;
266 312
 
@@ -330,7 +376,7 @@ get_tracking_id(ngx_http_request_t * r)
330 376
                 i = 1;
331 377
                 break;
332 378
             }
333
-            if (len<=0) {
379
+            else if (!len) {
334 380
                 break;
335 381
             }
336 382
         } while (p++);
@@ -357,6 +403,99 @@ get_tracking_id(ngx_http_request_t * r)
357 403
     return NULL;
358 404
 }
359 405
 
406
+static ngx_str_t*
407
+get_tracking_ids_mul(ngx_http_request_t * r)
408
+{
409
+    u_char                          *p, *start_p;
410
+    ngx_uint_t                       i;
411
+    ngx_list_part_t                 *part;
412
+    ngx_table_elt_t                 *header;
413
+    ngx_str_t                       *ret, args;
414
+    ngx_http_uploadprogress_conf_t  *upcf;
415
+
416
+    upcf = ngx_http_get_module_loc_conf(r, ngx_http_uploadprogress_module);
417
+
418
+    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "upload-progress: get_tracking_ids");
419
+
420
+    part = &r->headers_in.headers.part;
421
+    header = part->elts;
422
+
423
+    for (i = 0; /* void */; i++) {
424
+
425
+        if (i >= part->nelts) {
426
+            if (part->next == NULL) {
427
+                break;
428
+            }
429
+
430
+            part = part->next;
431
+            header = part->elts;
432
+            i = 0;
433
+        }
434
+
435
+        if (
436
+            header[i].key.len == upcf->header_mul.len &&
437
+            ngx_strncasecmp(header[i].key.data, upcf->header_mul.data, header[i].key.len) == 0
438
+        ) {
439
+            ret = ngx_calloc(sizeof(ngx_str_t), r->connection->log);
440
+            *ret = header[i].value;
441
+            ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
442
+                "upload-progress: get_tracking_ids found header: %V", ret);
443
+            return ret;
444
+        }
445
+    }
446
+
447
+    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
448
+        "upload-progress: get_tracking_ids no header found");
449
+
450
+    /* not found, check as a request arg */
451
+    /* it is possible the request args have not been yet created (or already released) */
452
+    /* so let's try harder first from the request line */
453
+    args = r->args;
454
+
455
+    if (args.len && args.data) {
456
+        ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
457
+            "upload-progress: get_tracking_id no header found, args found");
458
+        i = 0;
459
+        p = args.data;
460
+        do {
461
+            ngx_uint_t len = args.len - (p - args.data);
462
+            if (
463
+                len >= (upcf->header_mul.len + 1) &&
464
+                ngx_strncasecmp(p, upcf->header_mul.data, upcf->header_mul.len) == 0 &&
465
+                p[upcf->header_mul.len] == '='
466
+            ) {
467
+                ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
468
+                    "upload-progress: get_tracking_id found args: %s",p);
469
+                i = 1;
470
+                break;
471
+            }
472
+            else if (!len) {
473
+                break;
474
+            }
475
+        } while (p++);
476
+
477
+        if (i) {
478
+            start_p = p += upcf->header_mul.len + 1;
479
+            while (p < args.data + args.len) {
480
+                if (*(++p) == '&') {
481
+                    break;
482
+                }
483
+            }
484
+
485
+            ret = ngx_calloc(sizeof(ngx_str_t), r->connection->log);
486
+            ret->data = start_p;
487
+            ret->len = p - start_p;
488
+            ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
489
+                "upload-progress: get_tracking_id found args: %V",ret);
490
+            return ret;
491
+        }
492
+    }
493
+
494
+    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
495
+        "upload-progress: get_tracking_id no id found");
496
+    return NULL;
497
+}
498
+
360 499
 static ngx_http_uploadprogress_node_t *
361 500
 find_node(ngx_str_t * id, ngx_http_uploadprogress_ctx_t * ctx, ngx_log_t * log)
362 501
 {
@@ -583,8 +722,7 @@ ngx_http_reportuploads_handler(ngx_http_request_t * r)
583 722
     }
584 723
 
585 724
     /* get the tracking id if any */
586
-    id = get_tracking_id(r);
587
-
725
+    id = upcf->json_multiple ? get_tracking_ids_mul(r) : get_tracking_id(r);
588 726
 
589 727
     if (id == NULL) {
590 728
         ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
@@ -604,37 +742,9 @@ ngx_http_reportuploads_handler(ngx_http_request_t * r)
604 742
         return NGX_DECLINED;
605 743
     }
606 744
 
607
-    ctx = upcf->shm_zone->data;
608
-
609
-    /* get the original connection of the upload */
610
-    shpool = (ngx_slab_pool_t *) upcf->shm_zone->shm.addr;
611
-
612
-    ngx_shmtx_lock(&shpool->mutex);
613
-
614
-    up = find_node(id, ctx, r->connection->log);
615
-    if (up != NULL) {
616
-        ngx_log_debug5(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
617
-                       "reportuploads found node: %V (rest: %uO, length: %uO, done: %ui, err_status: %ui)", id, up->rest, up->length, up->done, up->err_status);
618
-        rest = up->rest;
619
-        length = up->length;
620
-        done = up->done;
621
-        err_status = up->err_status;
622
-        found = 1;
623
-    } else {
624
-        ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
625
-                       "reportuploads not found: %V", id);
626
-    }
627
-    ngx_shmtx_unlock(&shpool->mutex);
628
-	ngx_free(id);
629
-
630
-    /* send the output */
631
-    r->headers_out.content_type = upcf->content_type;
632
-
633 745
     /* force no-cache */
634 746
     expires = r->headers_out.expires;
635
-
636 747
     if (expires == NULL) {
637
-
638 748
         expires = ngx_list_push(&r->headers_out.headers);
639 749
         if (expires == NULL) {
640 750
             return NGX_HTTP_INTERNAL_SERVER_ERROR;
@@ -683,62 +793,289 @@ ngx_http_reportuploads_handler(ngx_http_request_t * r)
683 793
         }
684 794
     }
685 795
 
686
-    ngx_http_set_ctx(r, up, ngx_http_uploadprogress_module);
796
+    ctx = upcf->shm_zone->data;
687 797
 
688
-    /*
689
-     * There are 4 possibilities
690
-     *  * request not yet started: found = false
691
-     *  * request in error:        err_status >= NGX_HTTP_BAD_REQUEST
692
-     *  * request finished:        done = true
693
-     *  * request not yet started but registered:        length==0 && rest ==0
694
-     *  * reauest in progress:     rest > 0
695
-     */
798
+    /* get the original connection of the upload */
799
+    shpool = (ngx_slab_pool_t *) upcf->shm_zone->shm.addr;
696 800
 
697
-    if (!found) {
698
-        state = uploadprogress_state_starting;
699
-    } else if (err_status >= NGX_HTTP_BAD_REQUEST) {
700
-        state = uploadprogress_state_error;
701
-    } else if (done) {
702
-        state = uploadprogress_state_done;
703
-    } else if ( length == 0 && rest == 0 ) {
704
-        state = uploadprogress_state_starting;
705
-    } else {
706
-        state = uploadprogress_state_uploading;
707
-    }
801
+    if (upcf->json_multiple) {
802
+        ngx_chain_t * p_chain_end = 0;
803
+        ngx_chain_t * p_chain_start = 0;
804
+        size_t offs = 0;
805
+        u_char * p1 = id->data, * p2;
806
+        r->headers_out.content_length_n = 0;
807
+        while (offs < id->len) {
808
+            p2 = memchr((char *)id->data + offs, ';', id->len - offs);
809
+            if (!p2) {
810
+                p2 = id->data + id->len;
811
+            }
812
+            size_t len = p2 - p1;
813
+            if (len) {
814
+                ngx_str_t sub_id;
815
+                sub_id.data = p1;
816
+                sub_id.len = len;
817
+
818
+                // ---->
819
+
820
+                ngx_shmtx_lock(&shpool->mutex);
821
+
822
+                up = find_node(&sub_id, ctx, r->connection->log);
823
+                if (up != NULL) {
824
+                    ngx_log_debug5(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
825
+                        "reportuploads found node: %V (rest: %uO, length: %uO, done: %ui, err_status: %ui)",
826
+                        &sub_id, up->rest, up->length, up->done, up->err_status);
827
+
828
+                    rest = up->rest;
829
+                    length = up->length;
830
+                    done = up->done;
831
+                    err_status = up->err_status;
832
+                    found = 1;
833
+                } else {
834
+                    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
835
+                        "reportuploads not found: %V", &sub_id);
836
+                }
837
+                ngx_shmtx_unlock(&shpool->mutex);
838
+
839
+                /* send the output */
840
+                r->headers_out.content_type = upcf->content_type;
841
+
842
+                if (up == NULL) {
843
+                    // For current id
844
+                    ngx_http_uploadprogress_node_t * tmp_node = ngx_pcalloc(r->pool, sizeof(ngx_http_uploadprogress_node_t) + sub_id.len);
845
+                    tmp_node->len = sub_id.len;
846
+                    ngx_memcpy(tmp_node->data, sub_id.data, sub_id.len);
847
+                    ngx_http_set_ctx(r, tmp_node, ngx_http_uploadprogress_module);
848
+                } else {
849
+                    ngx_http_set_ctx(r, up, ngx_http_uploadprogress_module);
850
+                }
708 851
 
709
-    t = upcf->templates.elts;
852
+                if (!found) {
853
+                    state = uploadprogress_state_starting;
854
+                } else if (err_status >= NGX_HTTP_BAD_REQUEST) {
855
+                    state = uploadprogress_state_error;
856
+                } else if (done) {
857
+                    state = uploadprogress_state_done;
858
+                } else if ( length == 0 && rest == 0 ) {
859
+                    state = uploadprogress_state_starting;
860
+                } else {
861
+                    state = uploadprogress_state_uploading;
862
+                }
710 863
 
711
-    if (
712
-        ngx_http_script_run(
713
-            r, &response, t[(ngx_uint_t)state].lengths->elts, 0,
714
-            t[(ngx_uint_t)state].values->elts
715
-        ) == NULL
716
-    ) {
717
-        return NGX_HTTP_INTERNAL_SERVER_ERROR;
718
-    }
864
+                t = upcf->templates.elts;
719 865
 
720
-    ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
721
-        "upload progress: state=%d, err_status=%ui, remaining=%uO, length=%uO",
722
-        state, err_status, (length - rest), length);
866
+                if (
867
+                    ngx_http_script_run(
868
+                        r, &response, t[(ngx_uint_t)state].lengths->elts, 0,
869
+                        t[(ngx_uint_t)state].values->elts
870
+                    ) == NULL
871
+                ) {
872
+                    ngx_free(id);
873
+                    return NGX_HTTP_INTERNAL_SERVER_ERROR;
874
+                }
723 875
 
724
-    b = ngx_calloc_buf(r->pool);
725
-    if (b == NULL) {
726
-        return NGX_HTTP_INTERNAL_SERVER_ERROR;
876
+                ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
877
+                    "upload progress: state=%d, err_status=%ui, remaining=%uO, length=%uO",
878
+                    state, err_status, (length - rest), length);
879
+
880
+                if (p_chain_end) {
881
+                    p_chain_end->next = ngx_palloc(r->pool, sizeof(ngx_chain_t));
882
+                    if (p_chain_end->next == NULL) {
883
+                        ngx_free(id);
884
+                        return NGX_HTTP_INTERNAL_SERVER_ERROR;
885
+                    }
886
+                    p_chain_end = p_chain_end->next;
887
+
888
+                    // Insert comma
889
+                    b = ngx_calloc_buf(r->pool);
890
+                    if (b == NULL) {
891
+                        ngx_free(id);
892
+                        return NGX_HTTP_INTERNAL_SERVER_ERROR;
893
+                    }
894
+
895
+                    b->pos = b->start = ngx_palloc(r->pool, 2);
896
+                    if (b->pos == NULL) {
897
+                        ngx_free(id);
898
+                        return NGX_HTTP_INTERNAL_SERVER_ERROR;
899
+                    }
900
+                    b->last = b->end = b->pos + 2;
901
+                    ngx_memcpy(b->pos, ", ", 2);
902
+                    b->temporary = 1;
903
+                    b->memory = 1;
904
+
905
+                    p_chain_end->buf = b;
906
+                    p_chain_end->next = ngx_palloc(r->pool, sizeof(ngx_chain_t));
907
+                    if (p_chain_end->next == NULL) {
908
+                        ngx_free(id);
909
+                        return NGX_HTTP_INTERNAL_SERVER_ERROR;
910
+                    }
911
+                    p_chain_end = p_chain_end->next;
912
+                }
913
+                else
914
+                {
915
+                    p_chain_start = p_chain_end = ngx_palloc(r->pool, sizeof(ngx_chain_t));
916
+                }
917
+
918
+                b = ngx_calloc_buf(r->pool);
919
+                if (b == NULL) {
920
+                    ngx_free(id);
921
+                    return NGX_HTTP_INTERNAL_SERVER_ERROR;
922
+                }
923
+
924
+                b->pos = b->start = response.data;
925
+                b->last = b->end = response.data + response.len;
926
+
927
+                b->temporary = 1;
928
+                b->memory = 1;
929
+
930
+                p_chain_end->buf = b;
931
+                p_chain_end->next = NULL;
932
+
933
+                // ---->
934
+
935
+                r->headers_out.content_length_n += b->last - b->pos;
936
+
937
+                p1 = p2 + 1;
938
+            }
939
+            offs += len + 1;
940
+        }
941
+
942
+        ngx_free(id);
943
+
944
+        if (!p_chain_end) // Malformed id
945
+        {
946
+            ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
947
+                "reportuploads malformed multiple id");
948
+            return NGX_DECLINED;
949
+        }
950
+
951
+        // Prepend brace
952
+        b = ngx_calloc_buf(r->pool);
953
+        if (b == NULL) {
954
+            ngx_free(id);
955
+            return NGX_HTTP_INTERNAL_SERVER_ERROR;
956
+        }
957
+        b->pos = b->start = ngx_palloc(r->pool, 2);
958
+        if (b->pos == NULL) {
959
+            return NGX_HTTP_INTERNAL_SERVER_ERROR;
960
+        }
961
+        b->last = b->end = b->pos + 2;
962
+        ngx_memcpy(b->pos, "[ ", 2);
963
+        b->temporary = 1;
964
+        b->memory = 1;
965
+        r->headers_out.content_length_n += 2;
966
+
967
+        out.buf = b;
968
+        out.next = p_chain_start;
969
+
970
+        // Append brace
971
+        p_chain_end->next = ngx_palloc(r->pool, sizeof(ngx_chain_t));
972
+        if (p_chain_end->next == NULL) {
973
+            return NGX_HTTP_INTERNAL_SERVER_ERROR;
974
+        }
975
+        p_chain_end = p_chain_end->next;
976
+
977
+        b = ngx_calloc_buf(r->pool);
978
+        if (b == NULL) {
979
+            return NGX_HTTP_INTERNAL_SERVER_ERROR;
980
+        }
981
+
982
+        b->pos = b->start = ngx_palloc(r->pool, 2);
983
+        if (b->pos == NULL) {
984
+            return NGX_HTTP_INTERNAL_SERVER_ERROR;
985
+        }
986
+        b->last = b->end = b->pos + 4;
987
+        ngx_memcpy(b->pos, " ]\r\n", 4);
988
+        b->temporary = 1;
989
+        b->memory = 1;
990
+        r->headers_out.content_length_n += 4;
991
+
992
+        p_chain_end->buf = b;
993
+        p_chain_end->next = NULL;
994
+
995
+        r->headers_out.status = NGX_HTTP_OK;
996
+        p_chain_end->buf->last_buf = 1;
727 997
     }
998
+    else
999
+    {
1000
+        ngx_shmtx_lock(&shpool->mutex);
728 1001
 
729
-    b->pos = b->start = response.data;
730
-    b->last = b->end = response.data + response.len;
1002
+        up = find_node(id, ctx, r->connection->log);
1003
+        if (up != NULL) {
1004
+            ngx_log_debug5(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1005
+                "reportuploads found node: %V (rest: %uO, length: %uO, done: %ui, err_status: %ui)", id, up->rest, up->length, up->done, up->err_status);
1006
+            rest = up->rest;
1007
+            length = up->length;
1008
+            done = up->done;
1009
+            err_status = up->err_status;
1010
+            found = 1;
1011
+        } else {
1012
+            ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1013
+                "reportuploads not found: %V", id);
1014
+        }
1015
+        ngx_shmtx_unlock(&shpool->mutex);
1016
+        ngx_free(id);
731 1017
 
732
-    b->temporary = 1;
733
-    b->memory = 1;
1018
+        /* send the output */
1019
+        r->headers_out.content_type = upcf->content_type;
1020
+
1021
+        ngx_http_set_ctx(r, up, ngx_http_uploadprogress_module);
1022
+
1023
+        /*
1024
+         * There are 4 possibilities
1025
+         *  * request not yet started: found = false
1026
+         *  * request in error:        err_status >= NGX_HTTP_BAD_REQUEST
1027
+         *  * request finished:        done = true
1028
+         *  * request not yet started but registered:        length==0 && rest ==0
1029
+         *  * reauest in progress:     rest > 0
1030
+         */
1031
+
1032
+        if (!found) {
1033
+            state = uploadprogress_state_starting;
1034
+        } else if (err_status >= NGX_HTTP_BAD_REQUEST) {
1035
+            state = uploadprogress_state_error;
1036
+        } else if (done) {
1037
+            state = uploadprogress_state_done;
1038
+        } else if ( length == 0 && rest == 0 ) {
1039
+            state = uploadprogress_state_starting;
1040
+        } else {
1041
+            state = uploadprogress_state_uploading;
1042
+        }
734 1043
 
735
-    out.buf = b;
736
-    out.next = NULL;
1044
+        t = upcf->templates.elts;
737 1045
 
738
-    r->headers_out.status = NGX_HTTP_OK;
739
-    r->headers_out.content_length_n = b->last - b->pos;
1046
+        if (
1047
+            ngx_http_script_run(
1048
+                r, &response, t[(ngx_uint_t)state].lengths->elts, 0,
1049
+                t[(ngx_uint_t)state].values->elts
1050
+            ) == NULL
1051
+        ) {
1052
+            return NGX_HTTP_INTERNAL_SERVER_ERROR;
1053
+        }
1054
+
1055
+        ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1056
+            "upload progress: state=%d, err_status=%ui, remaining=%uO, length=%uO",
1057
+            state, err_status, (length - rest), length);
1058
+
1059
+        b = ngx_calloc_buf(r->pool);
1060
+        if (b == NULL) {
1061
+            return NGX_HTTP_INTERNAL_SERVER_ERROR;
1062
+        }
1063
+
1064
+        b->pos = b->start = response.data;
1065
+        b->last = b->end = response.data + response.len;
1066
+
1067
+        b->temporary = 1;
1068
+        b->memory = 1;
1069
+
1070
+        out.buf = b;
1071
+        out.next = NULL;
1072
+
1073
+        r->headers_out.status = NGX_HTTP_OK;
1074
+        r->headers_out.content_length_n = b->last - b->pos;
1075
+
1076
+        b->last_buf = 1;
1077
+    }
740 1078
 
741
-    b->last_buf = 1;
742 1079
     rc = ngx_http_send_header(r);
743 1080
 
744 1081
     if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
@@ -1312,6 +1649,7 @@ ngx_http_uploadprogress_merge_loc_conf(ngx_conf_t * cf, void *parent, void *chil
1312 1649
     }
1313 1650
 
1314 1651
     ngx_conf_merge_str_value(conf->header, prev->header, "X-Progress-ID");
1652
+    ngx_conf_merge_str_value(conf->header_mul, prev->header_mul, "X-ProgressMultiple-ID");
1315 1653
     ngx_conf_merge_str_value(conf->jsonp_parameter, prev->jsonp_parameter, "callback");
1316 1654
 
1317 1655
     return NGX_CONF_OK;
@@ -1539,6 +1877,8 @@ ngx_http_upload_progress_template(ngx_conf_t * cf, ngx_command_t * cmd, void *co
1539 1877
     ngx_http_uploadprogress_state_map_t  *m = ngx_http_uploadprogress_state_map;
1540 1878
     ngx_http_uploadprogress_template_t   *t;
1541 1879
 
1880
+    upcf->json_multiple = 0;
1881
+
1542 1882
     value = cf->args->elts;
1543 1883
 
1544 1884
     while (m->name.data != NULL) {
@@ -1574,6 +1914,8 @@ ngx_http_upload_progress_java_output(ngx_conf_t * cf, ngx_command_t * cmd, void
1574 1914
     ngx_uint_t                            i;
1575 1915
     char*                                 rc;
1576 1916
 
1917
+    upcf->json_multiple = 0;
1918
+
1577 1919
     t = (ngx_http_uploadprogress_template_t*)upcf->templates.elts;
1578 1920
 
1579 1921
     for (i = 0; i < upcf->templates.nelts; i++) {
@@ -1598,6 +1940,8 @@ ngx_http_upload_progress_json_output(ngx_conf_t * cf, ngx_command_t * cmd, void
1598 1940
     ngx_uint_t                            i;
1599 1941
     char*                                 rc;
1600 1942
 
1943
+    upcf->json_multiple = 0;
1944
+
1601 1945
     t = (ngx_http_uploadprogress_template_t*)upcf->templates.elts;
1602 1946
 
1603 1947
     for (i = 0; i < upcf->templates.nelts; i++) {
@@ -1622,6 +1966,8 @@ ngx_http_upload_progress_jsonp_output(ngx_conf_t * cf, ngx_command_t * cmd, void
1622 1966
     ngx_uint_t                            i;
1623 1967
     char*                                 rc;
1624 1968
 
1969
+    upcf->json_multiple = 0;
1970
+
1625 1971
     t = (ngx_http_uploadprogress_template_t*)upcf->templates.elts;
1626 1972
 
1627 1973
     for (i = 0; i < upcf->templates.nelts; i++) {
@@ -1638,6 +1984,58 @@ ngx_http_upload_progress_jsonp_output(ngx_conf_t * cf, ngx_command_t * cmd, void
1638 1984
     return NGX_CONF_OK;
1639 1985
 }
1640 1986
 
1987
+static char*
1988
+ngx_http_upload_progress_json_multiple_output(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
1989
+{
1990
+    ngx_http_uploadprogress_conf_t       *upcf = conf;
1991
+    ngx_http_uploadprogress_template_t   *t;
1992
+    ngx_uint_t                            i;
1993
+    char*                                 rc;
1994
+
1995
+    upcf->json_multiple = 1;
1996
+
1997
+    t = (ngx_http_uploadprogress_template_t*)upcf->templates.elts;
1998
+
1999
+    for (i = 0; i < upcf->templates.nelts; i++) {
2000
+        rc = ngx_http_upload_progress_set_template(
2001
+            cf, t + i, ngx_http_uploadprogress_json_multiple_defaults + i);
2002
+
2003
+        if (rc != NGX_CONF_OK) {
2004
+            return rc;
2005
+        }
2006
+    }
2007
+
2008
+    upcf->content_type = (ngx_str_t)ngx_string("application/json");
2009
+
2010
+    return NGX_CONF_OK;
2011
+}
2012
+
2013
+static char*
2014
+ngx_http_upload_progress_jsonp_multiple_output(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
2015
+{
2016
+    ngx_http_uploadprogress_conf_t       *upcf = conf;
2017
+    ngx_http_uploadprogress_template_t   *t;
2018
+    ngx_uint_t                            i;
2019
+    char*                                 rc;
2020
+
2021
+    upcf->json_multiple = 1;
2022
+
2023
+    t = (ngx_http_uploadprogress_template_t*)upcf->templates.elts;
2024
+
2025
+    for (i = 0; i < upcf->templates.nelts; i++) {
2026
+        rc = ngx_http_upload_progress_set_template(
2027
+            cf, t + i, ngx_http_uploadprogress_jsonp_multiple_defaults + i);
2028
+
2029
+        if (rc != NGX_CONF_OK) {
2030
+            return rc;
2031
+        }
2032
+    }
2033
+
2034
+    upcf->content_type = (ngx_str_t)ngx_string("application/json");
2035
+
2036
+    return NGX_CONF_OK;
2037
+}
2038
+
1641 2039
 static ngx_int_t ngx_http_uploadprogress_received_variable(ngx_http_request_t *r,
1642 2040
     ngx_http_variable_value_t *v, uintptr_t data)
1643 2041
 {
@@ -1714,6 +2112,30 @@ ngx_http_uploadprogress_status_variable(
1714 2112
     return NGX_OK;
1715 2113
 }
1716 2114
 
2115
+static ngx_int_t
2116
+ngx_http_uploadprogress_id_variable(
2117
+    ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data)
2118
+{
2119
+    ngx_http_uploadprogress_node_t  *up;
2120
+    u_char                          *p;
2121
+
2122
+    up = ngx_http_get_module_ctx(r, ngx_http_uploadprogress_module);
2123
+
2124
+    p = ngx_palloc(r->pool, up->len);
2125
+    if (p == NULL) {
2126
+        return NGX_ERROR;
2127
+    }
2128
+
2129
+    v->len = up->len;
2130
+    v->data = p;
2131
+    ngx_memcpy(v->data, up->data, up->len);
2132
+    v->valid = 1;
2133
+    v->no_cacheable = 0;
2134
+    v->not_found = 0;
2135
+
2136
+    return NGX_OK;
2137
+}
2138
+
1717 2139
 static ngx_int_t
1718 2140
 ngx_http_uploadprogress_callback_variable(
1719 2141
     ngx_http_request_t *r, ngx_http_variable_value_t *v,  uintptr_t data)

Завантаження…
Відмінити
Зберегти