278
316
ngx_http_gzip_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
280
int rc, wbits, memlevel;
282
struct gztrailer *trailer;
284
ngx_chain_t *cl, out;
285
ngx_http_gzip_ctx_t *ctx;
286
ngx_http_gzip_conf_t *conf;
320
ngx_http_gzip_ctx_t *ctx;
288
322
ctx = ngx_http_get_module_ctx(r, ngx_http_gzip_filter_module);
290
if (ctx == NULL || ctx->done) {
324
if (ctx == NULL || ctx->done || r->header_only) {
291
325
return ngx_http_next_body_filter(r, in);
294
conf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_filter_module);
328
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
331
if (ctx->buffering) {
334
* With default memory settings zlib starts to output gzipped data
335
* only after it has got about 90K, so it makes sense to allocate
336
* zlib memory (200-400K) only after we have enough data to compress.
337
* Although we copy buffers, nevertheless for not big responses
338
* this allows to allocate zlib memory, to compress and to output
339
* the response in one step using hot CPU cache.
343
switch (ngx_http_gzip_filter_buffer(ctx, in)) {
352
default: /* NGX_ERROR */
296
361
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;
362
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);
368
if (ngx_chain_add_copy(r->pool, &ctx->in, in) != NGX_OK) {
375
/* flush busy buffers */
377
if (ngx_http_next_body_filter(r, NULL) == NGX_ERROR) {
383
ngx_chain_update_chains(r->pool, &ctx->free, &ctx->busy, &cl,
384
(ngx_buf_tag_t) &ngx_http_gzip_filter_module);
390
/* 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;
394
/* cycle while there is data to feed zlib and ... */
396
rc = ngx_http_gzip_filter_add_data(r, ctx);
398
if (rc == NGX_DECLINED) {
402
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;
407
/* ... there are buffers to write zlib output */
409
rc = ngx_http_gzip_filter_get_buf(r, ctx);
411
if (rc == NGX_DECLINED) {
415
if (rc == NGX_ERROR) {
420
rc = ngx_http_gzip_filter_deflate(r, ctx);
426
if (rc == NGX_ERROR) {
430
/* rc == NGX_AGAIN */
649
433
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);
672
ngx_chain_update_chains(&ctx->free, &ctx->busy, &ctx->out,
434
ngx_http_gzip_filter_free_copy_buf(r, ctx);
436
return ctx->busy ? NGX_AGAIN : NGX_OK;
439
if (!ctx->gzheader) {
440
if (ngx_http_gzip_filter_gzheader(r, ctx) != NGX_OK) {
445
rc = ngx_http_next_body_filter(r, ctx->out);
447
if (rc == NGX_ERROR) {
451
ngx_http_gzip_filter_free_copy_buf(r, ctx);
453
ngx_chain_update_chains(r->pool, &ctx->free, &ctx->busy, &ctx->out,
673
454
(ngx_buf_tag_t) &ngx_http_gzip_filter_module);
674
455
ctx->last_out = &ctx->out;
470
if (ctx->preallocated) {
471
deflateEnd(&ctx->zstream);
473
ngx_pfree(r->pool, ctx->preallocated);
476
ngx_http_gzip_filter_free_copy_buf(r, ctx);
483
ngx_http_gzip_filter_memory(ngx_http_request_t *r, ngx_http_gzip_ctx_t *ctx)
486
ngx_http_gzip_conf_t *conf;
488
conf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_filter_module);
491
memlevel = conf->memlevel;
493
if (r->headers_out.content_length_n > 0) {
495
/* the actual zlib window size is smaller by 262 bytes */
497
while (r->headers_out.content_length_n < ((1 << (wbits - 1)) - 262)) {
508
ctx->memlevel = memlevel;
511
* We preallocate a memory for zlib in one buffer (200K-400K), this
512
* decreases a number of malloc() and free() calls and also probably
513
* decreases a number of syscalls (sbrk()/mmap() and so on).
514
* Besides we free the memory as soon as a gzipping will complete
515
* and do not wait while a whole response will be sent to a client.
517
* 8K is for zlib deflate_state, it takes
518
* *) 5816 bytes on i386 and sparc64 (32-bit mode)
519
* *) 5920 bytes on amd64 and sparc64
522
ctx->allocated = 8192 + (1 << (wbits + 2)) + (1 << (memlevel + 9));
527
ngx_http_gzip_filter_buffer(ngx_http_gzip_ctx_t *ctx, ngx_chain_t *in)
529
size_t size, buffered;
531
ngx_chain_t *cl, **ll;
532
ngx_http_request_t *r;
533
ngx_http_gzip_conf_t *conf;
537
r->connection->buffered |= NGX_HTTP_GZIP_BUFFERED;
542
for (cl = ctx->in; cl; cl = cl->next) {
543
buffered += cl->buf->last - cl->buf->pos;
547
conf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_filter_module);
550
cl = ngx_alloc_chain_link(r->pool);
557
size = b->last - b->pos;
560
if (b->flush || b->last_buf || buffered > conf->postpone_gzipping) {
564
if (ctx->buffering && size) {
566
buf = ngx_create_temp_buf(r->pool, size);
571
buf->last = ngx_cpymem(buf->pos, b->pos, size);
574
buf->last_buf = b->last_buf;
575
buf->tag = (ngx_buf_tag_t) &ngx_http_gzip_filter_module;
590
return ctx->buffering ? NGX_OK : NGX_DONE;
595
ngx_http_gzip_filter_deflate_start(ngx_http_request_t *r,
596
ngx_http_gzip_ctx_t *ctx)
599
ngx_http_gzip_conf_t *conf;
601
conf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_filter_module);
603
ctx->preallocated = ngx_palloc(r->pool, ctx->allocated);
604
if (ctx->preallocated == NULL) {
608
ctx->free_mem = ctx->preallocated;
610
ctx->zstream.zalloc = ngx_http_gzip_filter_alloc;
611
ctx->zstream.zfree = ngx_http_gzip_filter_free;
612
ctx->zstream.opaque = ctx;
614
rc = deflateInit2(&ctx->zstream, (int) conf->level, Z_DEFLATED,
615
- ctx->wbits, ctx->memlevel, Z_DEFAULT_STRATEGY);
618
ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
619
"deflateInit2() failed: %d", rc);
623
r->connection->buffered |= NGX_HTTP_GZIP_BUFFERED;
625
ctx->last_out = &ctx->out;
626
ctx->crc32 = crc32(0L, Z_NULL, 0);
627
ctx->flush = Z_NO_FLUSH;
634
ngx_http_gzip_filter_gzheader(ngx_http_request_t *r, ngx_http_gzip_ctx_t *ctx)
638
static u_char gzheader[10] =
639
{ 0x1f, 0x8b, Z_DEFLATED, 0, 0, 0, 0, 0, 0, 3 };
641
b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
648
b->last = b->pos + 10;
650
cl = ngx_alloc_chain_link(r->pool);
666
ngx_http_gzip_filter_add_data(ngx_http_request_t *r, ngx_http_gzip_ctx_t *ctx)
668
if (ctx->zstream.avail_in || ctx->flush != Z_NO_FLUSH || ctx->redo) {
672
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
673
"gzip in: %p", ctx->in);
675
if (ctx->in == NULL) {
682
* to avoid CPU cache trashing we do not free() just quit buf,
683
* but postpone free()ing after zlib compressing and data output
686
ctx->copy_buf->next = ctx->copied;
687
ctx->copied = ctx->copy_buf;
688
ctx->copy_buf = NULL;
691
ctx->in_buf = ctx->in->buf;
693
if (ctx->in_buf->tag == (ngx_buf_tag_t) &ngx_http_gzip_filter_module) {
694
ctx->copy_buf = ctx->in;
697
ctx->in = ctx->in->next;
699
ctx->zstream.next_in = ctx->in_buf->pos;
700
ctx->zstream.avail_in = ctx->in_buf->last - ctx->in_buf->pos;
702
ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
703
"gzip in_buf:%p ni:%p ai:%ud",
705
ctx->zstream.next_in, ctx->zstream.avail_in);
707
if (ctx->in_buf->last_buf) {
708
ctx->flush = Z_FINISH;
710
} else if (ctx->in_buf->flush) {
711
ctx->flush = Z_SYNC_FLUSH;
714
if (ctx->zstream.avail_in) {
716
ctx->crc32 = crc32(ctx->crc32, ctx->zstream.next_in,
717
ctx->zstream.avail_in);
719
} else if (ctx->flush == Z_NO_FLUSH) {
728
ngx_http_gzip_filter_get_buf(ngx_http_request_t *r, ngx_http_gzip_ctx_t *ctx)
730
ngx_http_gzip_conf_t *conf;
732
if (ctx->zstream.avail_out) {
736
conf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_filter_module);
739
ctx->out_buf = ctx->free->buf;
740
ctx->free = ctx->free->next;
742
} else if (ctx->bufs < conf->bufs.num) {
744
ctx->out_buf = ngx_create_temp_buf(r->pool, conf->bufs.size);
745
if (ctx->out_buf == NULL) {
749
ctx->out_buf->tag = (ngx_buf_tag_t) &ngx_http_gzip_filter_module;
750
ctx->out_buf->recycled = 1;
758
ctx->zstream.next_out = ctx->out_buf->pos;
759
ctx->zstream.avail_out = conf->bufs.size;
766
ngx_http_gzip_filter_deflate(ngx_http_request_t *r, ngx_http_gzip_ctx_t *ctx)
771
ngx_http_gzip_conf_t *conf;
773
ngx_log_debug6(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
774
"deflate in: ni:%p no:%p ai:%ud ao:%ud fl:%d redo:%d",
775
ctx->zstream.next_in, ctx->zstream.next_out,
776
ctx->zstream.avail_in, ctx->zstream.avail_out,
777
ctx->flush, ctx->redo);
779
rc = deflate(&ctx->zstream, ctx->flush);
781
if (rc != Z_OK && rc != Z_STREAM_END && rc != Z_BUF_ERROR) {
782
ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
783
"deflate() failed: %d, %d", ctx->flush, rc);
787
ngx_log_debug5(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
788
"deflate out: ni:%p no:%p ai:%ud ao:%ud rc:%d",
789
ctx->zstream.next_in, ctx->zstream.next_out,
790
ctx->zstream.avail_in, ctx->zstream.avail_out,
793
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
794
"gzip in_buf:%p pos:%p",
795
ctx->in_buf, ctx->in_buf->pos);
797
if (ctx->zstream.next_in) {
798
ctx->in_buf->pos = ctx->zstream.next_in;
800
if (ctx->zstream.avail_in == 0) {
801
ctx->zstream.next_in = NULL;
805
ctx->out_buf->last = ctx->zstream.next_out;
807
if (ctx->zstream.avail_out == 0) {
809
/* zlib wants to output some more gzipped data */
811
cl = ngx_alloc_chain_link(r->pool);
816
cl->buf = ctx->out_buf;
819
ctx->last_out = &cl->next;
828
if (ctx->flush == Z_SYNC_FLUSH) {
830
ctx->flush = Z_NO_FLUSH;
832
cl = ngx_alloc_chain_link(r->pool);
839
if (ngx_buf_size(b) == 0) {
841
b = ngx_calloc_buf(ctx->request->pool);
847
ctx->zstream.avail_out = 0;
855
ctx->last_out = &cl->next;
860
if (rc == Z_STREAM_END) {
862
if (ngx_http_gzip_filter_deflate_end(r, ctx) != NGX_OK) {
869
conf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_filter_module);
871
if (conf->no_buffer && ctx->in == NULL) {
873
cl = ngx_alloc_chain_link(r->pool);
878
cl->buf = ctx->out_buf;
881
ctx->last_out = &cl->next;
891
ngx_http_gzip_filter_deflate_end(ngx_http_request_t *r,
892
ngx_http_gzip_ctx_t *ctx)
897
struct gztrailer *trailer;
899
ctx->zin = ctx->zstream.total_in;
900
ctx->zout = 10 + ctx->zstream.total_out + 8;
902
rc = deflateEnd(&ctx->zstream);
905
ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
906
"deflateEnd() failed: %d", rc);
910
ngx_pfree(r->pool, ctx->preallocated);
912
cl = ngx_alloc_chain_link(r->pool);
917
cl->buf = ctx->out_buf;
920
ctx->last_out = &cl->next;
922
if (ctx->zstream.avail_out >= 8) {
923
trailer = (struct gztrailer *) ctx->out_buf->last;
924
ctx->out_buf->last += 8;
925
ctx->out_buf->last_buf = 1;
928
b = ngx_create_temp_buf(r->pool, 8);
935
cl = ngx_alloc_chain_link(r->pool);
943
ctx->last_out = &cl->next;
944
trailer = (struct gztrailer *) b->pos;
948
#if (NGX_HAVE_LITTLE_ENDIAN && NGX_HAVE_NONALIGNED)
950
trailer->crc32 = ctx->crc32;
951
trailer->zlen = ctx->zin;
955
trailer->crc32[0] = (u_char) (ctx->crc32 & 0xff);
956
trailer->crc32[1] = (u_char) ((ctx->crc32 >> 8) & 0xff);
957
trailer->crc32[2] = (u_char) ((ctx->crc32 >> 16) & 0xff);
958
trailer->crc32[3] = (u_char) ((ctx->crc32 >> 24) & 0xff);
960
trailer->zlen[0] = (u_char) (ctx->zin & 0xff);
961
trailer->zlen[1] = (u_char) ((ctx->zin >> 8) & 0xff);
962
trailer->zlen[2] = (u_char) ((ctx->zin >> 16) & 0xff);
963
trailer->zlen[3] = (u_char) ((ctx->zin >> 24) & 0xff);
967
ctx->zstream.avail_in = 0;
968
ctx->zstream.avail_out = 0;
972
r->connection->buffered &= ~NGX_HTTP_GZIP_BUFFERED;