mod_deflate.c revision a4b3fb28f3d2e0983b15b4c6828c6980f2fc9b15
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000-2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
*
* Portions of this software are based upon public domain software
* (zlib functions gz_open and gzwrite)
*/
/*
* mod_deflate.c: Perform deflate transfer-encoding on the fly
*
* Written by Ian Holsman (IanH@apache.org)
*
*/
#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"
#include "zlib.h"
#ifdef HAVE_ZUTIL_H
#include "zutil.h"
#else
/* As part of the encoding process, we must send what our OS_CODE is
* (or so it seems based on what I can tell of how gzip encoding works).
*
* zutil.h is not always included with zlib distributions (it is a private
* header), so this is straight from zlib 1.1.3's zutil.h.
*/
#ifdef OS2
# define OS_CODE 0x06
#endif
#ifdef WIN32 /* Window 95 & Windows NT */
# define OS_CODE 0x0b
#endif
# define OS_CODE 0x02
#endif
#ifdef AMIGA
# define OS_CODE 0x01
#endif
# define OS_CODE 0x05
#endif
#if defined(MACOS) || defined(TARGET_OS_MAC)
# define OS_CODE 0x07
#endif
#ifdef __50SERIES /* Prime/PRIMOS */
# define OS_CODE 0x0F
#endif
#ifdef TOPS20
# define OS_CODE 0x0a
#endif
#ifndef OS_CODE
#endif
#endif
static const char deflateFilterName[] = "DEFLATE";
typedef struct deflate_filter_config_t
{
int windowSize;
int memlevel;
char *noteName;
/* windowsize is negative to suppress Zlib header */
#define DEFAULT_WINDOWSIZE -15
#define DEFAULT_MEMLEVEL 9
#define FILTER_BUFSIZE 8096
/* Outputs a long in LSB order to the given file
* only the bottom 4 bits are required for the deflate file format.
*/
{
int n;
for (n = 0; n < 4; n++) {
string[n] = (int) (x & 0xff);
x >>= 8;
}
}
{
deflate_filter_config *c = apr_pcalloc(p, sizeof *c);
c->memlevel = DEFAULT_MEMLEVEL;
c->windowSize = DEFAULT_WINDOWSIZE;
return c;
}
const char* arg)
{
int i;
if (i < 1 || i > 15)
return "DeflateWindowSize must be between 1 and 15";
c->windowSize = i * -1;
return NULL;
}
const char* arg)
{
return NULL;
}
const char* arg)
{
int i;
if (i < 1 || i > 9)
return "DeflateMemLevel must be between 1 and 9";
c->memlevel = i;
return NULL;
}
/* magic header */
typedef struct deflate_ctx_t
{
unsigned char buffer[FILTER_BUFSIZE];
unsigned long crc;
} deflate_ctx;
{
apr_bucket *e;
const char *accepts;
request_rec *r = f->r;
int zRC;
/* 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 *buf;
if (r->main) {
}
/* some browsers might have problems, so set no-gzip
* (with browsermatch) for them */
}
/* if they don't have the line, then they can't play */
}
/* skip token */
accepts++;
}
/* No acceptable token found. */
}
/* We're cool with filtering this. */
/*
ctx->stream.zalloc = (alloc_func) 0;
ctx->stream.zfree = (free_func) 0;
ctx->stream.opaque = (voidpf) 0;
ctx->crc = 0L;
*/
c->windowSize, c->memlevel,
"unable to init Zlib: deflateInit2 returned %d: URL %s",
}
}
APR_BRIGADE_FOREACH(e, bb) {
const char *data;
apr_bucket *b;
int done = 0;
if (APR_BUCKET_IS_EOS(e)) {
char *buf, *p;
unsigned int deflate_len;
for (;;) {
if (deflate_len != 0) {
}
if (done) {
break;
}
}
break;
}
}
*p++ = crc_array[0];
*p++ = crc_array[1];
*p++ = crc_array[2];
*p++ = crc_array[3];
*p++ = len_array[0];
*p++ = len_array[1];
*p++ = len_array[2];
*p++ = len_array[3];
"Zlib: Compressed %ld to %ld : URL %s",
if (c->noteName) {
int total;
} else {
}
}
/* Remove EOS from the old list, and insert into the new. */
/* Okay, we've seen the EOS.
* Time to pass it along down the chain.
*/
}
if (APR_BUCKET_IS_FLUSH(e)) {
/* XXX FIX: do we need the Content-Size set, or can we stream?
* we should be able to stream */
/* ignore flush buckets for the moment.. we can't stream as we
* need the size ;( */
continue;
}
/* read */
/* This crc32 function is from zlib. */
/* write */
but we'll just have to trust zlib */
}
return APR_EGENERAL;
}
}
return APR_SUCCESS;
}
static void register_hooks(apr_pool_t * p)
{
}
static const command_rec deflate_filter_cmds[] = {
"Set a note to report on compression ratio"),
RSRC_CONF, "Set the Deflate window size (1-15)"),
"Set the Deflate Memory Level (1-9)"),
{NULL}
};
NULL,
NULL,
NULL,
};