0
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9 #include <ngx_http.h>
|
|
10
|
|
11 #include <zlib.h>
|
|
12
|
|
13
|
|
14 typedef struct {
|
|
15 ngx_flag_t enable;
|
|
16 ngx_flag_t no_buffer;
|
|
17
|
2
|
18 ngx_array_t *types; /* array of ngx_http_gzip_type_t */
|
|
19
|
0
|
20 ngx_bufs_t bufs;
|
|
21
|
|
22 ngx_uint_t http_version;
|
|
23 ngx_uint_t proxied;
|
|
24
|
|
25 int level;
|
|
26 size_t wbits;
|
|
27 size_t memlevel;
|
|
28 ssize_t min_length;
|
|
29 } ngx_http_gzip_conf_t;
|
|
30
|
|
31
|
2
|
32 typedef struct {
|
|
33 ngx_str_t name;
|
|
34 ngx_uint_t enable;
|
|
35 } ngx_http_gzip_type_t;
|
|
36
|
|
37
|
0
|
38 #define NGX_HTTP_GZIP_PROXIED_OFF 0x0002
|
|
39 #define NGX_HTTP_GZIP_PROXIED_EXPIRED 0x0004
|
|
40 #define NGX_HTTP_GZIP_PROXIED_NO_CACHE 0x0008
|
|
41 #define NGX_HTTP_GZIP_PROXIED_NO_STORE 0x0010
|
|
42 #define NGX_HTTP_GZIP_PROXIED_PRIVATE 0x0020
|
|
43 #define NGX_HTTP_GZIP_PROXIED_NO_LM 0x0040
|
|
44 #define NGX_HTTP_GZIP_PROXIED_NO_ETAG 0x0080
|
|
45 #define NGX_HTTP_GZIP_PROXIED_AUTH 0x0100
|
|
46 #define NGX_HTTP_GZIP_PROXIED_ANY 0x0200
|
|
47
|
|
48
|
|
49 typedef struct {
|
|
50 ngx_chain_t *in;
|
|
51 ngx_chain_t *free;
|
|
52 ngx_chain_t *busy;
|
|
53 ngx_chain_t *out;
|
|
54 ngx_chain_t **last_out;
|
|
55 ngx_buf_t *in_buf;
|
|
56 ngx_buf_t *out_buf;
|
|
57 ngx_int_t bufs;
|
|
58
|
|
59 off_t length;
|
|
60
|
|
61 void *preallocated;
|
|
62 char *free_mem;
|
|
63 ngx_uint_t allocated;
|
|
64
|
|
65 unsigned flush:4;
|
|
66 unsigned redo:1;
|
|
67 unsigned done:1;
|
|
68
|
|
69 size_t zin;
|
|
70 size_t zout;
|
|
71
|
|
72 uint32_t crc32;
|
|
73 z_stream zstream;
|
|
74 ngx_http_request_t *request;
|
|
75 } ngx_http_gzip_ctx_t;
|
|
76
|
|
77
|
|
78 static ngx_int_t ngx_http_gzip_proxied(ngx_http_request_t *r,
|
|
79 ngx_http_gzip_conf_t *conf);
|
|
80 static void *ngx_http_gzip_filter_alloc(void *opaque, u_int items,
|
|
81 u_int size);
|
|
82 static void ngx_http_gzip_filter_free(void *opaque, void *address);
|
10
|
83 static int ngx_http_gzip_error(ngx_http_gzip_ctx_t *ctx);
|
0
|
84
|
|
85 static u_char *ngx_http_gzip_log_ratio(ngx_http_request_t *r, u_char *buf,
|
|
86 uintptr_t data);
|
|
87
|
|
88 static ngx_int_t ngx_http_gzip_pre_conf(ngx_conf_t *cf);
|
|
89 static ngx_int_t ngx_http_gzip_filter_init(ngx_cycle_t *cycle);
|
|
90 static void *ngx_http_gzip_create_conf(ngx_conf_t *cf);
|
|
91 static char *ngx_http_gzip_merge_conf(ngx_conf_t *cf,
|
|
92 void *parent, void *child);
|
2
|
93 static char *ngx_http_gzip_set_types(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
94 void *conf);
|
0
|
95 static char *ngx_http_gzip_set_window(ngx_conf_t *cf, void *post, void *data);
|
|
96 static char *ngx_http_gzip_set_hash(ngx_conf_t *cf, void *post, void *data);
|
|
97
|
|
98
|
|
99 static ngx_conf_num_bounds_t ngx_http_gzip_comp_level_bounds = {
|
|
100 ngx_conf_check_num_bounds, 1, 9
|
|
101 };
|
|
102
|
|
103 static ngx_conf_post_handler_pt ngx_http_gzip_set_window_p =
|
|
104 ngx_http_gzip_set_window;
|
|
105 static ngx_conf_post_handler_pt ngx_http_gzip_set_hash_p =
|
|
106 ngx_http_gzip_set_hash;
|
|
107
|
|
108
|
|
109
|
|
110 static ngx_conf_enum_t ngx_http_gzip_http_version[] = {
|
|
111 { ngx_string("1.0"), NGX_HTTP_VERSION_10 },
|
|
112 { ngx_string("1.1"), NGX_HTTP_VERSION_11 },
|
|
113 { ngx_null_string, 0 }
|
|
114 };
|
|
115
|
|
116
|
|
117 static ngx_conf_bitmask_t ngx_http_gzip_proxied_mask[] = {
|
|
118 { ngx_string("off"), NGX_HTTP_GZIP_PROXIED_OFF },
|
|
119 { ngx_string("expired"), NGX_HTTP_GZIP_PROXIED_EXPIRED },
|
|
120 { ngx_string("no-cache"), NGX_HTTP_GZIP_PROXIED_NO_CACHE },
|
|
121 { ngx_string("no-store"), NGX_HTTP_GZIP_PROXIED_NO_STORE },
|
|
122 { ngx_string("private"), NGX_HTTP_GZIP_PROXIED_PRIVATE },
|
|
123 { ngx_string("no_last_modified"), NGX_HTTP_GZIP_PROXIED_NO_LM },
|
|
124 { ngx_string("no_etag"), NGX_HTTP_GZIP_PROXIED_NO_ETAG },
|
|
125 { ngx_string("auth"), NGX_HTTP_GZIP_PROXIED_AUTH },
|
|
126 { ngx_string("any"), NGX_HTTP_GZIP_PROXIED_ANY },
|
|
127 { ngx_null_string, 0 }
|
|
128 };
|
|
129
|
|
130
|
|
131 static ngx_command_t ngx_http_gzip_filter_commands[] = {
|
|
132
|
|
133 { ngx_string("gzip"),
|
|
134 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
135 ngx_conf_set_flag_slot,
|
|
136 NGX_HTTP_LOC_CONF_OFFSET,
|
|
137 offsetof(ngx_http_gzip_conf_t, enable),
|
|
138 NULL },
|
|
139
|
|
140 { ngx_string("gzip_buffers"),
|
|
141 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
|
|
142 ngx_conf_set_bufs_slot,
|
|
143 NGX_HTTP_LOC_CONF_OFFSET,
|
|
144 offsetof(ngx_http_gzip_conf_t, bufs),
|
|
145 NULL },
|
|
146
|
2
|
147 { ngx_string("gzip_types"),
|
|
148 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
|
|
149 ngx_http_gzip_set_types,
|
|
150 NGX_HTTP_LOC_CONF_OFFSET,
|
|
151 0,
|
|
152 NULL },
|
|
153
|
0
|
154 { ngx_string("gzip_comp_level"),
|
|
155 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
156 ngx_conf_set_num_slot,
|
|
157 NGX_HTTP_LOC_CONF_OFFSET,
|
|
158 offsetof(ngx_http_gzip_conf_t, level),
|
|
159 &ngx_http_gzip_comp_level_bounds },
|
|
160
|
|
161 { ngx_string("gzip_window"),
|
|
162 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
163 ngx_conf_set_size_slot,
|
|
164 NGX_HTTP_LOC_CONF_OFFSET,
|
|
165 offsetof(ngx_http_gzip_conf_t, wbits),
|
|
166 &ngx_http_gzip_set_window_p },
|
|
167
|
|
168 { ngx_string("gzip_hash"),
|
|
169 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
170 ngx_conf_set_size_slot,
|
|
171 NGX_HTTP_LOC_CONF_OFFSET,
|
|
172 offsetof(ngx_http_gzip_conf_t, memlevel),
|
|
173 &ngx_http_gzip_set_hash_p },
|
|
174
|
|
175 { ngx_string("gzip_no_buffer"),
|
|
176 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
177 ngx_conf_set_flag_slot,
|
|
178 NGX_HTTP_LOC_CONF_OFFSET,
|
|
179 offsetof(ngx_http_gzip_conf_t, no_buffer),
|
|
180 NULL },
|
|
181
|
|
182 { ngx_string("gzip_http_version"),
|
|
183 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_ANY,
|
|
184 ngx_conf_set_enum_slot,
|
|
185 NGX_HTTP_LOC_CONF_OFFSET,
|
|
186 offsetof(ngx_http_gzip_conf_t, http_version),
|
|
187 &ngx_http_gzip_http_version },
|
|
188
|
|
189 { ngx_string("gzip_proxied"),
|
|
190 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_ANY,
|
|
191 ngx_conf_set_bitmask_slot,
|
|
192 NGX_HTTP_LOC_CONF_OFFSET,
|
|
193 offsetof(ngx_http_gzip_conf_t, proxied),
|
|
194 &ngx_http_gzip_proxied_mask },
|
|
195
|
|
196 { ngx_string("gzip_min_length"),
|
|
197 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
198 ngx_conf_set_size_slot,
|
|
199 NGX_HTTP_LOC_CONF_OFFSET,
|
|
200 offsetof(ngx_http_gzip_conf_t, min_length),
|
|
201 NULL },
|
|
202
|
|
203 ngx_null_command
|
|
204 };
|
|
205
|
|
206
|
|
207 static ngx_http_module_t ngx_http_gzip_filter_module_ctx = {
|
|
208 ngx_http_gzip_pre_conf, /* pre conf */
|
|
209
|
|
210 NULL, /* create main configuration */
|
|
211 NULL, /* init main configuration */
|
|
212
|
|
213 NULL, /* create server configuration */
|
|
214 NULL, /* merge server configuration */
|
|
215
|
|
216 ngx_http_gzip_create_conf, /* create location configuration */
|
|
217 ngx_http_gzip_merge_conf, /* merge location configuration */
|
|
218 };
|
|
219
|
|
220
|
|
221 ngx_module_t ngx_http_gzip_filter_module = {
|
|
222 NGX_MODULE,
|
|
223 &ngx_http_gzip_filter_module_ctx, /* module context */
|
|
224 ngx_http_gzip_filter_commands, /* module directives */
|
|
225 NGX_HTTP_MODULE, /* module type */
|
|
226 ngx_http_gzip_filter_init, /* init module */
|
2
|
227 NULL /* init process */
|
0
|
228 };
|
|
229
|
|
230
|
|
231 static ngx_http_log_op_name_t ngx_http_gzip_log_fmt_ops[] = {
|
|
232 { ngx_string("gzip_ratio"), NGX_INT32_LEN + 3, ngx_http_gzip_log_ratio },
|
|
233 { ngx_null_string, 0, NULL }
|
|
234 };
|
|
235
|
|
236
|
|
237
|
|
238 static u_char gzheader[10] = { 0x1f, 0x8b, Z_DEFLATED, 0, 0, 0, 0, 0, 0, 3 };
|
|
239
|
18
|
240 #if (NGX_HAVE_LITTLE_ENDIAN)
|
0
|
241
|
|
242 struct gztrailer {
|
|
243 uint32_t crc32;
|
|
244 uint32_t zlen;
|
|
245 };
|
|
246
|
18
|
247 #else /* NGX_HAVE_BIG_ENDIAN */
|
0
|
248
|
|
249 struct gztrailer {
|
|
250 u_char crc32[4];
|
|
251 u_char zlen[4];
|
|
252 };
|
|
253
|
|
254 #endif
|
|
255
|
|
256
|
|
257 static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
|
|
258 static ngx_http_output_body_filter_pt ngx_http_next_body_filter;
|
|
259
|
|
260
|
|
261 static ngx_int_t ngx_http_gzip_header_filter(ngx_http_request_t *r)
|
|
262 {
|
2
|
263 ngx_uint_t i, found;
|
0
|
264 ngx_http_gzip_ctx_t *ctx;
|
|
265 ngx_http_gzip_conf_t *conf;
|
2
|
266 ngx_http_gzip_type_t *type;
|
0
|
267
|
|
268 conf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_filter_module);
|
|
269
|
|
270 if (!conf->enable
|
2
|
271 || (r->headers_out.status != NGX_HTTP_OK
|
|
272 && r->headers_out.status != NGX_HTTP_FORBIDDEN
|
|
273 && r->headers_out.status != NGX_HTTP_NOT_FOUND)
|
0
|
274 || r->header_only
|
|
275 || r->http_version < conf->http_version
|
2
|
276 || r->headers_out.content_type == NULL
|
0
|
277 || (r->headers_out.content_encoding
|
|
278 && r->headers_out.content_encoding->value.len)
|
|
279 || r->headers_in.accept_encoding == NULL
|
|
280 || (r->headers_out.content_length_n != -1
|
|
281 && r->headers_out.content_length_n < conf->min_length)
|
|
282 || ngx_strstr(r->headers_in.accept_encoding->value.data, "gzip") == NULL
|
|
283 )
|
|
284 {
|
|
285 return ngx_http_next_header_filter(r);
|
|
286 }
|
|
287
|
2
|
288
|
|
289 found = 0;
|
|
290 type = conf->types->elts;
|
|
291
|
|
292 for (i = 0; i < conf->types->nelts; i++) {
|
|
293 if (r->headers_out.content_type->value.len >= type[i].name.len
|
|
294 && ngx_strncasecmp(r->headers_out.content_type->value.data,
|
|
295 type[i].name.data, type[i].name.len) == 0)
|
|
296 {
|
|
297 found = 1;
|
|
298 break;
|
|
299 }
|
|
300 }
|
|
301
|
|
302 if (!found) {
|
0
|
303 return ngx_http_next_header_filter(r);
|
|
304 }
|
|
305
|
|
306
|
|
307 if (r->headers_in.via) {
|
|
308 if (conf->proxied & NGX_HTTP_GZIP_PROXIED_OFF) {
|
|
309 return ngx_http_next_header_filter(r);
|
|
310 }
|
|
311
|
|
312 if (!(conf->proxied & NGX_HTTP_GZIP_PROXIED_ANY)
|
|
313 && ngx_http_gzip_proxied(r, conf) == NGX_DECLINED)
|
|
314 {
|
|
315 return ngx_http_next_header_filter(r);
|
|
316 }
|
|
317 }
|
|
318
|
|
319
|
|
320 /*
|
|
321 * if the URL (without the "http://" prefix) is longer than 253 bytes
|
|
322 * then MSIE 4.x can not handle the compressed stream - it waits too long,
|
|
323 * hangs up or crashes
|
|
324 */
|
|
325
|
|
326 if (r->headers_in.msie4 && r->unparsed_uri.len > 200) {
|
|
327 return ngx_http_next_header_filter(r);
|
|
328 }
|
|
329
|
|
330
|
|
331 ngx_http_create_ctx(r, ctx, ngx_http_gzip_filter_module,
|
|
332 sizeof(ngx_http_gzip_ctx_t), NGX_ERROR);
|
|
333 ctx->request = r;
|
|
334
|
|
335 r->headers_out.content_encoding = ngx_list_push(&r->headers_out.headers);
|
|
336 if (r->headers_out.content_encoding == NULL) {
|
|
337 return NGX_ERROR;
|
|
338 }
|
|
339
|
|
340 r->headers_out.content_encoding->key.len = sizeof("Content-Encoding") - 1;
|
|
341 r->headers_out.content_encoding->key.data = (u_char *) "Content-Encoding";
|
|
342 r->headers_out.content_encoding->value.len = sizeof("gzip") - 1;
|
|
343 r->headers_out.content_encoding->value.data = (u_char *) "gzip";
|
|
344
|
|
345 ctx->length = r->headers_out.content_length_n;
|
|
346 r->headers_out.content_length_n = -1;
|
|
347 if (r->headers_out.content_length) {
|
|
348 r->headers_out.content_length->key.len = 0;
|
|
349 r->headers_out.content_length = NULL;
|
|
350 }
|
|
351 r->filter_need_in_memory = 1;
|
|
352
|
|
353 return ngx_http_next_header_filter(r);
|
|
354 }
|
|
355
|
|
356
|
|
357 static ngx_int_t ngx_http_gzip_proxied(ngx_http_request_t *r,
|
|
358 ngx_http_gzip_conf_t *conf)
|
|
359 {
|
|
360 time_t date, expires;
|
|
361
|
|
362 if (r->headers_in.authorization
|
|
363 && (conf->proxied & NGX_HTTP_GZIP_PROXIED_AUTH))
|
|
364 {
|
|
365 return NGX_OK;
|
|
366 }
|
|
367
|
|
368 if (r->headers_out.expires) {
|
|
369
|
|
370 if (!(conf->proxied & NGX_HTTP_GZIP_PROXIED_EXPIRED)) {
|
|
371 return NGX_DECLINED;
|
|
372 }
|
|
373
|
|
374 expires = ngx_http_parse_time(r->headers_out.expires->value.data,
|
|
375 r->headers_out.expires->value.len);
|
|
376 if (expires == NGX_ERROR) {
|
|
377 return NGX_DECLINED;
|
|
378 }
|
|
379
|
|
380 if (r->headers_out.date) {
|
|
381 date = ngx_http_parse_time(r->headers_out.date->value.data,
|
|
382 r->headers_out.date->value.len);
|
|
383 if (date == NGX_ERROR) {
|
|
384 return NGX_DECLINED;
|
|
385 }
|
|
386
|
|
387 } else {
|
|
388 date = ngx_time();
|
|
389 }
|
|
390
|
|
391 if (expires < date) {
|
|
392 return NGX_OK;
|
|
393 }
|
|
394
|
|
395 return NGX_DECLINED;
|
|
396 }
|
|
397
|
|
398 if (r->headers_out.cache_control) {
|
|
399
|
|
400 if ((conf->proxied & NGX_HTTP_GZIP_PROXIED_NO_CACHE)
|
|
401 && ngx_strstr(r->headers_out.cache_control->value.data, "no-cache"))
|
|
402 {
|
|
403 return NGX_OK;
|
|
404 }
|
|
405
|
|
406 if ((conf->proxied & NGX_HTTP_GZIP_PROXIED_NO_STORE)
|
|
407 && ngx_strstr(r->headers_out.cache_control->value.data, "no-store"))
|
|
408 {
|
|
409 return NGX_OK;
|
|
410 }
|
|
411
|
|
412 if ((conf->proxied & NGX_HTTP_GZIP_PROXIED_PRIVATE)
|
|
413 && ngx_strstr(r->headers_out.cache_control->value.data, "private"))
|
|
414 {
|
|
415 return NGX_OK;
|
|
416 }
|
|
417
|
|
418 return NGX_DECLINED;
|
|
419 }
|
|
420
|
|
421 if ((conf->proxied & NGX_HTTP_GZIP_PROXIED_NO_LM)
|
|
422 && r->headers_out.last_modified)
|
|
423 {
|
|
424 return NGX_DECLINED;
|
|
425 }
|
|
426
|
|
427 if ((conf->proxied & NGX_HTTP_GZIP_PROXIED_NO_ETAG)
|
|
428 && r->headers_out.etag)
|
|
429 {
|
|
430 return NGX_DECLINED;
|
|
431 }
|
|
432
|
|
433 return NGX_OK;
|
|
434 }
|
|
435
|
|
436
|
|
437 static ngx_int_t ngx_http_gzip_body_filter(ngx_http_request_t *r,
|
|
438 ngx_chain_t *in)
|
|
439 {
|
18
|
440 int rc, wbits, memlevel;
|
|
441 ngx_int_t last;
|
0
|
442 struct gztrailer *trailer;
|
|
443 ngx_buf_t *b;
|
|
444 ngx_chain_t *cl;
|
|
445 ngx_http_gzip_ctx_t *ctx;
|
|
446 ngx_http_gzip_conf_t *conf;
|
|
447
|
|
448 ctx = ngx_http_get_module_ctx(r, ngx_http_gzip_filter_module);
|
|
449
|
|
450 if (ctx == NULL || ctx->done) {
|
|
451 return ngx_http_next_body_filter(r, in);
|
|
452 }
|
|
453
|
|
454 conf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_filter_module);
|
|
455
|
|
456 if (ctx->preallocated == NULL) {
|
|
457 wbits = conf->wbits;
|
|
458 memlevel = conf->memlevel;
|
|
459
|
|
460 if (ctx->length > 0) {
|
|
461
|
|
462 /* the actual zlib window size is smaller by 262 bytes */
|
|
463
|
|
464 while (ctx->length < ((1 << (wbits - 1)) - 262)) {
|
|
465 wbits--;
|
|
466 memlevel--;
|
|
467 }
|
|
468 }
|
|
469
|
|
470 /*
|
|
471 * We preallocate a memory for zlib in one buffer (200K-400K), this
|
|
472 * dicreases a number of malloc() and free() calls and also probably
|
18
|
473 * dicreases a number of syscalls (sbrk() and so on).
|
0
|
474 * Besides we free this memory as soon as the gzipping will complete
|
|
475 * and do not wait while a whole response will be sent to a client.
|
|
476 *
|
|
477 * 8K is for zlib deflate_state, it takes
|
|
478 * * 5816 bytes on x86 and sparc64 (32-bit mode)
|
|
479 * * 5920 bytes on amd64 and sparc64
|
|
480 */
|
|
481
|
|
482 ctx->allocated = 8192 + (1 << (wbits + 2)) + (1 << (memlevel + 9));
|
|
483
|
|
484 if (!(ctx->preallocated = ngx_palloc(r->pool, ctx->allocated))) {
|
|
485 return NGX_ERROR;
|
|
486 }
|
|
487
|
|
488 ctx->free_mem = ctx->preallocated;
|
|
489
|
|
490 ctx->zstream.zalloc = ngx_http_gzip_filter_alloc;
|
|
491 ctx->zstream.zfree = ngx_http_gzip_filter_free;
|
|
492 ctx->zstream.opaque = ctx;
|
|
493
|
|
494 rc = deflateInit2(&ctx->zstream, conf->level, Z_DEFLATED,
|
|
495 -wbits, memlevel, Z_DEFAULT_STRATEGY);
|
|
496
|
|
497 if (rc != Z_OK) {
|
|
498 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
|
|
499 "deflateInit2() failed: %d", rc);
|
|
500 return ngx_http_gzip_error(ctx);
|
|
501 }
|
|
502
|
|
503 if (!(b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)))) {
|
|
504 return ngx_http_gzip_error(ctx);
|
|
505 }
|
|
506
|
|
507 b->memory = 1;
|
|
508 b->pos = gzheader;
|
|
509 b->last = b->pos + 10;
|
|
510
|
10
|
511 if (!(cl = ngx_alloc_chain_link(r->pool))) {
|
|
512 return ngx_http_gzip_error(ctx);
|
|
513 }
|
|
514 cl->buf = b;
|
|
515 cl->next = NULL;
|
18
|
516
|
|
517 /*
|
|
518 * We pass the gzheader to the next filter now to avoid its linking
|
|
519 * to the ctx->busy chain. zlib does not usually output the compressed
|
|
520 * data in the initial iterations, so the gzheader that was linked
|
|
521 * to the ctx->busy chain would be flushed by ngx_http_write_filter().
|
|
522 */
|
|
523
|
|
524 if (ngx_http_next_body_filter(r, cl) == NGX_ERROR) {
|
|
525 return ngx_http_gzip_error(ctx);
|
|
526 }
|
|
527
|
|
528 ctx->last_out = &ctx->out;
|
0
|
529
|
|
530 ctx->crc32 = crc32(0L, Z_NULL, 0);
|
|
531 ctx->flush = Z_NO_FLUSH;
|
|
532 }
|
|
533
|
|
534 if (in) {
|
|
535 if (ngx_chain_add_copy(r->pool, &ctx->in, in) == NGX_ERROR) {
|
|
536 return ngx_http_gzip_error(ctx);
|
|
537 }
|
|
538 }
|
|
539
|
|
540 last = NGX_NONE;
|
|
541
|
|
542 for ( ;; ) {
|
|
543
|
|
544 for ( ;; ) {
|
|
545
|
|
546 /* does zlib need a new data ? */
|
|
547
|
|
548 if (ctx->zstream.avail_in == 0
|
|
549 && ctx->flush == Z_NO_FLUSH
|
|
550 && !ctx->redo)
|
|
551 {
|
|
552 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
10
|
553 "gzip in: %p", ctx->in);
|
0
|
554
|
|
555 if (ctx->in == NULL) {
|
|
556 break;
|
|
557 }
|
|
558
|
|
559 ctx->in_buf = ctx->in->buf;
|
|
560 ctx->in = ctx->in->next;
|
|
561
|
|
562 ctx->zstream.next_in = ctx->in_buf->pos;
|
|
563 ctx->zstream.avail_in = ctx->in_buf->last - ctx->in_buf->pos;
|
|
564
|
|
565 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
10
|
566 "gzip in_buf:%p ni:%p ai:%ud",
|
0
|
567 ctx->in_buf,
|
|
568 ctx->zstream.next_in, ctx->zstream.avail_in);
|
|
569
|
|
570 /* STUB */
|
|
571 if (ctx->in_buf->last < ctx->in_buf->pos) {
|
|
572 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
|
|
573 "zstream.avail_in is huge");
|
|
574 ctx->done = 1;
|
|
575 return NGX_ERROR;
|
|
576 }
|
|
577 /**/
|
|
578
|
|
579 if (ctx->in_buf->last_buf) {
|
|
580 ctx->flush = Z_FINISH;
|
|
581
|
|
582 } else if (ctx->in_buf->flush) {
|
|
583 ctx->flush = Z_SYNC_FLUSH;
|
|
584 }
|
|
585
|
|
586 if (ctx->zstream.avail_in == 0) {
|
|
587 if (ctx->flush == Z_NO_FLUSH) {
|
|
588 continue;
|
|
589 }
|
|
590
|
|
591 } else {
|
|
592 ctx->crc32 = crc32(ctx->crc32, ctx->zstream.next_in,
|
|
593 ctx->zstream.avail_in);
|
|
594 }
|
|
595 }
|
|
596
|
|
597
|
|
598 /* is there a space for the gzipped data ? */
|
|
599
|
|
600 if (ctx->zstream.avail_out == 0) {
|
|
601
|
|
602 if (ctx->free) {
|
|
603 ctx->out_buf = ctx->free->buf;
|
|
604 ctx->free = ctx->free->next;
|
|
605
|
|
606 } else if (ctx->bufs < conf->bufs.num) {
|
|
607 ctx->out_buf = ngx_create_temp_buf(r->pool,
|
|
608 conf->bufs.size);
|
|
609 if (ctx->out_buf == NULL) {
|
|
610 return ngx_http_gzip_error(ctx);
|
|
611 }
|
|
612
|
|
613 ctx->out_buf->tag = (ngx_buf_tag_t)
|
|
614 &ngx_http_gzip_filter_module;
|
|
615 ctx->out_buf->recycled = 1;
|
|
616 ctx->bufs++;
|
|
617
|
|
618 } else {
|
|
619 break;
|
|
620 }
|
|
621
|
|
622 ctx->zstream.next_out = ctx->out_buf->pos;
|
|
623 ctx->zstream.avail_out = conf->bufs.size;
|
|
624 }
|
|
625
|
|
626 ngx_log_debug6(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
10
|
627 "deflate in: ni:%p no:%p ai:%ud ao:%ud fl:%d redo:%d",
|
|
628 ctx->zstream.next_in, ctx->zstream.next_out,
|
|
629 ctx->zstream.avail_in, ctx->zstream.avail_out,
|
|
630 ctx->flush, ctx->redo);
|
0
|
631
|
|
632 rc = deflate(&ctx->zstream, ctx->flush);
|
|
633
|
|
634 if (rc != Z_OK && rc != Z_STREAM_END) {
|
|
635 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
|
|
636 "deflate() failed: %d, %d", ctx->flush, rc);
|
|
637 return ngx_http_gzip_error(ctx);
|
|
638 }
|
|
639
|
|
640 ngx_log_debug5(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
10
|
641 "deflate out: ni:%p no:%p ai:%ud ao:%ud rc:%d",
|
0
|
642 ctx->zstream.next_in, ctx->zstream.next_out,
|
|
643 ctx->zstream.avail_in, ctx->zstream.avail_out,
|
|
644 rc);
|
|
645
|
|
646 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
10
|
647 "gzip in_buf:%p pos:%p",
|
0
|
648 ctx->in_buf, ctx->in_buf->pos);
|
|
649
|
|
650
|
|
651 if (ctx->zstream.next_in) {
|
|
652 ctx->in_buf->pos = ctx->zstream.next_in;
|
|
653
|
|
654 if (ctx->zstream.avail_in == 0) {
|
|
655 ctx->zstream.next_in = NULL;
|
|
656 }
|
|
657 }
|
|
658
|
|
659 ctx->out_buf->last = ctx->zstream.next_out;
|
|
660
|
|
661 if (ctx->zstream.avail_out == 0) {
|
|
662
|
|
663 /* zlib wants to output some more gzipped data */
|
|
664
|
10
|
665 if (!(cl = ngx_alloc_chain_link(r->pool))) {
|
|
666 return ngx_http_gzip_error(ctx);
|
|
667 }
|
|
668 cl->buf = ctx->out_buf;
|
|
669 cl->next = NULL;
|
0
|
670 *ctx->last_out = cl;
|
|
671 ctx->last_out = &cl->next;
|
|
672
|
|
673 ctx->redo = 1;
|
|
674
|
|
675 continue;
|
|
676 }
|
|
677
|
|
678 ctx->redo = 0;
|
|
679
|
|
680 if (ctx->flush == Z_SYNC_FLUSH) {
|
|
681
|
|
682 ctx->out_buf->flush = 0;
|
|
683 ctx->flush = Z_NO_FLUSH;
|
|
684
|
10
|
685 if (!(cl = ngx_alloc_chain_link(r->pool))) {
|
|
686 return ngx_http_gzip_error(ctx);
|
|
687 }
|
|
688 cl->buf = ctx->out_buf;
|
|
689 cl->next = NULL;
|
0
|
690 *ctx->last_out = cl;
|
|
691 ctx->last_out = &cl->next;
|
|
692
|
|
693 break;
|
|
694 }
|
|
695
|
|
696 if (rc == Z_STREAM_END) {
|
|
697
|
|
698 ctx->zin = ctx->zstream.total_in;
|
|
699 ctx->zout = 10 + ctx->zstream.total_out + 8;
|
|
700
|
|
701 rc = deflateEnd(&ctx->zstream);
|
|
702
|
|
703 if (rc != Z_OK) {
|
|
704 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
|
|
705 "deflateEnd() failed: %d", rc);
|
|
706 return ngx_http_gzip_error(ctx);
|
|
707 }
|
|
708
|
|
709 ngx_pfree(r->pool, ctx->preallocated);
|
|
710
|
10
|
711 if (!(cl = ngx_alloc_chain_link(r->pool))) {
|
|
712 return ngx_http_gzip_error(ctx);
|
|
713 }
|
|
714 cl->buf = ctx->out_buf;
|
|
715 cl->next = NULL;
|
0
|
716 *ctx->last_out = cl;
|
|
717 ctx->last_out = &cl->next;
|
|
718
|
|
719 if (ctx->zstream.avail_out >= 8) {
|
|
720 trailer = (struct gztrailer *) ctx->out_buf->last;
|
|
721 ctx->out_buf->last += 8;
|
|
722 ctx->out_buf->last_buf = 1;
|
|
723
|
|
724 } else {
|
|
725 if (!(b = ngx_create_temp_buf(r->pool, 8))) {
|
|
726 return ngx_http_gzip_error(ctx);
|
|
727 }
|
|
728
|
|
729 b->last_buf = 1;
|
|
730
|
10
|
731 if (!(cl = ngx_alloc_chain_link(r->pool))) {
|
|
732 return ngx_http_gzip_error(ctx);
|
|
733 }
|
|
734 cl->buf = b;
|
|
735 cl->next = NULL;
|
0
|
736 *ctx->last_out = cl;
|
|
737 ctx->last_out = &cl->next;
|
|
738 trailer = (struct gztrailer *) b->pos;
|
|
739 b->last += 8;
|
|
740 }
|
|
741
|
18
|
742 #if (NGX_HAVE_LITTLE_ENDIAN)
|
0
|
743 trailer->crc32 = ctx->crc32;
|
|
744 trailer->zlen = ctx->zin;
|
|
745 #else
|
|
746 trailer->crc32[0] = ctx->crc32 & 0xff;
|
|
747 trailer->crc32[1] = (ctx->crc32 >> 8) & 0xff;
|
|
748 trailer->crc32[2] = (ctx->crc32 >> 16) & 0xff;
|
|
749 trailer->crc32[3] = (ctx->crc32 >> 24) & 0xff;
|
|
750
|
|
751 trailer->zlen[0] = ctx->zin & 0xff;
|
|
752 trailer->zlen[1] = (ctx->zin >> 8) & 0xff;
|
|
753 trailer->zlen[2] = (ctx->zin >> 16) & 0xff;
|
|
754 trailer->zlen[3] = (ctx->zin >> 24) & 0xff;
|
|
755 #endif
|
|
756
|
|
757 ctx->zstream.avail_in = 0;
|
|
758 ctx->zstream.avail_out = 0;
|
|
759
|
|
760 ctx->done = 1;
|
|
761
|
|
762 break;
|
|
763 }
|
|
764
|
|
765 if (conf->no_buffer && ctx->in == NULL) {
|
10
|
766 if (!(cl = ngx_alloc_chain_link(r->pool))) {
|
|
767 return ngx_http_gzip_error(ctx);
|
|
768 }
|
|
769 cl->buf = ctx->out_buf;
|
|
770 cl->next = NULL;
|
0
|
771 *ctx->last_out = cl;
|
|
772 ctx->last_out = &cl->next;
|
|
773
|
|
774 break;
|
|
775 }
|
|
776 }
|
|
777
|
18
|
778 if (last == NGX_AGAIN && !ctx->done) {
|
0
|
779 return NGX_AGAIN;
|
|
780 }
|
|
781
|
|
782 if (ctx->out == NULL && ctx->busy == NULL) {
|
|
783 return NGX_OK;
|
|
784 }
|
|
785
|
|
786 last = ngx_http_next_body_filter(r, ctx->out);
|
|
787
|
|
788 /*
|
|
789 * we do not check NGX_AGAIN here because the downstream filters
|
|
790 * may free some buffers and zlib may compress some data into them
|
|
791 */
|
|
792
|
|
793 if (last == NGX_ERROR) {
|
|
794 return ngx_http_gzip_error(ctx);
|
|
795 }
|
|
796
|
|
797 ngx_chain_update_chains(&ctx->free, &ctx->busy, &ctx->out,
|
|
798 (ngx_buf_tag_t) &ngx_http_gzip_filter_module);
|
|
799 ctx->last_out = &ctx->out;
|
|
800
|
|
801 if (ctx->done) {
|
|
802 return last;
|
|
803 }
|
|
804 }
|
|
805 }
|
|
806
|
|
807
|
|
808 static void *ngx_http_gzip_filter_alloc(void *opaque, u_int items, u_int size)
|
|
809 {
|
|
810 ngx_http_gzip_ctx_t *ctx = opaque;
|
|
811
|
|
812 void *p;
|
|
813 ngx_uint_t alloc;
|
|
814
|
|
815 alloc = items * size;
|
10
|
816
|
0
|
817 if (alloc % 512 != 0) {
|
|
818
|
|
819 /*
|
10
|
820 * The zlib deflate_state allocation, it takes about 6K,
|
|
821 * we allocate 8K. Other allocations are divisible by 512.
|
0
|
822 */
|
|
823
|
|
824 alloc = (alloc + ngx_pagesize - 1) & ~(ngx_pagesize - 1);
|
|
825 }
|
|
826
|
|
827 if (alloc <= ctx->allocated) {
|
|
828 p = ctx->free_mem;
|
|
829 ctx->free_mem += alloc;
|
|
830 ctx->allocated -= alloc;
|
|
831
|
|
832 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, ctx->request->connection->log, 0,
|
10
|
833 "gzip alloc: n:%ud s:%ud a:%ud p:%p",
|
0
|
834 items, size, alloc, p);
|
|
835
|
|
836 return p;
|
|
837 }
|
|
838
|
|
839 ngx_log_error(NGX_LOG_ALERT, ctx->request->connection->log, 0,
|
10
|
840 "gzip filter failed to use preallocated memory: %ud of %ud",
|
0
|
841 items * size, ctx->allocated);
|
|
842
|
|
843 p = ngx_palloc(ctx->request->pool, items * size);
|
|
844
|
|
845 return p;
|
|
846 }
|
|
847
|
|
848
|
|
849 static void ngx_http_gzip_filter_free(void *opaque, void *address)
|
|
850 {
|
|
851 #if 0
|
|
852 ngx_http_gzip_ctx_t *ctx = opaque;
|
|
853
|
|
854 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->request->connection->log, 0,
|
10
|
855 "gzip free: %p", address);
|
0
|
856 #endif
|
|
857 }
|
|
858
|
|
859
|
|
860 static u_char *ngx_http_gzip_log_ratio(ngx_http_request_t *r, u_char *buf,
|
|
861 uintptr_t data)
|
|
862 {
|
|
863 ngx_uint_t zint, zfrac;
|
|
864 ngx_http_gzip_ctx_t *ctx;
|
|
865
|
|
866 ctx = ngx_http_get_module_ctx(r, ngx_http_gzip_filter_module);
|
|
867
|
|
868 if (ctx == NULL || ctx->zout == 0) {
|
|
869 *buf = '-';
|
|
870 return buf + 1;
|
|
871 }
|
|
872
|
|
873 zint = (ngx_uint_t) (ctx->zin / ctx->zout);
|
|
874 zfrac = (ngx_uint_t) ((ctx->zin * 100 / ctx->zout) % 100);
|
|
875
|
4
|
876 if ((ctx->zin * 1000 / ctx->zout) % 10 > 4) {
|
|
877
|
|
878 /* the rounding, e.g., 2.125 to 2.13 */
|
|
879
|
|
880 zfrac++;
|
|
881
|
|
882 if (zfrac > 99) {
|
0
|
883 zint++;
|
|
884 zfrac = 0;
|
|
885 }
|
|
886 }
|
|
887
|
4
|
888 return ngx_sprintf(buf, "%ui.%02ui", zint, zfrac);
|
0
|
889 }
|
|
890
|
|
891
|
10
|
892 static int ngx_http_gzip_error(ngx_http_gzip_ctx_t *ctx)
|
0
|
893 {
|
|
894 deflateEnd(&ctx->zstream);
|
|
895
|
18
|
896 if (ctx->preallocated) {
|
|
897 ngx_pfree(ctx->request->pool, ctx->preallocated);
|
|
898 }
|
0
|
899
|
|
900 ctx->zstream.avail_in = 0;
|
|
901 ctx->zstream.avail_out = 0;
|
|
902
|
|
903 ctx->done = 1;
|
|
904
|
|
905 return NGX_ERROR;
|
|
906 }
|
|
907
|
|
908
|
|
909 static ngx_int_t ngx_http_gzip_pre_conf(ngx_conf_t *cf)
|
|
910 {
|
|
911 ngx_http_log_op_name_t *op;
|
|
912
|
|
913 for (op = ngx_http_gzip_log_fmt_ops; op->name.len; op++) { /* void */ }
|
|
914 op->op = NULL;
|
|
915
|
|
916 op = ngx_http_log_fmt_ops;
|
|
917
|
|
918 for (op = ngx_http_log_fmt_ops; op->op; op++) {
|
|
919 if (op->name.len == 0) {
|
|
920 op = (ngx_http_log_op_name_t *) op->op;
|
|
921 }
|
|
922 }
|
|
923
|
|
924 op->op = (ngx_http_log_op_pt) ngx_http_gzip_log_fmt_ops;
|
|
925
|
|
926 return NGX_OK;
|
|
927 }
|
|
928
|
|
929
|
|
930 static ngx_int_t ngx_http_gzip_filter_init(ngx_cycle_t *cycle)
|
|
931 {
|
|
932 ngx_http_next_header_filter = ngx_http_top_header_filter;
|
|
933 ngx_http_top_header_filter = ngx_http_gzip_header_filter;
|
|
934
|
|
935 ngx_http_next_body_filter = ngx_http_top_body_filter;
|
|
936 ngx_http_top_body_filter = ngx_http_gzip_body_filter;
|
|
937
|
|
938 return NGX_OK;
|
|
939 }
|
|
940
|
|
941
|
|
942 static void *ngx_http_gzip_create_conf(ngx_conf_t *cf)
|
|
943 {
|
|
944 ngx_http_gzip_conf_t *conf;
|
|
945
|
|
946 if (!(conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_gzip_conf_t)))) {
|
|
947 return NGX_CONF_ERROR;
|
|
948 }
|
|
949
|
|
950 /*
|
|
951
|
|
952 set by ngx_pcalloc():
|
|
953
|
|
954 conf->bufs.num = 0;
|
|
955 conf->proxied = 0;
|
|
956
|
2
|
957 conf->types = NULL;
|
|
958
|
0
|
959 */
|
|
960
|
|
961 conf->enable = NGX_CONF_UNSET;
|
|
962 conf->no_buffer = NGX_CONF_UNSET;
|
|
963
|
|
964 conf->http_version = NGX_CONF_UNSET_UINT;
|
|
965
|
|
966 conf->level = NGX_CONF_UNSET;
|
|
967 conf->wbits = (size_t) NGX_CONF_UNSET;
|
|
968 conf->memlevel = (size_t) NGX_CONF_UNSET;
|
|
969 conf->min_length = NGX_CONF_UNSET;
|
|
970
|
|
971 return conf;
|
|
972 }
|
|
973
|
|
974
|
|
975 static char *ngx_http_gzip_merge_conf(ngx_conf_t *cf,
|
|
976 void *parent, void *child)
|
|
977 {
|
|
978 ngx_http_gzip_conf_t *prev = parent;
|
|
979 ngx_http_gzip_conf_t *conf = child;
|
|
980
|
2
|
981 ngx_http_gzip_type_t *type;
|
|
982
|
0
|
983 ngx_conf_merge_value(conf->enable, prev->enable, 0);
|
|
984
|
|
985 ngx_conf_merge_bufs_value(conf->bufs, prev->bufs, 4, ngx_pagesize);
|
|
986
|
|
987 ngx_conf_merge_unsigned_value(conf->http_version, prev->http_version,
|
|
988 NGX_HTTP_VERSION_11);
|
|
989 ngx_conf_merge_bitmask_value(conf->proxied, prev->proxied,
|
|
990 (NGX_CONF_BITMASK_SET
|
|
991 |NGX_HTTP_GZIP_PROXIED_OFF));
|
|
992
|
|
993 ngx_conf_merge_value(conf->level, prev->level, 1);
|
|
994 ngx_conf_merge_size_value(conf->wbits, prev->wbits, MAX_WBITS);
|
|
995 ngx_conf_merge_size_value(conf->memlevel, prev->memlevel,
|
|
996 MAX_MEM_LEVEL - 1);
|
|
997 ngx_conf_merge_value(conf->min_length, prev->min_length, 0);
|
|
998 ngx_conf_merge_value(conf->no_buffer, prev->no_buffer, 0);
|
|
999
|
2
|
1000 if (conf->types == NULL) {
|
|
1001 if (prev->types == NULL) {
|
|
1002 conf->types = ngx_array_create(cf->pool, 1,
|
|
1003 sizeof(ngx_http_gzip_type_t));
|
|
1004 if (conf->types == NULL) {
|
|
1005 return NGX_CONF_ERROR;
|
|
1006 }
|
|
1007
|
|
1008 if (!(type = ngx_array_push(conf->types))) {
|
|
1009 return NGX_CONF_ERROR;
|
|
1010 }
|
|
1011
|
|
1012 type->name.len = sizeof("text/html") - 1;
|
|
1013 type->name.data = (u_char *) "text/html";
|
|
1014 type->enable = 1;
|
|
1015
|
|
1016 } else {
|
|
1017 conf->types = prev->types;
|
|
1018 }
|
|
1019 }
|
|
1020
|
|
1021 return NGX_CONF_OK;
|
|
1022 }
|
|
1023
|
|
1024
|
|
1025 static char *ngx_http_gzip_set_types(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
1026 void *conf)
|
|
1027 {
|
|
1028 ngx_http_gzip_conf_t *gcf = conf;
|
|
1029
|
|
1030 ngx_str_t *value;
|
|
1031 ngx_uint_t i;
|
|
1032 ngx_http_gzip_type_t *type;
|
|
1033
|
|
1034 if (gcf->types == NULL) {
|
|
1035 gcf->types = ngx_array_create(cf->pool, 5,
|
|
1036 sizeof(ngx_http_gzip_type_t));
|
|
1037 if (gcf->types == NULL) {
|
|
1038 return NGX_CONF_ERROR;
|
|
1039 }
|
|
1040
|
|
1041 if (!(type = ngx_array_push(gcf->types))) {
|
|
1042 return NGX_CONF_ERROR;
|
|
1043 }
|
|
1044
|
|
1045 type->name.len = sizeof("text/html") - 1;
|
|
1046 type->name.data = (u_char *) "text/html";
|
|
1047 type->enable = 1;
|
|
1048 }
|
|
1049
|
|
1050 value = cf->args->elts;
|
|
1051
|
|
1052 for (i = 1; i < cf->args->nelts; i++) {
|
|
1053
|
|
1054 if (ngx_strcmp(value[i].data, "text/html") == 0) {
|
|
1055 continue;
|
|
1056 }
|
|
1057
|
|
1058 if (!(type = ngx_array_push(gcf->types))) {
|
|
1059 return NGX_CONF_ERROR;
|
|
1060 }
|
|
1061
|
|
1062 type->name.len = value[i].len;
|
|
1063
|
|
1064 if (!(type->name.data = ngx_palloc(cf->pool, type->name.len + 1))) {
|
|
1065 return NGX_CONF_ERROR;
|
|
1066 }
|
|
1067
|
|
1068 ngx_cpystrn(type->name.data, value[i].data, type->name.len + 1);
|
|
1069 }
|
|
1070
|
0
|
1071 return NGX_CONF_OK;
|
|
1072 }
|
|
1073
|
|
1074
|
|
1075 static char *ngx_http_gzip_set_window(ngx_conf_t *cf, void *post, void *data)
|
|
1076 {
|
|
1077 int *np = data;
|
|
1078
|
|
1079 int wbits, wsize;
|
|
1080
|
|
1081 wbits = 15;
|
|
1082
|
|
1083 for (wsize = 32 * 1024; wsize > 256; wsize >>= 1) {
|
|
1084
|
|
1085 if (wsize == *np) {
|
|
1086 *np = wbits;
|
|
1087
|
|
1088 return NGX_CONF_OK;
|
|
1089 }
|
|
1090
|
|
1091 wbits--;
|
|
1092 }
|
|
1093
|
|
1094 return "must be 512, 1k, 2k, 4k, 8k, 16k, or 32k";
|
|
1095 }
|
|
1096
|
|
1097
|
|
1098 static char *ngx_http_gzip_set_hash(ngx_conf_t *cf, void *post, void *data)
|
|
1099 {
|
|
1100 int *np = data;
|
|
1101
|
|
1102 int memlevel, hsize;
|
|
1103
|
|
1104 memlevel = 9;
|
|
1105
|
|
1106 for (hsize = 128 * 1024; hsize > 256; hsize >>= 1) {
|
|
1107
|
|
1108 if (hsize == *np) {
|
|
1109 *np = memlevel;
|
|
1110
|
|
1111 return NGX_CONF_OK;
|
|
1112 }
|
|
1113
|
|
1114 memlevel--;
|
|
1115 }
|
|
1116
|
|
1117 return "must be 512, 1k, 2k, 4k, 8k, 16k, 32k, 64k, or 128k";
|
|
1118 }
|