Przeglądaj źródła

support for progress status for multiple simultaneous uploads

pull/30/head
devgs 13 lat temu
rodzic
commit
842dca0a66
1 zmienionych plików z 451 dodań i 68 usunięć
  1. 451
    68
      ngx_http_uploadprogress_module.c

+ 451
- 68
ngx_http_uploadprogress_module.c Wyświetl plik

@@ -8,6 +8,8 @@
8 8
 #include <ngx_core.h>
9 9
 #include <ngx_http.h>
10 10
 
11
+#include <string.h>
12
+
11 13
 #define TIMER_FREQUENCY 15 * 1000
12 14
 
13 15
 typedef enum {
@@ -65,7 +67,10 @@ typedef struct {
65 67
     ngx_str_t                        content_type;
66 68
     ngx_array_t                      templates;
67 69
     ngx_str_t                        header;
70
+    ngx_str_t                        header_mul;
71
+
68 72
     ngx_str_t                        jsonp_parameter;
73
+    ngx_int_t                        json_multiple:1;
69 74
 } ngx_http_uploadprogress_conf_t;
70 75
 
71 76
 typedef struct {
@@ -87,6 +92,8 @@ static ngx_int_t ngx_http_uploadprogress_offset_variable(ngx_http_request_t *r,
87 92
     ngx_http_variable_value_t *v, uintptr_t data);
88 93
 static ngx_int_t ngx_http_uploadprogress_status_variable(ngx_http_request_t *r,
89 94
     ngx_http_variable_value_t *v, uintptr_t data);
95
+static ngx_int_t ngx_http_uploadprogress_id_variable(ngx_http_request_t *r,
96
+    ngx_http_variable_value_t *v, uintptr_t data);
90 97
 static ngx_int_t ngx_http_uploadprogress_callback_variable(ngx_http_request_t *r,
91 98
     ngx_http_variable_value_t *v, uintptr_t data);
92 99
 static char* ngx_http_upload_progress_set_template(ngx_conf_t * cf, ngx_http_uploadprogress_template_t *t, ngx_str_t *source);
@@ -97,6 +104,7 @@ static char* ngx_http_upload_progress_template(ngx_conf_t * cf, ngx_command_t *
97 104
 static char* ngx_http_upload_progress_java_output(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
98 105
 static char* ngx_http_upload_progress_json_output(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
99 106
 static char* ngx_http_upload_progress_jsonp_output(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
107
+static char* ngx_http_upload_progress_json_multiple_output(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
100 108
 static void ngx_clean_old_connections(ngx_event_t * ev);
101 109
 static ngx_int_t ngx_http_uploadprogress_content_handler(ngx_http_request_t *r);
102 110
 
@@ -160,6 +168,13 @@ static ngx_command_t ngx_http_uploadprogress_commands[] = {
160 168
      0,
161 169
      NULL},
162 170
 
171
+    {ngx_string("upload_progress_json_multiple_output"),
172
+     NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_NOARGS,
173
+     ngx_http_upload_progress_json_multiple_output,
174
+     NGX_HTTP_LOC_CONF_OFFSET,
175
+     0,
176
+     NULL},
177
+
163 178
     {ngx_string("upload_progress_header"),
164 179
      NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
165 180
      ngx_conf_set_str_slot,
@@ -167,6 +182,13 @@ static ngx_command_t ngx_http_uploadprogress_commands[] = {
167 182
      offsetof(ngx_http_uploadprogress_conf_t, header),
168 183
      NULL},
169 184
 
185
+    {ngx_string("upload_progress_header_mul"),
186
+     NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
187
+     ngx_conf_set_str_slot,
188
+     NGX_HTTP_LOC_CONF_OFFSET,
189
+     offsetof(ngx_http_uploadprogress_conf_t, header_mul),
190
+     NULL},
191
+
170 192
     {ngx_string("upload_progress_jsonp_parameter"),
171 193
      NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
172 194
      ngx_conf_set_str_slot,
@@ -195,6 +217,10 @@ static ngx_http_variable_t  ngx_http_uploadprogress_variables[] = {
195 217
       (uintptr_t) offsetof(ngx_http_uploadprogress_node_t, err_status),
196 218
       NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
197 219
 
220
+    { ngx_string("uploadprogress_id"), NULL, ngx_http_uploadprogress_id_variable,
221
+      (uintptr_t) offsetof(ngx_http_uploadprogress_node_t, err_status),
222
+      NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
223
+
198 224
     { ngx_string("uploadprogress_callback"), NULL, ngx_http_uploadprogress_callback_variable,
199 225
       (uintptr_t) NULL,
200 226
       NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
@@ -261,9 +287,17 @@ static ngx_str_t ngx_http_uploadprogress_jsonp_defaults[] = {
261 287
     ngx_string("$uploadprogress_callback({ \"state\" : \"uploading\", \"received\" : $uploadprogress_received, \"size\" : $uploadprogress_length });\r\n")
262 288
 };
263 289
 
290
+static ngx_str_t ngx_http_uploadprogress_json_multiple_defaults[] = {
291
+    ngx_string("{ \"id\" : $uploadprogress_id, \"state\" : \"starting\" }"),
292
+    ngx_string("{ \"id\" : $uploadprogress_id, \"state\" : \"error\", \"status\" : $uploadprogress_status }"),
293
+    ngx_string("{ \"id\" : $uploadprogress_id, \"state\" : \"done\" }"),
294
+    ngx_string("{ \"id\" : $uploadprogress_id, \"state\" : \"uploading\", \"received\" : $uploadprogress_received, \"size\" : $uploadprogress_length }")
295
+};
296
+
264 297
 
265 298
 static ngx_array_t ngx_http_uploadprogress_global_templates;
266 299
 
300
+
267 301
 static ngx_str_t*
268 302
 get_tracking_id(ngx_http_request_t * r)
269 303
 {
@@ -328,7 +362,7 @@ get_tracking_id(ngx_http_request_t * r)
328 362
                 i = 1;
329 363
                 break;
330 364
             }
331
-            if (len<=0)
365
+            else if (!len)
332 366
                 break;
333 367
         } 
334 368
         while(p++);
@@ -355,6 +389,97 @@ get_tracking_id(ngx_http_request_t * r)
355 389
     return NULL;
356 390
 }
357 391
 
392
+static ngx_str_t*
393
+get_tracking_ids_mul(ngx_http_request_t * r)
394
+{
395
+    u_char                          *p, *start_p;
396
+    ngx_uint_t                       i;
397
+    ngx_list_part_t                 *part;
398
+    ngx_table_elt_t                 *header;
399
+    ngx_str_t                       *ret, args;
400
+    ngx_http_uploadprogress_conf_t  *upcf;
401
+
402
+    upcf = ngx_http_get_module_loc_conf(r, ngx_http_uploadprogress_module);
403
+
404
+    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "upload-progress: get_tracking_ids");
405
+
406
+    part = &r->headers_in.headers.part;
407
+    header = part->elts;
408
+
409
+    for (i = 0; /* void */ ; i++) {
410
+
411
+        if (i >= part->nelts) {
412
+            if (part->next == NULL) {
413
+                break;
414
+            }
415
+
416
+            part = part->next;
417
+            header = part->elts;
418
+            i = 0;
419
+        }
420
+
421
+        if (header[i].key.len == upcf->header_mul.len
422
+            && ngx_strncasecmp(header[i].key.data, upcf->header_mul.data,
423
+                           header[i].key.len) == 0) {
424
+            ret = ngx_calloc(sizeof(ngx_str_t), r->connection->log );
425
+            ret->data = header[i].value.data;
426
+            ret->len = header[i].value.len;
427
+            ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
428
+                            "upload-progress: get_tracking_ids found header: %V", ret);
429
+            return ret;
430
+        }
431
+    }
432
+
433
+    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
434
+                    "upload-progress: get_tracking_ids no header found");
435
+
436
+    /* not found, check as a request arg */
437
+    /* it is possible the request args have not been yet created (or already released) */
438
+    /* so let's try harder first from the request line */
439
+    args.len =  r->args.len;
440
+    args.data = r->args.data;
441
+
442
+    if (args.len && args.data) {
443
+        ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
444
+                       "upload-progress: get_tracking_id no header found, args found");
445
+        i = 0;
446
+        p = args.data;
447
+        do {
448
+            ngx_uint_t len = args.len - (p - args.data);
449
+            if (len >= (upcf->header_mul.len + 1) && ngx_strncasecmp(p, upcf->header_mul.data, upcf->header_mul.len) == 0
450
+                && p[upcf->header_mul.len] == '=') {
451
+              ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
452
+                             "upload-progress: get_tracking_id found args: %s",p);
453
+                i = 1;
454
+                break;
455
+            }
456
+            else if (!len)
457
+                break;
458
+        }
459
+        while(p++);
460
+
461
+        if (i) {
462
+            start_p = p += upcf->header_mul.len + 1;
463
+            while (p < args.data + args.len) {
464
+                if (*((p++) + 1) == '&') {
465
+                    break;
466
+                }
467
+            }
468
+
469
+            ret = ngx_calloc(sizeof(ngx_str_t), r->connection->log);
470
+            ret->data = start_p;
471
+            ret->len = p - start_p;
472
+            ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
473
+                           "upload-progress: get_tracking_id found args: %V",ret);
474
+            return ret;
475
+        }
476
+    }
477
+
478
+    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
479
+                   "upload-progress: get_tracking_id no id found");
480
+    return NULL;
481
+}
482
+
358 483
 static ngx_http_uploadprogress_node_t *
359 484
 find_node(ngx_str_t * id, ngx_http_uploadprogress_ctx_t * ctx, ngx_log_t * log)
360 485
 {
@@ -558,9 +683,13 @@ ngx_http_reportuploads_handler(ngx_http_request_t * r)
558 683
         return rc;
559 684
     }
560 685
 
561
-    /* get the tracking id if any */
562
-    id = get_tracking_id(r);
686
+    upcf = ngx_http_get_module_loc_conf(r, ngx_http_uploadprogress_module);
563 687
 
688
+    /* get the tracking id if any */
689
+    if(upcf->json_multiple)
690
+        id = get_tracking_ids_mul(r);
691
+    else
692
+        id = get_tracking_id(r);
564 693
 
565 694
     if (id == NULL) {
566 695
         ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
@@ -571,8 +700,6 @@ ngx_http_reportuploads_handler(ngx_http_request_t * r)
571 700
     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
572 701
                    "reportuploads handler found id: %V", id);
573 702
 
574
-    upcf = ngx_http_get_module_loc_conf(r, ngx_http_uploadprogress_module);
575
-
576 703
     if (upcf->shm_zone == NULL) {
577 704
         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
578 705
                        "reportuploads no shm_zone for id: %V", id);
@@ -580,32 +707,6 @@ ngx_http_reportuploads_handler(ngx_http_request_t * r)
580 707
         return NGX_DECLINED;
581 708
     }
582 709
 
583
-    ctx = upcf->shm_zone->data;
584
-
585
-    /* get the original connection of the upload */
586
-    shpool = (ngx_slab_pool_t *) upcf->shm_zone->shm.addr;
587
-
588
-    ngx_shmtx_lock(&shpool->mutex);
589
-
590
-    up = find_node(id, ctx, r->connection->log);
591
-    if (up != NULL) {
592
-        ngx_log_debug5(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
593
-                       "reportuploads found node: %V (rest: %uO, length: %uO, done: %ui, err_status: %ui)", id, up->rest, up->length, up->done, up->err_status);
594
-        rest = up->rest;
595
-        length = up->length;
596
-        done = up->done;
597
-        err_status = up->err_status;
598
-        found = 1;
599
-    } else {
600
-        ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
601
-                       "reportuploads not found: %V", id);
602
-    }
603
-    ngx_shmtx_unlock(&shpool->mutex);
604
-	ngx_free(id);
605
-
606
-    /* send the output */
607
-    r->headers_out.content_type = upcf->content_type;
608
-
609 710
     /* force no-cache */
610 711
     expires = r->headers_out.expires;
611 712
 
@@ -631,7 +732,7 @@ ngx_http_reportuploads_handler(ngx_http_request_t * r)
631 732
 
632 733
         if (ngx_array_init(&r->headers_out.cache_control, r->pool,
633 734
                            1, sizeof(ngx_table_elt_t *))
634
-            != NGX_OK) {
735
+                != NGX_OK) {
635 736
             return NGX_HTTP_INTERNAL_SERVER_ERROR;
636 737
         }
637 738
 
@@ -675,59 +776,282 @@ ngx_http_reportuploads_handler(ngx_http_request_t * r)
675 776
         }
676 777
     }
677 778
 
678
-    ngx_http_set_ctx(r, up, ngx_http_uploadprogress_module);
779
+    ctx = upcf->shm_zone->data;
780
+
781
+    /* get the original connection of the upload */
782
+    shpool = (ngx_slab_pool_t *) upcf->shm_zone->shm.addr;
783
+
784
+    if(upcf->json_multiple)
785
+    {
786
+        ngx_chain_t * p_chain_end = 0;
787
+        ngx_chain_t * p_chain_start = 0;
788
+        size_t offs = 0;
789
+        u_char * p1 = id->data, * p2;
790
+        r->headers_out.content_length_n = 0;
791
+        while(offs < id->len)
792
+        {
793
+            p2 = memchr((char *)id->data + offs, ';', id->len - offs);
794
+            if(!p2) p2 = id->data + id->len;
795
+            size_t len = p2 - p1;
796
+            if(len)
797
+            {
798
+                ngx_str_t sub_id;
799
+                sub_id.data = p1;
800
+                sub_id.len = len;
801
+
802
+                // ---->
803
+
804
+                ngx_shmtx_lock(&shpool->mutex);
805
+
806
+                up = find_node(&sub_id, ctx, r->connection->log);
807
+                if (up != NULL) {
808
+                    ngx_log_debug5(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
809
+                                   "reportuploads found node: %V (rest: %uO, length: %uO, done: %ui, err_status: %ui)", &sub_id, up->rest, up->length, up->done, up->err_status);
810
+                    rest = up->rest;
811
+                    length = up->length;
812
+                    done = up->done;
813
+                    err_status = up->err_status;
814
+                    found = 1;
815
+                } else {
816
+                    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
817
+                                   "reportuploads not found: %V", &sub_id);
818
+                }
819
+                ngx_shmtx_unlock(&shpool->mutex);
820
+
821
+                /* send the output */
822
+                r->headers_out.content_type = upcf->content_type;
823
+
824
+                if(up == NULL)
825
+                {
826
+                    // For current id
827
+                    ngx_http_uploadprogress_node_t * tmp_node = ngx_pcalloc(r->pool, sizeof(ngx_http_uploadprogress_node_t) + sub_id.len);
828
+                    tmp_node->len = sub_id.len;
829
+                    ngx_memcpy(tmp_node->data, sub_id.data, sub_id.len);
830
+                    ngx_http_set_ctx(r, tmp_node, ngx_http_uploadprogress_module);
831
+                }
832
+                else
833
+                    ngx_http_set_ctx(r, up, ngx_http_uploadprogress_module);
834
+
835
+
836
+                if (!found) {
837
+                    state = uploadprogress_state_starting;
838
+                } else if (err_status >= NGX_HTTP_BAD_REQUEST) {
839
+                    state = uploadprogress_state_error;
840
+                } else if (done) {
841
+                    state = uploadprogress_state_done;
842
+                } else if ( length == 0 && rest == 0 ) {
843
+                    state = uploadprogress_state_starting;
844
+                } else {
845
+                    state = uploadprogress_state_uploading;
846
+                }
847
+
848
+                t = upcf->templates.elts;
849
+
850
+                if (ngx_http_script_run(r, &response, t[(ngx_uint_t)state].lengths->elts, 0,
851
+                                        t[(ngx_uint_t)state].values->elts) == NULL)
852
+                {
853
+                    ngx_free(id);
854
+                    return NGX_HTTP_INTERNAL_SERVER_ERROR;
855
+                }
856
+
857
+                ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
858
+                               "upload progress: state=%d, err_status=%ui, remaining=%uO, length=%uO",
859
+                               state, err_status, (length - rest), length);
860
+
861
+                if(p_chain_end)
862
+                {
863
+                    p_chain_end->next = ngx_palloc(r->pool, sizeof(ngx_chain_t));
864
+                    if (p_chain_end->next == NULL) {
865
+                        ngx_free(id);
866
+                        return NGX_HTTP_INTERNAL_SERVER_ERROR;
867
+                    }
868
+                    p_chain_end = p_chain_end->next;
869
+
870
+                    // Insert comma
871
+                    b = ngx_calloc_buf(r->pool);
872
+                    if (b == NULL) {
873
+                        ngx_free(id);
874
+                        return NGX_HTTP_INTERNAL_SERVER_ERROR;
875
+                    }
876
+
877
+                    b->pos = b->start = ngx_palloc(r->pool, 2);
878
+                    if (b->pos == NULL) {
879
+                        ngx_free(id);
880
+                        return NGX_HTTP_INTERNAL_SERVER_ERROR;
881
+                    }
882
+                    b->last = b->end = b->pos + 2;
883
+                    ngx_memcpy(b->pos, ", ", 2);
884
+                    b->temporary = 1;
885
+                    b->memory = 1;
886
+
887
+                    p_chain_end->buf = b;
888
+                    p_chain_end->next = ngx_palloc(r->pool, sizeof(ngx_chain_t));
889
+                    if (p_chain_end->next == NULL) {
890
+                        ngx_free(id);
891
+                        return NGX_HTTP_INTERNAL_SERVER_ERROR;
892
+                    }
893
+                    p_chain_end = p_chain_end->next;
894
+                }
895
+                else
896
+                {
897
+                    p_chain_start = p_chain_end = ngx_palloc(r->pool, sizeof(ngx_chain_t));
898
+                }
899
+
900
+                b = ngx_calloc_buf(r->pool);
901
+                if (b == NULL) {
902
+                    ngx_free(id);
903
+                    return NGX_HTTP_INTERNAL_SERVER_ERROR;
904
+                }
905
+
906
+                b->pos = b->start = response.data;
907
+                b->last = b->end = response.data + response.len;
908
+
909
+                b->temporary = 1;
910
+                b->memory = 1;
911
+
912
+                p_chain_end->buf = b;
913
+                p_chain_end->next = NULL;
914
+
915
+                // ---->
916
+
917
+                r->headers_out.content_length_n += b->last - b->pos;
918
+
919
+                p1 = p2 + 1;
920
+                offs += len + 1;
921
+            }
922
+        }
923
+        ngx_free(id);
924
+        if(!p_chain_end) // Malformed id
925
+        {
926
+            ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
927
+                           "reportuploads malformed multiple id");
928
+            return NGX_DECLINED;
929
+        }
930
+        // Prepend brace
931
+        b = ngx_calloc_buf(r->pool);
932
+        if (b == NULL) {
933
+            ngx_free(id);
934
+            return NGX_HTTP_INTERNAL_SERVER_ERROR;
935
+        }
936
+        b->pos = b->start = ngx_palloc(r->pool, 2);
937
+        if (b->pos == NULL) {
938
+            return NGX_HTTP_INTERNAL_SERVER_ERROR;
939
+        }
940
+        b->last = b->end = b->pos + 2;
941
+        ngx_memcpy(b->pos, "[ ", 2);
942
+        b->temporary = 1;
943
+        b->memory = 1;
944
+        r->headers_out.content_length_n += 2;
945
+
946
+        out.buf = b;
947
+        out.next = p_chain_start;
948
+
949
+        // Append brace
950
+        p_chain_end->next = ngx_palloc(r->pool, sizeof(ngx_chain_t));
951
+        if (p_chain_end->next == NULL) {
952
+            return NGX_HTTP_INTERNAL_SERVER_ERROR;
953
+        }
954
+        p_chain_end = p_chain_end->next;
955
+
956
+        b = ngx_calloc_buf(r->pool);
957
+        if (b == NULL) {
958
+            return NGX_HTTP_INTERNAL_SERVER_ERROR;
959
+        }
960
+
961
+        b->pos = b->start = ngx_palloc(r->pool, 2);
962
+        if (b->pos == NULL) {
963
+            return NGX_HTTP_INTERNAL_SERVER_ERROR;
964
+        }
965
+        b->last = b->end = b->pos + 4;
966
+        ngx_memcpy(b->pos, " ]\r\n", 4);
967
+        b->temporary = 1;
968
+        b->memory = 1;
969
+        r->headers_out.content_length_n += 4;
970
+
971
+        p_chain_end->buf = b;
972
+        p_chain_end->next = NULL;
973
+
974
+        r->headers_out.status = NGX_HTTP_OK;
975
+        p_chain_end->buf->last_buf = 1;
976
+    }
977
+    else
978
+    {
979
+        ngx_shmtx_lock(&shpool->mutex);
980
+
981
+        up = find_node(id, ctx, r->connection->log);
982
+        if (up != NULL) {
983
+            ngx_log_debug5(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
984
+                           "reportuploads found node: %V (rest: %uO, length: %uO, done: %ui, err_status: %ui)", id, up->rest, up->length, up->done, up->err_status);
985
+            rest = up->rest;
986
+            length = up->length;
987
+            done = up->done;
988
+            err_status = up->err_status;
989
+            found = 1;
990
+        } else {
991
+            ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
992
+                           "reportuploads not found: %V", id);
993
+        }
994
+        ngx_shmtx_unlock(&shpool->mutex);
995
+        ngx_free(id);
996
+
997
+        /* send the output */
998
+        r->headers_out.content_type = upcf->content_type;
679 999
 
680
-/*
1000
+        ngx_http_set_ctx(r, up, ngx_http_uploadprogress_module);
1001
+
1002
+        /*
681 1003
  There are 4 possibilities
682 1004
    * request not yet started: found = false
683 1005
    * request in error:        err_status >= NGX_HTTP_BAD_REQUEST
684 1006
    * request finished:        done = true
685 1007
    * request not yet started but registered:        length==0 && rest ==0
686
-   * reauest in progress:     rest > 0 
1008
+   * reauest in progress:     rest > 0
687 1009
  */
688 1010
 
689
-    if (!found) {
690
-        state = uploadprogress_state_starting;
691
-    } else if (err_status >= NGX_HTTP_BAD_REQUEST) {
692
-        state = uploadprogress_state_error;
693
-    } else if (done) {
694
-        state = uploadprogress_state_done;
695
-    } else if ( length == 0 && rest == 0 ) {
696
-        state = uploadprogress_state_starting;
697
-    } else {
698
-        state = uploadprogress_state_uploading;
699
-    }
1011
+        if (!found) {
1012
+            state = uploadprogress_state_starting;
1013
+        } else if (err_status >= NGX_HTTP_BAD_REQUEST) {
1014
+            state = uploadprogress_state_error;
1015
+        } else if (done) {
1016
+            state = uploadprogress_state_done;
1017
+        } else if ( length == 0 && rest == 0 ) {
1018
+            state = uploadprogress_state_starting;
1019
+        } else {
1020
+            state = uploadprogress_state_uploading;
1021
+        }
700 1022
 
701
-    t = upcf->templates.elts;
1023
+        t = upcf->templates.elts;
702 1024
 
703
-    if (ngx_http_script_run(r, &response, t[(ngx_uint_t)state].lengths->elts, 0,
704
-        t[(ngx_uint_t)state].values->elts) == NULL)
705
-    {
706
-        return NGX_HTTP_INTERNAL_SERVER_ERROR;
707
-    }
1025
+        if (ngx_http_script_run(r, &response, t[(ngx_uint_t)state].lengths->elts, 0,
1026
+                                t[(ngx_uint_t)state].values->elts) == NULL)
1027
+        {
1028
+            return NGX_HTTP_INTERNAL_SERVER_ERROR;
1029
+        }
708 1030
 
709
-    ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
710
-        "upload progress: state=%d, err_status=%ui, remaining=%uO, length=%uO",
711
-        state, err_status, (length - rest), length);
1031
+        ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1032
+                       "upload progress: state=%d, err_status=%ui, remaining=%uO, length=%uO",
1033
+                       state, err_status, (length - rest), length);
712 1034
 
713
-    b = ngx_calloc_buf(r->pool);
714
-    if (b == NULL) {
715
-        return NGX_HTTP_INTERNAL_SERVER_ERROR;
716
-    }
1035
+        b = ngx_calloc_buf(r->pool);
1036
+        if (b == NULL) {
1037
+            return NGX_HTTP_INTERNAL_SERVER_ERROR;
1038
+        }
1039
+
1040
+        b->pos = b->start = response.data;
1041
+        b->last = b->end = response.data + response.len;
717 1042
 
718
-    b->pos = b->start = response.data;
719
-    b->last = b->end = response.data + response.len;
1043
+        b->temporary = 1;
1044
+        b->memory = 1;
720 1045
 
721
-    b->temporary = 1;
722
-    b->memory = 1;
1046
+        out.buf = b;
1047
+        out.next = NULL;
723 1048
 
724
-    out.buf = b;
725
-    out.next = NULL;
1049
+        r->headers_out.status = NGX_HTTP_OK;
1050
+        r->headers_out.content_length_n = b->last - b->pos;
726 1051
 
727
-    r->headers_out.status = NGX_HTTP_OK;
728
-    r->headers_out.content_length_n = b->last - b->pos;
1052
+        b->last_buf = 1;
1053
+    }
729 1054
 
730
-    b->last_buf = 1;
731 1055
     rc = ngx_http_send_header(r);
732 1056
 
733 1057
     if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
@@ -1304,6 +1628,7 @@ ngx_http_uploadprogress_merge_loc_conf(ngx_conf_t * cf, void *parent, void *chil
1304 1628
     } 
1305 1629
 
1306 1630
     ngx_conf_merge_str_value(conf->header, prev->header, "X-Progress-ID");
1631
+    ngx_conf_merge_str_value(conf->header_mul, prev->header_mul, "X-ProgressMultiple-ID");
1307 1632
     ngx_conf_merge_str_value(conf->jsonp_parameter, prev->jsonp_parameter, "callback");
1308 1633
 
1309 1634
     return NGX_CONF_OK;
@@ -1530,6 +1855,8 @@ ngx_http_upload_progress_template(ngx_conf_t * cf, ngx_command_t * cmd, void *co
1530 1855
     ngx_http_uploadprogress_state_map_t  *m = ngx_http_uploadprogress_state_map;
1531 1856
     ngx_http_uploadprogress_template_t   *t;
1532 1857
 
1858
+    upcf->json_multiple = 0;
1859
+
1533 1860
     value = cf->args->elts;
1534 1861
 
1535 1862
     while(m->name.data != NULL) {
@@ -1559,6 +1886,8 @@ ngx_http_upload_progress_java_output(ngx_conf_t * cf, ngx_command_t * cmd, void
1559 1886
     ngx_uint_t                            i;
1560 1887
     char*                                 rc;
1561 1888
 
1889
+    upcf->json_multiple = 0;
1890
+
1562 1891
     t = (ngx_http_uploadprogress_template_t*)upcf->templates.elts;
1563 1892
 
1564 1893
     for(i = 0;i < upcf->templates.nelts;i++) {
@@ -1583,6 +1912,8 @@ ngx_http_upload_progress_json_output(ngx_conf_t * cf, ngx_command_t * cmd, void
1583 1912
     ngx_uint_t                            i;
1584 1913
     char*                                 rc;
1585 1914
 
1915
+    upcf->json_multiple = 0;
1916
+
1586 1917
     t = (ngx_http_uploadprogress_template_t*)upcf->templates.elts;
1587 1918
 
1588 1919
     for(i = 0;i < upcf->templates.nelts;i++) {
@@ -1607,6 +1938,8 @@ ngx_http_upload_progress_jsonp_output(ngx_conf_t * cf, ngx_command_t * cmd, void
1607 1938
     ngx_uint_t                            i;
1608 1939
     char*                                 rc;
1609 1940
 
1941
+    upcf->json_multiple = 0;
1942
+
1610 1943
     t = (ngx_http_uploadprogress_template_t*)upcf->templates.elts;
1611 1944
 
1612 1945
     for(i = 0;i < upcf->templates.nelts;i++) {
@@ -1623,6 +1956,32 @@ ngx_http_upload_progress_jsonp_output(ngx_conf_t * cf, ngx_command_t * cmd, void
1623 1956
     return NGX_CONF_OK;
1624 1957
 }
1625 1958
 
1959
+static char*
1960
+ngx_http_upload_progress_json_multiple_output(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
1961
+{
1962
+    ngx_http_uploadprogress_conf_t       *upcf = conf;
1963
+    ngx_http_uploadprogress_template_t   *t;
1964
+    ngx_uint_t                            i;
1965
+    char*                                 rc;
1966
+
1967
+    upcf->json_multiple = 1;
1968
+
1969
+    t = (ngx_http_uploadprogress_template_t*)upcf->templates.elts;
1970
+
1971
+    for(i = 0;i < upcf->templates.nelts;i++) {
1972
+        rc = ngx_http_upload_progress_set_template(cf, t + i, ngx_http_uploadprogress_json_multiple_defaults + i);
1973
+
1974
+        if(rc != NGX_CONF_OK) {
1975
+            return rc;
1976
+        }
1977
+    }
1978
+
1979
+    upcf->content_type.data = (u_char*)"application/json";
1980
+    upcf->content_type.len = sizeof("application/json") - 1;
1981
+
1982
+    return NGX_CONF_OK;
1983
+}
1984
+
1626 1985
 static ngx_int_t ngx_http_uploadprogress_received_variable(ngx_http_request_t *r,
1627 1986
     ngx_http_variable_value_t *v, uintptr_t data)
1628 1987
 {
@@ -1699,6 +2058,30 @@ ngx_http_uploadprogress_status_variable(ngx_http_request_t *r,
1699 2058
     return NGX_OK;
1700 2059
 }
1701 2060
 
2061
+static ngx_int_t
2062
+ngx_http_uploadprogress_id_variable(ngx_http_request_t *r,
2063
+    ngx_http_variable_value_t *v,  uintptr_t data)
2064
+{
2065
+    ngx_http_uploadprogress_node_t  *up;
2066
+    u_char                          *p;
2067
+
2068
+    up = ngx_http_get_module_ctx(r, ngx_http_uploadprogress_module);
2069
+
2070
+    p = ngx_palloc(r->pool, up->len);
2071
+    if (p == NULL) {
2072
+        return NGX_ERROR;
2073
+    }
2074
+
2075
+    v->len = up->len;
2076
+    v->data = p;
2077
+    ngx_memcpy(v->data, up->data, up->len);
2078
+    v->valid = 1;
2079
+    v->no_cacheable = 0;
2080
+    v->not_found = 0;
2081
+
2082
+    return NGX_OK;
2083
+}
2084
+
1702 2085
 static ngx_int_t
1703 2086
 ngx_http_uploadprogress_callback_variable(ngx_http_request_t *r,
1704 2087
     ngx_http_variable_value_t *v,  uintptr_t data)

Ładowanie…
Anuluj
Zapisz