Browse Source

- Add possibility to disable methods in mod_compress (#1773)

git-svn-id: svn://svn.lighttpd.net/lighttpd/branches/lighttpd-1.4.x@2325 152afb58-edef-0310-8abb-c4023f1b3aa9
svn/tags/lighttpd-1.4.21
Elan Ruusamäe 13 years ago
parent
commit
c6c2bf8308
  1. 1
      NEWS
  2. 6
      doc/compress.txt
  3. 57
      src/mod_compress.c
  4. 32
      tests/mod-compress.conf
  5. 13
      tests/mod-compress.t

1
NEWS

@ -15,6 +15,7 @@ NEWS
* Do not cache default vhost in mod_simple_vhost (#709)
* Trust pcre-config, do not check for pcre manually (#1769)
* Fix fastcgi authorization in subdirectories with check-local=disabled; don't split pathinfo for authorizer. (#963)
* Add possibility to disable methods in mod_compress (#1773)
- 1.4.20 - 2008-09-30

6
doc/compress.txt

@ -32,6 +32,12 @@ Supported are gzip, deflate, bzip.
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

57
src/mod_compress.c

@ -49,6 +49,7 @@ typedef struct {
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 @@ SETDEFAULTS_FUNC(mod_compress_setdefaults) {
{ "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 @@ SETDEFAULTS_FUNC(mod_compress_setdefaults) {
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 @@ SETDEFAULTS_FUNC(mod_compress_setdefaults) {
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 @@ static int mod_compress_patch_connection(server *srv, connection *con, plugin_da
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 @@ static int mod_compress_patch_connection(server *srv, connection *con, plugin_da
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 @@ PHYSICALPATH_FUNC(mod_compress_physical) {
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";

32
tests/mod-compress.conf

@ -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" )

13
tests/mod-compress.t

@ -8,12 +8,14 @@ BEGIN {
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} = ( <<EOF
@ -88,5 +90,14 @@ EOF
$t->{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} = ( <<EOF
GET /index.txt HTTP/1.0
Accept-Encoding: bzip2, gzip, deflate
Host: cache.example.org
EOF
);
$t->{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");

Loading…
Cancel
Save