📄 ngx_http_gzip_filter_module.c
字号:
} expires = ngx_http_parse_time(r->headers_out.expires->value.data, r->headers_out.expires->value.len); if (expires == NGX_ERROR) { return NGX_DECLINED; } if (r->headers_out.date) { date = ngx_http_parse_time(r->headers_out.date->value.data, r->headers_out.date->value.len); if (date == NGX_ERROR) { return NGX_DECLINED; } } else { date = ngx_time(); } if (expires < date) { return NGX_OK; } return NGX_DECLINED; } if (r->headers_out.cache_control.elts) { if ((conf->proxied & NGX_HTTP_GZIP_PROXIED_NO_CACHE) && ngx_http_parse_multi_header_lines(&r->headers_out.cache_control, &ngx_http_gzip_no_cache, NULL) >= 0) { return NGX_OK; } if ((conf->proxied & NGX_HTTP_GZIP_PROXIED_NO_STORE) && ngx_http_parse_multi_header_lines(&r->headers_out.cache_control, &ngx_http_gzip_no_store, NULL) >= 0) { return NGX_OK; } if ((conf->proxied & NGX_HTTP_GZIP_PROXIED_PRIVATE) && ngx_http_parse_multi_header_lines(&r->headers_out.cache_control, &ngx_http_gzip_private, NULL) >= 0) { return NGX_OK; } return NGX_DECLINED; } if ((conf->proxied & NGX_HTTP_GZIP_PROXIED_NO_LM) && r->headers_out.last_modified) { return NGX_DECLINED; } if ((conf->proxied & NGX_HTTP_GZIP_PROXIED_NO_ETAG) && r->headers_out.etag) { return NGX_DECLINED; } return NGX_OK;}static ngx_int_tngx_http_gzip_body_filter(ngx_http_request_t *r, ngx_chain_t *in){ int rc, wbits, memlevel; ngx_int_t last; struct gztrailer *trailer; ngx_buf_t *b; ngx_chain_t *cl, out; ngx_http_gzip_ctx_t *ctx; ngx_http_gzip_conf_t *conf; ctx = ngx_http_get_module_ctx(r, ngx_http_gzip_filter_module); if (ctx == NULL || ctx->done) { return ngx_http_next_body_filter(r, in); } conf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_filter_module); if (ctx->preallocated == NULL) { wbits = conf->wbits; memlevel = conf->memlevel; if (ctx->length > 0) { /* the actual zlib window size is smaller by 262 bytes */ while (ctx->length < ((1 << (wbits - 1)) - 262)) { wbits--; memlevel--; } } /* * We preallocate a memory for zlib in one buffer (200K-400K), this * decreases a number of malloc() and free() calls and also probably * decreases a number of syscalls (sbrk() and so on). * Besides we free this memory as soon as the gzipping will complete * and do not wait while a whole response will be sent to a client. * * 8K is for zlib deflate_state, it takes * *) 5816 bytes on i386 and sparc64 (32-bit mode) * *) 5920 bytes on amd64 and sparc64 */ ctx->allocated = 8192 + (1 << (wbits + 2)) + (1 << (memlevel + 9)); ctx->preallocated = ngx_palloc(r->pool, ctx->allocated); if (ctx->preallocated == NULL) { return NGX_ERROR; } ctx->free_mem = ctx->preallocated; ctx->zstream.zalloc = ngx_http_gzip_filter_alloc; ctx->zstream.zfree = ngx_http_gzip_filter_free; ctx->zstream.opaque = ctx; rc = deflateInit2(&ctx->zstream, (int) conf->level, Z_DEFLATED, -wbits, memlevel, Z_DEFAULT_STRATEGY); if (rc != Z_OK) { ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, "deflateInit2() failed: %d", rc); ngx_http_gzip_error(ctx); return NGX_ERROR; } b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)); if (b == NULL) { ngx_http_gzip_error(ctx); return NGX_ERROR; } b->memory = 1; b->pos = gzheader; b->last = b->pos + 10; out.buf = b; out.next = NULL; /* * We pass the gzheader to the next filter now to avoid its linking * to the ctx->busy chain. zlib does not usually output the compressed * data in the initial iterations, so the gzheader that was linked * to the ctx->busy chain would be flushed by ngx_http_write_filter(). */ if (ngx_http_next_body_filter(r, &out) == NGX_ERROR) { ngx_http_gzip_error(ctx); return NGX_ERROR; } r->connection->buffered |= NGX_HTTP_GZIP_BUFFERED; ctx->last_out = &ctx->out; ctx->crc32 = crc32(0L, Z_NULL, 0); ctx->flush = Z_NO_FLUSH; } if (in) { if (ngx_chain_add_copy(r->pool, &ctx->in, in) == NGX_ERROR) { ngx_http_gzip_error(ctx); return NGX_ERROR; } } last = NGX_NONE; for ( ;; ) { for ( ;; ) { /* does zlib need a new data ? */ if (ctx->zstream.avail_in == 0 && ctx->flush == Z_NO_FLUSH && !ctx->redo) { ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "gzip in: %p", ctx->in); if (ctx->in == NULL) { break; } ctx->in_buf = ctx->in->buf; ctx->in = ctx->in->next; ctx->zstream.next_in = ctx->in_buf->pos; ctx->zstream.avail_in = ctx->in_buf->last - ctx->in_buf->pos; ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "gzip in_buf:%p ni:%p ai:%ud", ctx->in_buf, ctx->zstream.next_in, ctx->zstream.avail_in); /* STUB */ if (ctx->in_buf->last < ctx->in_buf->pos) { ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, "zstream.avail_in is huge"); ctx->done = 1; return NGX_ERROR; } /**/ if (ctx->in_buf->last_buf) { ctx->flush = Z_FINISH; } else if (ctx->in_buf->flush) { ctx->flush = Z_SYNC_FLUSH; } if (ctx->zstream.avail_in == 0) { if (ctx->flush == Z_NO_FLUSH) { continue; } } else { ctx->crc32 = crc32(ctx->crc32, ctx->zstream.next_in, ctx->zstream.avail_in); } } /* is there a space for the gzipped data ? */ if (ctx->zstream.avail_out == 0) { if (ctx->free) { ctx->out_buf = ctx->free->buf; ctx->free = ctx->free->next; } else if (ctx->bufs < conf->bufs.num) { ctx->out_buf = ngx_create_temp_buf(r->pool, conf->bufs.size); if (ctx->out_buf == NULL) { ngx_http_gzip_error(ctx); return NGX_ERROR; } ctx->out_buf->tag = (ngx_buf_tag_t) &ngx_http_gzip_filter_module; ctx->out_buf->recycled = 1; ctx->bufs++; } else { break; } ctx->zstream.next_out = ctx->out_buf->pos; ctx->zstream.avail_out = conf->bufs.size; } ngx_log_debug6(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "deflate in: ni:%p no:%p ai:%ud ao:%ud fl:%d redo:%d", ctx->zstream.next_in, ctx->zstream.next_out, ctx->zstream.avail_in, ctx->zstream.avail_out, ctx->flush, ctx->redo); rc = deflate(&ctx->zstream, ctx->flush); if (rc != Z_OK && rc != Z_STREAM_END) { ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, "deflate() failed: %d, %d", ctx->flush, rc); ngx_http_gzip_error(ctx); return NGX_ERROR; } ngx_log_debug5(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "deflate out: ni:%p no:%p ai:%ud ao:%ud rc:%d", ctx->zstream.next_in, ctx->zstream.next_out, ctx->zstream.avail_in, ctx->zstream.avail_out, rc); ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "gzip in_buf:%p pos:%p", ctx->in_buf, ctx->in_buf->pos); if (ctx->zstream.next_in) { ctx->in_buf->pos = ctx->zstream.next_in; if (ctx->zstream.avail_in == 0) { ctx->zstream.next_in = NULL; } } ctx->out_buf->last = ctx->zstream.next_out; if (ctx->zstream.avail_out == 0) { /* zlib wants to output some more gzipped data */ cl = ngx_alloc_chain_link(r->pool); if (cl == NULL) { ngx_http_gzip_error(ctx); return NGX_ERROR; } cl->buf = ctx->out_buf; cl->next = NULL; *ctx->last_out = cl; ctx->last_out = &cl->next; ctx->redo = 1; continue; } ctx->redo = 0; if (ctx->flush == Z_SYNC_FLUSH) { ctx->zstream.avail_out = 0; ctx->out_buf->flush = 1; ctx->flush = Z_NO_FLUSH; cl = ngx_alloc_chain_link(r->pool); if (cl == NULL) { ngx_http_gzip_error(ctx); return NGX_ERROR; } cl->buf = ctx->out_buf; cl->next = NULL; *ctx->last_out = cl; ctx->last_out = &cl->next; break; } if (rc == Z_STREAM_END) { ctx->zin = ctx->zstream.total_in; ctx->zout = 10 + ctx->zstream.total_out + 8; rc = deflateEnd(&ctx->zstream); if (rc != Z_OK) { ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, "deflateEnd() failed: %d", rc); ngx_http_gzip_error(ctx); return NGX_ERROR; } ngx_pfree(r->pool, ctx->preallocated); cl = ngx_alloc_chain_link(r->pool); if (cl == NULL) { ngx_http_gzip_error(ctx); return NGX_ERROR; } cl->buf = ctx->out_buf; cl->next = NULL; *ctx->last_out = cl; ctx->last_out = &cl->next; if (ctx->zstream.avail_out >= 8) { trailer = (struct gztrailer *) ctx->out_buf->last; ctx->out_buf->last += 8; ctx->out_buf->last_buf = 1; } else { b = ngx_create_temp_buf(r->pool, 8); if (b == NULL) { ngx_http_gzip_error(ctx); return NGX_ERROR; } b->last_buf = 1; cl = ngx_alloc_chain_link(r->pool); if (cl == NULL) { ngx_http_gzip_error(ctx); return NGX_ERROR; } cl->buf = b; cl->next = NULL; *ctx->last_out = cl; ctx->last_out = &cl->next; trailer = (struct gztrailer *) b->pos; b->last += 8; }#if (NGX_HAVE_LITTLE_ENDIAN && NGX_HAVE_NONALIGNED) trailer->crc32 = ctx->crc32;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -