lighttpd1.4/src/mod_compress.c

923 lines
25 KiB
C
Raw Normal View History

#include "base.h"
#include "log.h"
#include "buffer.h"
#include "response.h"
#include "stat_cache.h"
#include "plugin.h"
#include "crc32.h"
#include "etag.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <assert.h>
#include <fcntl.h>
#include <unistd.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#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
#include "sys-mmap.h"
/* 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)
#define HTTP_ACCEPT_ENCODING_X_GZIP BV(5)
#define HTTP_ACCEPT_ENCODING_X_BZIP2 BV(6)
#ifdef __WIN32
# define mkdir(x,y) mkdir(x)
#endif
typedef struct {
buffer *compress_cache_dir;
array *compress;
off_t compress_max_filesize; /** max filesize in kb */
int allowed_encodings;
} plugin_config;
typedef struct {
PLUGIN_DATA;
buffer *ofn;
buffer *b;
plugin_config **config_storage;
plugin_config conf;
} plugin_data;
INIT_FUNC(mod_compress_init) {
plugin_data *p;
p = calloc(1, sizeof(*p));
p->ofn = buffer_init();
p->b = buffer_init();
return p;
}
FREE_FUNC(mod_compress_free) {
plugin_data *p = p_d;
UNUSED(srv);
if (!p) return HANDLER_GO_ON;
buffer_free(p->ofn);
buffer_free(p->b);
if (p->config_storage) {
size_t i;
for (i = 0; i < srv->config_context->used; i++) {
plugin_config *s = p->config_storage[i];
if (!s) continue;
array_free(s->compress);
buffer_free(s->compress_cache_dir);
free(s);
}
free(p->config_storage);
}
free(p);
return HANDLER_GO_ON;
}
/* 0 on success, -1 for error */
static int mkdir_recursive(char *dir) {
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++ = '/';
if (!*p) return 0; /* Ignore trailing slash */
}
return (mkdir(dir, 0700) != 0) && (errno != EEXIST) ? -1 : 0;
}
/* 0 on success, -1 for error */
static int mkdir_for_file(char *filename) {
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++ = '/';
if (!*p) return -1; /* Unexpected trailing slash in filename */
}
return 0;
}
SETDEFAULTS_FUNC(mod_compress_setdefaults) {
plugin_data *p = p_d;
size_t i = 0;
config_values_t cv[] = {
{ "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 }
};
p->config_storage = calloc(1, srv->config_context->used * sizeof(plugin_config *));
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;
if (0 != config_insert_values_global(srv, ((data_config *)srv->config_context->data[i])->value, cv)) {
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 | HTTP_ACCEPT_ENCODING_X_GZIP;
if (NULL != strstr(ds->value->ptr, "x-gzip"))
s->allowed_encodings |= HTTP_ACCEPT_ENCODING_X_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 | HTTP_ACCEPT_ENCODING_X_BZIP2;
if (NULL != strstr(ds->value->ptr, "x-bzip2"))
s->allowed_encodings |= HTTP_ACCEPT_ENCODING_X_BZIP2;
#endif
}
} else {
/* default encodings */
s->allowed_encodings = 0
#ifdef USE_ZLIB
| HTTP_ACCEPT_ENCODING_GZIP | HTTP_ACCEPT_ENCODING_X_GZIP | HTTP_ACCEPT_ENCODING_DEFLATE
#endif
#ifdef USE_BZ2LIB
| HTTP_ACCEPT_ENCODING_BZIP2 | HTTP_ACCEPT_ENCODING_X_BZIP2
#endif
;
}
array_free(encodings_arr);
fix buffer, chunk and http_chunk API * remove unused structs and functions (buffer_array, read_buffer) * change return type from int to void for many functions, as the return value (indicating error/success) was never checked, and the function would only fail on programming errors and not on invalid input; changed functions to use force_assert instead of returning an error. * all "len" parameters now are the real size of the memory to be read. the length of strings is given always without the terminating 0. * the "buffer" struct still counts the terminating 0 in ->used, provide buffer_string_length() to get the length of a string in a buffer. unset config "strings" have used == 0, which is used in some places to distinguish unset values from "" (empty string) values. * most buffer usages should now use it as string container. * optimise some buffer copying by "moving" data to other buffers * use (u)intmax_t for generic int-to-string functions * remove unused enum values: UNUSED_CHUNK, ENCODING_UNSET * converted BUFFER_APPEND_SLASH to inline function (no macro feature needed) * refactor: create chunkqueue_steal: moving (partial) chunks into another queue * http_chunk: added separate function to terminate chunked body instead of magic handling in http_chunk_append_mem(). http_chunk_append_* now handle empty chunks, and never terminate the chunked body. From: Stefan Bühler <stbuehler@web.de> git-svn-id: svn://svn.lighttpd.net/lighttpd/branches/lighttpd-1.4.x@2975 152afb58-edef-0310-8abb-c4023f1b3aa9
2015-02-08 12:37:10 +00:00
if (!buffer_string_is_empty(s->compress_cache_dir)) {
struct stat st;
mkdir_recursive(s->compress_cache_dir->ptr);
if (0 != stat(s->compress_cache_dir->ptr, &st)) {
log_error_write(srv, __FILE__, __LINE__, "sbs", "can't stat compress.cache-dir",
s->compress_cache_dir, strerror(errno));
return HANDLER_ERROR;
}
}
}
return HANDLER_GO_ON;
}
#ifdef USE_ZLIB
static int deflate_file_to_buffer_gzip(server *srv, connection *con, plugin_data *p, char *start, off_t st_size, time_t mtime) {
unsigned char *c;
unsigned long crc;
z_stream z;
UNUSED(srv);
UNUSED(con);
z.zalloc = Z_NULL;
z.zfree = Z_NULL;
z.opaque = Z_NULL;
if (Z_OK != deflateInit2(&z,
Z_DEFAULT_COMPRESSION,
Z_DEFLATED,
-MAX_WBITS, /* supress zlib-header */
8,
Z_DEFAULT_STRATEGY)) {
return -1;
}
z.next_in = (unsigned char *)start;
z.avail_in = st_size;
z.total_in = 0;
buffer_string_prepare_copy(p->b, (z.avail_in * 1.1) + 12 + 18);
/* write gzip header */
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 */
p->b->used = 10;
z.next_out = (unsigned char *)p->b->ptr + p->b->used;
z.avail_out = p->b->size - p->b->used - 9;
z.total_out = 0;
if (Z_STREAM_END != deflate(&z, Z_FINISH)) {
deflateEnd(&z);
return -1;
}
/* trailer */
p->b->used += z.total_out;
crc = generate_crc32c(start, st_size);
c = (unsigned char *)p->b->ptr + p->b->used;
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;
p->b->used += 8;
p->b->ptr[p->b->used++] = '\0';
if (Z_OK != deflateEnd(&z)) {
return -1;
}
return 0;
}
static int deflate_file_to_buffer_deflate(server *srv, connection *con, plugin_data *p, unsigned char *start, off_t st_size) {
z_stream z;
UNUSED(srv);
UNUSED(con);
z.zalloc = Z_NULL;
z.zfree = Z_NULL;
z.opaque = Z_NULL;
if (Z_OK != deflateInit2(&z,
Z_DEFAULT_COMPRESSION,
Z_DEFLATED,
-MAX_WBITS, /* supress zlib-header */
8,
Z_DEFAULT_STRATEGY)) {
return -1;
}
z.next_in = start;
z.avail_in = st_size;
z.total_in = 0;
buffer_string_prepare_copy(p->b, (z.avail_in * 1.1) + 12);
z.next_out = (unsigned char *)p->b->ptr;
z.avail_out = p->b->size - 1;
z.total_out = 0;
if (Z_STREAM_END != deflate(&z, Z_FINISH)) {
deflateEnd(&z);
return -1;
}
if (Z_OK != deflateEnd(&z)) {
return -1;
}
/* trailer */
buffer_commit(p->b, z.total_out);
return 0;
}
#endif
#ifdef USE_BZ2LIB
static int deflate_file_to_buffer_bzip2(server *srv, connection *con, plugin_data *p, unsigned char *start, off_t st_size) {
bz_stream bz;
UNUSED(srv);
UNUSED(con);
bz.bzalloc = NULL;
bz.bzfree = NULL;
bz.opaque = NULL;
if (BZ_OK != BZ2_bzCompressInit(&bz,
9, /* blocksize = 900k */
0, /* no output */
0)) { /* workFactor: default */
return -1;
}
bz.next_in = (char *)start;
bz.avail_in = st_size;
bz.total_in_lo32 = 0;
bz.total_in_hi32 = 0;
buffer_string_prepare_copy(p->b, (bz.avail_in * 1.1) + 12);
bz.next_out = p->b->ptr;
bz.avail_out = p->b->size - 1;
bz.total_out_lo32 = 0;
bz.total_out_hi32 = 0;
if (BZ_STREAM_END != BZ2_bzCompress(&bz, BZ_FINISH)) {
BZ2_bzCompressEnd(&bz);
return -1;
}
/* file is too large for now */
if (bz.total_out_hi32) return -1;
/* trailer */
p->b->used = bz.total_out_lo32;
p->b->ptr[p->b->used++] = '\0';
if (BZ_OK != BZ2_bzCompressEnd(&bz)) {
return -1;
}
return 0;
}
#endif
static int deflate_file_to_file(server *srv, connection *con, plugin_data *p, buffer *fn, stat_cache_entry *sce, int type) {
int ifd, ofd;
int ret;
void *start;
const char *filename = fn->ptr;
ssize_t r;
/* overflow */
if ((off_t)(sce->st.st_size * 1.1) < sce->st.st_size) return -1;
/* don't mmap files > 128Mb
*
* we could use a sliding window, but currently there is no need for it
*/
if (sce->st.st_size > 128 * 1024 * 1024) return -1;
buffer_reset(p->ofn);
fix buffer, chunk and http_chunk API * remove unused structs and functions (buffer_array, read_buffer) * change return type from int to void for many functions, as the return value (indicating error/success) was never checked, and the function would only fail on programming errors and not on invalid input; changed functions to use force_assert instead of returning an error. * all "len" parameters now are the real size of the memory to be read. the length of strings is given always without the terminating 0. * the "buffer" struct still counts the terminating 0 in ->used, provide buffer_string_length() to get the length of a string in a buffer. unset config "strings" have used == 0, which is used in some places to distinguish unset values from "" (empty string) values. * most buffer usages should now use it as string container. * optimise some buffer copying by "moving" data to other buffers * use (u)intmax_t for generic int-to-string functions * remove unused enum values: UNUSED_CHUNK, ENCODING_UNSET * converted BUFFER_APPEND_SLASH to inline function (no macro feature needed) * refactor: create chunkqueue_steal: moving (partial) chunks into another queue * http_chunk: added separate function to terminate chunked body instead of magic handling in http_chunk_append_mem(). http_chunk_append_* now handle empty chunks, and never terminate the chunked body. From: Stefan Bühler <stbuehler@web.de> git-svn-id: svn://svn.lighttpd.net/lighttpd/branches/lighttpd-1.4.x@2975 152afb58-edef-0310-8abb-c4023f1b3aa9
2015-02-08 12:37:10 +00:00
buffer_copy_buffer(p->ofn, p->conf.compress_cache_dir);
buffer_append_slash(p->ofn);
if (0 == strncmp(con->physical.path->ptr, con->physical.doc_root->ptr, con->physical.doc_root->used-1)) {
buffer_append_string(p->ofn, con->physical.path->ptr + con->physical.doc_root->used - 1);
} else {
buffer_append_string_buffer(p->ofn, con->uri.path);
}
switch(type) {
case HTTP_ACCEPT_ENCODING_GZIP:
case HTTP_ACCEPT_ENCODING_X_GZIP:
buffer_append_string_len(p->ofn, CONST_STR_LEN("-gzip-"));
break;
case HTTP_ACCEPT_ENCODING_DEFLATE:
buffer_append_string_len(p->ofn, CONST_STR_LEN("-deflate-"));
break;
case HTTP_ACCEPT_ENCODING_BZIP2:
case HTTP_ACCEPT_ENCODING_X_BZIP2:
buffer_append_string_len(p->ofn, CONST_STR_LEN("-bzip2-"));
break;
default:
log_error_write(srv, __FILE__, __LINE__, "sd", "unknown compression type", type);
return -1;
}
buffer_append_string_buffer(p->ofn, sce->etag);
if (-1 == mkdir_for_file(p->ofn->ptr)) {
log_error_write(srv, __FILE__, __LINE__, "sb", "couldn't create directory for file", p->ofn);
return -1;
}
if (-1 == (ofd = open(p->ofn->ptr, O_WRONLY | O_CREAT | O_EXCL | O_BINARY, 0600))) {
if (errno == EEXIST) {
/* cache-entry exists */
#if 0
log_error_write(srv, __FILE__, __LINE__, "bs", p->ofn, "compress-cache hit");
#endif
fix buffer, chunk and http_chunk API * remove unused structs and functions (buffer_array, read_buffer) * change return type from int to void for many functions, as the return value (indicating error/success) was never checked, and the function would only fail on programming errors and not on invalid input; changed functions to use force_assert instead of returning an error. * all "len" parameters now are the real size of the memory to be read. the length of strings is given always without the terminating 0. * the "buffer" struct still counts the terminating 0 in ->used, provide buffer_string_length() to get the length of a string in a buffer. unset config "strings" have used == 0, which is used in some places to distinguish unset values from "" (empty string) values. * most buffer usages should now use it as string container. * optimise some buffer copying by "moving" data to other buffers * use (u)intmax_t for generic int-to-string functions * remove unused enum values: UNUSED_CHUNK, ENCODING_UNSET * converted BUFFER_APPEND_SLASH to inline function (no macro feature needed) * refactor: create chunkqueue_steal: moving (partial) chunks into another queue * http_chunk: added separate function to terminate chunked body instead of magic handling in http_chunk_append_mem(). http_chunk_append_* now handle empty chunks, and never terminate the chunked body. From: Stefan Bühler <stbuehler@web.de> git-svn-id: svn://svn.lighttpd.net/lighttpd/branches/lighttpd-1.4.x@2975 152afb58-edef-0310-8abb-c4023f1b3aa9
2015-02-08 12:37:10 +00:00
buffer_copy_buffer(con->physical.path, p->ofn);
return 0;
}
log_error_write(srv, __FILE__, __LINE__, "sbss", "creating cachefile", p->ofn, "failed", strerror(errno));
return -1;
}
#if 0
log_error_write(srv, __FILE__, __LINE__, "bs", p->ofn, "compress-cache miss");
#endif
if (-1 == (ifd = open(filename, O_RDONLY | O_BINARY))) {
log_error_write(srv, __FILE__, __LINE__, "sbss", "opening plain-file", fn, "failed", strerror(errno));
close(ofd);
/* Remove the incomplete cache file, so that later hits aren't served from it */
if (-1 == unlink(p->ofn->ptr)) {
log_error_write(srv, __FILE__, __LINE__, "sbss", "unlinking incomplete cachefile", p->ofn, "failed:", strerror(errno));
}
return -1;
}
#ifdef USE_MMAP
if (MAP_FAILED == (start = mmap(NULL, sce->st.st_size, PROT_READ, MAP_SHARED, ifd, 0))) {
log_error_write(srv, __FILE__, __LINE__, "sbss", "mmaping", fn, "failed", strerror(errno));
close(ofd);
close(ifd);
/* Remove the incomplete cache file, so that later hits aren't served from it */
if (-1 == unlink(p->ofn->ptr)) {
log_error_write(srv, __FILE__, __LINE__, "sbss", "unlinking incomplete cachefile", p->ofn, "failed:", strerror(errno));
}
return -1;
}
#else
start = malloc(sce->st.st_size);
if (NULL == start || sce->st.st_size != read(ifd, start, sce->st.st_size)) {
log_error_write(srv, __FILE__, __LINE__, "sbss", "reading", fn, "failed", strerror(errno));
close(ofd);
close(ifd);
free(start);
/* Remove the incomplete cache file, so that later hits aren't served from it */
if (-1 == unlink(p->ofn->ptr)) {
log_error_write(srv, __FILE__, __LINE__, "sbss", "unlinking incomplete cachefile", p->ofn, "failed:", strerror(errno));
}
return -1;
}
#endif
ret = -1;
switch(type) {
#ifdef USE_ZLIB
case HTTP_ACCEPT_ENCODING_GZIP:
case HTTP_ACCEPT_ENCODING_X_GZIP:
ret = deflate_file_to_buffer_gzip(srv, con, p, start, sce->st.st_size, sce->st.st_mtime);
break;
case HTTP_ACCEPT_ENCODING_DEFLATE:
ret = deflate_file_to_buffer_deflate(srv, con, p, start, sce->st.st_size);
break;
#endif
#ifdef USE_BZ2LIB
case HTTP_ACCEPT_ENCODING_BZIP2:
case HTTP_ACCEPT_ENCODING_X_BZIP2:
ret = deflate_file_to_buffer_bzip2(srv, con, p, start, sce->st.st_size);
break;
#endif
}
if (ret == 0) {
r = write(ofd, CONST_BUF_LEN(p->b));
if (-1 == r) {
log_error_write(srv, __FILE__, __LINE__, "sbss", "writing cachefile", p->ofn, "failed:", strerror(errno));
ret = -1;
} else if ((size_t)r != buffer_string_length(p->b)) {
log_error_write(srv, __FILE__, __LINE__, "sbs", "writing cachefile", p->ofn, "failed: not enough bytes written");
ret = -1;
}
}
#ifdef USE_MMAP
munmap(start, sce->st.st_size);
#else
free(start);
#endif
close(ofd);
close(ifd);
if (ret != 0) {
/* Remove the incomplete cache file, so that later hits aren't served from it */
if (-1 == unlink(p->ofn->ptr)) {
log_error_write(srv, __FILE__, __LINE__, "sbss", "unlinking incomplete cachefile", p->ofn, "failed:", strerror(errno));
}
return -1;
}
fix buffer, chunk and http_chunk API * remove unused structs and functions (buffer_array, read_buffer) * change return type from int to void for many functions, as the return value (indicating error/success) was never checked, and the function would only fail on programming errors and not on invalid input; changed functions to use force_assert instead of returning an error. * all "len" parameters now are the real size of the memory to be read. the length of strings is given always without the terminating 0. * the "buffer" struct still counts the terminating 0 in ->used, provide buffer_string_length() to get the length of a string in a buffer. unset config "strings" have used == 0, which is used in some places to distinguish unset values from "" (empty string) values. * most buffer usages should now use it as string container. * optimise some buffer copying by "moving" data to other buffers * use (u)intmax_t for generic int-to-string functions * remove unused enum values: UNUSED_CHUNK, ENCODING_UNSET * converted BUFFER_APPEND_SLASH to inline function (no macro feature needed) * refactor: create chunkqueue_steal: moving (partial) chunks into another queue * http_chunk: added separate function to terminate chunked body instead of magic handling in http_chunk_append_mem(). http_chunk_append_* now handle empty chunks, and never terminate the chunked body. From: Stefan Bühler <stbuehler@web.de> git-svn-id: svn://svn.lighttpd.net/lighttpd/branches/lighttpd-1.4.x@2975 152afb58-edef-0310-8abb-c4023f1b3aa9
2015-02-08 12:37:10 +00:00
buffer_copy_buffer(con->physical.path, p->ofn);
return 0;
}
static int deflate_file_to_buffer(server *srv, connection *con, plugin_data *p, buffer *fn, stat_cache_entry *sce, int type) {
int ifd;
int ret = -1;
void *start;
/* overflow */
if ((off_t)(sce->st.st_size * 1.1) < sce->st.st_size) return -1;
/* don't mmap files > 128M
*
* we could use a sliding window, but currently there is no need for it
*/
if (sce->st.st_size > 128 * 1024 * 1024) return -1;
if (-1 == (ifd = open(fn->ptr, O_RDONLY | O_BINARY))) {
log_error_write(srv, __FILE__, __LINE__, "sbss", "opening plain-file", fn, "failed", strerror(errno));
return -1;
}
#ifdef USE_MMAP
if (MAP_FAILED == (start = mmap(NULL, sce->st.st_size, PROT_READ, MAP_SHARED, ifd, 0))) {
log_error_write(srv, __FILE__, __LINE__, "sbss", "mmaping", fn, "failed", strerror(errno));
close(ifd);
return -1;
}
#else
start = malloc(sce->st.st_size);
if (NULL == start || sce->st.st_size != read(ifd, start, sce->st.st_size)) {
log_error_write(srv, __FILE__, __LINE__, "sbss", "reading", fn, "failed", strerror(errno));
close(ifd);
free(start);
return -1;
}
#endif
switch(type) {
#ifdef USE_ZLIB
case HTTP_ACCEPT_ENCODING_GZIP:
case HTTP_ACCEPT_ENCODING_X_GZIP:
ret = deflate_file_to_buffer_gzip(srv, con, p, start, sce->st.st_size, sce->st.st_mtime);
break;
case HTTP_ACCEPT_ENCODING_DEFLATE:
ret = deflate_file_to_buffer_deflate(srv, con, p, start, sce->st.st_size);
break;
#endif
#ifdef USE_BZ2LIB
case HTTP_ACCEPT_ENCODING_BZIP2:
case HTTP_ACCEPT_ENCODING_X_BZIP2:
ret = deflate_file_to_buffer_bzip2(srv, con, p, start, sce->st.st_size);
break;
#endif
default:
ret = -1;
break;
}
#ifdef USE_MMAP
munmap(start, sce->st.st_size);
#else
free(start);
#endif
close(ifd);
if (ret != 0) return -1;
chunkqueue_reset(con->write_queue);
chunkqueue_append_buffer(con->write_queue, p->b);
buffer_reset(con->physical.path);
con->file_finished = 1;
con->file_started = 1;