--- lighttpd-1.4.11.orig/src/mod_compress.c (revision 861) +++ lighttpd-1.4.11/src/mod_compress.c (revision 1046) @@ -15,4 +15,5 @@ #include "response.h" #include "stat_cache.h" +#include "http_chunk.h" #include "plugin.h" @@ -327,4 +328,7 @@ const char *filename = fn->ptr; ssize_t r; + stat_cache_entry *compressed_sce = NULL; + + if (buffer_is_empty(p->conf.compress_cache_dir)) return -1; /* overflow */ @@ -384,25 +388,32 @@ buffer_append_string_buffer(p->ofn, sce->etag); + + + if (HANDLER_ERROR != stat_cache_get_entry(srv, con, p->ofn, &compressed_sce)) { + /* file exists */ + + http_chunk_append_file(srv, con, p->ofn, 0, compressed_sce->st.st_size); + con->file_finished = 1; + + return 0; + } 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 - buffer_copy_string_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 + log_error_write(srv, __FILE__, __LINE__, "sbss", + "creating cachefile", p->ofn, + "failed", strerror(errno)); + + return -1; + } + if (-1 == (ifd = open(filename, O_RDONLY | O_BINARY))) { - log_error_write(srv, __FILE__, __LINE__, "sbss", "opening plain-file", fn, "failed", strerror(errno)); + log_error_write(srv, __FILE__, __LINE__, "sbss", + "opening plain-file", fn, + "failed", strerror(errno)); close(ofd); @@ -413,5 +424,7 @@ 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)); + log_error_write(srv, __FILE__, __LINE__, "sbss", + "mmaping", fn, + "failed", strerror(errno)); close(ofd); @@ -456,6 +469,7 @@ if (ret != 0) return -1; - buffer_copy_string_buffer(con->physical.path, p->ofn); - + http_chunk_append_file(srv, con, p->ofn, 0, r); + con->file_finished = 1; + return 0; } @@ -477,5 +491,4 @@ 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)); @@ -483,13 +496,15 @@ return -1; } - - - if (MAP_FAILED == (start = mmap(NULL, sce->st.st_size, PROT_READ, MAP_SHARED, ifd, 0))) { + + start = mmap(NULL, sce->st.st_size, PROT_READ, MAP_SHARED, ifd, 0); + + close(ifd); + + if (MAP_FAILED == start) { log_error_write(srv, __FILE__, __LINE__, "sbss", "mmaping", fn, "failed", strerror(errno)); - close(ifd); - return -1; - } - + return -1; + } + switch(type) { #ifdef USE_ZLIB @@ -512,5 +527,4 @@ munmap(start, sce->st.st_size); - close(ifd); if (ret != 0) return -1; @@ -589,4 +603,7 @@ /* don't compress files that are too large as we need to much time to handle them */ if (max_fsize && (sce->st.st_size >> 10) > max_fsize) return HANDLER_GO_ON; + + /* compressing the file might lead to larger files instead */ + if (sce->st.st_size < 128) return HANDLER_GO_ON; /* check if mimetype is in compress-config */ @@ -639,5 +656,18 @@ const char *compression_name = NULL; int compression_type = 0; + buffer *mtime; + mtime = strftime_cache_get(srv, sce->st.st_mtime); + etag_mutate(con->physical.etag, sce->etag); + + response_header_overwrite(srv, con, CONST_STR_LEN("Last-Modified"), CONST_BUF_LEN(mtime)); + response_header_overwrite(srv, con, CONST_STR_LEN("ETag"), CONST_BUF_LEN(con->physical.etag)); + + /* perhaps we don't even have to compress the file as the browser still has the + * current version */ + if (HANDLER_FINISHED == http_response_handle_cachable(srv, con, mtime)) { + return HANDLER_FINISHED; + } + /* select best matching encoding */ if (matched_encodings & HTTP_ACCEPT_ENCODING_BZIP2) { @@ -652,28 +682,18 @@ } - /* deflate it */ - if (p->conf.compress_cache_dir->used) { - if (0 == deflate_file_to_file(srv, con, p, - con->physical.path, sce, compression_type)) { - buffer *mtime; + /* deflate it to file (cached) or to memory */ + if (0 == deflate_file_to_file(srv, con, p, + con->physical.path, sce, compression_type) || + 0 == deflate_file_to_buffer(srv, con, p, + con->physical.path, sce, compression_type)) { - response_header_overwrite(srv, con, CONST_STR_LEN("Content-Encoding"), compression_name, strlen(compression_name)); - - mtime = strftime_cache_get(srv, sce->st.st_mtime); - response_header_overwrite(srv, con, CONST_STR_LEN("Last-Modified"), CONST_BUF_LEN(mtime)); - - etag_mutate(con->physical.etag, sce->etag); - response_header_overwrite(srv, con, CONST_STR_LEN("ETag"), CONST_BUF_LEN(con->physical.etag)); - - response_header_overwrite(srv, con, CONST_STR_LEN("Content-Type"), CONST_BUF_LEN(sce->content_type)); - - return HANDLER_GO_ON; - } - } else if (0 == deflate_file_to_buffer(srv, con, p, - con->physical.path, sce, compression_type)) { - - response_header_overwrite(srv, con, CONST_STR_LEN("Content-Encoding"), compression_name, strlen(compression_name)); - response_header_overwrite(srv, con, CONST_STR_LEN("Content-Type"), CONST_BUF_LEN(sce->content_type)); - + response_header_overwrite(srv, con, + CONST_STR_LEN("Content-Encoding"), + compression_name, strlen(compression_name)); + + response_header_overwrite(srv, con, + CONST_STR_LEN("Content-Type"), + CONST_BUF_LEN(sce->content_type)); + return HANDLER_FINISHED; }