📄 mod_deflate.c
字号:
/* Copyright 2002-2005 The Apache Software Foundation or its licensors, as * applicable. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *//* * mod_deflate.c: Perform deflate content-encoding on the fly * * Written by Ian Holsman, Justin Erenkrantz, and Nick Kew *//* * Portions of this software are based upon zlib code by Jean-loup Gailly * (zlib functions gz_open and gzwrite, check_header) *//* zlib flags */#define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */#define HEAD_CRC 0x02 /* bit 1 set: header CRC present */#define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */#define ORIG_NAME 0x08 /* bit 3 set: original file name present */#define COMMENT 0x10 /* bit 4 set: file comment present */#define RESERVED 0xE0 /* bits 5..7: reserved */#include "httpd.h"#include "http_config.h"#include "http_log.h"#include "apr_strings.h"#include "apr_general.h"#include "util_filter.h"#include "apr_buckets.h"#include "http_request.h"#define APR_WANT_STRFUNC#include "apr_want.h"#include "zlib.h"static const char deflateFilterName[] = "DEFLATE";module AP_MODULE_DECLARE_DATA deflate_module;typedef struct deflate_filter_config_t{ int windowSize; int memlevel; int compressionlevel; apr_size_t bufferSize; char *note_ratio_name; char *note_input_name; char *note_output_name;} deflate_filter_config;/* RFC 1952 Section 2.3 defines the gzip header: * * +---+---+---+---+---+---+---+---+---+---+ * |ID1|ID2|CM |FLG| MTIME |XFL|OS | * +---+---+---+---+---+---+---+---+---+---+ */static const char gzip_header[10] ={ '\037', '\213', Z_DEFLATED, 0, 0, 0, 0, 0, /* mtime */ 0, 0x03 /* Unix OS_CODE */};/* magic header */static const char deflate_magic[2] = { '\037', '\213' };/* windowsize is negative to suppress Zlib header */#define DEFAULT_COMPRESSION Z_DEFAULT_COMPRESSION#define DEFAULT_WINDOWSIZE -15#define DEFAULT_MEMLEVEL 9#define DEFAULT_BUFFERSIZE 8096/* Outputs a long in LSB order to the given file * only the bottom 4 bits are required for the deflate file format. */static void putLong(unsigned char *string, unsigned long x){ string[0] = (unsigned char)(x & 0xff); string[1] = (unsigned char)((x & 0xff00) >> 8); string[2] = (unsigned char)((x & 0xff0000) >> 16); string[3] = (unsigned char)((x & 0xff000000) >> 24);}/* Inputs a string and returns a long. */static unsigned long getLong(unsigned char *string){ return ((unsigned long)string[0]) | (((unsigned long)string[1]) << 8) | (((unsigned long)string[2]) << 16) | (((unsigned long)string[3]) << 24);}static void *create_deflate_server_config(apr_pool_t *p, server_rec *s){ deflate_filter_config *c = apr_pcalloc(p, sizeof *c); c->memlevel = DEFAULT_MEMLEVEL; c->windowSize = DEFAULT_WINDOWSIZE; c->bufferSize = DEFAULT_BUFFERSIZE; c->compressionlevel = DEFAULT_COMPRESSION; return c;}static const char *deflate_set_window_size(cmd_parms *cmd, void *dummy, const char *arg){ deflate_filter_config *c = ap_get_module_config(cmd->server->module_config, &deflate_module); int i; i = atoi(arg); if (i < 1 || i > 15) return "DeflateWindowSize must be between 1 and 15"; c->windowSize = i * -1; return NULL;}static const char *deflate_set_buffer_size(cmd_parms *cmd, void *dummy, const char *arg){ deflate_filter_config *c = ap_get_module_config(cmd->server->module_config, &deflate_module); int n = atoi(arg); if (n <= 0) { return "DeflateBufferSize should be positive"; } c->bufferSize = (apr_size_t)n; return NULL;}static const char *deflate_set_note(cmd_parms *cmd, void *dummy, const char *arg1, const char *arg2){ deflate_filter_config *c = ap_get_module_config(cmd->server->module_config, &deflate_module); if (arg2 == NULL) { c->note_ratio_name = apr_pstrdup(cmd->pool, arg1); } else if (!strcasecmp(arg1, "ratio")) { c->note_ratio_name = apr_pstrdup(cmd->pool, arg2); } else if (!strcasecmp(arg1, "input")) { c->note_input_name = apr_pstrdup(cmd->pool, arg2); } else if (!strcasecmp(arg1, "output")) { c->note_output_name = apr_pstrdup(cmd->pool, arg2); } else { return apr_psprintf(cmd->pool, "Unknown note type %s", arg1); } return NULL;}static const char *deflate_set_memlevel(cmd_parms *cmd, void *dummy, const char *arg){ deflate_filter_config *c = ap_get_module_config(cmd->server->module_config, &deflate_module); int i; i = atoi(arg); if (i < 1 || i > 9) return "DeflateMemLevel must be between 1 and 9"; c->memlevel = i; return NULL;}static const char *deflate_set_compressionlevel(cmd_parms *cmd, void *dummy, const char *arg){ deflate_filter_config *c = ap_get_module_config(cmd->server->module_config, &deflate_module); int i; i = atoi(arg); if (i < 1 || i > 9) return "Compression Level must be between 1 and 9"; c->compressionlevel = i; return NULL;}typedef struct deflate_ctx_t{ z_stream stream; unsigned char *buffer; unsigned long crc; apr_bucket_brigade *bb, *proc_bb;} deflate_ctx;static apr_status_t deflate_out_filter(ap_filter_t *f, apr_bucket_brigade *bb){ apr_bucket *e; request_rec *r = f->r; deflate_ctx *ctx = f->ctx; int zRC; deflate_filter_config *c = ap_get_module_config(r->server->module_config, &deflate_module); /* Do nothing if asked to filter nothing. */ if (APR_BRIGADE_EMPTY(bb)) { return APR_SUCCESS; } /* If we don't have a context, we need to ensure that it is okay to send * the deflated content. If we have a context, that means we've done * this before and we liked it. * This could be not so nice if we always fail. But, if we succeed, * we're in better shape. */ if (!ctx) { char *token; const char *encoding; /* only work on main request/no subrequests */ if (r->main != NULL) { ap_remove_output_filter(f); return ap_pass_brigade(f->next, bb); } /* some browsers might have problems, so set no-gzip * (with browsermatch) for them */ if (apr_table_get(r->subprocess_env, "no-gzip")) { ap_remove_output_filter(f); return ap_pass_brigade(f->next, bb); } /* Some browsers might have problems with content types * other than text/html, so set gzip-only-text/html * (with browsermatch) for them */ if (r->content_type == NULL || strncmp(r->content_type, "text/html", 9)) { const char *env_value = apr_table_get(r->subprocess_env, "gzip-only-text/html"); if ( env_value && (strcmp(env_value,"1") == 0) ) { ap_remove_output_filter(f); return ap_pass_brigade(f->next, bb); } } /* Let's see what our current Content-Encoding is. * If it's already encoded, don't compress again. * (We could, but let's not.) */ encoding = apr_table_get(r->headers_out, "Content-Encoding"); if (encoding) { const char *err_enc; err_enc = apr_table_get(r->err_headers_out, "Content-Encoding"); if (err_enc) { encoding = apr_pstrcat(r->pool, encoding, ",", err_enc, NULL); } } else { encoding = apr_table_get(r->err_headers_out, "Content-Encoding"); } if (r->content_encoding) { encoding = encoding ? apr_pstrcat(r->pool, encoding, ",", r->content_encoding, NULL) : r->content_encoding; } if (encoding) { const char *tmp = encoding; token = ap_get_token(r->pool, &tmp, 0); while (token && *token) { /* stolen from mod_negotiation: */ if (strcmp(token, "identity") && strcmp(token, "7bit") && strcmp(token, "8bit") && strcmp(token, "binary")) { ap_remove_output_filter(f); return ap_pass_brigade(f->next, bb); } /* Otherwise, skip token */ if (*tmp) { ++tmp; } token = (*tmp) ? ap_get_token(r->pool, &tmp, 0) : NULL; } } /* Even if we don't accept this request based on it not having * the Accept-Encoding, we need to note that we were looking * for this header and downstream proxies should be aware of that. */ apr_table_mergen(r->headers_out, "Vary", "Accept-Encoding"); /* force-gzip will just force it out regardless if the browser * can actually do anything with it. */ if (!apr_table_get(r->subprocess_env, "force-gzip")) { const char *accepts; /* if they don't have the line, then they can't play */ accepts = apr_table_get(r->headers_in, "Accept-Encoding"); if (accepts == NULL) { ap_remove_output_filter(f); return ap_pass_brigade(f->next, bb); } token = ap_get_token(r->pool, &accepts, 0); while (token && token[0] && strcasecmp(token, "gzip")) { /* skip parameters, XXX: ;q=foo evaluation? */ while (*accepts == ';') { ++accepts; token = ap_get_token(r->pool, &accepts, 1); } /* retrieve next token */ if (*accepts == ',') { ++accepts; } token = (*accepts) ? ap_get_token(r->pool, &accepts, 0) : NULL; } /* No acceptable token found. */ if (token == NULL || token[0] == '\0') { ap_remove_output_filter(f); return ap_pass_brigade(f->next, bb); } } /* For a 304 or 204 response there is no entity included in * the response and hence nothing to deflate. */ if (r->status == HTTP_NOT_MODIFIED || r->status == HTTP_NO_CONTENT) { ap_remove_output_filter(f); return ap_pass_brigade(f->next, bb); } /* We're cool with filtering this. */ ctx = f->ctx = apr_pcalloc(r->pool, sizeof(*ctx)); ctx->bb = apr_brigade_create(r->pool, f->c->bucket_alloc); ctx->buffer = apr_palloc(r->pool, c->bufferSize); zRC = deflateInit2(&ctx->stream, c->compressionlevel, Z_DEFLATED, c->windowSize, c->memlevel, Z_DEFAULT_STRATEGY); if (zRC != Z_OK) { f->ctx = NULL; ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "unable to init Zlib: " "deflateInit2 returned %d: URL %s", zRC, r->uri);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -