291
304
return ngx_http_next_body_filter(r, in);
294
conf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_filter_module);
307
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
310
if (ctx->buffering) {
313
* With default memory settings zlib starts to output gzipped data
314
* only after it has got about 90K, so it makes sense to allocate
315
* zlib memory (200-400K) only after we have enough data to compress.
316
* Although we copy buffers, nevertheless for not big responses
317
* this allows to allocate zlib memory, to compress and to output
318
* the response in one step using hot CPU cache.
322
switch (ngx_http_gzip_filter_buffer(ctx, in)) {
331
default: /* NGX_ERROR */
296
340
if (ctx->preallocated == NULL) {
298
memlevel = conf->memlevel;
300
if (ctx->length > 0) {
302
/* the actual zlib window size is smaller by 262 bytes */
304
while (ctx->length < ((1 << (wbits - 1)) - 262)) {
311
* We preallocate a memory for zlib in one buffer (200K-400K), this
312
* decreases a number of malloc() and free() calls and also probably
313
* decreases a number of syscalls (sbrk() and so on).
314
* Besides we free this memory as soon as the gzipping will complete
315
* and do not wait while a whole response will be sent to a client.
317
* 8K is for zlib deflate_state, it takes
318
* *) 5816 bytes on i386 and sparc64 (32-bit mode)
319
* *) 5920 bytes on amd64 and sparc64
322
ctx->allocated = 8192 + (1 << (wbits + 2)) + (1 << (memlevel + 9));
324
ctx->preallocated = ngx_palloc(r->pool, ctx->allocated);
325
if (ctx->preallocated == NULL) {
329
ctx->free_mem = ctx->preallocated;
331
ctx->zstream.zalloc = ngx_http_gzip_filter_alloc;
332
ctx->zstream.zfree = ngx_http_gzip_filter_free;
333
ctx->zstream.opaque = ctx;
335
rc = deflateInit2(&ctx->zstream, (int) conf->level, Z_DEFLATED,
336
-wbits, memlevel, Z_DEFAULT_STRATEGY);
339
ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
340
"deflateInit2() failed: %d", rc);
341
ngx_http_gzip_error(ctx);
345
b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
347
ngx_http_gzip_error(ctx);
353
b->last = b->pos + 10;
359
* We pass the gzheader to the next filter now to avoid its linking
360
* to the ctx->busy chain. zlib does not usually output the compressed
361
* data in the initial iterations, so the gzheader that was linked
362
* to the ctx->busy chain would be flushed by ngx_http_write_filter().
365
if (ngx_http_next_body_filter(r, &out) == NGX_ERROR) {
366
ngx_http_gzip_error(ctx);
370
r->connection->buffered |= NGX_HTTP_GZIP_BUFFERED;
372
ctx->last_out = &ctx->out;
374
ctx->crc32 = crc32(0L, Z_NULL, 0);
375
ctx->flush = Z_NO_FLUSH;
341
if (ngx_http_gzip_filter_deflate_start(r, ctx) != NGX_OK) {
379
if (ngx_chain_add_copy(r->pool, &ctx->in, in) == NGX_ERROR) {
380
ngx_http_gzip_error(ctx);
347
if (ngx_chain_add_copy(r->pool, &ctx->in, in) != NGX_OK) {
354
/* flush busy buffers */
356
if (ngx_http_next_body_filter(r, NULL) == NGX_ERROR) {
362
ngx_chain_update_chains(&ctx->free, &ctx->busy, &cl,
363
(ngx_buf_tag_t) &ngx_http_gzip_filter_module);
369
/* cycle while we can write to a client */
391
/* does zlib need a new data ? */
393
if (ctx->zstream.avail_in == 0
394
&& ctx->flush == Z_NO_FLUSH
397
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
398
"gzip in: %p", ctx->in);
400
if (ctx->in == NULL) {
404
ctx->in_buf = ctx->in->buf;
405
ctx->in = ctx->in->next;
407
ctx->zstream.next_in = ctx->in_buf->pos;
408
ctx->zstream.avail_in = ctx->in_buf->last - ctx->in_buf->pos;
410
ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
411
"gzip in_buf:%p ni:%p ai:%ud",
413
ctx->zstream.next_in, ctx->zstream.avail_in);
416
if (ctx->in_buf->last < ctx->in_buf->pos) {
417
ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
418
"zstream.avail_in is huge");
424
if (ctx->in_buf->last_buf) {
425
ctx->flush = Z_FINISH;
427
} else if (ctx->in_buf->flush) {
428
ctx->flush = Z_SYNC_FLUSH;
431
if (ctx->zstream.avail_in == 0) {
432
if (ctx->flush == Z_NO_FLUSH) {
437
ctx->crc32 = crc32(ctx->crc32, ctx->zstream.next_in,
438
ctx->zstream.avail_in);
443
/* is there a space for the gzipped data ? */
445
if (ctx->zstream.avail_out == 0) {
448
ctx->out_buf = ctx->free->buf;
449
ctx->free = ctx->free->next;
451
} else if (ctx->bufs < conf->bufs.num) {
452
ctx->out_buf = ngx_create_temp_buf(r->pool,
454
if (ctx->out_buf == NULL) {
455
ngx_http_gzip_error(ctx);
459
ctx->out_buf->tag = (ngx_buf_tag_t)
460
&ngx_http_gzip_filter_module;
461
ctx->out_buf->recycled = 1;
468
ctx->zstream.next_out = ctx->out_buf->pos;
469
ctx->zstream.avail_out = conf->bufs.size;
472
ngx_log_debug6(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
473
"deflate in: ni:%p no:%p ai:%ud ao:%ud fl:%d redo:%d",
474
ctx->zstream.next_in, ctx->zstream.next_out,
475
ctx->zstream.avail_in, ctx->zstream.avail_out,
476
ctx->flush, ctx->redo);
478
rc = deflate(&ctx->zstream, ctx->flush);
480
if (rc != Z_OK && rc != Z_STREAM_END) {
481
ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
482
"deflate() failed: %d, %d", ctx->flush, rc);
483
ngx_http_gzip_error(ctx);
487
ngx_log_debug5(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
488
"deflate out: ni:%p no:%p ai:%ud ao:%ud rc:%d",
489
ctx->zstream.next_in, ctx->zstream.next_out,
490
ctx->zstream.avail_in, ctx->zstream.avail_out,
493
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
494
"gzip in_buf:%p pos:%p",
495
ctx->in_buf, ctx->in_buf->pos);
498
if (ctx->zstream.next_in) {
499
ctx->in_buf->pos = ctx->zstream.next_in;
501
if (ctx->zstream.avail_in == 0) {
502
ctx->zstream.next_in = NULL;
506
ctx->out_buf->last = ctx->zstream.next_out;
508
if (ctx->zstream.avail_out == 0) {
510
/* zlib wants to output some more gzipped data */
512
cl = ngx_alloc_chain_link(r->pool);
514
ngx_http_gzip_error(ctx);
518
cl->buf = ctx->out_buf;
521
ctx->last_out = &cl->next;
373
/* cycle while there is data to feed zlib and ... */
375
rc = ngx_http_gzip_filter_add_data(r, ctx);
377
if (rc == NGX_DECLINED) {
381
if (rc == NGX_AGAIN) {
530
if (ctx->flush == Z_SYNC_FLUSH) {
532
ctx->zstream.avail_out = 0;
533
ctx->out_buf->flush = 1;
534
ctx->flush = Z_NO_FLUSH;
536
cl = ngx_alloc_chain_link(r->pool);
538
ngx_http_gzip_error(ctx);
542
cl->buf = ctx->out_buf;
545
ctx->last_out = &cl->next;
550
if (rc == Z_STREAM_END) {
552
ctx->zin = ctx->zstream.total_in;
553
ctx->zout = 10 + ctx->zstream.total_out + 8;
555
rc = deflateEnd(&ctx->zstream);
558
ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
559
"deflateEnd() failed: %d", rc);
560
ngx_http_gzip_error(ctx);
564
ngx_pfree(r->pool, ctx->preallocated);
566
cl = ngx_alloc_chain_link(r->pool);
568
ngx_http_gzip_error(ctx);
572
cl->buf = ctx->out_buf;
575
ctx->last_out = &cl->next;
577
if (ctx->zstream.avail_out >= 8) {
578
trailer = (struct gztrailer *) ctx->out_buf->last;
579
ctx->out_buf->last += 8;
580
ctx->out_buf->last_buf = 1;
583
b = ngx_create_temp_buf(r->pool, 8);
585
ngx_http_gzip_error(ctx);
591
cl = ngx_alloc_chain_link(r->pool);
593
ngx_http_gzip_error(ctx);
600
ctx->last_out = &cl->next;
601
trailer = (struct gztrailer *) b->pos;
605
#if (NGX_HAVE_LITTLE_ENDIAN && NGX_HAVE_NONALIGNED)
607
trailer->crc32 = ctx->crc32;
608
trailer->zlen = ctx->zin;
611
trailer->crc32[0] = (u_char) (ctx->crc32 & 0xff);
612
trailer->crc32[1] = (u_char) ((ctx->crc32 >> 8) & 0xff);
613
trailer->crc32[2] = (u_char) ((ctx->crc32 >> 16) & 0xff);
614
trailer->crc32[3] = (u_char) ((ctx->crc32 >> 24) & 0xff);
616
trailer->zlen[0] = (u_char) (ctx->zin & 0xff);
617
trailer->zlen[1] = (u_char) ((ctx->zin >> 8) & 0xff);
618
trailer->zlen[2] = (u_char) ((ctx->zin >> 16) & 0xff);
619
trailer->zlen[3] = (u_char) ((ctx->zin >> 24) & 0xff);
622
ctx->zstream.avail_in = 0;
623
ctx->zstream.avail_out = 0;
627
r->connection->buffered &= ~NGX_HTTP_GZIP_BUFFERED;
632
if (conf->no_buffer && ctx->in == NULL) {
634
cl = ngx_alloc_chain_link(r->pool);
636
ngx_http_gzip_error(ctx);
640
cl->buf = ctx->out_buf;
643
ctx->last_out = &cl->next;
386
/* ... there are buffers to write zlib output */
388
rc = ngx_http_gzip_filter_get_buf(r, ctx);
390
if (rc == NGX_DECLINED) {
394
if (rc == NGX_ERROR) {
399
rc = ngx_http_gzip_filter_deflate(r, ctx);
405
if (rc == NGX_ERROR) {
409
/* rc == NGX_AGAIN */
649
412
if (ctx->out == NULL) {
651
if (last == NGX_AGAIN) {
655
if (ctx->busy == NULL) {
660
last = ngx_http_next_body_filter(r, ctx->out);
663
* we do not check NGX_AGAIN here because the downstream filters
664
* may free some buffers and zlib may compress some data into them
667
if (last == NGX_ERROR) {
668
ngx_http_gzip_error(ctx);
413
ngx_http_gzip_filter_free_copy_buf(r, ctx);
415
return ctx->busy ? NGX_AGAIN : NGX_OK;
418
if (!ctx->gzheader) {
419
if (ngx_http_gzip_filter_gzheader(r, ctx) != NGX_OK) {
424
rc = ngx_http_next_body_filter(r, ctx->out);
426
if (rc == NGX_ERROR) {
430
ngx_http_gzip_filter_free_copy_buf(r, ctx);
672
432
ngx_chain_update_chains(&ctx->free, &ctx->busy, &ctx->out,
673
433
(ngx_buf_tag_t) &ngx_http_gzip_filter_module);
674
434
ctx->last_out = &ctx->out;
449
if (ctx->preallocated) {
450
deflateEnd(&ctx->zstream);
452
ngx_pfree(r->pool, ctx->preallocated);
455
ngx_http_gzip_filter_free_copy_buf(r, ctx);
462
ngx_http_gzip_filter_memory(ngx_http_request_t *r, ngx_http_gzip_ctx_t *ctx)
465
ngx_http_gzip_conf_t *conf;
467
conf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_filter_module);
470
memlevel = conf->memlevel;
472
if (r->headers_out.content_length_n > 0) {
474
/* the actual zlib window size is smaller by 262 bytes */
476
while (r->headers_out.content_length_n < ((1 << (wbits - 1)) - 262)) {
483
ctx->memlevel = memlevel;
486
* We preallocate a memory for zlib in one buffer (200K-400K), this
487
* decreases a number of malloc() and free() calls and also probably
488
* decreases a number of syscalls (sbrk()/mmap() and so on).
489
* Besides we free the memory as soon as a gzipping will complete
490
* and do not wait while a whole response will be sent to a client.
492
* 8K is for zlib deflate_state, it takes
493
* *) 5816 bytes on i386 and sparc64 (32-bit mode)
494
* *) 5920 bytes on amd64 and sparc64
497
ctx->allocated = 8192 + (1 << (wbits + 2)) + (1 << (memlevel + 9));
502
ngx_http_gzip_filter_buffer(ngx_http_gzip_ctx_t *ctx, ngx_chain_t *in)
504
size_t size, buffered;
506
ngx_chain_t *cl, **ll;
507
ngx_http_request_t *r;
508
ngx_http_gzip_conf_t *conf;
512
r->connection->buffered |= NGX_HTTP_GZIP_BUFFERED;
517
for (cl = ctx->in; cl; cl = cl->next) {
518
buffered += cl->buf->last - cl->buf->pos;
522
conf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_filter_module);
525
cl = ngx_alloc_chain_link(r->pool);
532
size = b->last - b->pos;
535
if (b->flush || b->last_buf || buffered > conf->postpone_gzipping) {
539
if (ctx->buffering && size) {
541
buf = ngx_create_temp_buf(r->pool, size);
546
buf->last = ngx_cpymem(buf->pos, b->pos, size);
549
buf->last_buf = b->last_buf;
550
buf->tag = (ngx_buf_tag_t) &ngx_http_gzip_filter_module;
565
return ctx->buffering ? NGX_OK : NGX_DONE;
570
ngx_http_gzip_filter_deflate_start(ngx_http_request_t *r,
571
ngx_http_gzip_ctx_t *ctx)
574
ngx_http_gzip_conf_t *conf;
576
conf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_filter_module);
578
ctx->preallocated = ngx_palloc(r->pool, ctx->allocated);
579
if (ctx->preallocated == NULL) {
583
ctx->free_mem = ctx->preallocated;
585
ctx->zstream.zalloc = ngx_http_gzip_filter_alloc;
586
ctx->zstream.zfree = ngx_http_gzip_filter_free;
587
ctx->zstream.opaque = ctx;
589
rc = deflateInit2(&ctx->zstream, (int) conf->level, Z_DEFLATED,
590
- ctx->wbits, ctx->memlevel, Z_DEFAULT_STRATEGY);
593
ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
594
"deflateInit2() failed: %d", rc);
598
ctx->last_out = &ctx->out;
599
ctx->crc32 = crc32(0L, Z_NULL, 0);
600
ctx->flush = Z_NO_FLUSH;
607
ngx_http_gzip_filter_gzheader(ngx_http_request_t *r, ngx_http_gzip_ctx_t *ctx)
611
static u_char gzheader[10] =
612
{ 0x1f, 0x8b, Z_DEFLATED, 0, 0, 0, 0, 0, 0, 3 };
614
b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
621
b->last = b->pos + 10;
623
cl = ngx_alloc_chain_link(r->pool);
639
ngx_http_gzip_filter_add_data(ngx_http_request_t *r, ngx_http_gzip_ctx_t *ctx)
641
if (ctx->zstream.avail_in || ctx->flush != Z_NO_FLUSH || ctx->redo) {
645
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
646
"gzip in: %p", ctx->in);
648
if (ctx->in == NULL) {
655
* to avoid CPU cache trashing we do not free() just quit buf,
656
* but postpone free()ing after zlib compressing and data output
659
ctx->copy_buf->next = ctx->copied;
660
ctx->copied = ctx->copy_buf;
661
ctx->copy_buf = NULL;
664
ctx->in_buf = ctx->in->buf;
666
if (ctx->in_buf->tag == (ngx_buf_tag_t) &ngx_http_gzip_filter_module) {
667
ctx->copy_buf = ctx->in;
670
ctx->in = ctx->in->next;
672
ctx->zstream.next_in = ctx->in_buf->pos;
673
ctx->zstream.avail_in = ctx->in_buf->last - ctx->in_buf->pos;
675
ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
676
"gzip in_buf:%p ni:%p ai:%ud",
678
ctx->zstream.next_in, ctx->zstream.avail_in);
680
if (ctx->in_buf->last_buf) {
681
ctx->flush = Z_FINISH;
683
} else if (ctx->in_buf->flush) {
684
ctx->flush = Z_SYNC_FLUSH;
687
if (ctx->zstream.avail_in) {
689
ctx->crc32 = crc32(ctx->crc32, ctx->zstream.next_in,
690
ctx->zstream.avail_in);
692
} else if (ctx->flush == Z_NO_FLUSH) {
701
ngx_http_gzip_filter_get_buf(ngx_http_request_t *r, ngx_http_gzip_ctx_t *ctx)
703
ngx_http_gzip_conf_t *conf;
705
if (ctx->zstream.avail_out) {
709
conf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_filter_module);
712
ctx->out_buf = ctx->free->buf;
713
ctx->free = ctx->free->next;
715
} else if (ctx->bufs < conf->bufs.num) {
717
ctx->out_buf = ngx_create_temp_buf(r->pool, conf->bufs.size);
718
if (ctx->out_buf == NULL) {
722
ctx->out_buf->tag = (ngx_buf_tag_t) &ngx_http_gzip_filter_module;
723
ctx->out_buf->recycled = 1;
731
ctx->zstream.next_out = ctx->out_buf->pos;
732
ctx->zstream.avail_out = conf->bufs.size;
739
ngx_http_gzip_filter_deflate(ngx_http_request_t *r, ngx_http_gzip_ctx_t *ctx)
743
ngx_http_gzip_conf_t *conf;
745
ngx_log_debug6(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
746
"deflate in: ni:%p no:%p ai:%ud ao:%ud fl:%d redo:%d",
747
ctx->zstream.next_in, ctx->zstream.next_out,
748
ctx->zstream.avail_in, ctx->zstream.avail_out,
749
ctx->flush, ctx->redo);
751
rc = deflate(&ctx->zstream, ctx->flush);
753
if (rc != Z_OK && rc != Z_STREAM_END) {
754
ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
755
"deflate() failed: %d, %d", ctx->flush, rc);
759
ngx_log_debug5(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
760
"deflate out: ni:%p no:%p ai:%ud ao:%ud rc:%d",
761
ctx->zstream.next_in, ctx->zstream.next_out,
762
ctx->zstream.avail_in, ctx->zstream.avail_out,
765
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
766
"gzip in_buf:%p pos:%p",
767
ctx->in_buf, ctx->in_buf->pos);
769
if (ctx->zstream.next_in) {
770
ctx->in_buf->pos = ctx->zstream.next_in;
772
if (ctx->zstream.avail_in == 0) {
773
ctx->zstream.next_in = NULL;
777
ctx->out_buf->last = ctx->zstream.next_out;
779
if (ctx->zstream.avail_out == 0) {
781
/* zlib wants to output some more gzipped data */
783
cl = ngx_alloc_chain_link(r->pool);
788
cl->buf = ctx->out_buf;
791
ctx->last_out = &cl->next;
800
if (ctx->flush == Z_SYNC_FLUSH) {
802
ctx->zstream.avail_out = 0;
803
ctx->out_buf->flush = 1;
804
ctx->flush = Z_NO_FLUSH;
806
cl = ngx_alloc_chain_link(r->pool);
811
cl->buf = ctx->out_buf;
814
ctx->last_out = &cl->next;
819
if (rc == Z_STREAM_END) {
821
if (ngx_http_gzip_filter_deflate_end(r, ctx) != NGX_OK) {
828
conf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_filter_module);
830
if (conf->no_buffer && ctx->in == NULL) {
832
cl = ngx_alloc_chain_link(r->pool);
837
cl->buf = ctx->out_buf;
840
ctx->last_out = &cl->next;
850
ngx_http_gzip_filter_deflate_end(ngx_http_request_t *r,
851
ngx_http_gzip_ctx_t *ctx)
856
struct gztrailer *trailer;
858
ctx->zin = ctx->zstream.total_in;
859
ctx->zout = 10 + ctx->zstream.total_out + 8;
861
rc = deflateEnd(&ctx->zstream);
864
ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
865
"deflateEnd() failed: %d", rc);
869
ngx_pfree(r->pool, ctx->preallocated);
871
cl = ngx_alloc_chain_link(r->pool);
876
cl->buf = ctx->out_buf;
879
ctx->last_out = &cl->next;
881
if (ctx->zstream.avail_out >= 8) {
882
trailer = (struct gztrailer *) ctx->out_buf->last;
883
ctx->out_buf->last += 8;
884
ctx->out_buf->last_buf = 1;
887
b = ngx_create_temp_buf(r->pool, 8);
894
cl = ngx_alloc_chain_link(r->pool);
902
ctx->last_out = &cl->next;
903
trailer = (struct gztrailer *) b->pos;
907
#if (NGX_HAVE_LITTLE_ENDIAN && NGX_HAVE_NONALIGNED)
909
trailer->crc32 = ctx->crc32;
910
trailer->zlen = ctx->zin;
914
trailer->crc32[0] = (u_char) (ctx->crc32 & 0xff);
915
trailer->crc32[1] = (u_char) ((ctx->crc32 >> 8) & 0xff);
916
trailer->crc32[2] = (u_char) ((ctx->crc32 >> 16) & 0xff);
917
trailer->crc32[3] = (u_char) ((ctx->crc32 >> 24) & 0xff);
919
trailer->zlen[0] = (u_char) (ctx->zin & 0xff);
920
trailer->zlen[1] = (u_char) ((ctx->zin >> 8) & 0xff);
921
trailer->zlen[2] = (u_char) ((ctx->zin >> 16) & 0xff);
922
trailer->zlen[3] = (u_char) ((ctx->zin >> 24) & 0xff);
926
ctx->zstream.avail_in = 0;
927
ctx->zstream.avail_out = 0;
931
r->connection->buffered &= ~NGX_HTTP_GZIP_BUFFERED;