소스 검색

Added directive to format output as JSON object

tags/v0.7
Valery Kholodkov 17 년 전
부모
커밋
acc2c4bc60
1개의 변경된 파일74개의 추가작업 그리고 28개의 파일을 삭제
  1. 74
    28
      ngx_http_uploadprogress_module.c

+ 74
- 28
ngx_http_uploadprogress_module.c 파일 보기

21
 typedef struct {
21
 typedef struct {
22
     ngx_str_t                           name;
22
     ngx_str_t                           name;
23
     ngx_http_uploadprogress_state_t     idx;
23
     ngx_http_uploadprogress_state_t     idx;
24
-    ngx_str_t                           def;
25
 } ngx_http_uploadprogress_state_map_t;
24
 } ngx_http_uploadprogress_state_map_t;
26
 
25
 
27
 typedef struct ngx_http_uploadprogress_node_s ngx_http_uploadprogress_node_t;
26
 typedef struct ngx_http_uploadprogress_node_s ngx_http_uploadprogress_node_t;
82
 static char *ngx_http_report_uploads(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
81
 static char *ngx_http_report_uploads(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
83
 static char *ngx_http_upload_progress(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
82
 static char *ngx_http_upload_progress(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
84
 static char* ngx_http_upload_progress_template(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
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
 static void ngx_clean_old_connections(ngx_event_t * ev);
85
 static void ngx_clean_old_connections(ngx_event_t * ev);
86
 static ngx_int_t ngx_http_uploadprogress_content_handler(ngx_http_request_t *r);
86
 static ngx_int_t ngx_http_uploadprogress_content_handler(ngx_http_request_t *r);
87
 
87
 
124
      offsetof(ngx_http_uploadprogress_conf_t, templates),
124
      offsetof(ngx_http_uploadprogress_conf_t, templates),
125
      NULL},
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
     ngx_null_command
134
     ngx_null_command
128
 };
135
 };
129
 
136
 
161
 static ngx_str_t x_progress_id = ngx_string("X-Progress-ID");
168
 static ngx_str_t x_progress_id = ngx_string("X-Progress-ID");
162
 
169
 
163
 static ngx_http_uploadprogress_state_map_t ngx_http_uploadprogress_state_map[] = {
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
 static ngx_array_t ngx_http_uploadprogress_global_templates;
191
 static ngx_array_t ngx_http_uploadprogress_global_templates;
1125
     ngx_http_uploadprogress_state_map_t  *m;
1139
     ngx_http_uploadprogress_state_map_t  *m;
1126
     ngx_http_script_compile_t             sc;
1140
     ngx_http_script_compile_t             sc;
1127
     ssize_t                               n;
1141
     ssize_t                               n;
1142
+    ngx_uint_t                            i;
1128
 
1143
 
1129
     cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
1144
     cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
1130
 
1145
 
1156
 
1171
 
1157
     m = ngx_http_uploadprogress_state_map;
1172
     m = ngx_http_uploadprogress_state_map;
1158
     t = ngx_http_uploadprogress_global_templates.elts;
1173
     t = ngx_http_uploadprogress_global_templates.elts;
1174
+    i = 0;
1159
 
1175
 
1160
     while(m->name.data != NULL) {
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
         ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
1179
         ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
1164
 
1180
 
1165
         sc.cf = cf;
1181
         sc.cf = cf;
1166
-        sc.source = &m->def;
1182
+        sc.source = ngx_http_uploadprogress_java_defaults + i;
1167
         sc.lengths = &t->lengths;
1183
         sc.lengths = &t->lengths;
1168
         sc.values = &t->values;
1184
         sc.values = &t->values;
1169
         sc.variables = n;
1185
         sc.variables = n;
1175
         }
1191
         }
1176
         
1192
         
1177
         m++;
1193
         m++;
1194
+        i++;
1178
     }
1195
     }
1179
 
1196
 
1180
     return NGX_OK;
1197
     return NGX_OK;
1389
     return NGX_CONF_OK;
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
 static char*
1434
 static char*
1393
 ngx_http_upload_progress_template(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
1435
 ngx_http_upload_progress_template(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
1394
 {
1436
 {
1395
     ngx_http_uploadprogress_conf_t       *upcf = conf;
1437
     ngx_http_uploadprogress_conf_t       *upcf = conf;
1396
-    ssize_t                               n;
1397
     ngx_str_t                            *value;
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
     ngx_http_uploadprogress_template_t   *t;
1440
     ngx_http_uploadprogress_template_t   *t;
1400
-    ngx_http_script_compile_t             sc;
1401
 
1441
 
1402
     value = cf->args->elts;
1442
     value = cf->args->elts;
1403
 
1443
 
1404
-    m = ngx_http_uploadprogress_state_map;
1405
-
1406
     while(m->name.data != NULL) {
1444
     while(m->name.data != NULL) {
1407
         if((value[1].len == m->name.len && !ngx_strncmp(value[1].data, m->name.data, m->name.len))
1445
         if((value[1].len == m->name.len && !ngx_strncmp(value[1].data, m->name.data, m->name.len))
1408
            || (value[1].len == 2 && !ngx_strncmp(value[1].data, m->name.data, 2))) {
1446
            || (value[1].len == 2 && !ngx_strncmp(value[1].data, m->name.data, 2))) {
1419
 
1457
 
1420
     t = (ngx_http_uploadprogress_template_t*)upcf->templates.elts + (ngx_uint_t)m->idx;
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
     return NGX_CONF_OK;
1484
     return NGX_CONF_OK;
1439
 }
1485
 }
1440
 
1486
 

Loading…
취소
저장