1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
--- 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;
}
|