Browse Source

Added directive to format output as JSON object

tags/v0.7
Valery Kholodkov 17 years ago
parent
commit
acc2c4bc60
1 changed files with 74 additions and 28 deletions
  1. 74
    28
      ngx_http_uploadprogress_module.c

+ 74
- 28
ngx_http_uploadprogress_module.c View File

@@ -21,7 +21,6 @@ typedef enum {
21 21
 typedef struct {
22 22
     ngx_str_t                           name;
23 23
     ngx_http_uploadprogress_state_t     idx;
24
-    ngx_str_t                           def;
25 24
 } ngx_http_uploadprogress_state_map_t;
26 25
 
27 26
 typedef struct ngx_http_uploadprogress_node_s ngx_http_uploadprogress_node_t;
@@ -82,6 +81,7 @@ static char *ngx_http_track_uploads(ngx_conf_t * cf, ngx_command_t * cmd, void *
82 81
 static char *ngx_http_report_uploads(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
83 82
 static char *ngx_http_upload_progress(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
84 83
 static char* ngx_http_upload_progress_template(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
84
+static char* ngx_http_upload_progress_json_output(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
85 85
 static void ngx_clean_old_connections(ngx_event_t * ev);
86 86
 static ngx_int_t ngx_http_uploadprogress_content_handler(ngx_http_request_t *r);
87 87
 
@@ -124,6 +124,13 @@ static ngx_command_t ngx_http_uploadprogress_commands[] = {
124 124
      offsetof(ngx_http_uploadprogress_conf_t, templates),
125 125
      NULL},
126 126
 
127
+    {ngx_string("upload_progress_json_output"),
128
+     NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_NOARGS,
129
+     ngx_http_upload_progress_json_output,
130
+     0,
131
+     0,
132
+     NULL},
133
+
127 134
     ngx_null_command
128 135
 };
129 136
 
@@ -161,17 +168,24 @@ ngx_module_t                     ngx_http_uploadprogress_module = {
161 168
 static ngx_str_t x_progress_id = ngx_string("X-Progress-ID");
162 169
 
163 170
 static ngx_http_uploadprogress_state_map_t ngx_http_uploadprogress_state_map[] = {
164
-    {ngx_string("starting"),  uploadprogress_state_starting,
165
-     ngx_string("new Object({ 'state' : 'starting' })\r\n")},
166
-
167
-    {ngx_string("error"),     uploadprogress_state_error,
168
-     ngx_string("new Object({ 'state' : 'error', 'status' : $uploadprogress_status })\r\n")},
171
+    {ngx_string("starting"),  uploadprogress_state_starting},
172
+    {ngx_string("error"),     uploadprogress_state_error},
173
+    {ngx_string("done"),      uploadprogress_state_done},
174
+    {ngx_string("uploading"), uploadprogress_state_uploading},
175
+};
169 176
 
170
-    {ngx_string("done"),      uploadprogress_state_done,
171
-     ngx_string("new Object({ 'state' : 'done' })\r\n")},
177
+static ngx_str_t ngx_http_uploadprogress_java_defaults[] = {
178
+    ngx_string("new Object({ 'state' : 'starting' })\r\n"),
179
+    ngx_string("new Object({ 'state' : 'error', 'status' : $uploadprogress_status })\r\n"),
180
+    ngx_string("new Object({ 'state' : 'done' })\r\n"),
181
+    ngx_string("new Object({ 'state' : 'uploading', 'received' : $uploadprogress_received, 'size' : $uploadprogress_length })\r\n")
182
+};
172 183
 
173
-    {ngx_string("uploading"), uploadprogress_state_uploading,
174
-     ngx_string("new Object({ 'state' : 'uploading', 'received' : $uploadprogress_received, 'size' : $uploadprogress_length })\r\n")},
184
+static ngx_str_t ngx_http_uploadprogress_json_defaults[] = {
185
+    ngx_string("{ \"state\" : \"starting\" }\r\n"),
186
+    ngx_string("{ \"state\" : \"error\", \"status\" : $uploadprogress_status }\r\n"),
187
+    ngx_string("{ \"state\" : \"done\" }\r\n"),
188
+    ngx_string("{ \"state\" : \"uploading\", \"received\" : $uploadprogress_received, \"size\" : $uploadprogress_length }\r\n")
175 189
 };
176 190
 
177 191
 static ngx_array_t ngx_http_uploadprogress_global_templates;
@@ -1125,6 +1139,7 @@ ngx_http_uploadprogress_init(ngx_conf_t * cf)
1125 1139
     ngx_http_uploadprogress_state_map_t  *m;
1126 1140
     ngx_http_script_compile_t             sc;
1127 1141
     ssize_t                               n;
1142
+    ngx_uint_t                            i;
1128 1143
 
1129 1144
     cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
1130 1145
 
@@ -1156,14 +1171,15 @@ ngx_http_uploadprogress_init(ngx_conf_t * cf)
1156 1171
 
1157 1172
     m = ngx_http_uploadprogress_state_map;
1158 1173
     t = ngx_http_uploadprogress_global_templates.elts;
1174
+    i = 0;
1159 1175
 
1160 1176
     while(m->name.data != NULL) {
1161
-        n = ngx_http_script_variables_count(&m->def);
1177
+        n = ngx_http_script_variables_count(ngx_http_uploadprogress_java_defaults + i);
1162 1178
 
1163 1179
         ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
1164 1180
 
1165 1181
         sc.cf = cf;
1166
-        sc.source = &m->def;
1182
+        sc.source = ngx_http_uploadprogress_java_defaults + i;
1167 1183
         sc.lengths = &t->lengths;
1168 1184
         sc.values = &t->values;
1169 1185
         sc.variables = n;
@@ -1175,6 +1191,7 @@ ngx_http_uploadprogress_init(ngx_conf_t * cf)
1175 1191
         }
1176 1192
         
1177 1193
         m++;
1194
+        i++;
1178 1195
     }
1179 1196
 
1180 1197
     return NGX_OK;
@@ -1389,20 +1406,41 @@ ngx_http_report_uploads(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
1389 1406
     return NGX_CONF_OK;
1390 1407
 }
1391 1408
 
1409
+static char*
1410
+ngx_http_upload_progress_set_template(ngx_conf_t * cf, ngx_http_uploadprogress_template_t *t, ngx_str_t *source)
1411
+{
1412
+    ssize_t                               n;
1413
+    ngx_http_script_compile_t             sc;
1414
+
1415
+    n = ngx_http_script_variables_count(source);
1416
+
1417
+    ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
1418
+
1419
+    sc.cf = cf;
1420
+    sc.source = source;
1421
+    sc.lengths = &t->lengths;
1422
+    sc.values = &t->values;
1423
+    sc.variables = n;
1424
+    sc.complete_lengths = 1;
1425
+    sc.complete_values = 1;
1426
+
1427
+    if (ngx_http_script_compile(&sc) != NGX_OK) {
1428
+        return NGX_CONF_ERROR;
1429
+    }
1430
+
1431
+    return NGX_CONF_OK;
1432
+}
1433
+
1392 1434
 static char*
1393 1435
 ngx_http_upload_progress_template(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
1394 1436
 {
1395 1437
     ngx_http_uploadprogress_conf_t       *upcf = conf;
1396
-    ssize_t                               n;
1397 1438
     ngx_str_t                            *value;
1398
-    ngx_http_uploadprogress_state_map_t  *m;
1439
+    ngx_http_uploadprogress_state_map_t  *m = ngx_http_uploadprogress_state_map;
1399 1440
     ngx_http_uploadprogress_template_t   *t;
1400
-    ngx_http_script_compile_t             sc;
1401 1441
 
1402 1442
     value = cf->args->elts;
1403 1443
 
1404
-    m = ngx_http_uploadprogress_state_map;
1405
-
1406 1444
     while(m->name.data != NULL) {
1407 1445
         if((value[1].len == m->name.len && !ngx_strncmp(value[1].data, m->name.data, m->name.len))
1408 1446
            || (value[1].len == 2 && !ngx_strncmp(value[1].data, m->name.data, 2))) {
@@ -1419,22 +1457,30 @@ ngx_http_upload_progress_template(ngx_conf_t * cf, ngx_command_t * cmd, void *co
1419 1457
 
1420 1458
     t = (ngx_http_uploadprogress_template_t*)upcf->templates.elts + (ngx_uint_t)m->idx;
1421 1459
 
1422
-    n = ngx_http_script_variables_count(&value[2]);
1460
+    return ngx_http_upload_progress_set_template(cf, t, &value[2]);
1461
+}
1423 1462
 
1424
-    ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
1463
+static char*
1464
+ngx_http_upload_progress_json_output(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
1465
+{
1466
+    ngx_http_uploadprogress_conf_t       *upcf = conf;
1467
+    ngx_http_uploadprogress_template_t   *t;
1468
+    ngx_uint_t                            i;
1469
+    static char*                          rc;
1425 1470
 
1426
-    sc.cf = cf;
1427
-    sc.source = &value[2];
1428
-    sc.lengths = &t->lengths;
1429
-    sc.values = &t->values;
1430
-    sc.variables = n;
1431
-    sc.complete_lengths = 1;
1432
-    sc.complete_values = 1;
1471
+    t = (ngx_http_uploadprogress_template_t*)upcf->templates.elts;
1433 1472
 
1434
-    if (ngx_http_script_compile(&sc) != NGX_OK) {
1435
-        return NGX_CONF_ERROR;
1473
+    for(i = 0;i < upcf->templates.nelts;i++) {
1474
+        rc = ngx_http_upload_progress_set_template(cf, t, ngx_http_uploadprogress_json_defaults + i);
1475
+
1476
+        if(rc != NGX_CONF_OK) {
1477
+            return rc;
1478
+        }
1436 1479
     }
1437 1480
 
1481
+    upcf->content_type.data = (u_char*)"application/json";
1482
+    upcf->content_type.len = sizeof("application/json") - 1;
1483
+
1438 1484
     return NGX_CONF_OK;
1439 1485
 }
1440 1486
 

Loading…
Cancel
Save