Kaynağa Gözat

Merge 070e0368a0 into 68b3ab3b64

pull/30/merge
devgs 4 yıl önce
ebeveyn
işleme
0d58a03193
No account linked to committer's email address
1 değiştirilmiş dosya ile 542 ekleme ve 70 silme
  1. 542
    70
      ngx_http_uploadprogress_module.c

+ 542
- 70
ngx_http_uploadprogress_module.c Dosyayı Görüntüle

@@ -8,6 +8,7 @@
8 8
 #include <ngx_core.h>
9 9
 #include <ngx_http.h>
10 10
 
11
+
11 12
 #define TIMER_FREQUENCY 15 * 1000
12 13
 
13 14
 typedef enum {
@@ -31,6 +32,8 @@ struct ngx_http_uploadprogress_node_s {
31 32
     off_t                            rest;
32 33
     off_t                            length;
33 34
     ngx_uint_t                       done;
35
+    ngx_uint_t                       sequence;
36
+    ngx_uint_t                       sent_portion;
34 37
     time_t                           timeout;
35 38
     struct ngx_http_uploadprogress_node_s *prev;
36 39
     struct ngx_http_uploadprogress_node_s *next;
@@ -65,7 +68,13 @@ typedef struct {
65 68
     ngx_str_t                        content_type;
66 69
     ngx_array_t                      templates;
67 70
     ngx_str_t                        header;
71
+    ngx_str_t                        header_mul;
72
+
73
+    ngx_addr_t                       progress_server;
74
+    int                              udp_socket;
75
+
68 76
     ngx_str_t                        jsonp_parameter;
77
+    ngx_int_t                        json_multiple:1;
69 78
 } ngx_http_uploadprogress_conf_t;
70 79
 
71 80
 typedef struct {
@@ -87,6 +96,8 @@ static ngx_int_t ngx_http_uploadprogress_offset_variable(ngx_http_request_t *r,
87 96
     ngx_http_variable_value_t *v, uintptr_t data);
88 97
 static ngx_int_t ngx_http_uploadprogress_status_variable(ngx_http_request_t *r,
89 98
     ngx_http_variable_value_t *v, uintptr_t data);
99
+static ngx_int_t ngx_http_uploadprogress_id_variable(ngx_http_request_t *r,
100
+    ngx_http_variable_value_t *v, uintptr_t data);
90 101
 static ngx_int_t ngx_http_uploadprogress_callback_variable(ngx_http_request_t *r,
91 102
     ngx_http_variable_value_t *v, uintptr_t data);
92 103
 static char* ngx_http_upload_progress_set_template(ngx_conf_t * cf, ngx_http_uploadprogress_template_t *t, ngx_str_t *source);
@@ -97,6 +108,8 @@ static char* ngx_http_upload_progress_template(ngx_conf_t * cf, ngx_command_t *
97 108
 static char* ngx_http_upload_progress_java_output(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
98 109
 static char* ngx_http_upload_progress_json_output(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
99 110
 static char* ngx_http_upload_progress_jsonp_output(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
111
+static char* ngx_http_upload_progress_json_multiple_output(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
112
+static char* ngx_http_upload_progress_jsonp_multiple_output(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
100 113
 static void ngx_clean_old_connections(ngx_event_t * ev);
101 114
 static ngx_int_t ngx_http_uploadprogress_content_handler(ngx_http_request_t *r);
102 115
 
@@ -112,7 +125,7 @@ static ngx_command_t ngx_http_uploadprogress_commands[] = {
112 125
      NULL},
113 126
 
114 127
     {ngx_string("track_uploads"),
115
-     NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE2,
128
+     NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE2 | NGX_CONF_TAKE3,
116 129
      ngx_http_track_uploads,
117 130
      NGX_HTTP_LOC_CONF_OFFSET,
118 131
      0,
@@ -160,6 +173,20 @@ static ngx_command_t ngx_http_uploadprogress_commands[] = {
160 173
      0,
161 174
      NULL},
162 175
 
176
+    {ngx_string("upload_progress_json_multiple_output"),
177
+     NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_NOARGS,
178
+     ngx_http_upload_progress_json_multiple_output,
179
+     NGX_HTTP_LOC_CONF_OFFSET,
180
+     0,
181
+     NULL},
182
+
183
+    {ngx_string("upload_progress_jsonp_multiple_output"),
184
+     NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_NOARGS,
185
+     ngx_http_upload_progress_jsonp_multiple_output,
186
+     NGX_HTTP_LOC_CONF_OFFSET,
187
+     0,
188
+     NULL},
189
+
163 190
     {ngx_string("upload_progress_header"),
164 191
      NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
165 192
      ngx_conf_set_str_slot,
@@ -167,6 +194,13 @@ static ngx_command_t ngx_http_uploadprogress_commands[] = {
167 194
      offsetof(ngx_http_uploadprogress_conf_t, header),
168 195
      NULL},
169 196
 
197
+    {ngx_string("upload_progress_header_mul"),
198
+     NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
199
+     ngx_conf_set_str_slot,
200
+     NGX_HTTP_LOC_CONF_OFFSET,
201
+     offsetof(ngx_http_uploadprogress_conf_t, header_mul),
202
+     NULL},
203
+
170 204
     {ngx_string("upload_progress_jsonp_parameter"),
171 205
      NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
172 206
      ngx_conf_set_str_slot,
@@ -195,6 +229,10 @@ static ngx_http_variable_t  ngx_http_uploadprogress_variables[] = {
195 229
       (uintptr_t) offsetof(ngx_http_uploadprogress_node_t, err_status),
196 230
       NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
197 231
 
232
+    { ngx_string("uploadprogress_id"), NULL, ngx_http_uploadprogress_id_variable,
233
+      (uintptr_t) offsetof(ngx_http_uploadprogress_node_t, err_status),
234
+      NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
235
+
198 236
     { ngx_string("uploadprogress_callback"), NULL, ngx_http_uploadprogress_callback_variable,
199 237
       (uintptr_t) NULL,
200 238
       NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
@@ -261,9 +299,24 @@ static ngx_str_t ngx_http_uploadprogress_jsonp_defaults[] = {
261 299
     ngx_string("$uploadprogress_callback({ \"state\" : \"uploading\", \"received\" : $uploadprogress_received, \"size\" : $uploadprogress_length });\r\n")
262 300
 };
263 301
 
302
+static ngx_str_t ngx_http_uploadprogress_json_multiple_defaults[] = {
303
+    ngx_string("{ \"id\" : $uploadprogress_id, \"state\" : \"starting\" }"),
304
+    ngx_string("{ \"id\" : $uploadprogress_id, \"state\" : \"error\", \"status\" : $uploadprogress_status }"),
305
+    ngx_string("{ \"id\" : $uploadprogress_id, \"state\" : \"done\" }"),
306
+    ngx_string("{ \"id\" : $uploadprogress_id, \"state\" : \"uploading\", \"received\" : $uploadprogress_received, \"size\" : $uploadprogress_length }")
307
+};
308
+
309
+static ngx_str_t ngx_http_uploadprogress_jsonp_multiple_defaults[] = {
310
+    ngx_string("$uploadprogress_callback({ \"id\" : $uploadprogress_id, \"state\" : \"starting\" });\r\n"),
311
+    ngx_string("$uploadprogress_callback({ \"id\" : $uploadprogress_id, \"state\" : \"error\", \"status\" : $uploadprogress_status });\r\n"),
312
+    ngx_string("$uploadprogress_callback({ \"id\" : $uploadprogress_id, \"state\" : \"done\" });\r\n"),
313
+    ngx_string("$uploadprogress_callback({ \"id\" : $uploadprogress_id, \"state\" : \"uploading\", \"received\" : $uploadprogress_received, \"size\" : $uploadprogress_length });\r\n")
314
+};
315
+
264 316
 
265 317
 static ngx_array_t ngx_http_uploadprogress_global_templates;
266 318
 
319
+
267 320
 static ngx_str_t*
268 321
 get_tracking_id(ngx_http_request_t * r)
269 322
 {
@@ -328,7 +381,7 @@ get_tracking_id(ngx_http_request_t * r)
328 381
                 i = 1;
329 382
                 break;
330 383
             }
331
-            if (len<=0)
384
+            else if (!len)
332 385
                 break;
333 386
         } 
334 387
         while(p++);
@@ -355,6 +408,97 @@ get_tracking_id(ngx_http_request_t * r)
355 408
     return NULL;
356 409
 }
357 410
 
411
+static ngx_str_t*
412
+get_tracking_ids_mul(ngx_http_request_t * r)
413
+{
414
+    u_char                          *p, *start_p;
415
+    ngx_uint_t                       i;
416
+    ngx_list_part_t                 *part;
417
+    ngx_table_elt_t                 *header;
418
+    ngx_str_t                       *ret, args;
419
+    ngx_http_uploadprogress_conf_t  *upcf;
420
+
421
+    upcf = ngx_http_get_module_loc_conf(r, ngx_http_uploadprogress_module);
422
+
423
+    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "upload-progress: get_tracking_ids");
424
+
425
+    part = &r->headers_in.headers.part;
426
+    header = part->elts;
427
+
428
+    for (i = 0; /* void */ ; i++) {
429
+
430
+        if (i >= part->nelts) {
431
+            if (part->next == NULL) {
432
+                break;
433
+            }
434
+
435
+            part = part->next;
436
+            header = part->elts;
437
+            i = 0;
438
+        }
439
+
440
+        if (header[i].key.len == upcf->header_mul.len
441
+            && ngx_strncasecmp(header[i].key.data, upcf->header_mul.data,
442
+                           header[i].key.len) == 0) {
443
+            ret = ngx_calloc(sizeof(ngx_str_t), r->connection->log );
444
+            ret->data = header[i].value.data;
445
+            ret->len = header[i].value.len;
446
+            ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
447
+                            "upload-progress: get_tracking_ids found header: %V", ret);
448
+            return ret;
449
+        }
450
+    }
451
+
452
+    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
453
+                    "upload-progress: get_tracking_ids no header found");
454
+
455
+    /* not found, check as a request arg */
456
+    /* it is possible the request args have not been yet created (or already released) */
457
+    /* so let's try harder first from the request line */
458
+    args.len =  r->args.len;
459
+    args.data = r->args.data;
460
+
461
+    if (args.len && args.data) {
462
+        ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
463
+                       "upload-progress: get_tracking_id no header found, args found");
464
+        i = 0;
465
+        p = args.data;
466
+        do {
467
+            ngx_uint_t len = args.len - (p - args.data);
468
+            if (len >= (upcf->header_mul.len + 1) && ngx_strncasecmp(p, upcf->header_mul.data, upcf->header_mul.len) == 0
469
+                && p[upcf->header_mul.len] == '=') {
470
+              ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
471
+                             "upload-progress: get_tracking_id found args: %s",p);
472
+                i = 1;
473
+                break;
474
+            }
475
+            else if (!len)
476
+                break;
477
+        }
478
+        while(p++);
479
+
480
+        if (i) {
481
+            start_p = p += upcf->header_mul.len + 1;
482
+            while (p < args.data + args.len) {
483
+                if (*((p++) + 1) == '&') {
484
+                    break;
485
+                }
486
+            }
487
+
488
+            ret = ngx_calloc(sizeof(ngx_str_t), r->connection->log);
489
+            ret->data = start_p;
490
+            ret->len = p - start_p;
491
+            ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
492
+                           "upload-progress: get_tracking_id found args: %V",ret);
493
+            return ret;
494
+        }
495
+    }
496
+
497
+    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
498
+                   "upload-progress: get_tracking_id no id found");
499
+    return NULL;
500
+}
501
+
358 502
 static ngx_http_uploadprogress_node_t *
359 503
 find_node(ngx_str_t * id, ngx_http_uploadprogress_ctx_t * ctx, ngx_log_t * log)
360 504
 {
@@ -540,6 +684,27 @@ static void ngx_http_uploadprogress_event_handler(ngx_http_request_t *r)
540 684
         
541 685
         if(up->length == 0)
542 686
             up->length = r->headers_in.content_length_n;
687
+        if(upcf->udp_socket != -1 && upcf->progress_server.socklen != 0)
688
+        {
689
+            u_char      datagram_buf[1024];
690
+            u_char *    end;
691
+            off_t       uploaded;
692
+            ngx_uint_t  portion;
693
+
694
+            uploaded = up->length - up->rest;
695
+            if(up->length)
696
+                portion = 100 * uploaded / up->length;
697
+            else
698
+                portion = 100;
699
+            if(portion > up->sent_portion)
700
+            {
701
+                end = ngx_snprintf(datagram_buf, sizeof(datagram_buf), "{\"id\" : \"%V\", \"sequence\" : %d, \"size\" : %uO, \"uploaded\" : %uO }",
702
+                                   id, up->sequence, up->length, uploaded);
703
+                sendto(upcf->udp_socket, datagram_buf, end - datagram_buf, 0, (struct sockaddr*)upcf->progress_server.sockaddr, upcf->progress_server.socklen);
704
+                up->sent_portion = portion;
705
+                ++up->sequence;
706
+            }
707
+        }
543 708
         ngx_log_debug3(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
544 709
                        "upload-progress: read_event_handler storing rest %uO/%uO for %V", up->rest, up->length, id);
545 710
     } else {
@@ -578,9 +743,13 @@ ngx_http_reportuploads_handler(ngx_http_request_t * r)
578 743
         return rc;
579 744
     }
580 745
 
581
-    /* get the tracking id if any */
582
-    id = get_tracking_id(r);
746
+    upcf = ngx_http_get_module_loc_conf(r, ngx_http_uploadprogress_module);
583 747
 
748
+    /* get the tracking id if any */
749
+    if(upcf->json_multiple)
750
+        id = get_tracking_ids_mul(r);
751
+    else
752
+        id = get_tracking_id(r);
584 753
 
585 754
     if (id == NULL) {
586 755
         ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
@@ -591,8 +760,6 @@ ngx_http_reportuploads_handler(ngx_http_request_t * r)
591 760
     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
592 761
                    "reportuploads handler found id: %V", id);
593 762
 
594
-    upcf = ngx_http_get_module_loc_conf(r, ngx_http_uploadprogress_module);
595
-
596 763
     if (upcf->shm_zone == NULL) {
597 764
         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
598 765
                        "reportuploads no shm_zone for id: %V", id);
@@ -600,32 +767,6 @@ ngx_http_reportuploads_handler(ngx_http_request_t * r)
600 767
         return NGX_DECLINED;
601 768
     }
602 769
 
603
-    ctx = upcf->shm_zone->data;
604
-
605
-    /* get the original connection of the upload */
606
-    shpool = (ngx_slab_pool_t *) upcf->shm_zone->shm.addr;
607
-
608
-    ngx_shmtx_lock(&shpool->mutex);
609
-
610
-    up = find_node(id, ctx, r->connection->log);
611
-    if (up != NULL) {
612
-        ngx_log_debug5(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
613
-                       "reportuploads found node: %V (rest: %uO, length: %uO, done: %ui, err_status: %ui)", id, up->rest, up->length, up->done, up->err_status);
614
-        rest = up->rest;
615
-        length = up->length;
616
-        done = up->done;
617
-        err_status = up->err_status;
618
-        found = 1;
619
-    } else {
620
-        ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
621
-                       "reportuploads not found: %V", id);
622
-    }
623
-    ngx_shmtx_unlock(&shpool->mutex);
624
-	ngx_free(id);
625
-
626
-    /* send the output */
627
-    r->headers_out.content_type = upcf->content_type;
628
-
629 770
     /* force no-cache */
630 771
     expires = r->headers_out.expires;
631 772
 
@@ -651,7 +792,7 @@ ngx_http_reportuploads_handler(ngx_http_request_t * r)
651 792
 
652 793
         if (ngx_array_init(&r->headers_out.cache_control, r->pool,
653 794
                            1, sizeof(ngx_table_elt_t *))
654
-            != NGX_OK) {
795
+                != NGX_OK) {
655 796
             return NGX_HTTP_INTERNAL_SERVER_ERROR;
656 797
         }
657 798
 
@@ -695,59 +836,282 @@ ngx_http_reportuploads_handler(ngx_http_request_t * r)
695 836
         }
696 837
     }
697 838
 
698
-    ngx_http_set_ctx(r, up, ngx_http_uploadprogress_module);
839
+    ctx = upcf->shm_zone->data;
699 840
 
700
-/*
841
+    /* get the original connection of the upload */
842
+    shpool = (ngx_slab_pool_t *) upcf->shm_zone->shm.addr;
843
+
844
+    if(upcf->json_multiple)
845
+    {
846
+        ngx_chain_t * p_chain_end = 0;
847
+        ngx_chain_t * p_chain_start = 0;
848
+        size_t offs = 0;
849
+        u_char * p1 = id->data, * p2;
850
+        r->headers_out.content_length_n = 0;
851
+        while(offs < id->len)
852
+        {
853
+            p2 = memchr((char *)id->data + offs, ';', id->len - offs);
854
+            if(!p2) p2 = id->data + id->len;
855
+            size_t len = p2 - p1;
856
+            if(len)
857
+            {
858
+                ngx_str_t sub_id;
859
+                sub_id.data = p1;
860
+                sub_id.len = len;
861
+
862
+                // ---->
863
+
864
+                ngx_shmtx_lock(&shpool->mutex);
865
+
866
+                up = find_node(&sub_id, ctx, r->connection->log);
867
+                if (up != NULL) {
868
+                    ngx_log_debug5(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
869
+                                   "reportuploads found node: %V (rest: %uO, length: %uO, done: %ui, err_status: %ui)", &sub_id, up->rest, up->length, up->done, up->err_status);
870
+                    rest = up->rest;
871
+                    length = up->length;
872
+                    done = up->done;
873
+                    err_status = up->err_status;
874
+                    found = 1;
875
+                } else {
876
+                    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
877
+                                   "reportuploads not found: %V", &sub_id);
878
+                }
879
+                ngx_shmtx_unlock(&shpool->mutex);
880
+
881
+                /* send the output */
882
+                r->headers_out.content_type = upcf->content_type;
883
+
884
+                if(up == NULL)
885
+                {
886
+                    // For current id
887
+                    ngx_http_uploadprogress_node_t * tmp_node = ngx_pcalloc(r->pool, sizeof(ngx_http_uploadprogress_node_t) + sub_id.len);
888
+                    tmp_node->len = sub_id.len;
889
+                    ngx_memcpy(tmp_node->data, sub_id.data, sub_id.len);
890
+                    ngx_http_set_ctx(r, tmp_node, ngx_http_uploadprogress_module);
891
+                }
892
+                else
893
+                    ngx_http_set_ctx(r, up, ngx_http_uploadprogress_module);
894
+
895
+
896
+                if (!found) {
897
+                    state = uploadprogress_state_starting;
898
+                } else if (err_status >= NGX_HTTP_BAD_REQUEST) {
899
+                    state = uploadprogress_state_error;
900
+                } else if (done) {
901
+                    state = uploadprogress_state_done;
902
+                } else if ( length == 0 && rest == 0 ) {
903
+                    state = uploadprogress_state_starting;
904
+                } else {
905
+                    state = uploadprogress_state_uploading;
906
+                }
907
+
908
+                t = upcf->templates.elts;
909
+
910
+                if (ngx_http_script_run(r, &response, t[(ngx_uint_t)state].lengths->elts, 0,
911
+                                        t[(ngx_uint_t)state].values->elts) == NULL)
912
+                {
913
+                    ngx_free(id);
914
+                    return NGX_HTTP_INTERNAL_SERVER_ERROR;
915
+                }
916
+
917
+                ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
918
+                               "upload progress: state=%d, err_status=%ui, remaining=%uO, length=%uO",
919
+                               state, err_status, (length - rest), length);
920
+
921
+                if(p_chain_end)
922
+                {
923
+                    p_chain_end->next = ngx_palloc(r->pool, sizeof(ngx_chain_t));
924
+                    if (p_chain_end->next == NULL) {
925
+                        ngx_free(id);
926
+                        return NGX_HTTP_INTERNAL_SERVER_ERROR;
927
+                    }
928
+                    p_chain_end = p_chain_end->next;
929
+
930
+                    // Insert comma
931
+                    b = ngx_calloc_buf(r->pool);
932
+                    if (b == NULL) {
933
+                        ngx_free(id);
934
+                        return NGX_HTTP_INTERNAL_SERVER_ERROR;
935
+                    }
936
+
937
+                    b->pos = b->start = ngx_palloc(r->pool, 2);
938
+                    if (b->pos == NULL) {
939
+                        ngx_free(id);
940
+                        return NGX_HTTP_INTERNAL_SERVER_ERROR;
941
+                    }
942
+                    b->last = b->end = b->pos + 2;
943
+                    ngx_memcpy(b->pos, ", ", 2);
944
+                    b->temporary = 1;
945
+                    b->memory = 1;
946
+
947
+                    p_chain_end->buf = b;
948
+                    p_chain_end->next = ngx_palloc(r->pool, sizeof(ngx_chain_t));
949
+                    if (p_chain_end->next == NULL) {
950
+                        ngx_free(id);
951
+                        return NGX_HTTP_INTERNAL_SERVER_ERROR;
952
+                    }
953
+                    p_chain_end = p_chain_end->next;
954
+                }
955
+                else
956
+                {
957
+                    p_chain_start = p_chain_end = ngx_palloc(r->pool, sizeof(ngx_chain_t));
958
+                }
959
+
960
+                b = ngx_calloc_buf(r->pool);
961
+                if (b == NULL) {
962
+                    ngx_free(id);
963
+                    return NGX_HTTP_INTERNAL_SERVER_ERROR;
964
+                }
965
+
966
+                b->pos = b->start = response.data;
967
+                b->last = b->end = response.data + response.len;
968
+
969
+                b->temporary = 1;
970
+                b->memory = 1;
971
+
972
+                p_chain_end->buf = b;
973
+                p_chain_end->next = NULL;
974
+
975
+                // ---->
976
+
977
+                r->headers_out.content_length_n += b->last - b->pos;
978
+
979
+                p1 = p2 + 1;
980
+            }
981
+            offs += len + 1;
982
+        }
983
+        ngx_free(id);
984
+        if(!p_chain_end) // Malformed id
985
+        {
986
+            ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
987
+                           "reportuploads malformed multiple id");
988
+            return NGX_DECLINED;
989
+        }
990
+        // Prepend brace
991
+        b = ngx_calloc_buf(r->pool);
992
+        if (b == NULL) {
993
+            ngx_free(id);
994
+            return NGX_HTTP_INTERNAL_SERVER_ERROR;
995
+        }
996
+        b->pos = b->start = ngx_palloc(r->pool, 2);
997
+        if (b->pos == NULL) {
998
+            return NGX_HTTP_INTERNAL_SERVER_ERROR;
999
+        }
1000
+        b->last = b->end = b->pos + 2;
1001
+        ngx_memcpy(b->pos, "[ ", 2);
1002
+        b->temporary = 1;
1003
+        b->memory = 1;
1004
+        r->headers_out.content_length_n += 2;
1005
+
1006
+        out.buf = b;
1007
+        out.next = p_chain_start;
1008
+
1009
+        // Append brace
1010
+        p_chain_end->next = ngx_palloc(r->pool, sizeof(ngx_chain_t));
1011
+        if (p_chain_end->next == NULL) {
1012
+            return NGX_HTTP_INTERNAL_SERVER_ERROR;
1013
+        }
1014
+        p_chain_end = p_chain_end->next;
1015
+
1016
+        b = ngx_calloc_buf(r->pool);
1017
+        if (b == NULL) {
1018
+            return NGX_HTTP_INTERNAL_SERVER_ERROR;
1019
+        }
1020
+
1021
+        b->pos = b->start = ngx_palloc(r->pool, 2);
1022
+        if (b->pos == NULL) {
1023
+            return NGX_HTTP_INTERNAL_SERVER_ERROR;
1024
+        }
1025
+        b->last = b->end = b->pos + 4;
1026
+        ngx_memcpy(b->pos, " ]\r\n", 4);
1027
+        b->temporary = 1;
1028
+        b->memory = 1;
1029
+        r->headers_out.content_length_n += 4;
1030
+
1031
+        p_chain_end->buf = b;
1032
+        p_chain_end->next = NULL;
1033
+
1034
+        r->headers_out.status = NGX_HTTP_OK;
1035
+        p_chain_end->buf->last_buf = 1;
1036
+    }
1037
+    else
1038
+    {
1039
+        ngx_shmtx_lock(&shpool->mutex);
1040
+
1041
+        up = find_node(id, ctx, r->connection->log);
1042
+        if (up != NULL) {
1043
+            ngx_log_debug5(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1044
+                           "reportuploads found node: %V (rest: %uO, length: %uO, done: %ui, err_status: %ui)", id, up->rest, up->length, up->done, up->err_status);
1045
+            rest = up->rest;
1046
+            length = up->length;
1047
+            done = up->done;
1048
+            err_status = up->err_status;
1049
+            found = 1;
1050
+        } else {
1051
+            ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1052
+                           "reportuploads not found: %V", id);
1053
+        }
1054
+        ngx_shmtx_unlock(&shpool->mutex);
1055
+        ngx_free(id);
1056
+
1057
+        /* send the output */
1058
+        r->headers_out.content_type = upcf->content_type;
1059
+
1060
+        ngx_http_set_ctx(r, up, ngx_http_uploadprogress_module);
1061
+
1062
+        /*
701 1063
  There are 4 possibilities
702 1064
    * request not yet started: found = false
703 1065
    * request in error:        err_status >= NGX_HTTP_BAD_REQUEST
704 1066
    * request finished:        done = true
705 1067
    * request not yet started but registered:        length==0 && rest ==0
706
-   * reauest in progress:     rest > 0 
1068
+   * reauest in progress:     rest > 0
707 1069
  */
708 1070
 
709
-    if (!found) {
710
-        state = uploadprogress_state_starting;
711
-    } else if (err_status >= NGX_HTTP_BAD_REQUEST) {
712
-        state = uploadprogress_state_error;
713
-    } else if (done) {
714
-        state = uploadprogress_state_done;
715
-    } else if ( length == 0 && rest == 0 ) {
716
-        state = uploadprogress_state_starting;
717
-    } else {
718
-        state = uploadprogress_state_uploading;
719
-    }
1071
+        if (!found) {
1072
+            state = uploadprogress_state_starting;
1073
+        } else if (err_status >= NGX_HTTP_BAD_REQUEST) {
1074
+            state = uploadprogress_state_error;
1075
+        } else if (done) {
1076
+            state = uploadprogress_state_done;
1077
+        } else if ( length == 0 && rest == 0 ) {
1078
+            state = uploadprogress_state_starting;
1079
+        } else {
1080
+            state = uploadprogress_state_uploading;
1081
+        }
720 1082
 
721
-    t = upcf->templates.elts;
1083
+        t = upcf->templates.elts;
722 1084
 
723
-    if (ngx_http_script_run(r, &response, t[(ngx_uint_t)state].lengths->elts, 0,
724
-        t[(ngx_uint_t)state].values->elts) == NULL)
725
-    {
726
-        return NGX_HTTP_INTERNAL_SERVER_ERROR;
727
-    }
1085
+        if (ngx_http_script_run(r, &response, t[(ngx_uint_t)state].lengths->elts, 0,
1086
+                                t[(ngx_uint_t)state].values->elts) == NULL)
1087
+        {
1088
+            return NGX_HTTP_INTERNAL_SERVER_ERROR;
1089
+        }
728 1090
 
729
-    ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
730
-        "upload progress: state=%d, err_status=%ui, remaining=%uO, length=%uO",
731
-        state, err_status, (length - rest), length);
1091
+        ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1092
+                       "upload progress: state=%d, err_status=%ui, remaining=%uO, length=%uO",
1093
+                       state, err_status, (length - rest), length);
732 1094
 
733
-    b = ngx_calloc_buf(r->pool);
734
-    if (b == NULL) {
735
-        return NGX_HTTP_INTERNAL_SERVER_ERROR;
736
-    }
1095
+        b = ngx_calloc_buf(r->pool);
1096
+        if (b == NULL) {
1097
+            return NGX_HTTP_INTERNAL_SERVER_ERROR;
1098
+        }
737 1099
 
738
-    b->pos = b->start = response.data;
739
-    b->last = b->end = response.data + response.len;
1100
+        b->pos = b->start = response.data;
1101
+        b->last = b->end = response.data + response.len;
740 1102
 
741
-    b->temporary = 1;
742
-    b->memory = 1;
1103
+        b->temporary = 1;
1104
+        b->memory = 1;
743 1105
 
744
-    out.buf = b;
745
-    out.next = NULL;
1106
+        out.buf = b;
1107
+        out.next = NULL;
746 1108
 
747
-    r->headers_out.status = NGX_HTTP_OK;
748
-    r->headers_out.content_length_n = b->last - b->pos;
1109
+        r->headers_out.status = NGX_HTTP_OK;
1110
+        r->headers_out.content_length_n = b->last - b->pos;
1111
+
1112
+        b->last_buf = 1;
1113
+    }
749 1114
 
750
-    b->last_buf = 1;
751 1115
     rc = ngx_http_send_header(r);
752 1116
 
753 1117
     if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
@@ -852,6 +1216,8 @@ ngx_http_uploadprogress_handler(ngx_http_request_t * r)
852 1216
     up->rest = 0;
853 1217
     up->length = 0;
854 1218
     up->timeout = 0;
1219
+    up->sequence = 0;
1220
+    up->sent_portion = 0;
855 1221
 
856 1222
     /* Properly handles small files where no read events happen after the */
857 1223
     /* request is first handled (apparently this can happen on linux with epoll) */
@@ -1197,6 +1563,8 @@ ngx_http_uploadprogress_errortracker(ngx_http_request_t * r)
1197 1563
         up->rest = 0;
1198 1564
         up->length = 0;
1199 1565
         up->timeout = 0;
1566
+        up->sequence = 0;
1567
+        up->sent_portion = 0;
1200 1568
 
1201 1569
         ngx_memcpy(up->data, id->data, id->len);
1202 1570
 
@@ -1288,6 +1656,7 @@ ngx_http_uploadprogress_create_loc_conf(ngx_conf_t * cf)
1288 1656
         elt->values = NULL;
1289 1657
         elt->lengths = NULL;
1290 1658
     } 
1659
+    conf->udp_socket = -1;
1291 1660
 
1292 1661
     return conf;
1293 1662
 }
@@ -1329,6 +1698,7 @@ ngx_http_uploadprogress_merge_loc_conf(ngx_conf_t * cf, void *parent, void *chil
1329 1698
     } 
1330 1699
 
1331 1700
     ngx_conf_merge_str_value(conf->header, prev->header, "X-Progress-ID");
1701
+    ngx_conf_merge_str_value(conf->header_mul, prev->header_mul, "X-ProgressMultiple-ID");
1332 1702
     ngx_conf_merge_str_value(conf->jsonp_parameter, prev->jsonp_parameter, "callback");
1333 1703
 
1334 1704
     return NGX_CONF_OK;
@@ -1447,6 +1817,7 @@ ngx_http_track_uploads(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
1447 1817
     ngx_http_core_loc_conf_t        *clcf;
1448 1818
     ngx_http_uploadprogress_conf_t  *lzcf = conf;
1449 1819
     ngx_str_t                       *value;
1820
+    ngx_url_t                       url;
1450 1821
 
1451 1822
     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cf->log, 0, "ngx_track_uploads in");
1452 1823
 
@@ -1471,10 +1842,27 @@ ngx_http_track_uploads(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
1471 1842
     lzcf->timeout = ngx_parse_time(&value[2], 1);
1472 1843
     if (lzcf->timeout == NGX_ERROR) {
1473 1844
         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1474
-                           "track_uploads \"%V\" timeout value invalid", &value[1]);
1845
+                           "track_uploads \"%V\" timeout value invalid", &value[2]);
1475 1846
         return NGX_CONF_ERROR;
1476 1847
     }
1477 1848
 
1849
+    if(cf->args->nelts > 3)
1850
+    {
1851
+        ngx_memzero(&url, sizeof(ngx_url_t));
1852
+        url.url = value[3];
1853
+        url.default_port = 80;
1854
+        url.no_resolve = 0;
1855
+
1856
+        if(ngx_parse_url(cf->pool, &url) != NGX_OK)
1857
+        {
1858
+            ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "Invalid graphite server %V: %s", &url.host, url.err);
1859
+            return NGX_CONF_ERROR;
1860
+        }
1861
+        lzcf->progress_server = url.addrs[0];
1862
+        if(lzcf->udp_socket == -1)
1863
+            lzcf->udp_socket = ngx_socket(PF_INET, SOCK_DGRAM, 0);
1864
+    }
1865
+
1478 1866
     clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
1479 1867
     lzcf->handler = clcf->handler;
1480 1868
     if ( lzcf->handler == NULL )
@@ -1555,6 +1943,8 @@ ngx_http_upload_progress_template(ngx_conf_t * cf, ngx_command_t * cmd, void *co
1555 1943
     ngx_http_uploadprogress_state_map_t  *m = ngx_http_uploadprogress_state_map;
1556 1944
     ngx_http_uploadprogress_template_t   *t;
1557 1945
 
1946
+    upcf->json_multiple = 0;
1947
+
1558 1948
     value = cf->args->elts;
1559 1949
 
1560 1950
     while(m->name.data != NULL) {
@@ -1584,6 +1974,8 @@ ngx_http_upload_progress_java_output(ngx_conf_t * cf, ngx_command_t * cmd, void
1584 1974
     ngx_uint_t                            i;
1585 1975
     char*                                 rc;
1586 1976
 
1977
+    upcf->json_multiple = 0;
1978
+
1587 1979
     t = (ngx_http_uploadprogress_template_t*)upcf->templates.elts;
1588 1980
 
1589 1981
     for(i = 0;i < upcf->templates.nelts;i++) {
@@ -1608,6 +2000,8 @@ ngx_http_upload_progress_json_output(ngx_conf_t * cf, ngx_command_t * cmd, void
1608 2000
     ngx_uint_t                            i;
1609 2001
     char*                                 rc;
1610 2002
 
2003
+    upcf->json_multiple = 0;
2004
+
1611 2005
     t = (ngx_http_uploadprogress_template_t*)upcf->templates.elts;
1612 2006
 
1613 2007
     for(i = 0;i < upcf->templates.nelts;i++) {
@@ -1632,6 +2026,8 @@ ngx_http_upload_progress_jsonp_output(ngx_conf_t * cf, ngx_command_t * cmd, void
1632 2026
     ngx_uint_t                            i;
1633 2027
     char*                                 rc;
1634 2028
 
2029
+    upcf->json_multiple = 0;
2030
+
1635 2031
     t = (ngx_http_uploadprogress_template_t*)upcf->templates.elts;
1636 2032
 
1637 2033
     for(i = 0;i < upcf->templates.nelts;i++) {
@@ -1648,6 +2044,58 @@ ngx_http_upload_progress_jsonp_output(ngx_conf_t * cf, ngx_command_t * cmd, void
1648 2044
     return NGX_CONF_OK;
1649 2045
 }
1650 2046
 
2047
+static char*
2048
+ngx_http_upload_progress_json_multiple_output(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
2049
+{
2050
+    ngx_http_uploadprogress_conf_t       *upcf = conf;
2051
+    ngx_http_uploadprogress_template_t   *t;
2052
+    ngx_uint_t                            i;
2053
+    char*                                 rc;
2054
+
2055
+    upcf->json_multiple = 1;
2056
+
2057
+    t = (ngx_http_uploadprogress_template_t*)upcf->templates.elts;
2058
+
2059
+    for(i = 0;i < upcf->templates.nelts;i++) {
2060
+        rc = ngx_http_upload_progress_set_template(cf, t + i, ngx_http_uploadprogress_json_multiple_defaults + i);
2061
+
2062
+        if(rc != NGX_CONF_OK) {
2063
+            return rc;
2064
+        }
2065
+    }
2066
+
2067
+    upcf->content_type.data = (u_char*)"application/json";
2068
+    upcf->content_type.len = sizeof("application/json") - 1;
2069
+
2070
+    return NGX_CONF_OK;
2071
+}
2072
+
2073
+static char*
2074
+ngx_http_upload_progress_jsonp_multiple_output(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
2075
+{
2076
+    ngx_http_uploadprogress_conf_t       *upcf = conf;
2077
+    ngx_http_uploadprogress_template_t   *t;
2078
+    ngx_uint_t                            i;
2079
+    char*                                 rc;
2080
+
2081
+    upcf->json_multiple = 1;
2082
+
2083
+    t = (ngx_http_uploadprogress_template_t*)upcf->templates.elts;
2084
+
2085
+    for(i = 0;i < upcf->templates.nelts;i++) {
2086
+        rc = ngx_http_upload_progress_set_template(cf, t + i, ngx_http_uploadprogress_jsonp_multiple_defaults + i);
2087
+
2088
+        if(rc != NGX_CONF_OK) {
2089
+            return rc;
2090
+        }
2091
+    }
2092
+
2093
+    upcf->content_type.data = (u_char*)"application/json";
2094
+    upcf->content_type.len = sizeof("application/json") - 1;
2095
+
2096
+    return NGX_CONF_OK;
2097
+}
2098
+
1651 2099
 static ngx_int_t ngx_http_uploadprogress_received_variable(ngx_http_request_t *r,
1652 2100
     ngx_http_variable_value_t *v, uintptr_t data)
1653 2101
 {
@@ -1724,6 +2172,30 @@ ngx_http_uploadprogress_status_variable(ngx_http_request_t *r,
1724 2172
     return NGX_OK;
1725 2173
 }
1726 2174
 
2175
+static ngx_int_t
2176
+ngx_http_uploadprogress_id_variable(ngx_http_request_t *r,
2177
+    ngx_http_variable_value_t *v,  uintptr_t data)
2178
+{
2179
+    ngx_http_uploadprogress_node_t  *up;
2180
+    u_char                          *p;
2181
+
2182
+    up = ngx_http_get_module_ctx(r, ngx_http_uploadprogress_module);
2183
+
2184
+    p = ngx_palloc(r->pool, up->len);
2185
+    if (p == NULL) {
2186
+        return NGX_ERROR;
2187
+    }
2188
+
2189
+    v->len = up->len;
2190
+    v->data = p;
2191
+    ngx_memcpy(v->data, up->data, up->len);
2192
+    v->valid = 1;
2193
+    v->no_cacheable = 0;
2194
+    v->not_found = 0;
2195
+
2196
+    return NGX_OK;
2197
+}
2198
+
1727 2199
 static ngx_int_t
1728 2200
 ngx_http_uploadprogress_callback_variable(ngx_http_request_t *r,
1729 2201
     ngx_http_variable_value_t *v,  uintptr_t data)

Loading…
İptal
Kaydet