70 #if (NGX_HAVE_LITTLE_ENDIAN && NGX_HAVE_NONALIGNED)
104 static void *ngx_http_gzip_filter_alloc(
void *opaque, u_int items,
106 static void ngx_http_gzip_filter_free(
void *opaque,
void *address);
115 static void *ngx_http_gzip_create_conf(
ngx_conf_t *cf);
116 static char *ngx_http_gzip_merge_conf(
ngx_conf_t *cf,
117 void *parent,
void *child);
118 static char *ngx_http_gzip_window(
ngx_conf_t *cf,
void *post,
void *data);
119 static char *ngx_http_gzip_hash(
ngx_conf_t *cf,
void *post,
void *data);
159 &ngx_http_gzip_comp_level_bounds },
166 &ngx_http_gzip_window_p },
173 &ngx_http_gzip_hash_p },
201 ngx_http_gzip_add_variables,
202 ngx_http_gzip_filter_init,
210 ngx_http_gzip_create_conf,
211 ngx_http_gzip_merge_conf
217 &ngx_http_gzip_filter_module_ctx,
218 ngx_http_gzip_filter_commands,
257 return ngx_http_next_header_filter(r);
262 #if (NGX_HTTP_DEGRADATION)
269 return ngx_http_next_header_filter(r);
274 if (!r->gzip_tested) {
275 if (ngx_http_gzip_ok(r) !=
NGX_OK) {
276 return ngx_http_next_header_filter(r);
279 }
else if (!r->gzip_ok) {
280 return ngx_http_next_header_filter(r);
293 ngx_http_gzip_filter_memory(r, ctx);
311 return ngx_http_next_header_filter(r);
325 return ngx_http_next_body_filter(r, in);
343 switch (ngx_http_gzip_filter_buffer(ctx, in)) {
362 if (ngx_http_gzip_filter_deflate_start(r, ctx) !=
NGX_OK) {
377 if (ngx_http_next_body_filter(r, NULL) ==
NGX_ERROR) {
396 rc = ngx_http_gzip_filter_add_data(r, ctx);
409 rc = ngx_http_gzip_filter_get_buf(r, ctx);
420 rc = ngx_http_gzip_filter_deflate(r, ctx);
433 if (ctx->
out == NULL) {
434 ngx_http_gzip_filter_free_copy_buf(r, ctx);
440 if (ngx_http_gzip_filter_gzheader(r, ctx) !=
NGX_OK) {
445 rc = ngx_http_next_body_filter(r, ctx->
out);
451 ngx_http_gzip_filter_free_copy_buf(r, ctx);
476 ngx_http_gzip_filter_free_copy_buf(r, ctx);
522 ctx->
allocated = 8192 + (1 << (wbits + 2)) + (1 << (memlevel + 9));
529 size_t size, buffered;
542 for (cl = ctx->
in; cl; cl = cl->
next) {
610 ctx->
zstream.zalloc = ngx_http_gzip_filter_alloc;
611 ctx->
zstream.zfree = ngx_http_gzip_filter_free;
614 rc = deflateInit2(&ctx->
zstream, (
int) conf->
level, Z_DEFLATED,
619 "deflateInit2() failed: %d", rc);
626 ctx->
crc32 = crc32(0L, Z_NULL, 0);
627 ctx->
flush = Z_NO_FLUSH;
638 static u_char gzheader[10] =
639 { 0x1f, 0x8b, Z_DEFLATED, 0, 0, 0, 0, 0, 0, 3 };
673 "gzip in: %p", ctx->
in);
675 if (ctx->
in == NULL) {
703 "gzip in_buf:%p ni:%p ai:%ud",
708 ctx->
flush = Z_FINISH;
711 ctx->
flush = Z_SYNC_FLUSH;
719 }
else if (ctx->
flush == Z_NO_FLUSH) {
774 "deflate in: ni:%p no:%p ai:%ud ao:%ud fl:%d redo:%d",
781 if (rc != Z_OK && rc != Z_STREAM_END && rc != Z_BUF_ERROR) {
783 "deflate() failed: %d, %d", ctx->
flush, rc);
788 "deflate out: ni:%p no:%p ai:%ud ao:%ud rc:%d",
794 "gzip in_buf:%p pos:%p",
800 if (ctx->
zstream.avail_in == 0) {
807 if (ctx->
zstream.avail_out == 0) {
828 if (ctx->
flush == Z_SYNC_FLUSH) {
830 ctx->
flush = Z_NO_FLUSH;
860 if (rc == Z_STREAM_END) {
862 if (ngx_http_gzip_filter_deflate_end(r, ctx) !=
NGX_OK) {
902 rc = deflateEnd(&ctx->
zstream);
906 "deflateEnd() failed: %d", rc);
922 if (ctx->
zstream.avail_out >= 8) {
948 #if (NGX_HAVE_LITTLE_ENDIAN && NGX_HAVE_NONALIGNED)
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);
979 ngx_http_gzip_filter_alloc(
void *opaque, u_int items, u_int size)
986 alloc = items * size;
988 if (alloc % 512 != 0 && alloc < 8192) {
998 if (alloc <= ctx->allocated) {
1004 "gzip alloc: n:%ud s:%ud a:%ud p:%p",
1005 items, size, alloc, p);
1011 "gzip filter failed to use preallocated memory: %ud of %ud",
1021 ngx_http_gzip_filter_free(
void *opaque,
void *address)
1027 "gzip free: %p", address);
1075 if (ctx == NULL || ctx->
zout == 0) {
1081 if (v->
data == NULL) {
1088 if ((ctx->
zin * 1000 / ctx->
zout) % 10 > 4) {
1138 ngx_http_gzip_merge_conf(
ngx_conf_t *cf,
void *parent,
void *child)
1183 ngx_http_gzip_window(
ngx_conf_t *cf,
void *post,
void *data)
1187 size_t wbits, wsize;
1191 for (wsize = 32 * 1024; wsize > 256; wsize >>= 1) {
1202 return "must be 512, 1k, 2k, 4k, 8k, 16k, or 32k";
1207 ngx_http_gzip_hash(
ngx_conf_t *cf,
void *post,
void *data)
1211 size_t memlevel, hsize;
1215 for (hsize = 128 * 1024; hsize > 256; hsize >>= 1) {
1226 return "must be 512, 1k, 2k, 4k, 8k, 16k, 32k, 64k, or 128k";