2016-03-19 15:14:35 +00:00
|
|
|
#include "first.h"
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
#include "base.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "buffer.h"
|
2019-03-26 00:56:48 +00:00
|
|
|
#include "fdevent.h"
|
2018-09-09 05:50:33 +00:00
|
|
|
#include "http_header.h"
|
2005-02-20 14:27:00 +00:00
|
|
|
#include "response.h"
|
2005-08-08 08:22:06 +00:00
|
|
|
#include "stat_cache.h"
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
#include "plugin.h"
|
|
|
|
|
|
|
|
#include "crc32.h"
|
2005-08-18 09:26:29 +00:00
|
|
|
#include "etag.h"
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2009-10-11 14:31:42 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
#if defined HAVE_ZLIB_H && defined HAVE_LIBZ
|
|
|
|
# define USE_ZLIB
|
|
|
|
# include <zlib.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined HAVE_BZLIB_H && defined HAVE_LIBBZ2
|
|
|
|
# define USE_BZ2LIB
|
|
|
|
/* we don't need stdio interface */
|
|
|
|
# define BZ_NO_STDIO
|
|
|
|
# include <bzlib.h>
|
|
|
|
#endif
|
|
|
|
|
2016-04-17 06:34:51 +00:00
|
|
|
#if defined HAVE_SYS_MMAN_H && defined HAVE_MMAP && defined ENABLE_MMAP
|
|
|
|
#define USE_MMAP
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
#include "sys-mmap.h"
|
2016-04-17 06:34:51 +00:00
|
|
|
#include <setjmp.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
static volatile int sigbus_jmp_valid;
|
|
|
|
static sigjmp_buf sigbus_jmp;
|
|
|
|
|
|
|
|
static void sigbus_handler(int sig) {
|
|
|
|
UNUSED(sig);
|
|
|
|
if (sigbus_jmp_valid) siglongjmp(sigbus_jmp, 1);
|
|
|
|
log_failed_assert(__FILE__, __LINE__, "SIGBUS");
|
|
|
|
}
|
|
|
|
#endif
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
/* request: accept-encoding */
|
|
|
|
#define HTTP_ACCEPT_ENCODING_IDENTITY BV(0)
|
|
|
|
#define HTTP_ACCEPT_ENCODING_GZIP BV(1)
|
|
|
|
#define HTTP_ACCEPT_ENCODING_DEFLATE BV(2)
|
|
|
|
#define HTTP_ACCEPT_ENCODING_COMPRESS BV(3)
|
|
|
|
#define HTTP_ACCEPT_ENCODING_BZIP2 BV(4)
|
2012-11-09 14:23:24 +00:00
|
|
|
#define HTTP_ACCEPT_ENCODING_X_GZIP BV(5)
|
|
|
|
#define HTTP_ACCEPT_ENCODING_X_BZIP2 BV(6)
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
#ifdef __WIN32
|
2009-10-11 14:31:42 +00:00
|
|
|
# define mkdir(x,y) mkdir(x)
|
2005-02-20 14:27:00 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef struct {
|
2019-11-07 04:25:06 +00:00
|
|
|
const array *compress;
|
|
|
|
const buffer *compress_cache_dir;
|
|
|
|
off_t compress_max_filesize; /** max filesize in kb */
|
|
|
|
double max_loadavg;
|
|
|
|
unsigned int allowed_encodings;
|
2005-02-20 14:27:00 +00:00
|
|
|
} plugin_config;
|
|
|
|
|
|
|
|
typedef struct {
|
2019-11-07 04:25:06 +00:00
|
|
|
PLUGIN_DATA;
|
|
|
|
plugin_config defaults;
|
|
|
|
plugin_config conf;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2019-11-07 04:25:06 +00:00
|
|
|
buffer *ofn;
|
|
|
|
buffer *b;
|
2005-02-20 14:27:00 +00:00
|
|
|
} plugin_data;
|
|
|
|
|
|
|
|
INIT_FUNC(mod_compress_init) {
|
2019-11-07 04:25:06 +00:00
|
|
|
plugin_data * const p = calloc(1, sizeof(plugin_data));
|
|
|
|
p->ofn = buffer_init();
|
|
|
|
p->b = buffer_init();
|
|
|
|
return p;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FREE_FUNC(mod_compress_free) {
|
2019-11-07 04:25:06 +00:00
|
|
|
plugin_data *p = p_d;
|
|
|
|
buffer_free(p->ofn);
|
|
|
|
buffer_free(p->b);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2008-04-23 13:10:41 +00:00
|
|
|
/* 0 on success, -1 for error */
|
2009-03-07 21:05:37 +00:00
|
|
|
static int mkdir_recursive(char *dir) {
|
2008-02-26 16:19:53 +00:00
|
|
|
char *p = dir;
|
|
|
|
|
|
|
|
if (!dir || !dir[0])
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
while ((p = strchr(p + 1, '/')) != NULL) {
|
|
|
|
|
|
|
|
*p = '\0';
|
|
|
|
if ((mkdir(dir, 0700) != 0) && (errno != EEXIST)) {
|
|
|
|
*p = '/';
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*p++ = '/';
|
2008-04-23 13:10:41 +00:00
|
|
|
if (!*p) return 0; /* Ignore trailing slash */
|
2008-02-26 16:19:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (mkdir(dir, 0700) != 0) && (errno != EEXIST) ? -1 : 0;
|
|
|
|
}
|
|
|
|
|
2008-04-23 13:10:41 +00:00
|
|
|
/* 0 on success, -1 for error */
|
2009-03-07 21:05:37 +00:00
|
|
|
static int mkdir_for_file(char *filename) {
|
2008-02-26 16:19:53 +00:00
|
|
|
char *p = filename;
|
|
|
|
|
|
|
|
if (!filename || !filename[0])
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
while ((p = strchr(p + 1, '/')) != NULL) {
|
|
|
|
|
|
|
|
*p = '\0';
|
|
|
|
if ((mkdir(filename, 0700) != 0) && (errno != EEXIST)) {
|
|
|
|
*p = '/';
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*p++ = '/';
|
2008-04-23 13:10:41 +00:00
|
|
|
if (!*p) return -1; /* Unexpected trailing slash in filename */
|
2008-02-26 16:19:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-11-07 04:25:06 +00:00
|
|
|
static void mod_compress_merge_config_cpv(plugin_config * const pconf, const config_plugin_value_t * const cpv) {
|
|
|
|
switch (cpv->k_id) { /* index into static config_plugin_keys_t cpk[] */
|
|
|
|
case 0: /* compress.filetype */
|
|
|
|
pconf->compress = cpv->v.a;
|
|
|
|
break;
|
|
|
|
case 1: /* compress.allowed-encodings */
|
|
|
|
pconf->allowed_encodings = cpv->v.u;
|
|
|
|
break;
|
|
|
|
case 2: /* compress.cache-dir */
|
|
|
|
pconf->compress_cache_dir = cpv->v.b;
|
|
|
|
break;
|
|
|
|
case 3: /* compress.max-filesize */
|
|
|
|
pconf->compress_max_filesize = cpv->v.o;
|
|
|
|
break;
|
|
|
|
case 4: /* compress.max-loadavg */
|
|
|
|
pconf->max_loadavg = cpv->v.d;
|
|
|
|
break;
|
|
|
|
default:/* should not happen */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2008-02-26 16:19:53 +00:00
|
|
|
|
2019-11-07 04:25:06 +00:00
|
|
|
static void mod_compress_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv) {
|
|
|
|
do {
|
|
|
|
mod_compress_merge_config_cpv(pconf, cpv);
|
|
|
|
} while ((++cpv)->k_id != -1);
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
static void mod_compress_patch_config(request_st * const r, plugin_data * const p) {
|
2019-11-07 04:25:06 +00:00
|
|
|
memcpy(&p->conf, &p->defaults, sizeof(plugin_config));
|
|
|
|
for (int i = 1, used = p->nconfig; i < used; ++i) {
|
2020-01-13 02:51:12 +00:00
|
|
|
if (config_check_cond(r, (uint32_t)p->cvlist[i].k_id))
|
2019-11-07 04:25:06 +00:00
|
|
|
mod_compress_merge_config(&p->conf, p->cvlist + p->cvlist[i].v.u2[0]);
|
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2019-11-07 04:25:06 +00:00
|
|
|
static short mod_compress_encodings_to_flags(const array *encodings) {
|
|
|
|
short allowed_encodings = 0;
|
|
|
|
if (encodings->used) {
|
|
|
|
for (uint32_t j = 0; j < encodings->used; ++j) {
|
|
|
|
#if defined(USE_ZLIB) || defined(USE_BZ2LIB)
|
|
|
|
data_string *ds = (data_string *)encodings->data[j];
|
|
|
|
#endif
|
|
|
|
#ifdef USE_ZLIB
|
|
|
|
if (NULL != strstr(ds->value.ptr, "gzip"))
|
|
|
|
allowed_encodings |= HTTP_ACCEPT_ENCODING_GZIP
|
|
|
|
| HTTP_ACCEPT_ENCODING_X_GZIP;
|
|
|
|
if (NULL != strstr(ds->value.ptr, "x-gzip"))
|
|
|
|
allowed_encodings |= HTTP_ACCEPT_ENCODING_X_GZIP;
|
|
|
|
if (NULL != strstr(ds->value.ptr, "deflate"))
|
|
|
|
allowed_encodings |= HTTP_ACCEPT_ENCODING_DEFLATE;
|
|
|
|
/*
|
|
|
|
if (NULL != strstr(ds->value.ptr, "compress"))
|
|
|
|
allowed_encodings |= HTTP_ACCEPT_ENCODING_COMPRESS;
|
|
|
|
*/
|
|
|
|
#endif
|
|
|
|
#ifdef USE_BZ2LIB
|
|
|
|
if (NULL != strstr(ds->value.ptr, "bzip2"))
|
|
|
|
allowed_encodings |= HTTP_ACCEPT_ENCODING_BZIP2
|
|
|
|
| HTTP_ACCEPT_ENCODING_X_BZIP2;
|
|
|
|
if (NULL != strstr(ds->value.ptr, "x-bzip2"))
|
|
|
|
allowed_encodings |= HTTP_ACCEPT_ENCODING_X_BZIP2;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* default encodings */
|
|
|
|
#ifdef USE_ZLIB
|
|
|
|
allowed_encodings |= HTTP_ACCEPT_ENCODING_GZIP
|
|
|
|
| HTTP_ACCEPT_ENCODING_X_GZIP
|
|
|
|
| HTTP_ACCEPT_ENCODING_DEFLATE;
|
|
|
|
#endif
|
|
|
|
#ifdef USE_BZ2LIB
|
|
|
|
allowed_encodings |= HTTP_ACCEPT_ENCODING_BZIP2
|
|
|
|
| HTTP_ACCEPT_ENCODING_X_BZIP2;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return allowed_encodings;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2019-11-07 04:25:06 +00:00
|
|
|
SETDEFAULTS_FUNC(mod_compress_set_defaults) {
|
|
|
|
static const config_plugin_keys_t cpk[] = {
|
|
|
|
{ CONST_STR_LEN("compress.filetype"),
|
2019-12-08 00:15:55 +00:00
|
|
|
T_CONFIG_ARRAY_VLIST,
|
2019-11-07 04:25:06 +00:00
|
|
|
T_CONFIG_SCOPE_CONNECTION }
|
|
|
|
,{ CONST_STR_LEN("compress.allowed-encodings"),
|
2019-12-08 00:15:55 +00:00
|
|
|
T_CONFIG_ARRAY_VLIST,
|
2019-11-07 04:25:06 +00:00
|
|
|
T_CONFIG_SCOPE_CONNECTION }
|
|
|
|
,{ CONST_STR_LEN("compress.cache-dir"),
|
|
|
|
T_CONFIG_STRING,
|
|
|
|
T_CONFIG_SCOPE_CONNECTION }
|
|
|
|
,{ CONST_STR_LEN("compress.max-filesize"),
|
|
|
|
T_CONFIG_SHORT,
|
|
|
|
T_CONFIG_SCOPE_CONNECTION }
|
|
|
|
,{ CONST_STR_LEN("compress.max-loadavg"),
|
|
|
|
T_CONFIG_STRING,
|
|
|
|
T_CONFIG_SCOPE_CONNECTION }
|
|
|
|
,{ NULL, 0,
|
|
|
|
T_CONFIG_UNSET,
|
|
|
|
T_CONFIG_SCOPE_UNSET }
|
|
|
|
};
|
|
|
|
|
|
|
|
plugin_data * const p = p_d;
|
|
|
|
if (!config_plugin_values_init(srv, p, cpk, "mod_compress"))
|
|
|
|
return HANDLER_ERROR;
|
|
|
|
|
|
|
|
/* process and validate config directives
|
|
|
|
* (init i to 0 if global context; to 1 to skip empty global context) */
|
|
|
|
for (int i = !p->cvlist[0].v.u2[1]; i < p->nconfig; ++i) {
|
|
|
|
config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0];
|
|
|
|
for (; -1 != cpv->k_id; ++cpv) {
|
|
|
|
switch (cpv->k_id) {
|
|
|
|
case 0: /* compress.filetype */
|
|
|
|
if (0 == cpv->v.a->used) cpv->v.a = NULL;
|
|
|
|
break;
|
|
|
|
case 1: /* compress.allowed-encodings */
|
|
|
|
cpv->v.u = (unsigned int)
|
|
|
|
mod_compress_encodings_to_flags(cpv->v.a);
|
|
|
|
cpv->vtype = T_CONFIG_INT;
|
|
|
|
break;
|
|
|
|
case 2: /* compress.cache-dir */
|
|
|
|
if (!buffer_string_is_empty(cpv->v.b)) {
|
|
|
|
struct stat st;
|
|
|
|
mkdir_recursive(cpv->v.b->ptr);
|
|
|
|
if (0 != stat(cpv->v.b->ptr, &st)) {
|
|
|
|
log_perror(srv->errh, __FILE__, __LINE__,
|
|
|
|
"can't stat %s %s", cpk[cpv->k_id].k, cpv->v.b->ptr);
|
|
|
|
return HANDLER_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 3: /* compress.max-filesize */
|
|
|
|
cpv->v.o = ((off_t)cpv->v.shrt) << 10; /* KB to bytes */
|
|
|
|
break;
|
|
|
|
case 4: /* compress.max-loadavg */
|
|
|
|
cpv->v.d = (!buffer_string_is_empty(cpv->v.b))
|
|
|
|
? strtod(cpv->v.b->ptr, NULL)
|
|
|
|
: 0.0;
|
|
|
|
break;
|
|
|
|
default:/* should not happen */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p->defaults.max_loadavg = 0.0;
|
|
|
|
|
|
|
|
/* initialize p->defaults from global config context */
|
|
|
|
if (p->nconfig > 0 && p->cvlist->v.u2[1]) {
|
|
|
|
const config_plugin_value_t *cpv = p->cvlist + p->cvlist->v.u2[0];
|
|
|
|
if (-1 != cpv->k_id)
|
|
|
|
mod_compress_merge_config(&p->defaults, cpv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return HANDLER_GO_ON;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2019-11-07 04:25:06 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
#ifdef USE_ZLIB
|
2019-11-25 06:54:08 +00:00
|
|
|
static int deflate_file_to_buffer_gzip(plugin_data *p, char *start, off_t st_size, time_t mtime) {
|
2005-02-20 14:27:00 +00:00
|
|
|
unsigned char *c;
|
|
|
|
unsigned long crc;
|
|
|
|
z_stream z;
|
2015-02-08 19:10:44 +00:00
|
|
|
size_t outlen;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
z.zalloc = Z_NULL;
|
|
|
|
z.zfree = Z_NULL;
|
|
|
|
z.opaque = Z_NULL;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
if (Z_OK != deflateInit2(&z,
|
2005-02-20 14:27:00 +00:00
|
|
|
Z_DEFAULT_COMPRESSION,
|
2006-10-04 13:26:23 +00:00
|
|
|
Z_DEFLATED,
|
2020-02-22 22:24:12 +00:00
|
|
|
-MAX_WBITS, /* suppress zlib-header */
|
2005-02-20 14:27:00 +00:00
|
|
|
8,
|
|
|
|
Z_DEFAULT_STRATEGY)) {
|
|
|
|
return -1;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-05 11:59:33 +00:00
|
|
|
z.next_in = (unsigned char *)start;
|
2005-02-20 14:27:00 +00:00
|
|
|
z.avail_in = st_size;
|
|
|
|
z.total_in = 0;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
|
2015-02-08 19:10:39 +00:00
|
|
|
buffer_string_prepare_copy(p->b, (z.avail_in * 1.1) + 12 + 18);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* write gzip header */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
c = (unsigned char *)p->b->ptr;
|
|
|
|
c[0] = 0x1f;
|
|
|
|
c[1] = 0x8b;
|
|
|
|
c[2] = Z_DEFLATED;
|
|
|
|
c[3] = 0; /* options */
|
|
|
|
c[4] = (mtime >> 0) & 0xff;
|
|
|
|
c[5] = (mtime >> 8) & 0xff;
|
|
|
|
c[6] = (mtime >> 16) & 0xff;
|
|
|
|
c[7] = (mtime >> 24) & 0xff;
|
|
|
|
c[8] = 0x00; /* extra flags */
|
|
|
|
c[9] = 0x03; /* UNIX */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 19:10:44 +00:00
|
|
|
outlen = 10;
|
|
|
|
z.next_out = (unsigned char *)p->b->ptr + outlen;
|
|
|
|
z.avail_out = p->b->size - outlen - 9;
|
2005-02-20 14:27:00 +00:00
|
|
|
z.total_out = 0;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (Z_STREAM_END != deflate(&z, Z_FINISH)) {
|
|
|
|
deflateEnd(&z);
|
|
|
|
return -1;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* trailer */
|
2015-02-08 19:10:44 +00:00
|
|
|
outlen += z.total_out;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
crc = generate_crc32c(start, st_size);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 19:10:44 +00:00
|
|
|
c = (unsigned char *)p->b->ptr + outlen;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
c[0] = (crc >> 0) & 0xff;
|
|
|
|
c[1] = (crc >> 8) & 0xff;
|
|
|
|
c[2] = (crc >> 16) & 0xff;
|
|
|
|
c[3] = (crc >> 24) & 0xff;
|
|
|
|
c[4] = (z.total_in >> 0) & 0xff;
|
|
|
|
c[5] = (z.total_in >> 8) & 0xff;
|
|
|
|
c[6] = (z.total_in >> 16) & 0xff;
|
|
|
|
c[7] = (z.total_in >> 24) & 0xff;
|
2015-02-08 19:10:44 +00:00
|
|
|
outlen += 8;
|
|
|
|
buffer_commit(p->b, outlen);
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
if (Z_OK != deflateEnd(&z)) {
|
|
|
|
return -1;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-11-25 06:54:08 +00:00
|
|
|
static int deflate_file_to_buffer_deflate(plugin_data *p, unsigned char *start, off_t st_size) {
|
2005-02-20 14:27:00 +00:00
|
|
|
z_stream z;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
z.zalloc = Z_NULL;
|
|
|
|
z.zfree = Z_NULL;
|
|
|
|
z.opaque = Z_NULL;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
if (Z_OK != deflateInit2(&z,
|
2005-02-20 14:27:00 +00:00
|
|
|
Z_DEFAULT_COMPRESSION,
|
2006-10-04 13:26:23 +00:00
|
|
|
Z_DEFLATED,
|
2020-02-22 22:24:12 +00:00
|
|
|
-MAX_WBITS, /* suppress zlib-header */
|
2005-02-20 14:27:00 +00:00
|
|
|
8,
|
|
|
|
Z_DEFAULT_STRATEGY)) {
|
|
|
|
return -1;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
z.next_in = start;
|
|
|
|
z.avail_in = st_size;
|
|
|
|
z.total_in = 0;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 19:10:39 +00:00
|
|
|
buffer_string_prepare_copy(p->b, (z.avail_in * 1.1) + 12);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
z.next_out = (unsigned char *)p->b->ptr;
|
2015-02-08 19:10:39 +00:00
|
|
|
z.avail_out = p->b->size - 1;
|
2005-02-20 14:27:00 +00:00
|
|
|
z.total_out = 0;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (Z_STREAM_END != deflate(&z, Z_FINISH)) {
|
|
|
|
deflateEnd(&z);
|
|
|
|
return -1;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (Z_OK != deflateEnd(&z)) {
|
|
|
|
return -1;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 19:10:39 +00:00
|
|
|
/* trailer */
|
|
|
|
buffer_commit(p->b, z.total_out);
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USE_BZ2LIB
|
2019-11-25 06:54:08 +00:00
|
|
|
static int deflate_file_to_buffer_bzip2(plugin_data *p, unsigned char *start, off_t st_size) {
|
2005-02-20 14:27:00 +00:00
|
|
|
bz_stream bz;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
bz.bzalloc = NULL;
|
|
|
|
bz.bzfree = NULL;
|
|
|
|
bz.opaque = NULL;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
if (BZ_OK != BZ2_bzCompressInit(&bz,
|
2005-02-20 14:27:00 +00:00
|
|
|
9, /* blocksize = 900k */
|
|
|
|
0, /* no output */
|
|
|
|
0)) { /* workFactor: default */
|
|
|
|
return -1;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
bz.next_in = (char *)start;
|
|
|
|
bz.avail_in = st_size;
|
|
|
|
bz.total_in_lo32 = 0;
|
|
|
|
bz.total_in_hi32 = 0;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 19:10:39 +00:00
|
|
|
buffer_string_prepare_copy(p->b, (bz.avail_in * 1.1) + 12);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
bz.next_out = p->b->ptr;
|
2015-02-08 19:10:39 +00:00
|
|
|
bz.avail_out = p->b->size - 1;
|
2005-02-20 14:27:00 +00:00
|
|
|
bz.total_out_lo32 = 0;
|
|
|
|
bz.total_out_hi32 = 0;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (BZ_STREAM_END != BZ2_bzCompress(&bz, BZ_FINISH)) {
|
|
|
|
BZ2_bzCompressEnd(&bz);
|
|
|
|
return -1;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 19:10:44 +00:00
|
|
|
if (BZ_OK != BZ2_bzCompressEnd(&bz)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* file is too large for now */
|
|
|
|
if (bz.total_out_hi32) return -1;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* trailer */
|
2015-02-08 19:10:44 +00:00
|
|
|
buffer_commit(p->b, bz.total_out_lo32);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
static void mod_compress_note_ratio(request_st * const r, off_t in, off_t out) {
|
2018-09-09 05:50:33 +00:00
|
|
|
/* store compression ratio in environment
|
2016-10-19 10:01:10 +00:00
|
|
|
* for possible logging by mod_accesslog
|
|
|
|
* (late in response handling, so not seen by most other modules) */
|
|
|
|
/*(should be called only at end of successful response compression)*/
|
|
|
|
char ratio[LI_ITOSTRING_LENGTH];
|
|
|
|
if (0 == in) return;
|
2020-01-13 02:51:12 +00:00
|
|
|
http_header_env_set(r, CONST_STR_LEN("ratio"),
|
2019-11-28 15:27:01 +00:00
|
|
|
ratio, li_itostrn(ratio,sizeof(ratio),out*100/in));
|
2016-10-19 10:01:10 +00:00
|
|
|
}
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
static int deflate_file_to_file(request_st * const r, plugin_data *p, int ifd, buffer *fn, stat_cache_entry *sce, int type) {
|
2019-04-30 00:20:47 +00:00
|
|
|
int ofd;
|
2014-02-14 21:06:14 +00:00
|
|
|
int ret;
|
2016-04-17 06:34:51 +00:00
|
|
|
#ifdef USE_MMAP
|
|
|
|
volatile int mapped = 0;/* quiet warning: might be clobbered by 'longjmp' */
|
|
|
|
#endif
|
2005-02-20 14:27:00 +00:00
|
|
|
void *start;
|
2016-10-19 10:01:10 +00:00
|
|
|
stat_cache_entry *sce_ofn;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* overflow */
|
2005-08-08 08:22:06 +00:00
|
|
|
if ((off_t)(sce->st.st_size * 1.1) < sce->st.st_size) return -1;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
/* don't mmap files > 128Mb
|
|
|
|
*
|
2005-02-20 14:27:00 +00:00
|
|
|
* we could use a sliding window, but currently there is no need for it
|
|
|
|
*/
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-11-18 12:00:48 +00:00
|
|
|
if (sce->st.st_size > 128 * 1024 * 1024) return -1;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
buffer_reset(p->ofn);
|
2015-02-08 12:37:10 +00:00
|
|
|
buffer_copy_buffer(p->ofn, p->conf.compress_cache_dir);
|
|
|
|
buffer_append_slash(p->ofn);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
if (0 == strncmp(r->physical.path.ptr, r->physical.doc_root.ptr, buffer_string_length(&r->physical.doc_root))) {
|
|
|
|
buffer_append_string(p->ofn, r->physical.path.ptr + buffer_string_length(&r->physical.doc_root));
|
2005-02-20 14:27:00 +00:00
|
|
|
} else {
|
2020-01-13 02:51:12 +00:00
|
|
|
buffer_append_string_buffer(p->ofn, &r->uri.path);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
switch(type) {
|
|
|
|
case HTTP_ACCEPT_ENCODING_GZIP:
|
2012-11-09 14:23:24 +00:00
|
|
|
case HTTP_ACCEPT_ENCODING_X_GZIP:
|
2008-07-30 19:38:32 +00:00
|
|
|
buffer_append_string_len(p->ofn, CONST_STR_LEN("-gzip-"));
|
2005-02-20 14:27:00 +00:00
|
|
|
break;
|
|
|
|
case HTTP_ACCEPT_ENCODING_DEFLATE:
|
2008-07-30 19:38:32 +00:00
|
|
|
buffer_append_string_len(p->ofn, CONST_STR_LEN("-deflate-"));
|
2005-02-20 14:27:00 +00:00
|
|
|
break;
|
|
|
|
case HTTP_ACCEPT_ENCODING_BZIP2:
|
2012-11-09 14:23:24 +00:00
|
|
|
case HTTP_ACCEPT_ENCODING_X_BZIP2:
|
2008-07-30 19:38:32 +00:00
|
|
|
buffer_append_string_len(p->ofn, CONST_STR_LEN("-bzip2-"));
|
2005-02-20 14:27:00 +00:00
|
|
|
break;
|
|
|
|
default:
|
2020-01-13 02:51:12 +00:00
|
|
|
log_error(r->conf.errh, __FILE__, __LINE__,
|
2019-11-25 06:54:08 +00:00
|
|
|
"unknown compression type %d", type);
|
2005-02-20 14:27:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
const buffer *etag = stat_cache_etag_get(sce, r->conf.etag_flags);
|
2019-12-05 08:16:25 +00:00
|
|
|
buffer_append_string_buffer(p->ofn, etag);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2019-12-05 08:16:25 +00:00
|
|
|
sce_ofn = stat_cache_get_entry(p->ofn);
|
|
|
|
if (sce_ofn) {
|
2016-10-19 10:01:10 +00:00
|
|
|
if (0 == sce->st.st_size) return -1; /* cache file being created */
|
|
|
|
/* cache-entry exists */
|
|
|
|
#if 0
|
2020-01-13 02:51:12 +00:00
|
|
|
log_error(r->conf.errh, __FILE__, __LINE__, "%s compress-cache hit", p->ofn->ptr);
|
2016-10-19 10:01:10 +00:00
|
|
|
#endif
|
2020-01-13 02:51:12 +00:00
|
|
|
mod_compress_note_ratio(r, sce->st.st_size, sce_ofn->st.st_size);
|
|
|
|
buffer_copy_buffer(&r->physical.path, p->ofn);
|
2016-10-19 10:01:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
if (0.0 < p->conf.max_loadavg && p->conf.max_loadavg < r->con->srv->loadavg[0]) {
|
2016-10-19 20:09:29 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-02-26 16:19:53 +00:00
|
|
|
if (-1 == mkdir_for_file(p->ofn->ptr)) {
|
2020-01-13 02:51:12 +00:00
|
|
|
log_error(r->conf.errh, __FILE__, __LINE__,
|
2019-11-25 06:54:08 +00:00
|
|
|
"couldn't create directory for file %s", p->ofn->ptr);
|
2008-02-26 16:19:53 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-03-26 00:56:48 +00:00
|
|
|
/*(note: follows symlinks in protected cache dir)*/
|
|
|
|
if (-1 == (ofd = fdevent_open_cloexec(p->ofn->ptr, 1, O_WRONLY | O_CREAT | O_EXCL, 0600))) {
|
2005-02-20 14:27:00 +00:00
|
|
|
if (errno == EEXIST) {
|
2016-10-19 10:01:10 +00:00
|
|
|
return -1; /* cache file being created */
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
log_perror(r->conf.errh, __FILE__, __LINE__,
|
2019-11-25 06:54:08 +00:00
|
|
|
"creating cachefile %s failed", p->ofn->ptr);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#if 0
|
2020-01-13 02:51:12 +00:00
|
|
|
log_error(r->conf.errh, __FILE__, __LINE__, "%s compress-cache miss", p->ofn->ptr);
|
2006-10-04 13:26:23 +00:00
|
|
|
#endif
|
|
|
|
|
2012-02-24 18:34:20 +00:00
|
|
|
#ifdef USE_MMAP
|
2017-12-10 01:22:29 +00:00
|
|
|
if (MAP_FAILED != (start = mmap(NULL, sce->st.st_size, PROT_READ, MAP_SHARED, ifd, 0))
|
|
|
|
|| (errno == EINVAL && MAP_FAILED != (start = mmap(NULL, sce->st.st_size, PROT_READ, MAP_PRIVATE, ifd, 0)))) {
|
2016-04-17 06:34:51 +00:00
|
|
|
mapped = 1;
|
|
|
|
signal(SIGBUS, sigbus_handler);
|
|
|
|
sigbus_jmp_valid = 1;
|
|
|
|
if (0 != sigsetjmp(sigbus_jmp, 1)) {
|
|
|
|
sigbus_jmp_valid = 0;
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
log_error(r->conf.errh, __FILE__, __LINE__,
|
2019-11-25 06:54:08 +00:00
|
|
|
"SIGBUS in mmap: %s %d", fn->ptr, ifd);
|
2016-04-17 06:34:51 +00:00
|
|
|
|
|
|
|
munmap(start, sce->st.st_size);
|
|
|
|
close(ofd);
|
|
|
|
|
|
|
|
/* Remove the incomplete cache file, so that later hits aren't served from it */
|
|
|
|
if (-1 == unlink(p->ofn->ptr)) {
|
2020-01-13 02:51:12 +00:00
|
|
|
log_perror(r->conf.errh, __FILE__, __LINE__,
|
2019-11-25 06:54:08 +00:00
|
|
|
"unlinking incomplete cachefile %s failed", p->ofn->ptr);
|
2016-04-17 06:34:51 +00:00
|
|
|
}
|
2008-02-27 19:36:34 +00:00
|
|
|
|
2016-04-17 06:34:51 +00:00
|
|
|
return -1;
|
2008-02-27 19:36:34 +00:00
|
|
|
}
|
2016-04-17 06:34:51 +00:00
|
|
|
} else
|
|
|
|
#endif /* FIXME: might attempt to read very large file completely into memory; see compress.max-filesize config option */
|
|
|
|
if (NULL == (start = malloc(sce->st.st_size)) || sce->st.st_size != read(ifd, start, sce->st.st_size)) {
|
2020-01-13 02:51:12 +00:00
|
|
|
log_perror(r->conf.errh, __FILE__, __LINE__,
|
2019-11-25 06:54:08 +00:00
|
|
|
"reading %s failed", fn->ptr);
|
2012-02-24 18:34:20 +00:00
|
|
|
|
|
|
|
close(ofd);
|
|
|
|
free(start);
|
|
|
|
|
|
|
|
/* Remove the incomplete cache file, so that later hits aren't served from it */
|
|
|
|
if (-1 == unlink(p->ofn->ptr)) {
|
2020-01-13 02:51:12 +00:00
|
|
|
log_perror(r->conf.errh, __FILE__, __LINE__,
|
2019-11-25 06:54:08 +00:00
|
|
|
"unlinking incomplete cachefile %s failed", p->ofn->ptr);
|
2012-02-24 18:34:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2014-02-14 21:06:14 +00:00
|
|
|
ret = -1;
|
2005-02-20 14:27:00 +00:00
|
|
|
switch(type) {
|
|
|
|
#ifdef USE_ZLIB
|
2006-10-04 13:26:23 +00:00
|
|
|
case HTTP_ACCEPT_ENCODING_GZIP:
|
2012-11-09 14:23:24 +00:00
|
|
|
case HTTP_ACCEPT_ENCODING_X_GZIP:
|
2019-11-25 06:54:08 +00:00
|
|
|
ret = deflate_file_to_buffer_gzip(p, start, sce->st.st_size, sce->st.st_mtime);
|
2005-02-20 14:27:00 +00:00
|
|
|
break;
|
2006-10-04 13:26:23 +00:00
|
|
|
case HTTP_ACCEPT_ENCODING_DEFLATE:
|
2019-11-25 06:54:08 +00:00
|
|
|
ret = deflate_file_to_buffer_deflate(p, start, sce->st.st_size);
|
2005-02-20 14:27:00 +00:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifdef USE_BZ2LIB
|
2006-10-04 13:26:23 +00:00
|
|
|
case HTTP_ACCEPT_ENCODING_BZIP2:
|
2012-11-09 14:23:24 +00:00
|
|
|
case HTTP_ACCEPT_ENCODING_X_BZIP2:
|
2019-11-25 06:54:08 +00:00
|
|
|
ret = deflate_file_to_buffer_bzip2(p, start, sce->st.st_size);
|
2005-02-20 14:27:00 +00:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2008-02-27 19:36:34 +00:00
|
|
|
if (ret == 0) {
|
2019-12-08 01:50:42 +00:00
|
|
|
ssize_t wr = write(ofd, CONST_BUF_LEN(p->b));
|
|
|
|
if (-1 == wr) {
|
2020-01-13 02:51:12 +00:00
|
|
|
log_perror(r->conf.errh, __FILE__, __LINE__,
|
2019-11-25 06:54:08 +00:00
|
|
|
"writing cachefile %s failed", p->ofn->ptr);
|
2008-02-27 19:36:34 +00:00
|
|
|
ret = -1;
|
2019-12-08 01:50:42 +00:00
|
|
|
} else if ((size_t)wr != buffer_string_length(p->b)) {
|
2020-01-13 02:51:12 +00:00
|
|
|
log_error(r->conf.errh, __FILE__, __LINE__,
|
2019-11-25 06:54:08 +00:00
|
|
|
"writing cachefile %s failed: not enough bytes written",
|
|
|
|
p->ofn->ptr);
|
2008-02-27 19:36:34 +00:00
|
|
|
ret = -1;
|
|
|
|
}
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2012-02-24 18:34:20 +00:00
|
|
|
#ifdef USE_MMAP
|
2016-04-17 06:34:51 +00:00
|
|
|
if (mapped) {
|
|
|
|
sigbus_jmp_valid = 0;
|
|
|
|
munmap(start, sce->st.st_size);
|
|
|
|
} else
|
2012-02-24 18:34:20 +00:00
|
|
|
#endif
|
2016-04-17 06:34:51 +00:00
|
|
|
free(start);
|
2012-02-24 18:34:20 +00:00
|
|
|
|
2016-05-11 02:17:22 +00:00
|
|
|
if (0 != close(ofd) || ret != 0) {
|
|
|
|
if (0 == ret) {
|
2020-01-13 02:51:12 +00:00
|
|
|
log_perror(r->conf.errh, __FILE__, __LINE__,
|
2019-11-25 06:54:08 +00:00
|
|
|
"writing cachefile %s failed", p->ofn->ptr);
|
2016-05-11 02:17:22 +00:00
|
|
|
}
|
|
|
|
|
2008-02-27 19:36:34 +00:00
|
|
|
/* Remove the incomplete cache file, so that later hits aren't served from it */
|
|
|
|
if (-1 == unlink(p->ofn->ptr)) {
|
2020-01-13 02:51:12 +00:00
|
|
|
log_perror(r->conf.errh, __FILE__, __LINE__,
|
2019-11-25 06:54:08 +00:00
|
|
|
"unlinking incomplete cachefile %s failed", p->ofn->ptr);
|
2008-02-27 19:36:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
buffer_copy_buffer(&r->physical.path, p->ofn);
|
|