mirror of
https://gitlab.alpinelinux.org/alpine/aports.git
synced 2026-04-08 15:21:51 +02:00
938 lines
35 KiB
Diff
938 lines
35 KiB
Diff
Patch-Source: https://github.com/masterzen/nginx-upload-progress-module/compare/master...msva:nginx-upload-progress-module:master
|
|
|
|
Ref https://github.com/masterzen/nginx-upload-progress-module/issues/56
|
|
|
|
diff --git a/ngx_http_uploadprogress_module.c b/ngx_http_uploadprogress_module.c
|
|
index 33bdaf7..577c146 100644
|
|
--- a/ngx_http_uploadprogress_module.c
|
|
+++ b/ngx_http_uploadprogress_module.c
|
|
@@ -8,6 +8,7 @@
|
|
#include <ngx_core.h>
|
|
#include <ngx_http.h>
|
|
|
|
+
|
|
#define TIMER_FREQUENCY 15 * 1000
|
|
|
|
typedef enum {
|
|
@@ -31,6 +32,8 @@ struct ngx_http_uploadprogress_node_s {
|
|
off_t rest;
|
|
off_t length;
|
|
ngx_uint_t done;
|
|
+ ngx_uint_t sequence;
|
|
+ ngx_uint_t sent_portion;
|
|
time_t timeout;
|
|
struct ngx_http_uploadprogress_node_s *prev;
|
|
struct ngx_http_uploadprogress_node_s *next;
|
|
@@ -65,7 +68,13 @@ typedef struct {
|
|
ngx_str_t content_type;
|
|
ngx_array_t templates;
|
|
ngx_str_t header;
|
|
+ ngx_str_t header_mul;
|
|
+
|
|
+ ngx_addr_t progress_server;
|
|
+ int udp_socket;
|
|
+
|
|
ngx_str_t jsonp_parameter;
|
|
+ ngx_int_t json_multiple:1;
|
|
} ngx_http_uploadprogress_conf_t;
|
|
|
|
typedef struct {
|
|
@@ -87,6 +96,8 @@ static ngx_int_t ngx_http_uploadprogress_offset_variable(ngx_http_request_t *r,
|
|
ngx_http_variable_value_t *v, uintptr_t data);
|
|
static ngx_int_t ngx_http_uploadprogress_status_variable(ngx_http_request_t *r,
|
|
ngx_http_variable_value_t *v, uintptr_t data);
|
|
+static ngx_int_t ngx_http_uploadprogress_id_variable(ngx_http_request_t *r,
|
|
+ ngx_http_variable_value_t *v, uintptr_t data);
|
|
static ngx_int_t ngx_http_uploadprogress_callback_variable(ngx_http_request_t *r,
|
|
ngx_http_variable_value_t *v, uintptr_t data);
|
|
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 *
|
|
static char* ngx_http_upload_progress_java_output(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
|
|
static char* ngx_http_upload_progress_json_output(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
|
|
static char* ngx_http_upload_progress_jsonp_output(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
|
|
+static char* ngx_http_upload_progress_json_multiple_output(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
|
|
+static char* ngx_http_upload_progress_jsonp_multiple_output(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
|
|
static void ngx_clean_old_connections(ngx_event_t * ev);
|
|
static ngx_int_t ngx_http_uploadprogress_content_handler(ngx_http_request_t *r);
|
|
|
|
@@ -112,7 +125,7 @@ static ngx_command_t ngx_http_uploadprogress_commands[] = {
|
|
NULL},
|
|
|
|
{ngx_string("track_uploads"),
|
|
- NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE2,
|
|
+ NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE2 | NGX_CONF_TAKE3,
|
|
ngx_http_track_uploads,
|
|
NGX_HTTP_LOC_CONF_OFFSET,
|
|
0,
|
|
@@ -160,6 +173,20 @@ static ngx_command_t ngx_http_uploadprogress_commands[] = {
|
|
0,
|
|
NULL},
|
|
|
|
+ {ngx_string("upload_progress_json_multiple_output"),
|
|
+ NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_NOARGS,
|
|
+ ngx_http_upload_progress_json_multiple_output,
|
|
+ NGX_HTTP_LOC_CONF_OFFSET,
|
|
+ 0,
|
|
+ NULL},
|
|
+
|
|
+ {ngx_string("upload_progress_jsonp_multiple_output"),
|
|
+ NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_NOARGS,
|
|
+ ngx_http_upload_progress_jsonp_multiple_output,
|
|
+ NGX_HTTP_LOC_CONF_OFFSET,
|
|
+ 0,
|
|
+ NULL},
|
|
+
|
|
{ngx_string("upload_progress_header"),
|
|
NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
|
|
ngx_conf_set_str_slot,
|
|
@@ -167,6 +194,13 @@ static ngx_command_t ngx_http_uploadprogress_commands[] = {
|
|
offsetof(ngx_http_uploadprogress_conf_t, header),
|
|
NULL},
|
|
|
|
+ {ngx_string("upload_progress_header_mul"),
|
|
+ NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
|
|
+ ngx_conf_set_str_slot,
|
|
+ NGX_HTTP_LOC_CONF_OFFSET,
|
|
+ offsetof(ngx_http_uploadprogress_conf_t, header_mul),
|
|
+ NULL},
|
|
+
|
|
{ngx_string("upload_progress_jsonp_parameter"),
|
|
NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
|
|
ngx_conf_set_str_slot,
|
|
@@ -195,6 +229,10 @@ static ngx_http_variable_t ngx_http_uploadprogress_variables[] = {
|
|
(uintptr_t) offsetof(ngx_http_uploadprogress_node_t, err_status),
|
|
NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
|
|
|
|
+ { ngx_string("uploadprogress_id"), NULL, ngx_http_uploadprogress_id_variable,
|
|
+ (uintptr_t) offsetof(ngx_http_uploadprogress_node_t, err_status),
|
|
+ NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
|
|
+
|
|
{ ngx_string("uploadprogress_callback"), NULL, ngx_http_uploadprogress_callback_variable,
|
|
(uintptr_t) NULL,
|
|
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[] = {
|
|
ngx_string("$uploadprogress_callback({ \"state\" : \"uploading\", \"received\" : $uploadprogress_received, \"size\" : $uploadprogress_length });\r\n")
|
|
};
|
|
|
|
+static ngx_str_t ngx_http_uploadprogress_json_multiple_defaults[] = {
|
|
+ ngx_string("{ \"id\" : $uploadprogress_id, \"state\" : \"starting\" }"),
|
|
+ ngx_string("{ \"id\" : $uploadprogress_id, \"state\" : \"error\", \"status\" : $uploadprogress_status }"),
|
|
+ ngx_string("{ \"id\" : $uploadprogress_id, \"state\" : \"done\" }"),
|
|
+ ngx_string("{ \"id\" : $uploadprogress_id, \"state\" : \"uploading\", \"received\" : $uploadprogress_received, \"size\" : $uploadprogress_length }")
|
|
+};
|
|
+
|
|
+static ngx_str_t ngx_http_uploadprogress_jsonp_multiple_defaults[] = {
|
|
+ ngx_string("$uploadprogress_callback({ \"id\" : $uploadprogress_id, \"state\" : \"starting\" });\r\n"),
|
|
+ ngx_string("$uploadprogress_callback({ \"id\" : $uploadprogress_id, \"state\" : \"error\", \"status\" : $uploadprogress_status });\r\n"),
|
|
+ ngx_string("$uploadprogress_callback({ \"id\" : $uploadprogress_id, \"state\" : \"done\" });\r\n"),
|
|
+ ngx_string("$uploadprogress_callback({ \"id\" : $uploadprogress_id, \"state\" : \"uploading\", \"received\" : $uploadprogress_received, \"size\" : $uploadprogress_length });\r\n")
|
|
+};
|
|
+
|
|
|
|
static ngx_array_t ngx_http_uploadprogress_global_templates;
|
|
|
|
+
|
|
static ngx_str_t*
|
|
get_tracking_id(ngx_http_request_t * r)
|
|
{
|
|
@@ -328,7 +381,7 @@ get_tracking_id(ngx_http_request_t * r)
|
|
i = 1;
|
|
break;
|
|
}
|
|
- if (len<=0)
|
|
+ else if (!len)
|
|
break;
|
|
}
|
|
while(p++);
|
|
@@ -355,6 +408,97 @@ get_tracking_id(ngx_http_request_t * r)
|
|
return NULL;
|
|
}
|
|
|
|
+static ngx_str_t*
|
|
+get_tracking_ids_mul(ngx_http_request_t * r)
|
|
+{
|
|
+ u_char *p, *start_p;
|
|
+ ngx_uint_t i;
|
|
+ ngx_list_part_t *part;
|
|
+ ngx_table_elt_t *header;
|
|
+ ngx_str_t *ret, args;
|
|
+ ngx_http_uploadprogress_conf_t *upcf;
|
|
+
|
|
+ upcf = ngx_http_get_module_loc_conf(r, ngx_http_uploadprogress_module);
|
|
+
|
|
+ ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "upload-progress: get_tracking_ids");
|
|
+
|
|
+ part = &r->headers_in.headers.part;
|
|
+ header = part->elts;
|
|
+
|
|
+ for (i = 0; /* void */ ; i++) {
|
|
+
|
|
+ if (i >= part->nelts) {
|
|
+ if (part->next == NULL) {
|
|
+ break;
|
|
+ }
|
|
+
|
|
+ part = part->next;
|
|
+ header = part->elts;
|
|
+ i = 0;
|
|
+ }
|
|
+
|
|
+ if (header[i].key.len == upcf->header_mul.len
|
|
+ && ngx_strncasecmp(header[i].key.data, upcf->header_mul.data,
|
|
+ header[i].key.len) == 0) {
|
|
+ ret = ngx_calloc(sizeof(ngx_str_t), r->connection->log );
|
|
+ ret->data = header[i].value.data;
|
|
+ ret->len = header[i].value.len;
|
|
+ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
+ "upload-progress: get_tracking_ids found header: %V", ret);
|
|
+ return ret;
|
|
+ }
|
|
+ }
|
|
+
|
|
+ ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
+ "upload-progress: get_tracking_ids no header found");
|
|
+
|
|
+ /* not found, check as a request arg */
|
|
+ /* it is possible the request args have not been yet created (or already released) */
|
|
+ /* so let's try harder first from the request line */
|
|
+ args.len = r->args.len;
|
|
+ args.data = r->args.data;
|
|
+
|
|
+ if (args.len && args.data) {
|
|
+ ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
+ "upload-progress: get_tracking_id no header found, args found");
|
|
+ i = 0;
|
|
+ p = args.data;
|
|
+ do {
|
|
+ ngx_uint_t len = args.len - (p - args.data);
|
|
+ if (len >= (upcf->header_mul.len + 1) && ngx_strncasecmp(p, upcf->header_mul.data, upcf->header_mul.len) == 0
|
|
+ && p[upcf->header_mul.len] == '=') {
|
|
+ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
+ "upload-progress: get_tracking_id found args: %s",p);
|
|
+ i = 1;
|
|
+ break;
|
|
+ }
|
|
+ else if (!len)
|
|
+ break;
|
|
+ }
|
|
+ while(p++);
|
|
+
|
|
+ if (i) {
|
|
+ start_p = p += upcf->header_mul.len + 1;
|
|
+ while (p < args.data + args.len) {
|
|
+ if (*((p++) + 1) == '&') {
|
|
+ break;
|
|
+ }
|
|
+ }
|
|
+
|
|
+ ret = ngx_calloc(sizeof(ngx_str_t), r->connection->log);
|
|
+ ret->data = start_p;
|
|
+ ret->len = p - start_p;
|
|
+ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
+ "upload-progress: get_tracking_id found args: %V",ret);
|
|
+ return ret;
|
|
+ }
|
|
+ }
|
|
+
|
|
+ ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
+ "upload-progress: get_tracking_id no id found");
|
|
+ return NULL;
|
|
+}
|
|
+
|
|
static ngx_http_uploadprogress_node_t *
|
|
find_node(ngx_str_t * id, ngx_http_uploadprogress_ctx_t * ctx, ngx_log_t * log)
|
|
{
|
|
@@ -540,6 +684,27 @@ static void ngx_http_uploadprogress_event_handler(ngx_http_request_t *r)
|
|
|
|
if(up->length == 0)
|
|
up->length = r->headers_in.content_length_n;
|
|
+ if(upcf->udp_socket != -1 && upcf->progress_server.socklen != 0)
|
|
+ {
|
|
+ u_char datagram_buf[1024];
|
|
+ u_char * end;
|
|
+ off_t uploaded;
|
|
+ ngx_uint_t portion;
|
|
+
|
|
+ uploaded = up->length - up->rest;
|
|
+ if(up->length)
|
|
+ portion = 100 * uploaded / up->length;
|
|
+ else
|
|
+ portion = 100;
|
|
+ if(portion > up->sent_portion)
|
|
+ {
|
|
+ end = ngx_snprintf(datagram_buf, sizeof(datagram_buf), "{\"id\" : \"%V\", \"sequence\" : %d, \"size\" : %uO, \"uploaded\" : %uO }",
|
|
+ id, up->sequence, up->length, uploaded);
|
|
+ sendto(upcf->udp_socket, datagram_buf, end - datagram_buf, 0, (struct sockaddr*)upcf->progress_server.sockaddr, upcf->progress_server.socklen);
|
|
+ up->sent_portion = portion;
|
|
+ ++up->sequence;
|
|
+ }
|
|
+ }
|
|
ngx_log_debug3(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
|
|
"upload-progress: read_event_handler storing rest %uO/%uO for %V", up->rest, up->length, id);
|
|
} else {
|
|
@@ -559,12 +724,12 @@ ngx_http_reportuploads_handler(ngx_http_request_t * r)
|
|
ngx_chain_t out;
|
|
ngx_int_t rc, found=0, done=0, err_status=0;
|
|
off_t rest=0, length=0;
|
|
- ngx_uint_t len, i;
|
|
+ ngx_uint_t len;
|
|
ngx_slab_pool_t *shpool;
|
|
ngx_http_uploadprogress_conf_t *upcf;
|
|
ngx_http_uploadprogress_ctx_t *ctx;
|
|
ngx_http_uploadprogress_node_t *up;
|
|
- ngx_table_elt_t *expires, *cc, **ccp;
|
|
+ ngx_table_elt_t *expires, *cc;
|
|
ngx_http_uploadprogress_state_t state;
|
|
ngx_http_uploadprogress_template_t *t;
|
|
|
|
@@ -578,9 +743,13 @@ ngx_http_reportuploads_handler(ngx_http_request_t * r)
|
|
return rc;
|
|
}
|
|
|
|
- /* get the tracking id if any */
|
|
- id = get_tracking_id(r);
|
|
+ upcf = ngx_http_get_module_loc_conf(r, ngx_http_uploadprogress_module);
|
|
|
|
+ /* get the tracking id if any */
|
|
+ if(upcf->json_multiple)
|
|
+ id = get_tracking_ids_mul(r);
|
|
+ else
|
|
+ id = get_tracking_id(r);
|
|
|
|
if (id == NULL) {
|
|
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
@@ -591,8 +760,6 @@ ngx_http_reportuploads_handler(ngx_http_request_t * r)
|
|
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
"reportuploads handler found id: %V", id);
|
|
|
|
- upcf = ngx_http_get_module_loc_conf(r, ngx_http_uploadprogress_module);
|
|
-
|
|
if (upcf->shm_zone == NULL) {
|
|
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
"reportuploads no shm_zone for id: %V", id);
|
|
@@ -600,32 +767,6 @@ ngx_http_reportuploads_handler(ngx_http_request_t * r)
|
|
return NGX_DECLINED;
|
|
}
|
|
|
|
- ctx = upcf->shm_zone->data;
|
|
-
|
|
- /* get the original connection of the upload */
|
|
- shpool = (ngx_slab_pool_t *) upcf->shm_zone->shm.addr;
|
|
-
|
|
- ngx_shmtx_lock(&shpool->mutex);
|
|
-
|
|
- up = find_node(id, ctx, r->connection->log);
|
|
- if (up != NULL) {
|
|
- ngx_log_debug5(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
- "reportuploads found node: %V (rest: %uO, length: %uO, done: %ui, err_status: %ui)", id, up->rest, up->length, up->done, up->err_status);
|
|
- rest = up->rest;
|
|
- length = up->length;
|
|
- done = up->done;
|
|
- err_status = up->err_status;
|
|
- found = 1;
|
|
- } else {
|
|
- ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
- "reportuploads not found: %V", id);
|
|
- }
|
|
- ngx_shmtx_unlock(&shpool->mutex);
|
|
- ngx_free(id);
|
|
-
|
|
- /* send the output */
|
|
- r->headers_out.content_type = upcf->content_type;
|
|
-
|
|
/* force no-cache */
|
|
expires = r->headers_out.expires;
|
|
|
|
@@ -637,6 +778,7 @@ ngx_http_reportuploads_handler(ngx_http_request_t * r)
|
|
}
|
|
|
|
r->headers_out.expires = expires;
|
|
+ expires->next = NULL;
|
|
|
|
expires->hash = 1;
|
|
expires->key.len = sizeof("Expires") - 1;
|
|
@@ -646,37 +788,28 @@ ngx_http_reportuploads_handler(ngx_http_request_t * r)
|
|
len = sizeof("Mon, 28 Sep 1970 06:00:00 GMT");
|
|
expires->value.len = len - 1;
|
|
|
|
- ccp = r->headers_out.cache_control.elts;
|
|
- if (ccp == NULL) {
|
|
-
|
|
- if (ngx_array_init(&r->headers_out.cache_control, r->pool,
|
|
- 1, sizeof(ngx_table_elt_t *))
|
|
- != NGX_OK) {
|
|
- return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
- }
|
|
-
|
|
- ccp = ngx_array_push(&r->headers_out.cache_control);
|
|
- if (ccp == NULL) {
|
|
- return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
- }
|
|
-
|
|
+ cc = r->headers_out.cache_control;
|
|
+ if (cc == NULL) {
|
|
cc = ngx_list_push(&r->headers_out.headers);
|
|
if (cc == NULL) {
|
|
+ expires->hash = 0;
|
|
return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
}
|
|
|
|
+ r->headers_out.cache_control = cc;
|
|
+ cc->next = NULL;
|
|
+
|
|
cc->hash = 1;
|
|
cc->key.len = sizeof("Cache-Control") - 1;
|
|
cc->key.data = (u_char *) "Cache-Control";
|
|
|
|
- *ccp = cc;
|
|
-
|
|
} else {
|
|
- for (i = 1; i < r->headers_out.cache_control.nelts; i++) {
|
|
- ccp[i]->hash = 0;
|
|
+ for (cc = cc->next; cc; cc = cc->next) {
|
|
+ cc->hash = 0;
|
|
}
|
|
|
|
- cc = ccp[0];
|
|
+ cc = r->headers_out.cache_control;
|
|
+ cc->next = NULL;
|
|
}
|
|
|
|
expires->value.data = (u_char *) "Thu, 01 Jan 1970 00:00:01 GMT";
|
|
@@ -695,59 +828,282 @@ ngx_http_reportuploads_handler(ngx_http_request_t * r)
|
|
}
|
|
}
|
|
|
|
- ngx_http_set_ctx(r, up, ngx_http_uploadprogress_module);
|
|
+ ctx = upcf->shm_zone->data;
|
|
|
|
-/*
|
|
+ /* get the original connection of the upload */
|
|
+ shpool = (ngx_slab_pool_t *) upcf->shm_zone->shm.addr;
|
|
+
|
|
+ if(upcf->json_multiple)
|
|
+ {
|
|
+ ngx_chain_t * p_chain_end = 0;
|
|
+ ngx_chain_t * p_chain_start = 0;
|
|
+ size_t offs = 0;
|
|
+ u_char * p1 = id->data, * p2;
|
|
+ r->headers_out.content_length_n = 0;
|
|
+ while(offs < id->len)
|
|
+ {
|
|
+ p2 = memchr((char *)id->data + offs, ';', id->len - offs);
|
|
+ if(!p2) p2 = id->data + id->len;
|
|
+ size_t len = p2 - p1;
|
|
+ if(len)
|
|
+ {
|
|
+ ngx_str_t sub_id;
|
|
+ sub_id.data = p1;
|
|
+ sub_id.len = len;
|
|
+
|
|
+ // ---->
|
|
+
|
|
+ ngx_shmtx_lock(&shpool->mutex);
|
|
+
|
|
+ up = find_node(&sub_id, ctx, r->connection->log);
|
|
+ if (up != NULL) {
|
|
+ ngx_log_debug5(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
+ "reportuploads found node: %V (rest: %uO, length: %uO, done: %ui, err_status: %ui)", &sub_id, up->rest, up->length, up->done, up->err_status);
|
|
+ rest = up->rest;
|
|
+ length = up->length;
|
|
+ done = up->done;
|
|
+ err_status = up->err_status;
|
|
+ found = 1;
|
|
+ } else {
|
|
+ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
+ "reportuploads not found: %V", &sub_id);
|
|
+ }
|
|
+ ngx_shmtx_unlock(&shpool->mutex);
|
|
+
|
|
+ /* send the output */
|
|
+ r->headers_out.content_type = upcf->content_type;
|
|
+
|
|
+ if(up == NULL)
|
|
+ {
|
|
+ // For current id
|
|
+ ngx_http_uploadprogress_node_t * tmp_node = ngx_pcalloc(r->pool, sizeof(ngx_http_uploadprogress_node_t) + sub_id.len);
|
|
+ tmp_node->len = sub_id.len;
|
|
+ ngx_memcpy(tmp_node->data, sub_id.data, sub_id.len);
|
|
+ ngx_http_set_ctx(r, tmp_node, ngx_http_uploadprogress_module);
|
|
+ }
|
|
+ else
|
|
+ ngx_http_set_ctx(r, up, ngx_http_uploadprogress_module);
|
|
+
|
|
+
|
|
+ if (!found) {
|
|
+ state = uploadprogress_state_starting;
|
|
+ } else if (err_status >= NGX_HTTP_BAD_REQUEST) {
|
|
+ state = uploadprogress_state_error;
|
|
+ } else if (done) {
|
|
+ state = uploadprogress_state_done;
|
|
+ } else if ( length == 0 && rest == 0 ) {
|
|
+ state = uploadprogress_state_starting;
|
|
+ } else {
|
|
+ state = uploadprogress_state_uploading;
|
|
+ }
|
|
+
|
|
+ t = upcf->templates.elts;
|
|
+
|
|
+ if (ngx_http_script_run(r, &response, t[(ngx_uint_t)state].lengths->elts, 0,
|
|
+ t[(ngx_uint_t)state].values->elts) == NULL)
|
|
+ {
|
|
+ ngx_free(id);
|
|
+ return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
+ }
|
|
+
|
|
+ ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
+ "upload progress: state=%d, err_status=%ui, remaining=%uO, length=%uO",
|
|
+ state, err_status, (length - rest), length);
|
|
+
|
|
+ if(p_chain_end)
|
|
+ {
|
|
+ p_chain_end->next = ngx_palloc(r->pool, sizeof(ngx_chain_t));
|
|
+ if (p_chain_end->next == NULL) {
|
|
+ ngx_free(id);
|
|
+ return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
+ }
|
|
+ p_chain_end = p_chain_end->next;
|
|
+
|
|
+ // Insert comma
|
|
+ b = ngx_calloc_buf(r->pool);
|
|
+ if (b == NULL) {
|
|
+ ngx_free(id);
|
|
+ return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
+ }
|
|
+
|
|
+ b->pos = b->start = ngx_palloc(r->pool, 2);
|
|
+ if (b->pos == NULL) {
|
|
+ ngx_free(id);
|
|
+ return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
+ }
|
|
+ b->last = b->end = b->pos + 2;
|
|
+ ngx_memcpy(b->pos, ", ", 2);
|
|
+ b->temporary = 1;
|
|
+ b->memory = 1;
|
|
+
|
|
+ p_chain_end->buf = b;
|
|
+ p_chain_end->next = ngx_palloc(r->pool, sizeof(ngx_chain_t));
|
|
+ if (p_chain_end->next == NULL) {
|
|
+ ngx_free(id);
|
|
+ return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
+ }
|
|
+ p_chain_end = p_chain_end->next;
|
|
+ }
|
|
+ else
|
|
+ {
|
|
+ p_chain_start = p_chain_end = ngx_palloc(r->pool, sizeof(ngx_chain_t));
|
|
+ }
|
|
+
|
|
+ b = ngx_calloc_buf(r->pool);
|
|
+ if (b == NULL) {
|
|
+ ngx_free(id);
|
|
+ return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
+ }
|
|
+
|
|
+ b->pos = b->start = response.data;
|
|
+ b->last = b->end = response.data + response.len;
|
|
+
|
|
+ b->temporary = 1;
|
|
+ b->memory = 1;
|
|
+
|
|
+ p_chain_end->buf = b;
|
|
+ p_chain_end->next = NULL;
|
|
+
|
|
+ // ---->
|
|
+
|
|
+ r->headers_out.content_length_n += b->last - b->pos;
|
|
+
|
|
+ p1 = p2 + 1;
|
|
+ }
|
|
+ offs += len + 1;
|
|
+ }
|
|
+ ngx_free(id);
|
|
+ if(!p_chain_end) // Malformed id
|
|
+ {
|
|
+ ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
+ "reportuploads malformed multiple id");
|
|
+ return NGX_DECLINED;
|
|
+ }
|
|
+ // Prepend brace
|
|
+ b = ngx_calloc_buf(r->pool);
|
|
+ if (b == NULL) {
|
|
+ ngx_free(id);
|
|
+ return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
+ }
|
|
+ b->pos = b->start = ngx_palloc(r->pool, 2);
|
|
+ if (b->pos == NULL) {
|
|
+ return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
+ }
|
|
+ b->last = b->end = b->pos + 2;
|
|
+ ngx_memcpy(b->pos, "[ ", 2);
|
|
+ b->temporary = 1;
|
|
+ b->memory = 1;
|
|
+ r->headers_out.content_length_n += 2;
|
|
+
|
|
+ out.buf = b;
|
|
+ out.next = p_chain_start;
|
|
+
|
|
+ // Append brace
|
|
+ p_chain_end->next = ngx_palloc(r->pool, sizeof(ngx_chain_t));
|
|
+ if (p_chain_end->next == NULL) {
|
|
+ return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
+ }
|
|
+ p_chain_end = p_chain_end->next;
|
|
+
|
|
+ b = ngx_calloc_buf(r->pool);
|
|
+ if (b == NULL) {
|
|
+ return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
+ }
|
|
+
|
|
+ b->pos = b->start = ngx_palloc(r->pool, 2);
|
|
+ if (b->pos == NULL) {
|
|
+ return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
+ }
|
|
+ b->last = b->end = b->pos + 4;
|
|
+ ngx_memcpy(b->pos, " ]\r\n", 4);
|
|
+ b->temporary = 1;
|
|
+ b->memory = 1;
|
|
+ r->headers_out.content_length_n += 4;
|
|
+
|
|
+ p_chain_end->buf = b;
|
|
+ p_chain_end->next = NULL;
|
|
+
|
|
+ r->headers_out.status = NGX_HTTP_OK;
|
|
+ p_chain_end->buf->last_buf = 1;
|
|
+ }
|
|
+ else
|
|
+ {
|
|
+ ngx_shmtx_lock(&shpool->mutex);
|
|
+
|
|
+ up = find_node(id, ctx, r->connection->log);
|
|
+ if (up != NULL) {
|
|
+ ngx_log_debug5(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
+ "reportuploads found node: %V (rest: %uO, length: %uO, done: %ui, err_status: %ui)", id, up->rest, up->length, up->done, up->err_status);
|
|
+ rest = up->rest;
|
|
+ length = up->length;
|
|
+ done = up->done;
|
|
+ err_status = up->err_status;
|
|
+ found = 1;
|
|
+ } else {
|
|
+ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
+ "reportuploads not found: %V", id);
|
|
+ }
|
|
+ ngx_shmtx_unlock(&shpool->mutex);
|
|
+ ngx_free(id);
|
|
+
|
|
+ /* send the output */
|
|
+ r->headers_out.content_type = upcf->content_type;
|
|
+
|
|
+ ngx_http_set_ctx(r, up, ngx_http_uploadprogress_module);
|
|
+
|
|
+ /*
|
|
There are 4 possibilities
|
|
* request not yet started: found = false
|
|
* request in error: err_status >= NGX_HTTP_BAD_REQUEST
|
|
* request finished: done = true
|
|
* request not yet started but registered: length==0 && rest ==0
|
|
- * reauest in progress: rest > 0
|
|
+ * reauest in progress: rest > 0
|
|
*/
|
|
|
|
- if (!found) {
|
|
- state = uploadprogress_state_starting;
|
|
- } else if (err_status >= NGX_HTTP_BAD_REQUEST) {
|
|
- state = uploadprogress_state_error;
|
|
- } else if (done) {
|
|
- state = uploadprogress_state_done;
|
|
- } else if ( length == 0 && rest == 0 ) {
|
|
- state = uploadprogress_state_starting;
|
|
- } else {
|
|
- state = uploadprogress_state_uploading;
|
|
- }
|
|
+ if (!found) {
|
|
+ state = uploadprogress_state_starting;
|
|
+ } else if (err_status >= NGX_HTTP_BAD_REQUEST) {
|
|
+ state = uploadprogress_state_error;
|
|
+ } else if (done) {
|
|
+ state = uploadprogress_state_done;
|
|
+ } else if ( length == 0 && rest == 0 ) {
|
|
+ state = uploadprogress_state_starting;
|
|
+ } else {
|
|
+ state = uploadprogress_state_uploading;
|
|
+ }
|
|
|
|
- t = upcf->templates.elts;
|
|
+ t = upcf->templates.elts;
|
|
|
|
- if (ngx_http_script_run(r, &response, t[(ngx_uint_t)state].lengths->elts, 0,
|
|
- t[(ngx_uint_t)state].values->elts) == NULL)
|
|
- {
|
|
- return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
- }
|
|
+ if (ngx_http_script_run(r, &response, t[(ngx_uint_t)state].lengths->elts, 0,
|
|
+ t[(ngx_uint_t)state].values->elts) == NULL)
|
|
+ {
|
|
+ return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
+ }
|
|
|
|
- ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
- "upload progress: state=%d, err_status=%ui, remaining=%uO, length=%uO",
|
|
- state, err_status, (length - rest), length);
|
|
+ ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
+ "upload progress: state=%d, err_status=%ui, remaining=%uO, length=%uO",
|
|
+ state, err_status, (length - rest), length);
|
|
|
|
- b = ngx_calloc_buf(r->pool);
|
|
- if (b == NULL) {
|
|
- return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
- }
|
|
+ b = ngx_calloc_buf(r->pool);
|
|
+ if (b == NULL) {
|
|
+ return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
+ }
|
|
|
|
- b->pos = b->start = response.data;
|
|
- b->last = b->end = response.data + response.len;
|
|
+ b->pos = b->start = response.data;
|
|
+ b->last = b->end = response.data + response.len;
|
|
|
|
- b->temporary = 1;
|
|
- b->memory = 1;
|
|
+ b->temporary = 1;
|
|
+ b->memory = 1;
|
|
|
|
- out.buf = b;
|
|
- out.next = NULL;
|
|
+ out.buf = b;
|
|
+ out.next = NULL;
|
|
|
|
- r->headers_out.status = NGX_HTTP_OK;
|
|
- r->headers_out.content_length_n = b->last - b->pos;
|
|
+ r->headers_out.status = NGX_HTTP_OK;
|
|
+ r->headers_out.content_length_n = b->last - b->pos;
|
|
+
|
|
+ b->last_buf = 1;
|
|
+ }
|
|
|
|
- b->last_buf = 1;
|
|
rc = ngx_http_send_header(r);
|
|
|
|
if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
|
|
@@ -774,8 +1130,8 @@ ngx_http_uploadprogress_handler(ngx_http_request_t * r)
|
|
ngx_http_uploadprogress_cleanup_t *upcln;
|
|
ngx_pool_cleanup_t *cln;
|
|
|
|
- /* Is it a POST connection */
|
|
- if (r->method != NGX_HTTP_POST) {
|
|
+ /* Only look for progress IDs on POST, PUT and PATCH */
|
|
+ if (!(r->method & (NGX_HTTP_POST | NGX_HTTP_PUT | NGX_HTTP_PATCH))) {
|
|
return NGX_DECLINED;
|
|
}
|
|
|
|
@@ -852,6 +1208,8 @@ ngx_http_uploadprogress_handler(ngx_http_request_t * r)
|
|
up->rest = 0;
|
|
up->length = 0;
|
|
up->timeout = 0;
|
|
+ up->sequence = 0;
|
|
+ up->sent_portion = 0;
|
|
|
|
/* Properly handles small files where no read events happen after the */
|
|
/* request is first handled (apparently this can happen on linux with epoll) */
|
|
@@ -859,6 +1217,8 @@ ngx_http_uploadprogress_handler(ngx_http_request_t * r)
|
|
up->length = r->headers_in.content_length_n;
|
|
if (r->request_body) {
|
|
up->rest = r->request_body->rest;
|
|
+ } else {
|
|
+ up->rest = up->length;
|
|
}
|
|
}
|
|
|
|
@@ -1197,6 +1557,8 @@ ngx_http_uploadprogress_errortracker(ngx_http_request_t * r)
|
|
up->rest = 0;
|
|
up->length = 0;
|
|
up->timeout = 0;
|
|
+ up->sequence = 0;
|
|
+ up->sent_portion = 0;
|
|
|
|
ngx_memcpy(up->data, id->data, id->len);
|
|
|
|
@@ -1288,6 +1650,7 @@ ngx_http_uploadprogress_create_loc_conf(ngx_conf_t * cf)
|
|
elt->values = NULL;
|
|
elt->lengths = NULL;
|
|
}
|
|
+ conf->udp_socket = -1;
|
|
|
|
return conf;
|
|
}
|
|
@@ -1329,6 +1692,7 @@ ngx_http_uploadprogress_merge_loc_conf(ngx_conf_t * cf, void *parent, void *chil
|
|
}
|
|
|
|
ngx_conf_merge_str_value(conf->header, prev->header, "X-Progress-ID");
|
|
+ ngx_conf_merge_str_value(conf->header_mul, prev->header_mul, "X-ProgressMultiple-ID");
|
|
ngx_conf_merge_str_value(conf->jsonp_parameter, prev->jsonp_parameter, "callback");
|
|
|
|
return NGX_CONF_OK;
|
|
@@ -1447,6 +1811,7 @@ ngx_http_track_uploads(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
|
|
ngx_http_core_loc_conf_t *clcf;
|
|
ngx_http_uploadprogress_conf_t *lzcf = conf;
|
|
ngx_str_t *value;
|
|
+ ngx_url_t url;
|
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cf->log, 0, "ngx_track_uploads in");
|
|
|
|
@@ -1471,10 +1836,27 @@ ngx_http_track_uploads(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
|
|
lzcf->timeout = ngx_parse_time(&value[2], 1);
|
|
if (lzcf->timeout == NGX_ERROR) {
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
- "track_uploads \"%V\" timeout value invalid", &value[1]);
|
|
+ "track_uploads \"%V\" timeout value invalid", &value[2]);
|
|
return NGX_CONF_ERROR;
|
|
}
|
|
|
|
+ if(cf->args->nelts > 3)
|
|
+ {
|
|
+ ngx_memzero(&url, sizeof(ngx_url_t));
|
|
+ url.url = value[3];
|
|
+ url.default_port = 80;
|
|
+ url.no_resolve = 0;
|
|
+
|
|
+ if(ngx_parse_url(cf->pool, &url) != NGX_OK)
|
|
+ {
|
|
+ ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "Invalid graphite server %V: %s", &url.host, url.err);
|
|
+ return NGX_CONF_ERROR;
|
|
+ }
|
|
+ lzcf->progress_server = url.addrs[0];
|
|
+ if(lzcf->udp_socket == -1)
|
|
+ lzcf->udp_socket = ngx_socket(PF_INET, SOCK_DGRAM, 0);
|
|
+ }
|
|
+
|
|
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
|
|
lzcf->handler = clcf->handler;
|
|
if ( lzcf->handler == NULL )
|
|
@@ -1555,6 +1937,8 @@ ngx_http_upload_progress_template(ngx_conf_t * cf, ngx_command_t * cmd, void *co
|
|
ngx_http_uploadprogress_state_map_t *m = ngx_http_uploadprogress_state_map;
|
|
ngx_http_uploadprogress_template_t *t;
|
|
|
|
+ upcf->json_multiple = 0;
|
|
+
|
|
value = cf->args->elts;
|
|
|
|
while(m->name.data != NULL) {
|
|
@@ -1584,6 +1968,8 @@ ngx_http_upload_progress_java_output(ngx_conf_t * cf, ngx_command_t * cmd, void
|
|
ngx_uint_t i;
|
|
char* rc;
|
|
|
|
+ upcf->json_multiple = 0;
|
|
+
|
|
t = (ngx_http_uploadprogress_template_t*)upcf->templates.elts;
|
|
|
|
for(i = 0;i < upcf->templates.nelts;i++) {
|
|
@@ -1608,6 +1994,8 @@ ngx_http_upload_progress_json_output(ngx_conf_t * cf, ngx_command_t * cmd, void
|
|
ngx_uint_t i;
|
|
char* rc;
|
|
|
|
+ upcf->json_multiple = 0;
|
|
+
|
|
t = (ngx_http_uploadprogress_template_t*)upcf->templates.elts;
|
|
|
|
for(i = 0;i < upcf->templates.nelts;i++) {
|
|
@@ -1632,6 +2020,8 @@ ngx_http_upload_progress_jsonp_output(ngx_conf_t * cf, ngx_command_t * cmd, void
|
|
ngx_uint_t i;
|
|
char* rc;
|
|
|
|
+ upcf->json_multiple = 0;
|
|
+
|
|
t = (ngx_http_uploadprogress_template_t*)upcf->templates.elts;
|
|
|
|
for(i = 0;i < upcf->templates.nelts;i++) {
|
|
@@ -1648,6 +2038,58 @@ ngx_http_upload_progress_jsonp_output(ngx_conf_t * cf, ngx_command_t * cmd, void
|
|
return NGX_CONF_OK;
|
|
}
|
|
|
|
+static char*
|
|
+ngx_http_upload_progress_json_multiple_output(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
|
|
+{
|
|
+ ngx_http_uploadprogress_conf_t *upcf = conf;
|
|
+ ngx_http_uploadprogress_template_t *t;
|
|
+ ngx_uint_t i;
|
|
+ char* rc;
|
|
+
|
|
+ upcf->json_multiple = 1;
|
|
+
|
|
+ t = (ngx_http_uploadprogress_template_t*)upcf->templates.elts;
|
|
+
|
|
+ for(i = 0;i < upcf->templates.nelts;i++) {
|
|
+ rc = ngx_http_upload_progress_set_template(cf, t + i, ngx_http_uploadprogress_json_multiple_defaults + i);
|
|
+
|
|
+ if(rc != NGX_CONF_OK) {
|
|
+ return rc;
|
|
+ }
|
|
+ }
|
|
+
|
|
+ upcf->content_type.data = (u_char*)"application/json";
|
|
+ upcf->content_type.len = sizeof("application/json") - 1;
|
|
+
|
|
+ return NGX_CONF_OK;
|
|
+}
|
|
+
|
|
+static char*
|
|
+ngx_http_upload_progress_jsonp_multiple_output(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
|
|
+{
|
|
+ ngx_http_uploadprogress_conf_t *upcf = conf;
|
|
+ ngx_http_uploadprogress_template_t *t;
|
|
+ ngx_uint_t i;
|
|
+ char* rc;
|
|
+
|
|
+ upcf->json_multiple = 1;
|
|
+
|
|
+ t = (ngx_http_uploadprogress_template_t*)upcf->templates.elts;
|
|
+
|
|
+ for(i = 0;i < upcf->templates.nelts;i++) {
|
|
+ rc = ngx_http_upload_progress_set_template(cf, t + i, ngx_http_uploadprogress_jsonp_multiple_defaults + i);
|
|
+
|
|
+ if(rc != NGX_CONF_OK) {
|
|
+ return rc;
|
|
+ }
|
|
+ }
|
|
+
|
|
+ upcf->content_type.data = (u_char*)"application/json";
|
|
+ upcf->content_type.len = sizeof("application/json") - 1;
|
|
+
|
|
+ return NGX_CONF_OK;
|
|
+}
|
|
+
|
|
static ngx_int_t ngx_http_uploadprogress_received_variable(ngx_http_request_t *r,
|
|
ngx_http_variable_value_t *v, uintptr_t data)
|
|
{
|
|
@@ -1724,6 +2166,30 @@ ngx_http_uploadprogress_status_variable(ngx_http_request_t *r,
|
|
return NGX_OK;
|
|
}
|
|
|
|
+static ngx_int_t
|
|
+ngx_http_uploadprogress_id_variable(ngx_http_request_t *r,
|
|
+ ngx_http_variable_value_t *v, uintptr_t data)
|
|
+{
|
|
+ ngx_http_uploadprogress_node_t *up;
|
|
+ u_char *p;
|
|
+
|
|
+ up = ngx_http_get_module_ctx(r, ngx_http_uploadprogress_module);
|
|
+
|
|
+ p = ngx_palloc(r->pool, up->len);
|
|
+ if (p == NULL) {
|
|
+ return NGX_ERROR;
|
|
+ }
|
|
+
|
|
+ v->len = up->len;
|
|
+ v->data = p;
|
|
+ ngx_memcpy(v->data, up->data, up->len);
|
|
+ v->valid = 1;
|
|
+ v->no_cacheable = 0;
|
|
+ v->not_found = 0;
|
|
+
|
|
+ return NGX_OK;
|
|
+}
|
|
+
|
|
static ngx_int_t
|
|
ngx_http_uploadprogress_callback_variable(ngx_http_request_t *r,
|
|
ngx_http_variable_value_t *v, uintptr_t data)
|