1
1
/*
2
- * Copyright (C) 2006, 2008 Valery Kholodkov
2
+ * Copyright (C) 2008 Valery Kholodkov
3
3
*/
4
4
#include <ngx_config.h>
5
5
#include <ngx_core.h>
@@ -169,8 +169,13 @@ typedef struct ngx_unzip_ctx_s {
169
169
ngx_unzip_extra_data_record_t extra_data_record ;
170
170
};
171
171
172
+ ngx_str_t archive_name ;
172
173
ngx_str_t file_name ;
173
174
175
+ ngx_str_t prev_elm , current_elm ;
176
+ ngx_str_t prev_archive_path , current_archive_path ;
177
+ ngx_int_t entry_no ;
178
+
174
179
void * preallocated ;
175
180
char * free_mem ;
176
181
ngx_uint_t allocated ;
@@ -260,7 +265,7 @@ static ngx_command_t ngx_http_unzip_filter_commands[] = { /* {{{ */
260
265
/*
261
266
* Enables unzipping of uploaded file
262
267
*/
263
- { ngx_string ("unzip " ),
268
+ { ngx_string ("upload_unzip " ),
264
269
NGX_HTTP_LOC_CONF |NGX_CONF_TAKE1 ,
265
270
ngx_http_unzip_command ,
266
271
NGX_HTTP_LOC_CONF_OFFSET ,
@@ -270,7 +275,7 @@ static ngx_command_t ngx_http_unzip_filter_commands[] = { /* {{{ */
270
275
/*
271
276
* Specifies size and number of buffers to use for decompressing
272
277
*/
273
- { ngx_string ("unzip_buffers " ),
278
+ { ngx_string ("upload_unzip_buffers " ),
274
279
NGX_HTTP_LOC_CONF |NGX_CONF_TAKE2 ,
275
280
ngx_conf_set_bufs_slot ,
276
281
NGX_HTTP_LOC_CONF_OFFSET ,
@@ -280,7 +285,7 @@ static ngx_command_t ngx_http_unzip_filter_commands[] = { /* {{{ */
280
285
/*
281
286
* Specifies size window to use for decompressing
282
287
*/
283
- { ngx_string ("unzip_window " ),
288
+ { ngx_string ("upload_unzip_window " ),
284
289
NGX_HTTP_LOC_CONF |NGX_CONF_TAKE1 ,
285
290
ngx_conf_set_size_slot ,
286
291
NGX_HTTP_LOC_CONF_OFFSET ,
@@ -291,7 +296,7 @@ static ngx_command_t ngx_http_unzip_filter_commands[] = { /* {{{ */
291
296
* Specifies a form field with a special content to generate
292
297
* in output form
293
298
*/
294
- { ngx_string ("unzip_set_form_field " ),
299
+ { ngx_string ("upload_unzip_set_form_field " ),
295
300
NGX_HTTP_LOC_CONF |NGX_CONF_TAKE2 ,
296
301
ngx_conf_set_size_slot ,
297
302
NGX_HTTP_LOC_CONF_OFFSET ,
@@ -302,7 +307,7 @@ static ngx_command_t ngx_http_unzip_filter_commands[] = { /* {{{ */
302
307
* Specifies a form field with a special aggregate content to generate
303
308
* in output form
304
309
*/
305
- { ngx_string ("unzip_aggregate_form_field " ),
310
+ { ngx_string ("upload_unzip_aggregate_form_field " ),
306
311
NGX_HTTP_LOC_CONF |NGX_CONF_TAKE2 ,
307
312
ngx_conf_set_size_slot ,
308
313
NGX_HTTP_LOC_CONF_OFFSET ,
@@ -312,7 +317,7 @@ static ngx_command_t ngx_http_unzip_filter_commands[] = { /* {{{ */
312
317
/*
313
318
* Specifies the maximal length of a file name in archive
314
319
*/
315
- { ngx_string ("unzip_max_file_name_len " ),
320
+ { ngx_string ("upload_unzip_max_file_name_len " ),
316
321
NGX_HTTP_LOC_CONF |NGX_CONF_TAKE1 ,
317
322
ngx_conf_set_size_slot ,
318
323
NGX_HTTP_LOC_CONF_OFFSET ,
@@ -852,8 +857,10 @@ static ngx_int_t /* {{{ ngx_http_unzip_start_handler */
852
857
ngx_http_unzip_start_handler (ngx_http_upload_ctx_t * u ) {
853
858
ngx_unzip_conf_t * uzcf ;
854
859
ngx_unzip_ctx_t * ctx , * parent ;
860
+ ngx_http_upload_loc_conf_t * ulcf ;
855
861
856
862
uzcf = ngx_http_get_module_loc_conf (u -> request , ngx_http_unzip_filter_module );
863
+ ulcf = ngx_http_get_module_loc_conf (u -> request , ngx_http_upload_module );
857
864
858
865
ctx = ngx_http_get_module_ctx (u -> request , ngx_http_unzip_filter_module );
859
866
@@ -875,12 +882,27 @@ ngx_http_unzip_start_handler(ngx_http_upload_ctx_t *u) {
875
882
876
883
ctx -> state = unzip_state_signature ;
877
884
ctx -> upload_ctx = u ;
878
- ctx -> next_field_filter = ngx_upload_get_next_field_filter (u );
879
- ctx -> next_content_filter = ngx_upload_get_next_content_filter (u );
880
885
881
886
ctx -> pool = u -> request -> pool ;
882
887
ctx -> log = u -> log ;
883
888
889
+ ctx -> next_field_filter = ngx_upload_get_next_field_filter (u );
890
+ ctx -> next_content_filter = ngx_upload_get_next_content_filter (u );
891
+
892
+ ngx_upload_get_archive_elm (u , & ctx -> prev_elm );
893
+
894
+ ngx_upload_get_archive_path (u , & ctx -> prev_archive_path );
895
+
896
+ ngx_upload_get_file_name (u , & ctx -> archive_name );
897
+
898
+ ctx -> entry_no = 0 ;
899
+
900
+ ctx -> current_elm .len = ctx -> prev_elm .len + ulcf -> archive_elm_separator .len + NGX_OFF_T_LEN ;
901
+ ctx -> current_elm .data = ngx_palloc (ctx -> pool , ctx -> current_elm .len );
902
+
903
+ if (ctx -> current_elm .data == NULL )
904
+ return NGX_UPLOAD_NOMEM ;
905
+
884
906
return NGX_OK ;
885
907
} /* }}} */
886
908
@@ -895,6 +917,10 @@ ngx_http_unzip_finish_handler(ngx_http_upload_ctx_t *u) {
895
917
ctx -> decompression_method -> abort (ctx );
896
918
}
897
919
920
+ ngx_upload_set_archive_elm (u , & ctx -> prev_elm );
921
+
922
+ ngx_upload_set_archive_path (u , & ctx -> prev_archive_path );
923
+
898
924
ngx_http_set_ctx (u -> request , ctx -> parent , ngx_http_unzip_filter_module );
899
925
} /* }}} */
900
926
@@ -909,6 +935,10 @@ ngx_http_unzip_abort_handler(ngx_http_upload_ctx_t *u) {
909
935
ctx -> decompression_method -> abort (ctx );
910
936
}
911
937
938
+ ngx_upload_set_archive_elm (u , & ctx -> prev_elm );
939
+
940
+ ngx_upload_set_archive_path (u , & ctx -> prev_archive_path );
941
+
912
942
ngx_http_set_ctx (u -> request , ctx -> parent , ngx_http_unzip_filter_module );
913
943
} /* }}} */
914
944
@@ -1118,22 +1148,40 @@ static ngx_int_t /* {{{ ngx_http_unzip_inflate_process_chain */
1118
1148
ngx_http_unzip_inflate_process_chain (ngx_unzip_ctx_t * ctx , ngx_chain_t * chain ) {
1119
1149
int rc ;
1120
1150
size_t remaining ;
1151
+ int flush ;
1121
1152
1122
1153
while (chain != NULL && !chain -> buf -> last_in_chain ) {
1123
1154
remaining = chain -> buf -> last - chain -> buf -> pos ;
1124
1155
1125
- if (ctx -> current_field_len - ctx -> current_field_pos > remaining )
1156
+ if (ctx -> current_field_len - ctx -> current_field_pos > remaining ) {
1126
1157
ctx -> stream .avail_in = remaining ;
1127
- else
1158
+ flush = Z_NO_FLUSH ;
1159
+ }else {
1128
1160
ctx -> stream .avail_in = ctx -> current_field_len - ctx -> current_field_pos ;
1161
+ flush = Z_SYNC_FLUSH ;
1162
+ }
1129
1163
1130
1164
ctx -> stream .next_in = chain -> buf -> pos ;
1131
1165
1132
1166
do {
1133
1167
ctx -> stream .avail_out = ctx -> output_buffer -> end - ctx -> output_buffer -> start ;
1134
1168
ctx -> stream .next_out = ctx -> output_buffer -> pos = ctx -> output_buffer -> start ;
1135
1169
1136
- rc = inflate (& ctx -> stream , Z_NO_FLUSH );
1170
+ ngx_log_debug5 (NGX_LOG_DEBUG_HTTP , ctx -> log , 0 ,
1171
+ "inflate in: ai:%ud ni:%p ao:%ud no:%p fl:%d" ,
1172
+ ctx -> stream .avail_in , ctx -> stream .next_in ,
1173
+ ctx -> stream .avail_out , ctx -> stream .next_out ,
1174
+ flush
1175
+ );
1176
+
1177
+ rc = inflate (& ctx -> stream , flush );
1178
+
1179
+ ngx_log_debug5 (NGX_LOG_DEBUG_HTTP , ctx -> log , 0 ,
1180
+ "inflate out: ai:%ud ni:%p ao:%ud no:%p rc:%d" ,
1181
+ ctx -> stream .avail_in , ctx -> stream .next_in ,
1182
+ ctx -> stream .avail_out , ctx -> stream .next_out ,
1183
+ rc
1184
+ );
1137
1185
1138
1186
if (rc == Z_OK || rc == Z_STREAM_END ) {
1139
1187
ctx -> output_buffer -> last = ctx -> stream .next_out ;
@@ -1155,7 +1203,7 @@ ngx_http_unzip_inflate_process_chain(ngx_unzip_ctx_t *ctx, ngx_chain_t *chain) {
1155
1203
"inflate() failed: %d" , rc );
1156
1204
return NGX_ERROR ;
1157
1205
}
1158
- }while (ctx -> stream .avail_out == 0 && rc == Z_OK );
1206
+ }while (ctx -> stream .avail_out == 0 && ctx -> stream . avail_in > 0 && rc == Z_OK );
1159
1207
1160
1208
ctx -> current_field_pos += (ctx -> stream .next_in - chain -> buf -> pos );
1161
1209
@@ -1229,6 +1277,37 @@ ngx_http_unzip_extract_process_chain(ngx_unzip_ctx_t *ctx, ngx_chain_t *chain) {
1229
1277
return NGX_OK ;
1230
1278
} /* }}} */
1231
1279
1280
+ static void /* {{{ ngx_http_unzip_set_archive_elm */
1281
+ ngx_http_unzip_set_archive_elm (ngx_unzip_ctx_t * ctx , off_t elm_id ) {
1282
+ ngx_http_upload_loc_conf_t * ulcf ;
1283
+
1284
+ ulcf = ngx_http_get_module_loc_conf (ctx -> upload_ctx -> request , ngx_http_upload_module );
1285
+
1286
+ ctx -> current_elm .len = ngx_sprintf (ctx -> current_elm .data , "%V%V%O" , & ctx -> prev_elm , & ulcf -> archive_elm_separator , elm_id ) - ctx -> current_elm .data ;
1287
+
1288
+ ngx_upload_set_archive_elm (ctx -> upload_ctx , & ctx -> current_elm );
1289
+ } /* }}} */
1290
+
1291
+ static ngx_int_t /* {{{ ngx_http_unzip_set_archive_path */
1292
+ ngx_http_unzip_set_archive_path (ngx_unzip_ctx_t * ctx , ngx_str_t * file_name , ngx_str_t * path ) {
1293
+ ngx_http_upload_loc_conf_t * ulcf ;
1294
+
1295
+ ulcf = ngx_http_get_module_loc_conf (ctx -> upload_ctx -> request , ngx_http_upload_module );
1296
+
1297
+ ctx -> current_archive_path .len = ctx -> prev_archive_path .len + file_name -> len + ulcf -> archive_path_separator .len + path -> len ;
1298
+
1299
+ ctx -> current_archive_path .data = ngx_palloc (ctx -> pool , ctx -> current_archive_path .len );
1300
+
1301
+ if (ctx -> current_archive_path .data == NULL )
1302
+ return NGX_UPLOAD_NOMEM ;
1303
+
1304
+ ctx -> current_archive_path .len = ngx_sprintf (ctx -> current_archive_path .data , "%V%V%V%V" , & ctx -> prev_archive_path , file_name , & ulcf -> archive_path_separator , path ) - ctx -> current_archive_path .data ;
1305
+
1306
+ ngx_upload_set_archive_path (ctx -> upload_ctx , & ctx -> current_archive_path );
1307
+
1308
+ return NGX_OK ;
1309
+ } /* }}} */
1310
+
1232
1311
static ngx_int_t /* {{{ ngx_http_unzip_parse_file_name */
1233
1312
ngx_http_unzip_parse_file_name (ngx_unzip_ctx_t * ctx , ngx_str_t * file_name ) {
1234
1313
u_char * p ;
@@ -1251,11 +1330,7 @@ ngx_http_unzip_parse_file_name(ngx_unzip_ctx_t *ctx, ngx_str_t *file_name) {
1251
1330
archive_path .len = 0 ;
1252
1331
1253
1332
set :
1254
- rc = ngx_upload_set_file_name (ctx -> upload_ctx , & element_name );
1255
-
1256
- if (rc != NGX_OK ) {
1257
- return rc ;
1258
- }
1333
+ ngx_upload_set_file_name (ctx -> upload_ctx , & element_name );
1259
1334
1260
1335
rc = ngx_upload_set_exten (ctx -> upload_ctx , & element_name , & exten );
1261
1336
@@ -1269,18 +1344,16 @@ ngx_http_unzip_parse_file_name(ngx_unzip_ctx_t *ctx, ngx_str_t *file_name) {
1269
1344
return rc ;
1270
1345
}
1271
1346
1272
- rc = ngx_upload_set_content_type (ctx -> upload_ctx , & content_type );
1347
+ ngx_upload_set_content_type (ctx -> upload_ctx , & content_type );
1273
1348
1274
- if (rc != NGX_OK ) {
1275
- return rc ;
1276
- }
1277
-
1278
- rc = ngx_upload_set_archive_path (ctx -> upload_ctx , & archive_path );
1349
+ rc = ngx_http_unzip_set_archive_path (ctx , & ctx -> archive_name , & archive_path );
1279
1350
1280
1351
if (rc != NGX_OK ) {
1281
1352
return rc ;
1282
1353
}
1283
1354
1355
+ ngx_http_unzip_set_archive_elm (ctx , ctx -> entry_no ++ );
1356
+
1284
1357
return NGX_OK ;
1285
1358
} /* }}} */
1286
1359
0 commit comments