forked from evanmiller/mod_zip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngx_http_zip_module.c
774 lines (641 loc) · 26.7 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
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
/*
* 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 size_t ngx_chain_length(ngx_chain_t *chain_link);
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_chain_t* ngx_http_zip_subrequest_range(ngx_http_request_t *r, ngx_chain_t *in,
ngx_http_zip_sr_ctx_t *sr_ctx);
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_send_pieces(ngx_http_request_t *r,
ngx_http_zip_ctx_t *ctx);
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 *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 size_t ngx_chain_length(ngx_chain_t *chain_link)
{
size_t len;
for (len=0; chain_link; chain_link = chain_link->next) {
len += chain_link->buf->last - chain_link->buf->pos;
}
return len;
}
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 == NULL) || (range2 == NULL) ||
!(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_str_t *old_unparsed_request;
ngx_chain_t *chain_link;
size_t len, offset = 0;
old_unparsed_request = ctx->unparsed_request;
len = ngx_chain_length(in);
if (old_unparsed_request != NULL)
len += old_unparsed_request->len;
if ((ctx->unparsed_request = ngx_palloc(r->pool, sizeof(ngx_str_t))) == NULL
|| (ctx->unparsed_request->data = ngx_palloc(r->pool, len)) == NULL) {
return NGX_ERROR;
}
if (old_unparsed_request != NULL) {
ngx_memcpy(ctx->unparsed_request->data, old_unparsed_request->data, old_unparsed_request->len);
offset += old_unparsed_request->len;
}
for (chain_link = in; chain_link; chain_link = chain_link->next ) {
ngx_memcpy(ctx->unparsed_request->data + offset, chain_link->buf->pos, chain_link->buf->last - chain_link->buf->pos);
offset += chain_link->buf->last - chain_link->buf->pos;
}
ctx->unparsed_request->len = offset;
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_upstream_header_variable(r, vv,
(uintptr_t)(&ngx_http_zip_header_variable_name));
} else {
variable_header_status = ngx_http_variable_unknown_header(vv,
&ngx_http_zip_header_variable_name,
&r->headers_out.headers.part, sizeof("upstream_http_") - 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->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)
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) {
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;
r->headers_out.content_type.data = (u_char *)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_ctx_t *ctx;
ngx_http_zip_sr_ctx_t *sr_ctx;
ngx_chain_t *out;
ctx = ngx_http_get_module_ctx(r->main, ngx_http_zip_module);
sr_ctx = ngx_http_get_module_ctx(r, ngx_http_zip_module);
if (ctx && sr_ctx && in) {
if (sr_ctx->range != NULL) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "mod_zip: cutting subrequest to fit range");
if ((out = ngx_http_zip_subrequest_range(r, in, sr_ctx)) == NULL)
return NGX_OK;
return ngx_http_next_body_filter(r, out);
}
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "mod_zip: No range for subrequest to satisfy");
if (sr_ctx->requesting_file->missing_crc32) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "mod_zip: updating CRC-32");
ngx_http_zip_subrequest_update_crc32(in, sr_ctx->requesting_file);
}
}
return ngx_http_next_body_filter(r, in);
}
/* more or less copied from ngx_http_range_filter_module.c */
static ngx_chain_t*
ngx_http_zip_subrequest_range(ngx_http_request_t *r, ngx_chain_t *in,
ngx_http_zip_sr_ctx_t *sr_ctx)
{
ngx_chain_t *out, *cl, **ll;
ngx_buf_t *buf;
ngx_http_zip_range_t *range;
off_t start, last;
range = sr_ctx->range;
out = NULL;
ll = &out;
for (cl = in; cl != NULL; cl = cl->next) {
buf = cl->buf;
start = sr_ctx->subrequest_pos;
last = sr_ctx->subrequest_pos + ngx_buf_size(buf);
sr_ctx->subrequest_pos = last;
ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"mod_zip: Buffer range %O-%O Request range %O-%O",
start, last, range->start, range->end);
if (range->end <= start || range->start >= last) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"mod_zip: range body skip");
if (buf->in_file) {
buf->file_pos = buf->file_last;
}
buf->pos = buf->last;
buf->sync = 1;
continue;
}
if (range->start > start) {
if (buf->in_file) {
buf->file_pos += range->start - start;
}
if (ngx_buf_in_memory(buf)) {
buf->pos += (size_t) (range->start - start);
}
}
if (range->end <= last) {
if (buf->in_file) {
buf->file_last -= last - range->end;
}
if (ngx_buf_in_memory(buf)) {
buf->last -= (size_t) (last - range->end);
}
buf->last_buf = 1;
*ll = cl;
cl->next = NULL;
break;
}
*ll = cl;
ll = &cl->next;
}
return out;
}
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;
while(len--) {
file->crc32 = ngx_crc32_table256[(file->crc32 ^ *p++) & 0xff]
^ (file->crc32 >> 8);
}
}
return NGX_OK;
}
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);
}
if ((rc = ngx_http_send_header(r)) != NGX_OK) {
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_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;
ngx_http_request_t *sr;
ngx_http_zip_sr_ctx_t *sr_ctx;
ngx_int_t rc;
if (piece->type == zip_header_piece) {
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);
}
// need to check if the context has something going on....
if (piece->type == zip_file_piece) {
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "mod_zip: subrequest for \"%V?%V\"", &piece->file->uri, &piece->file->args);
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 \"%V?%V\" done",
&ctx->wait->uri, &ctx->wait->args);
ctx->wait = NULL;
} else {
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;
}
}
rc = ngx_http_subrequest(r, &piece->file->uri, &piece->file->args, &sr, NULL, NGX_HTTP_SUBREQUEST_WAITED);
ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "mod_zip: subrequest for \"%V?%V\" result %d, allocating some mem on main request's pool", &piece->file->uri, &piece->file->args, rc);
if (rc == NGX_ERROR) {
return NGX_ERROR;
}
if ((sr_ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_zip_ctx_t))) == NULL)
return NGX_ERROR;
sr_ctx->requesting_file = piece->file;
sr_ctx->subrequest_pos = piece->range.start;
sr_ctx->range = range;
ngx_http_set_ctx(sr, sr_ctx, ngx_http_zip_module);
ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "mod_zip: subrequest for \"%V?%V\" result %d", &piece->file->uri, &piece->file->args, rc);
if (ctx->wait == NULL) {
ctx->wait = sr;
return NGX_AGAIN; // must be NGX_AGAIN
} else {
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;
}
if (piece->type == zip_trailer_piece) {
if (piece->file->missing_crc32) // should always be true, but if we somehow needed trailer piece - go on
piece->file->crc32 ^= 0xffffffff;
if ((link = ngx_http_zip_data_descriptor_chain_link(r, piece, 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);
}
if (piece->type == zip_central_directory_piece) {
if ((link = ngx_http_zip_central_directory_chain_link(r, ctx, piece, 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);
}
return NGX_ERROR;
}
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 *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);
}
break;
case 1:
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, 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, range);
}
}
break;
default:
while (rc == NGX_OK && ctx->ranges_i < ctx->ranges.nelts) {
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, range->start, range->end, range->boundary_header.len);
rc = ngx_http_zip_send_boundary(r, ctx, 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, 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, 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;
}