|
|
|
@ -467,14 +467,13 @@ static void mod_compress_note_ratio(server *srv, connection *con, off_t in, off_
|
|
|
|
|
UNUSED(srv);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int deflate_file_to_file(server *srv, connection *con, plugin_data *p, buffer *fn, stat_cache_entry *sce, int type) {
|
|
|
|
|
int ifd, ofd;
|
|
|
|
|
static int deflate_file_to_file(server *srv, connection *con, plugin_data *p, int ifd, buffer *fn, stat_cache_entry *sce, int type) {
|
|
|
|
|
int ofd;
|
|
|
|
|
int ret;
|
|
|
|
|
#ifdef USE_MMAP
|
|
|
|
|
volatile int mapped = 0;/* quiet warning: might be clobbered by 'longjmp' */
|
|
|
|
|
#endif
|
|
|
|
|
void *start;
|
|
|
|
|
const char *filename = fn->ptr;
|
|
|
|
|
stat_cache_entry *sce_ofn;
|
|
|
|
|
ssize_t r;
|
|
|
|
|
|
|
|
|
@ -550,18 +549,6 @@ static int deflate_file_to_file(server *srv, connection *con, plugin_data *p, bu
|
|
|
|
|
#if 0
|
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "bs", p->ofn, "compress-cache miss");
|
|
|
|
|
#endif
|
|
|
|
|
if (-1 == (ifd = fdevent_open_cloexec(filename, con->conf.follow_symlink, O_RDONLY, 0))) {
|
|
|
|
|
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))
|
|
|
|
@ -663,8 +650,7 @@ static int deflate_file_to_file(server *srv, connection *con, plugin_data *p, bu
|
|
|
|
|
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;
|
|
|
|
|
static int deflate_file_to_buffer(server *srv, connection *con, plugin_data *p, int ifd, buffer *fn, stat_cache_entry *sce, int type) {
|
|
|
|
|
int ret = -1;
|
|
|
|
|
#ifdef USE_MMAP
|
|
|
|
|
volatile int mapped = 0;/* quiet warning: might be clobbered by 'longjmp' */
|
|
|
|
@ -685,9 +671,9 @@ static int deflate_file_to_buffer(server *srv, connection *con, plugin_data *p,
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (-1 == (ifd = fdevent_open_cloexec(fn->ptr, con->conf.follow_symlink, O_RDONLY, 0))) {
|
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sbss", "opening plain-file", fn, "failed", strerror(errno));
|
|
|
|
|
|
|
|
|
|
if (-1 == ifd) {
|
|
|
|
|
/* not called; call exists to de-optimize and avoid "clobbered by 'longjmp'" compiler warning */
|
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "s", "");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -942,6 +928,13 @@ PHYSICALPATH_FUNC(mod_compress_physical) {
|
|
|
|
|
return HANDLER_GO_ON;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const int fd = fdevent_open_cloexec(con->physical.path->ptr, con->conf.follow_symlink, O_RDONLY, 0);
|
|
|
|
|
if (fd < 0) {
|
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sbss",
|
|
|
|
|
"opening plain-file", con->physical.path, "failed", strerror(errno));
|
|
|
|
|
return HANDLER_GO_ON;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mtime = strftime_cache_get(srv, sce->st.st_mtime);
|
|
|
|
|
|
|
|
|
|
/* try matching original etag of uncompressed version */
|
|
|
|
@ -951,6 +944,7 @@ PHYSICALPATH_FUNC(mod_compress_physical) {
|
|
|
|
|
http_header_response_set(con, HTTP_HEADER_CONTENT_TYPE, CONST_STR_LEN("Content-Type"), CONST_BUF_LEN(sce->content_type));
|
|
|
|
|
http_header_response_set(con, HTTP_HEADER_LAST_MODIFIED, CONST_STR_LEN("Last-Modified"), CONST_BUF_LEN(mtime));
|
|
|
|
|
http_header_response_set(con, HTTP_HEADER_ETAG, CONST_STR_LEN("ETag"), CONST_BUF_LEN(con->physical.etag));
|
|
|
|
|
close(fd);
|
|
|
|
|
return HANDLER_FINISHED;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -989,17 +983,23 @@ PHYSICALPATH_FUNC(mod_compress_physical) {
|
|
|
|
|
if (use_etag) {
|
|
|
|
|
http_header_response_set(con, HTTP_HEADER_ETAG, CONST_STR_LEN("ETag"), CONST_BUF_LEN(con->physical.etag));
|
|
|
|
|
}
|
|
|
|
|
close(fd);
|
|
|
|
|
return HANDLER_FINISHED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* deflate it */
|
|
|
|
|
if (use_etag && !buffer_string_is_empty(p->conf.compress_cache_dir)) {
|
|
|
|
|
if (0 != deflate_file_to_file(srv, con, p, con->physical.path, sce, compression_type))
|
|
|
|
|
if (0 != deflate_file_to_file(srv, con, p, fd, con->physical.path, sce, compression_type)) {
|
|
|
|
|
close(fd);
|
|
|
|
|
return HANDLER_GO_ON;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (0 != deflate_file_to_buffer(srv, con, p, con->physical.path, sce, compression_type))
|
|
|
|
|
if (0 != deflate_file_to_buffer(srv, con, p, fd, con->physical.path, sce, compression_type)) {
|
|
|
|
|
close(fd);
|
|
|
|
|
return HANDLER_GO_ON;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
close(fd);
|
|
|
|
|
http_header_response_set(con, HTTP_HEADER_CONTENT_ENCODING, CONST_STR_LEN("Content-Encoding"), compression_name, strlen(compression_name));
|
|
|
|
|
http_header_response_set(con, HTTP_HEADER_LAST_MODIFIED, CONST_STR_LEN("Last-Modified"), CONST_BUF_LEN(mtime));
|
|
|
|
|
if (use_etag) {
|
|
|
|
|