-
Notifications
You must be signed in to change notification settings - Fork 64
/
ngx_http_zip_module.c
758 lines (634 loc) · 27 KB
/
ngx_http_zip_module.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
/*
* mod_zip
*
* Copyright (C) Evan Miller
*
* This module may be distributed under the same terms as Nginx itself.
*/
#include "ngx_http_zip_module.h"
#include "ngx_http_zip_parsers.h"
#include "ngx_http_zip_file.h"
#include "ngx_http_zip_headers.h"
static ngx_chain_t *ngx_chain_last_link(ngx_chain_t *chain_link);
static ngx_int_t ngx_http_zip_discard_chain(ngx_http_request_t *r,
ngx_chain_t *in);
static ngx_int_t ngx_http_zip_ranges_intersect(ngx_http_zip_range_t *range1,
ngx_http_zip_range_t *range2);
static ngx_int_t ngx_http_zip_copy_unparsed_request(ngx_http_request_t *r,
ngx_chain_t *in, ngx_http_zip_ctx_t *ctx);
static ngx_int_t ngx_http_zip_set_headers(ngx_http_request_t *r,
ngx_http_zip_ctx_t *ctx);
static ngx_int_t ngx_http_zip_header_filter(ngx_http_request_t *r);
static ngx_int_t ngx_http_zip_body_filter(ngx_http_request_t *r,
ngx_chain_t *in);
static ngx_int_t ngx_http_zip_main_request_body_filter(ngx_http_request_t *r,
ngx_chain_t *in);
static ngx_int_t ngx_http_zip_subrequest_body_filter(ngx_http_request_t *r,
ngx_chain_t *in);
static ngx_int_t ngx_http_zip_subrequest_update_crc32(ngx_chain_t *in,
ngx_http_zip_file_t *file);
static ngx_int_t ngx_http_zip_subrequest_done(ngx_http_request_t *r, void *data, ngx_int_t rc);
static ngx_int_t ngx_http_zip_send_pieces(ngx_http_request_t *r,
ngx_http_zip_ctx_t *ctx);
static ngx_int_t ngx_http_zip_send_header_piece(ngx_http_request_t *r,
ngx_http_zip_ctx_t *ctx, ngx_http_zip_piece_t *piece, ngx_http_zip_range_t *req_range);
static ngx_int_t ngx_http_zip_send_file_piece(ngx_http_request_t *r,
ngx_http_zip_ctx_t *ctx, ngx_http_zip_piece_t *piece, ngx_http_zip_range_t *req_range);
static ngx_int_t ngx_http_zip_send_directory_piece(ngx_http_request_t *r,
ngx_http_zip_ctx_t *ctx, ngx_http_zip_piece_t *piece, ngx_http_zip_range_t *req_range);
static ngx_int_t ngx_http_zip_send_trailer_piece(ngx_http_request_t *r,
ngx_http_zip_ctx_t *ctx, ngx_http_zip_piece_t *piece, ngx_http_zip_range_t *req_range);
static ngx_int_t ngx_http_zip_send_central_directory_piece(ngx_http_request_t *r,
ngx_http_zip_ctx_t *ctx, ngx_http_zip_piece_t *piece, ngx_http_zip_range_t *req_range);
static ngx_int_t ngx_http_zip_send_piece(ngx_http_request_t *r,
ngx_http_zip_ctx_t *ctx, ngx_http_zip_piece_t *piece, ngx_http_zip_range_t *req_range);
static ngx_int_t ngx_http_zip_send_boundary(ngx_http_request_t *r,
ngx_http_zip_ctx_t *ctx, ngx_http_zip_range_t *range);
static ngx_int_t ngx_http_zip_send_final_boundary(ngx_http_request_t *r,
ngx_http_zip_ctx_t *ctx);
static ngx_int_t ngx_http_zip_init(ngx_conf_t *cf);
static ngx_int_t ngx_http_zip_main_request_header_filter(ngx_http_request_t *r);
static ngx_int_t ngx_http_zip_subrequest_header_filter(ngx_http_request_t *r);
static ngx_str_t ngx_http_zip_header_variable_name = ngx_string("upstream_http_x_archive_files");
static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
static ngx_http_output_body_filter_pt ngx_http_next_body_filter;
static ngx_http_module_t ngx_http_zip_module_ctx = {
NULL, /* preconfiguration */
ngx_http_zip_init, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
NULL, /* create location configuration */
NULL /* merge location configuration */
};
ngx_module_t ngx_http_zip_module = {
NGX_MODULE_V1,
&ngx_http_zip_module_ctx, /* module context */
NULL, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
static ngx_chain_t *ngx_chain_last_link(ngx_chain_t *chain_link)
{
ngx_chain_t *cl;
for (cl = chain_link; cl->next; cl = cl->next) {
/* void */
}
return cl;
}
static ngx_int_t
ngx_http_zip_discard_chain(ngx_http_request_t *r, ngx_chain_t *in)
{
ngx_chain_t *chain_link;
for (chain_link = in; chain_link; chain_link = chain_link->next) {
chain_link->buf->flush = 1;
chain_link->buf->sync = 1;
chain_link->buf->temporary = 0;
chain_link->buf->memory = 0;
chain_link->buf->mmap = 0;
chain_link->buf->last = chain_link->buf->pos;
}
return ngx_http_next_body_filter(r, in);
}
static ngx_int_t
ngx_http_zip_ranges_intersect(ngx_http_zip_range_t *range1, ngx_http_zip_range_t *range2)
{
return !(range1->start >= range2->end || range2->start >= range1->end);
}
static ngx_int_t ngx_http_zip_copy_unparsed_request(ngx_http_request_t *r,
ngx_chain_t *in, ngx_http_zip_ctx_t *ctx)
{
ngx_chain_t *chain_link;
void *buf;
for (chain_link = in; chain_link; chain_link = chain_link->next ) {
buf = ngx_array_push_n(&ctx->unparsed_request, chain_link->buf->last - chain_link->buf->pos);
ngx_memcpy(buf, chain_link->buf->pos, chain_link->buf->last - chain_link->buf->pos);
}
chain_link = ngx_chain_last_link(in);
return chain_link->buf->last_buf ? NGX_OK: NGX_AGAIN;
}
/*
* The header filter looks for "X-Archive-Files: zip" and allocates
* a module context struct if found
*/
static ngx_int_t ngx_http_zip_header_filter(ngx_http_request_t *r)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "mod_zip: entering header filter");
if (r != r->main)
return ngx_http_zip_subrequest_header_filter(r);
return ngx_http_zip_main_request_header_filter(r);
}
static ngx_int_t
ngx_http_zip_main_request_header_filter(ngx_http_request_t *r)
{
ngx_http_variable_value_t *vv;
ngx_http_zip_ctx_t *ctx;
if ((ctx = ngx_http_get_module_ctx(r, ngx_http_zip_module)) != NULL)
return ngx_http_next_header_filter(r);
if ((vv = ngx_palloc(r->pool, sizeof(ngx_http_variable_value_t))) == NULL)
return NGX_ERROR;
/* Look for X-Archive-Files */
ngx_int_t variable_header_status = NGX_OK;
if (r->upstream) {
variable_header_status = ngx_http_zip_variable_unknown_header(r, vv,
&ngx_http_zip_header_variable_name,
&r->upstream->headers_in.headers.part, sizeof("upstream_http_") - 1);
} else if (r->headers_out.status == NGX_HTTP_OK) {
variable_header_status = ngx_http_zip_variable_unknown_header(r, vv,
&ngx_http_zip_header_variable_name,
&r->headers_out.headers.part, sizeof("upstream_http_") - 1);
} else {
vv->not_found = 1;
}
if (variable_header_status != NGX_OK || vv->not_found ||
ngx_strncmp(vv->data, "zip", sizeof("zip") - 1) != 0) {
return ngx_http_next_header_filter(r);
}
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "mod_zip: X-Archive-Files found");
if ((ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_zip_ctx_t))) == NULL
|| ngx_array_init(&ctx->unparsed_request, r->pool, 64 * 1024, 1) == NGX_ERROR
|| ngx_array_init(&ctx->files, r->pool, 1, sizeof(ngx_http_zip_file_t)) == NGX_ERROR
|| ngx_array_init(&ctx->ranges, r->pool, 1, sizeof(ngx_http_zip_range_t)) == NGX_ERROR
|| ngx_array_init(&ctx->pass_srq_headers, r->pool, 1, sizeof(ngx_str_t)) == NGX_ERROR)
return NGX_ERROR;
ngx_http_set_ctx(r, ctx, ngx_http_zip_module);
return NGX_OK;
}
static ngx_int_t
ngx_http_zip_subrequest_header_filter(ngx_http_request_t *r)
{
ngx_http_zip_ctx_t *ctx;
ctx = ngx_http_get_module_ctx(r->main, ngx_http_zip_module);
if (ctx != NULL) {
if (r->headers_out.status != NGX_HTTP_OK &&
r->headers_out.status != NGX_HTTP_PARTIAL_CONTENT) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"mod_zip: a subrequest returned %d, aborting...",
r->headers_out.status);
ctx->abort = 1;
return NGX_ERROR;
}
if (ctx->missing_crc32) {
r->filter_need_in_memory = 1;
}
}
return ngx_http_next_header_filter(r);
}
static ngx_int_t
ngx_http_zip_set_headers(ngx_http_request_t *r, ngx_http_zip_ctx_t *ctx)
{
time_t if_range, last_modified;
if (ngx_http_zip_add_cache_control(r) == NGX_ERROR) {
return NGX_ERROR;
}
r->headers_out.content_type_len = sizeof(NGX_ZIP_MIME_TYPE) - 1;
ngx_str_set(&r->headers_out.content_type, NGX_ZIP_MIME_TYPE);
ngx_http_clear_content_length(r);
if (ctx->missing_crc32) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"mod_zip: Clearing Accept-Ranges header");
ngx_http_clear_accept_ranges(r);
}
r->headers_out.content_length_n = ctx->archive_size;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"mod_zip: Archive will be %O bytes", ctx->archive_size);
if (r->headers_in.range) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"mod_zip: Range found");
if (ctx->missing_crc32) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"mod_zip: Missing checksums, ignoring Range");
return NGX_OK;
}
if (r->headers_in.if_range && r->upstream) {
if_range = ngx_http_parse_time(r->headers_in.if_range->value.data,
r->headers_in.if_range->value.len);
if (if_range == NGX_ERROR) { /* treat as ETag */
if (r->upstream->headers_in.etag) {
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"mod_zip: If-Range = %V, ETag = %V",
&r->headers_in.if_range->value, &r->upstream->headers_in.etag->value);
if (r->upstream->headers_in.etag->value.len != r->headers_in.if_range->value.len
|| ngx_strncmp(r->upstream->headers_in.etag->value.data,
r->headers_in.if_range->value.data,
r->headers_in.if_range->value.len)) {
return NGX_OK;
}
} else {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"mod_zip: No ETag from upstream");
return NGX_OK;
}
} else { /* treat as modification time */
if (r->upstream->headers_in.last_modified) {
last_modified = ngx_http_parse_time(r->upstream->headers_in.last_modified->value.data,
r->upstream->headers_in.last_modified->value.len);
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"mod_zip: If-Range = %d, Last-Modified = %d",
if_range, last_modified);
if (if_range != last_modified && last_modified != -1) {
return NGX_OK;
}
} else {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"mod_zip: No Last-Modified from upstream");
return NGX_OK;
}
}
}
if (ngx_http_zip_parse_range(r, &r->headers_in.range->value, ctx)
== NGX_ERROR) {
r->headers_out.status = NGX_HTTP_RANGE_NOT_SATISFIABLE;
if (ngx_http_zip_add_full_content_range(r) == NGX_ERROR) {
return NGX_ERROR;
}
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"mod_zip: Range not satisfiable");
ctx->ranges.nelts = 0;
return NGX_HTTP_RANGE_NOT_SATISFIABLE;
}
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"mod_zip: Range is satisfiable");
if (ctx->ranges.nelts == 1) {
if (ngx_http_zip_add_partial_content_range(r, ctx) == NGX_ERROR) {
return NGX_ERROR;
}
} else {
if (ngx_http_zip_init_multipart_range(r, ctx) == NGX_ERROR) {
return NGX_ERROR;
}
}
r->headers_out.status = NGX_HTTP_PARTIAL_CONTENT;
r->headers_out.status_line.len = 0;
}
return NGX_OK;
}
static ngx_int_t
ngx_http_zip_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
{
if (r != r->main) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "mod_zip: entering subrequest body filter");
return ngx_http_zip_subrequest_body_filter(r, in);
}
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "mod_zip: entering main request body filter");
return ngx_http_zip_main_request_body_filter(r, in);
}
static ngx_int_t
ngx_http_zip_subrequest_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
{
ngx_http_zip_sr_ctx_t *sr_ctx;
sr_ctx = ngx_http_get_module_ctx(r, ngx_http_zip_module);
if (in && sr_ctx && sr_ctx->requesting_file->missing_crc32) {
uint32_t old_crc32 = sr_ctx->requesting_file->crc32;
ngx_http_zip_subrequest_update_crc32(in, sr_ctx->requesting_file);
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"mod_zip: updated CRC-32 (%08Xd -> %08Xd)", old_crc32, sr_ctx->requesting_file->crc32);
(void)old_crc32;
}
return ngx_http_next_body_filter(r, in);
}
static ngx_int_t
ngx_http_zip_subrequest_update_crc32(ngx_chain_t *in,
ngx_http_zip_file_t *file)
{
ngx_chain_t *cl;
size_t len;
u_char *p;
if (file == NULL)
return NGX_ERROR;
for (cl = in; cl != NULL; cl = cl->next) {
p = cl->buf->pos;
len = cl->buf->last - p;
ngx_crc32_update(&file->crc32, p, len);
}
return NGX_OK;
}
static ngx_int_t
ngx_http_zip_subrequest_done(ngx_http_request_t *r, void *data, ngx_int_t rc)
{
ngx_http_zip_piece_t *piece = (ngx_http_zip_piece_t *)data;
(void)piece; /* fix warning */
ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"mod_zip: subrequest for \"%V?%V\" done, result %d",
&piece->file->uri, &piece->file->args, rc);
return rc;
}
static ngx_int_t
ngx_http_zip_main_request_body_filter(ngx_http_request_t *r,
ngx_chain_t *in)
{
ngx_http_zip_ctx_t *ctx;
ngx_chain_t *chain_link;
int rc;
ctx = ngx_http_get_module_ctx(r, ngx_http_zip_module);
if (ctx == NULL || ctx->trailer_sent) {
return ngx_http_next_body_filter(r, in);
}
if (ctx->abort) {
return NGX_ERROR;
}
if (r->headers_out.status != NGX_HTTP_OK &&
r->headers_out.status != NGX_HTTP_PARTIAL_CONTENT) {
return ngx_http_next_body_filter(r, in);
}
if (ctx->parsed) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"mod_zip: restarting subrequests");
return ngx_http_zip_send_pieces(r, ctx);
}
if (in == NULL) {
return ngx_http_next_body_filter(r, NULL);
}
rc = ngx_http_zip_copy_unparsed_request(r, in, ctx);
if (rc == NGX_AGAIN) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"mod_zip: not the last buf");
return ngx_http_zip_discard_chain(r, in);
} else if (rc == NGX_ERROR) {
return NGX_ERROR;
}
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"mod_zip: about to parse list");
if (ngx_http_zip_parse_request(ctx) == NGX_ERROR) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"mod_zip: invalid file list from upstream");
return NGX_ERROR;
}
if (ngx_http_zip_generate_pieces(r, ctx) == NGX_ERROR) {
return NGX_ERROR;
}
if (!r->header_sent) {
rc = ngx_http_zip_set_headers(r, ctx);
if (rc == NGX_ERROR) {
return NGX_ERROR;
}
if (rc == NGX_HTTP_RANGE_NOT_SATISFIABLE) {
return ngx_http_special_response_handler(r, rc);
}
rc = ngx_http_send_header(r);
if (rc != NGX_OK &&
!(rc == NGX_AGAIN && r->connection->buffered)) {
return rc;
}
}
chain_link = ngx_chain_last_link(in);
chain_link->buf->last_buf = 0;
if (ngx_http_zip_strip_range_header(r) == NGX_ERROR) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"mod_zip: failed to strip Range: header from request");
return NGX_ERROR;
}
return ngx_http_zip_send_pieces(r, ctx);
}
static ngx_int_t
ngx_http_zip_send_header_piece(ngx_http_request_t *r, ngx_http_zip_ctx_t *ctx,
ngx_http_zip_piece_t *piece, ngx_http_zip_range_t *range)
{
ngx_chain_t *link;
if ((link = ngx_http_zip_file_header_chain_link(r, ctx, piece, range)) == NULL)
return NGX_ERROR;
return ngx_http_next_body_filter(r, link);
}
static ngx_int_t
ngx_http_zip_send_file_piece(ngx_http_request_t *r, ngx_http_zip_ctx_t *ctx,
ngx_http_zip_piece_t *piece, ngx_http_zip_range_t *req_range)
{
ngx_http_zip_sr_ctx_t *sr_ctx;
ngx_http_request_t *sr;
ngx_http_post_subrequest_t *ps;
ngx_int_t rc;
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"mod_zip: subrequest for \"%V?%V\"", &piece->file->uri, &piece->file->args);
// need to check if the context has something going on....
if (ctx->wait) {
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"mod_zip: have a wait context for \"%V?%V\"",
&ctx->wait->uri, &ctx->wait->args);
if (!ctx->wait->done) {
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"mod_zip: wait NOT DONE \"%V?%V\"",
&ctx->wait->uri, &ctx->wait->args);
return NGX_AGAIN;
}
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"mod_zip: wait \"%V?%V\" done",
&ctx->wait->uri, &ctx->wait->args);
ctx->wait = NULL;
}
ps = ngx_palloc(r->pool, sizeof(ngx_http_post_subrequest_t));
if (ps == NULL) {
return NGX_ERROR;
}
ps->handler = ngx_http_zip_subrequest_done;
ps->data = piece;
rc = ngx_http_subrequest(r, &piece->file->uri, &piece->file->args, &sr, ps, NGX_HTTP_SUBREQUEST_WAITED);
ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"mod_zip: subrequest for \"%V?%V\" initiated, result %d",
&piece->file->uri, &piece->file->args, rc);
if (rc == NGX_ERROR) {
return NGX_ERROR;
}
sr->allow_ranges = 1;
sr->subrequest_ranges = 1;
sr->single_range = 1;
rc = ngx_http_zip_init_subrequest_headers(r, ctx, sr, &piece->range, req_range);
if (sr->headers_in.range) {
ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"mod_zip: subrequest for \"%V?%V\" Range: %V",
&piece->file->uri, &piece->file->args, &sr->headers_in.range->value);
}
if (rc == NGX_ERROR) {
return NGX_ERROR;
}
if ((sr_ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_zip_sr_ctx_t))) == NULL) {
return NGX_ERROR;
}
sr_ctx->requesting_file = piece->file;
ngx_http_set_ctx(sr, sr_ctx, ngx_http_zip_module);
if (ctx->wait) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"mod_zip : only one subrequest may be waited at the same time; ");
return NGX_ERROR;
}
ctx->wait = sr;
return NGX_AGAIN; // must be NGX_AGAIN
}
static ngx_int_t ngx_http_zip_send_directory_piece(ngx_http_request_t *r,
ngx_http_zip_ctx_t *ctx, ngx_http_zip_piece_t *piece, ngx_http_zip_range_t *req_range)
{
// Directory has no data.
return NGX_OK;
}
static ngx_int_t
ngx_http_zip_send_trailer_piece(ngx_http_request_t *r, ngx_http_zip_ctx_t *ctx,
ngx_http_zip_piece_t *piece, ngx_http_zip_range_t *req_range)
{
ngx_chain_t *link;
if (piece->file->missing_crc32) { // should always be true, but if we somehow needed trailer piece - go on
uint32_t old_crc32 = piece->file->crc32;
ngx_crc32_final(piece->file->crc32);
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"mod_zip: finalized CRC-32 (%08Xd -> %08Xd)", old_crc32, piece->file->crc32);
(void)old_crc32;
}
if ((link = ngx_http_zip_data_descriptor_chain_link(r, piece, req_range)) == NULL) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "mod_zip: data descriptor failed");
return NGX_ERROR;
}
return ngx_http_next_body_filter(r, link);
}
static ngx_int_t
ngx_http_zip_send_central_directory_piece(ngx_http_request_t *r, ngx_http_zip_ctx_t *ctx,
ngx_http_zip_piece_t *piece, ngx_http_zip_range_t *req_range)
{
ngx_chain_t *link;
if ((link = ngx_http_zip_central_directory_chain_link(r, ctx, piece, req_range)) == NULL) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "mod_zip: CD piece failed");
return NGX_ERROR;
}
return ngx_http_next_body_filter(r, link);
}
static ngx_int_t
ngx_http_zip_send_piece(ngx_http_request_t *r, ngx_http_zip_ctx_t *ctx,
ngx_http_zip_piece_t *piece, ngx_http_zip_range_t *req_range)
{
ngx_int_t rc = NGX_ERROR;
if (piece->type == zip_header_piece) {
rc = ngx_http_zip_send_header_piece(r, ctx, piece, req_range);
} else if (piece->type == zip_file_piece) {
rc = ngx_http_zip_send_file_piece(r, ctx, piece, req_range);
} else if (piece->type == zip_dir_piece) {
rc = ngx_http_zip_send_directory_piece(r, ctx, piece, req_range);
} else if (piece->type == zip_trailer_piece) {
rc = ngx_http_zip_send_trailer_piece(r, ctx, piece, req_range);
} else if (piece->type == zip_central_directory_piece) {
rc = ngx_http_zip_send_central_directory_piece(r, ctx, piece, req_range);
}
return rc;
}
static ngx_int_t
ngx_http_zip_send_boundary(ngx_http_request_t *r, ngx_http_zip_ctx_t *ctx,
ngx_http_zip_range_t *range)
{
ngx_chain_t *link;
ngx_buf_t *b;
if (range->boundary_sent)
return NGX_OK;
if ((link = ngx_alloc_chain_link(r->pool)) == NULL
|| (b = ngx_calloc_buf(r->pool)) == NULL)
return NGX_ERROR;
b->memory = 1;
b->pos = range->boundary_header.data;
b->last = b->pos + range->boundary_header.len;
link->buf = b;
link->next = NULL;
range->boundary_sent = 1;
return ngx_http_next_body_filter(r, link);
}
static ngx_int_t
ngx_http_zip_send_final_boundary(ngx_http_request_t *r, ngx_http_zip_ctx_t *ctx)
{
size_t len;
ngx_chain_t *link;
ngx_buf_t *b;
if ((link = ngx_alloc_chain_link(r->pool)) == NULL
|| (b = ngx_calloc_buf(r->pool)) == NULL)
return NGX_ERROR;
len = sizeof(CRLF "--") - 1 + NGX_ATOMIC_T_LEN + sizeof("--" CRLF) - 1;
b->memory = 1;
if ((b->pos = ngx_palloc(r->pool, len)) == NULL)
return NGX_ERROR;
b->last = ngx_sprintf(b->pos, CRLF "--%0muA--" CRLF, ctx->boundary);
link->buf = b;
link->next = NULL;
return ngx_http_next_body_filter(r, link);
}
/* Initiate one or more subrequests for files to put in the ZIP archive */
static ngx_int_t
ngx_http_zip_send_pieces(ngx_http_request_t *r,
ngx_http_zip_ctx_t *ctx)
{
ngx_int_t rc = NGX_OK, pieces_sent = 0;
ngx_http_zip_piece_t *piece;
ngx_http_zip_range_t *req_range = NULL;
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"mod_zip: sending pieces, starting with piece %d of total %d", ctx->pieces_i, ctx->pieces_n);
switch(ctx->ranges.nelts) {
case 0:
while (rc == NGX_OK && ctx->pieces_i < ctx->pieces_n) {
piece = &ctx->pieces[ctx->pieces_i++];
pieces_sent++;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "mod_zip: no ranges / sending piece type %d", piece->type);
rc = ngx_http_zip_send_piece(r, ctx, piece, NULL);
if (rc == NGX_AGAIN && r->connection->buffered && !r->postponed) {
rc = NGX_OK;
}
}
break;
case 1:
req_range = &((ngx_http_zip_range_t *)ctx->ranges.elts)[0];
while (rc == NGX_OK && ctx->pieces_i < ctx->pieces_n) {
piece = &ctx->pieces[ctx->pieces_i++];
if (ngx_http_zip_ranges_intersect(&piece->range, req_range)) {
pieces_sent++;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "mod_zip: 1 range / sending piece type %d", piece->type);
rc = ngx_http_zip_send_piece(r, ctx, piece, req_range);
}
}
break;
default:
while (rc == NGX_OK && ctx->ranges_i < ctx->ranges.nelts) {
req_range = &((ngx_http_zip_range_t *)ctx->ranges.elts)[ctx->ranges_i];
ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"mod_zip: sending range #%d start=%O end=%O (size %d)",
ctx->ranges_i, req_range->start, req_range->end, req_range->boundary_header.len);
rc = ngx_http_zip_send_boundary(r, ctx, req_range);
while (rc == NGX_OK && ctx->pieces_i < ctx->pieces_n) {
piece = &ctx->pieces[ctx->pieces_i++];
if (ngx_http_zip_ranges_intersect(&piece->range, req_range)) {
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"mod_zip: sending range=%d piece=%d",
ctx->ranges_i, pieces_sent);
pieces_sent++;
rc = ngx_http_zip_send_piece(r, ctx, piece, req_range);
}
}
if (rc == NGX_OK) {
ctx->ranges_i++;
ctx->pieces_i = 0;
}
}
if (rc == NGX_OK) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"mod_zip: sending final boundary");
rc = ngx_http_zip_send_final_boundary(r, ctx);
}
break;
}
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"mod_zip: sent %d pieces, last rc = %d", pieces_sent, rc);
if (rc == NGX_OK) {
ctx->trailer_sent = 1;
return ngx_http_send_special(r, NGX_HTTP_LAST);
}
/* NGX_DONE, NGX_AGAIN or NGX_ERROR */
return rc;
}
/* Install the module filters */
static ngx_int_t
ngx_http_zip_init(ngx_conf_t *cf)
{
ngx_http_next_header_filter = ngx_http_top_header_filter;
ngx_http_top_header_filter = ngx_http_zip_header_filter;
ngx_http_next_body_filter = ngx_http_top_body_filter;
ngx_http_top_body_filter = ngx_http_zip_body_filter;
return NGX_OK;
}