Parcourir la source

Fix template system crashes

* global templates were not inited before their use
* relying on unpushed array made nelts == 0 and no array use
* code deduplication when initializing global templates

Signed-off-by: Brice Figureau <brice@daysofwonder.com>
tags/v0.7
Brice Figureau il y a 16 ans
Parent
révision
fa18146ff4
1 fichiers modifiés avec 48 ajouts et 52 suppressions
  1. 48
    52
      ngx_http_uploadprogress_module.c

+ 48
- 52
ngx_http_uploadprogress_module.c Voir le fichier

@@ -78,7 +78,7 @@ static ngx_int_t ngx_http_uploadprogress_init_zone(ngx_shm_zone_t * shm_zone, vo
78 78
 static ngx_int_t ngx_http_uploadprogress_init(ngx_conf_t * cf);
79 79
 static void *ngx_http_uploadprogress_create_loc_conf(ngx_conf_t *cf);
80 80
 static char *ngx_http_uploadprogress_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child);
81
-static ngx_int_t ngx_http_uploadprogress_add_variables(ngx_conf_t *cf);
81
+static ngx_int_t ngx_http_uploadprogress_init_variables_and_templates(ngx_conf_t *cf);
82 82
 
83 83
 static ngx_int_t ngx_http_uploadprogress_received_variable(ngx_http_request_t *r,
84 84
     ngx_http_variable_value_t *v, uintptr_t data);
@@ -86,7 +86,7 @@ static ngx_int_t ngx_http_uploadprogress_offset_variable(ngx_http_request_t *r,
86 86
     ngx_http_variable_value_t *v, uintptr_t data);
87 87
 static ngx_int_t ngx_http_uploadprogress_status_variable(ngx_http_request_t *r,
88 88
     ngx_http_variable_value_t *v, uintptr_t data);
89
-
89
+static char* ngx_http_upload_progress_set_template(ngx_conf_t * cf, ngx_http_uploadprogress_template_t *t, ngx_str_t *source);
90 90
 static char *ngx_http_track_uploads(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
91 91
 static char *ngx_http_report_uploads(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
92 92
 static char *ngx_http_upload_progress(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
@@ -137,7 +137,7 @@ static ngx_command_t ngx_http_uploadprogress_commands[] = {
137 137
     {ngx_string("upload_progress_json_output"),
138 138
      NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_NOARGS,
139 139
      ngx_http_upload_progress_json_output,
140
-     0,
140
+     NGX_HTTP_LOC_CONF_OFFSET,
141 141
      0,
142 142
      NULL},
143 143
 
@@ -173,7 +173,7 @@ static ngx_http_variable_t  ngx_http_uploadprogress_variables[] = {
173 173
 };
174 174
 
175 175
 static ngx_http_module_t         ngx_http_uploadprogress_module_ctx = {
176
-    ngx_http_uploadprogress_add_variables,      /* preconfiguration */
176
+    ngx_http_uploadprogress_init_variables_and_templates,      /* preconfiguration */
177 177
     ngx_http_uploadprogress_init,       /* postconfiguration */
178 178
 
179 179
     NULL,                       /* create main configuration */
@@ -207,6 +207,7 @@ static ngx_http_uploadprogress_state_map_t ngx_http_uploadprogress_state_map[] =
207 207
     {ngx_string("error"),     uploadprogress_state_error},
208 208
     {ngx_string("done"),      uploadprogress_state_done},
209 209
     {ngx_string("uploading"), uploadprogress_state_uploading},
210
+    {ngx_null_string,         uploadprogress_state_none},
210 211
 };
211 212
 
212 213
 static ngx_str_t ngx_http_uploadprogress_java_defaults[] = {
@@ -1085,7 +1086,7 @@ ngx_http_uploadprogress_errortracker(ngx_http_request_t * r)
1085 1086
         if (upcf->shm_zone == NULL) {
1086 1087
             ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1087 1088
                            "trackuploads no shm_zone for id: %V", id);
1088
-            ngx_free(id);							 
1089
+            ngx_free(id);
1089 1090
             goto finish;
1090 1091
         }
1091 1092
 
@@ -1106,7 +1107,7 @@ ngx_http_uploadprogress_errortracker(ngx_http_request_t * r)
1106 1107
                            "trackuploads error-tracking found node for id: %V", id);
1107 1108
             up->err_status = r->err_status;
1108 1109
             ngx_shmtx_unlock(&shpool->mutex);
1109
-            ngx_free(id);							 
1110
+            ngx_free(id);
1110 1111
             goto finish;
1111 1112
         }
1112 1113
 
@@ -1116,7 +1117,7 @@ ngx_http_uploadprogress_errortracker(ngx_http_request_t * r)
1116 1117
         cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_http_uploadprogress_cleanup_t));
1117 1118
         if (cln == NULL) {
1118 1119
             ngx_shmtx_unlock(&shpool->mutex);
1119
-            ngx_free(id);							 
1120
+            ngx_free(id);
1120 1121
             goto finish;
1121 1122
         }
1122 1123
 
@@ -1161,7 +1162,7 @@ ngx_http_uploadprogress_errortracker(ngx_http_request_t * r)
1161 1162
 
1162 1163
         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1163 1164
                        "trackuploads error-tracking adding: %08XD", node->key);
1164
-        ngx_free(id);							 
1165
+        ngx_free(id);
1165 1166
     }
1166 1167
 
1167 1168
   finish:
@@ -1175,11 +1176,6 @@ ngx_http_uploadprogress_init(ngx_conf_t * cf)
1175 1176
 {
1176 1177
     ngx_http_handler_pt             *h;
1177 1178
     ngx_http_core_main_conf_t       *cmcf;
1178
-    ngx_http_uploadprogress_template_t   *t;
1179
-    ngx_http_uploadprogress_state_map_t  *m;
1180
-    ngx_http_script_compile_t             sc;
1181
-    ssize_t                               n;
1182
-    ngx_uint_t                            i;
1183 1179
 
1184 1180
     cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
1185 1181
 
@@ -1201,39 +1197,6 @@ ngx_http_uploadprogress_init(ngx_conf_t * cf)
1201 1197
     ngx_http_next_header_filter = ngx_http_top_header_filter;
1202 1198
     ngx_http_top_header_filter = ngx_http_uploadprogress_errortracker;
1203 1199
 
1204
-    /*
1205
-     * Compile global templates
1206
-     */
1207
-    if(ngx_array_init(&ngx_http_uploadprogress_global_templates, cf->pool, 4,
1208
-        sizeof(ngx_http_uploadprogress_template_t)) != NGX_OK) {
1209
-        return NGX_ERROR;
1210
-    }
1211
-
1212
-    m = ngx_http_uploadprogress_state_map;
1213
-    t = ngx_http_uploadprogress_global_templates.elts;
1214
-    i = 0;
1215
-
1216
-    while(m->name.data != NULL) {
1217
-        n = ngx_http_script_variables_count(ngx_http_uploadprogress_java_defaults + i);
1218
-
1219
-        ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
1220
-
1221
-        sc.cf = cf;
1222
-        sc.source = ngx_http_uploadprogress_java_defaults + i;
1223
-        sc.lengths = &t->lengths;
1224
-        sc.values = &t->values;
1225
-        sc.variables = n;
1226
-        sc.complete_lengths = 1;
1227
-        sc.complete_values = 1;
1228
-
1229
-        if (ngx_http_script_compile(&sc) != NGX_OK) {
1230
-            return NGX_ERROR;
1231
-        }
1232
-        
1233
-        m++;
1234
-        i++;
1235
-    }
1236
-
1237 1200
     return NGX_OK;
1238 1201
 }
1239 1202
 
@@ -1254,9 +1217,14 @@ ngx_http_uploadprogress_create_loc_conf(ngx_conf_t * cf)
1254 1217
     }
1255 1218
 
1256 1219
     t = conf->templates.elts;
1257
-    for(i = 0;i < conf->templates.nelts; i++) {
1258
-        t[i].values = NULL;
1259
-        t[i].lengths = NULL;
1220
+    for(i = 0;i < conf->templates.nalloc; i++) {
1221
+        ngx_http_uploadprogress_template_t *elt = ngx_array_push(&conf->templates);
1222
+        if (elt == NULL) {
1223
+            return NGX_CONF_ERROR;
1224
+        }
1225
+        
1226
+        elt->values = NULL;
1227
+        elt->lengths = NULL;
1260 1228
     } 
1261 1229
 
1262 1230
     return conf;
@@ -1283,11 +1251,11 @@ ngx_http_uploadprogress_merge_loc_conf(ngx_conf_t * cf, void *parent, void *chil
1283 1251
 
1284 1252
     for(i = 0;i < conf->templates.nelts; i++) {
1285 1253
         if(t[i].values == NULL) {
1286
-            if(pt[i].values == NULL) {
1254
+            if(pt[i].values == NULL && gt != NULL) {
1287 1255
                 t[i].values = gt[i].values;
1288 1256
                 t[i].lengths = gt[i].lengths;
1289 1257
             }
1290
-            else{
1258
+            else {
1291 1259
                 t[i].values = pt[i].values;
1292 1260
                 t[i].lengths = pt[i].lengths;
1293 1261
             }
@@ -1300,10 +1268,15 @@ ngx_http_uploadprogress_merge_loc_conf(ngx_conf_t * cf, void *parent, void *chil
1300 1268
 }
1301 1269
 
1302 1270
 static ngx_int_t
1303
-ngx_http_uploadprogress_add_variables(ngx_conf_t *cf)
1271
+ngx_http_uploadprogress_init_variables_and_templates(ngx_conf_t *cf)
1304 1272
 {
1305 1273
     ngx_http_variable_t  *var, *v;
1274
+    ngx_http_uploadprogress_template_t   *t;
1275
+    ngx_http_uploadprogress_state_map_t  *m;
1276
+    ssize_t                               n;
1277
+    ngx_uint_t                            i;
1306 1278
 
1279
+    /* Add variables */
1307 1280
     for (v = ngx_http_uploadprogress_variables; v->name.len; v++) {
1308 1281
         var = ngx_http_add_variable(cf, &v->name, v->flags);
1309 1282
         if (var == NULL) {
@@ -1314,6 +1287,29 @@ ngx_http_uploadprogress_add_variables(ngx_conf_t *cf)
1314 1287
         var->data = v->data;
1315 1288
     }
1316 1289
 
1290
+    /* Compile global templates (containing Javascript output) */
1291
+    if(ngx_array_init(&ngx_http_uploadprogress_global_templates, cf->pool, 4,
1292
+        sizeof(ngx_http_uploadprogress_template_t)) != NGX_OK) {
1293
+        return NGX_ERROR;
1294
+    }
1295
+
1296
+    m = ngx_http_uploadprogress_state_map;
1297
+    t = ngx_http_uploadprogress_global_templates.elts;
1298
+    i = 0;
1299
+
1300
+    while(m->name.data != NULL) {
1301
+        ngx_http_uploadprogress_template_t *elt = ngx_array_push(&ngx_http_uploadprogress_global_templates);
1302
+        n = ngx_http_script_variables_count(ngx_http_uploadprogress_java_defaults + i);
1303
+
1304
+        if (ngx_http_upload_progress_set_template(cf, elt, ngx_http_uploadprogress_java_defaults + i) != NGX_CONF_OK) {
1305
+            return NGX_ERROR;
1306
+        }
1307
+        
1308
+        m++;
1309
+        i++;
1310
+    }
1311
+
1312
+
1317 1313
     return NGX_OK;
1318 1314
 }
1319 1315
 

Chargement…
Annuler
Enregistrer