--- lighttpd-1.4.19/src/mod_compress.c 2008-09-22 16:24:49.930179728 +0300 +++ lighttpd-1.4.19/src/mod_compress.c 2008-09-22 17:02:36.097001825 +0300 @@ -49,6 +49,7 @@ buffer *compress_cache_dir; array *compress; off_t compress_max_filesize; /** max filesize in kb */ + int allowed_encodings; } plugin_config; typedef struct { @@ -154,6 +155,7 @@ { "compress.cache-dir", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, { "compress.filetype", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, { "compress.max-filesize", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, + { "compress.allowed-encodings", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET } }; @@ -161,15 +163,18 @@ for (i = 0; i < srv->config_context->used; i++) { plugin_config *s; + array *encodings_arr = array_init(); s = calloc(1, sizeof(plugin_config)); s->compress_cache_dir = buffer_init(); s->compress = array_init(); s->compress_max_filesize = 0; + s->allowed_encodings = 0; cv[0].destination = s->compress_cache_dir; cv[1].destination = s->compress; cv[2].destination = &(s->compress_max_filesize); + cv[3].destination = encodings_arr; /* temp array for allowed encodings list */ p->config_storage[i] = s; @@ -177,6 +182,39 @@ return HANDLER_ERROR; } + if (encodings_arr->used) { + size_t j = 0; + for (j = 0; j < encodings_arr->used; j++) { + data_string *ds = (data_string *)encodings_arr->data[j]; +#ifdef USE_ZLIB + if (NULL != strstr(ds->value->ptr, "gzip")) + s->allowed_encodings |= HTTP_ACCEPT_ENCODING_GZIP; + if (NULL != strstr(ds->value->ptr, "deflate")) + s->allowed_encodings |= HTTP_ACCEPT_ENCODING_DEFLATE; + /* + if (NULL != strstr(ds->value->ptr, "compress")) + s->allowed_encodings |= HTTP_ACCEPT_ENCODING_COMPRESS; + */ +#endif +#ifdef USE_BZ2LIB + if (NULL != strstr(ds->value->ptr, "bzip2")) + s->allowed_encodings |= HTTP_ACCEPT_ENCODING_BZIP2; +#endif + } + } else { + /* default encodings */ + s->allowed_encodings = 0 +#ifdef USE_ZLIB + | HTTP_ACCEPT_ENCODING_GZIP | HTTP_ACCEPT_ENCODING_DEFLATE +#endif +#ifdef USE_BZ2LIB + | HTTP_ACCEPT_ENCODING_BZIP2 +#endif + ; + } + + array_free(encodings_arr); + if (!buffer_is_empty(s->compress_cache_dir)) { struct stat st; mkdir_recursive(s->compress_cache_dir->ptr); @@ -587,6 +625,7 @@ PATCH(compress_cache_dir); PATCH(compress); PATCH(compress_max_filesize); + PATCH(allowed_encodings); /* skip the first, the global context */ for (i = 1; i < srv->config_context->used; i++) { @@ -606,6 +645,8 @@ PATCH(compress); } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("compress.max-filesize"))) { PATCH(compress_max_filesize); + } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("compress.allowed-encodings"))) { + PATCH(allowed_encodings); } } } @@ -668,27 +709,21 @@ if (NULL != (ds = (data_string *)array_get_element(con->request.headers, "Accept-Encoding"))) { int accept_encoding = 0; char *value = ds->value->ptr; - int srv_encodings = 0; int matched_encodings = 0; /* get client side support encodings */ +#ifdef USE_ZLIB if (NULL != strstr(value, "gzip")) accept_encoding |= HTTP_ACCEPT_ENCODING_GZIP; if (NULL != strstr(value, "deflate")) accept_encoding |= HTTP_ACCEPT_ENCODING_DEFLATE; if (NULL != strstr(value, "compress")) accept_encoding |= HTTP_ACCEPT_ENCODING_COMPRESS; - if (NULL != strstr(value, "bzip2")) accept_encoding |= HTTP_ACCEPT_ENCODING_BZIP2; - if (NULL != strstr(value, "identity")) accept_encoding |= HTTP_ACCEPT_ENCODING_IDENTITY; - - /* get server side supported ones */ -#ifdef USE_BZ2LIB - srv_encodings |= HTTP_ACCEPT_ENCODING_BZIP2; #endif -#ifdef USE_ZLIB - srv_encodings |= HTTP_ACCEPT_ENCODING_GZIP; - srv_encodings |= HTTP_ACCEPT_ENCODING_DEFLATE; +#ifdef USE_BZ2LIB + if (NULL != strstr(value, "bzip2")) accept_encoding |= HTTP_ACCEPT_ENCODING_BZIP2; #endif + if (NULL != strstr(value, "identity")) accept_encoding |= HTTP_ACCEPT_ENCODING_IDENTITY; /* find matching entries */ - matched_encodings = accept_encoding & srv_encodings; + matched_encodings = accept_encoding & p->conf.allowed_encodings; if (matched_encodings) { const char *dflt_gzip = "gzip"; --- lighttpd-1.4.19/doc/compress.txt 2008-09-19 13:27:27.571638906 +0300 +++ lighttpd-1.4.19/doc/compress.txt 2008-09-19 14:24:12.308407368 +0300 @@ -32,6 +32,12 @@ Options ======= +compress.allowed-encodings + override default set of allowed encodings + + e.g.: :: + compress.allowed-encodings = ("bzip2", "gzip", "deflate") + compress.cache-dir name of the directory where compressed content will be cached Index: mod-compress.conf --- ./tests/mod-compress.conf (revision 0) +++ ./tests/mod-compress.conf (revision 0) @@ -0,0 +1,32 @@ +debug.log-request-handling = "enable" +debug.log-response-header = "disable" +debug.log-request-header = "disable" + +server.document-root = env.SRCDIR + "/tmp/lighttpd/servers/www.example.org/pages/" +server.pid-file = env.SRCDIR + "/tmp/lighttpd/lighttpd.pid" + +## bind to port (default: 80) +server.port = 2048 + +## bind to localhost (default: all interfaces) +server.bind = "localhost" +server.errorlog = env.SRCDIR + "/tmp/lighttpd/logs/lighttpd.error.log" +server.name = "www.example.org" + +server.modules = ( + "mod_compress" +) + +######################## MODULE CONFIG ############################ + +mimetype.assign = ( + ".html" => "text/html", + ".txt" => "text/plain", +) + +$HTTP["host"] == "cache.example.org" { + compress.cache-dir = env.SRCDIR + "/tmp/lighttpd/cache/compress/" +} +compress.filetype = ("text/plain", "text/html") + +compress.allowed-encodings = ( "gzip", "deflate" ) Index: mod-compress.t =================================================================== --- ./tests/mod-compress.t (revision 2191) +++ ./tests/mod-compress.t (working copy) @@ -8,12 +8,14 @@ use strict; use IO::Socket; -use Test::More tests => 10; +use Test::More tests => 11; use LightyTest; my $tf = LightyTest->new(); my $t; +$tf->{CONFIGFILE} = 'mod-compress.conf'; + ok($tf->start_proc == 0, "Starting lighttpd") or die(); $t->{REQUEST} = ( <{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, '+Vary' => '', 'Content-Type' => "text/plain" } ]; ok($tf->handle_http($t) == 0, 'Empty Accept-Encoding'); +$t->{REQUEST} = ( <{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, '+Vary' => '', 'Content-Encoding' => 'gzip', 'Content-Type' => "text/plain" } ]; +ok($tf->handle_http($t) == 0, 'bzip2 requested but disabled'); + ok($tf->stop_proc == 0, "Stopping lighttpd");