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
|
|
240 #if (HAVE_LITTLE_ENDIAN)
|
|
241
|
|
242 struct gztrailer {
|
|
243 uint32_t crc32;
|
|
244 uint32_t zlen;
|
|
245 };
|
|
246
|
|
247 #else /* HAVE_BIG_ENDIAN */
|
|
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 {
|
|
440 int rc, wbits, memlevel, last;
|
|
441 struct gztrailer *trailer;
|
|
442 ngx_buf_t *b;
|
|
443 ngx_chain_t *cl;
|
|
444 ngx_http_gzip_ctx_t *ctx;
|
|
445 ngx_http_gzip_conf_t *conf;
|
|
446
|
|
447 ctx = ngx_http_get_module_ctx(r, ngx_http_gzip_filter_module);
|
|
448
|
|
449 if (ctx == NULL || ctx->done) {
|
|
450 return ngx_http_next_body_filter(r, in);
|
|
451 }
|
|
452
|
|
453 conf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_filter_module);
|
|
454
|
|
455 if (ctx->preallocated == NULL) {
|
|
456 wbits = conf->wbits;
|
|
457 memlevel = conf->memlevel;
|
|
458
|
|
459 if (ctx->length > 0) {
|
|
460
|
|
461 /* the actual zlib window size is smaller by 262 bytes */
|
|
462
|
|
463 while (ctx->length < ((1 << (wbits - 1)) - 262)) {
|
|
464 wbits--;
|
|
465 memlevel--;
|
|
466 }
|
|
467 }
|
|
468
|
|
469 /*
|
|
470 * We preallocate a memory for zlib in one buffer (200K-400K), this
|
|
471 * dicreases a number of malloc() and free() calls and also probably
|
|
472 * dicreases a number of syscalls (sbrk() or so).
|
|
473 * Besides we free this memory as soon as the gzipping will complete
|
|
474 * and do not wait while a whole response will be sent to a client.
|
|
475 *
|
|
476 * 8K is for zlib deflate_state, it takes
|
|
477 * * 5816 bytes on x86 and sparc64 (32-bit mode)
|
|
478 * * 5920 bytes on amd64 and sparc64
|
|
479 */
|
|
480
|
|
481 ctx->allocated = 8192 + (1 << (wbits + 2)) + (1 << (memlevel + 9));
|
|
482
|
|
483 if (!(ctx->preallocated = ngx_palloc(r->pool, ctx->allocated))) {
|
|
484 return NGX_ERROR;
|
|
485 }
|
|
486
|
|
487 ctx->free_mem = ctx->preallocated;
|
|
488
|
|
489 ctx->zstream.zalloc = ngx_http_gzip_filter_alloc;
|
|
490 ctx->zstream.zfree = ngx_http_gzip_filter_free;
|
|
491 ctx->zstream.opaque = ctx;
|
|
492
|
|
493 rc = deflateInit2(&ctx->zstream, conf->level, Z_DEFLATED,
|
|
494 -wbits, memlevel, Z_DEFAULT_STRATEGY);
|
|
495
|
|
496 if (rc != Z_OK) {
|
|
497 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
|
|
498 "deflateInit2() failed: %d", rc);
|
|
499 return ngx_http_gzip_error(ctx);
|
|
500 }
|
|
501
|
|
502 if (!(b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)))) {
|
|
503 return ngx_http_gzip_error(ctx);
|
|
504 }
|
|
505
|
|
506 b->memory = 1;
|
|
507 b->pos = gzheader;
|
|
508 b->last = b->pos + 10;
|
|
509
|
10
|
510 if (!(cl = ngx_alloc_chain_link(r->pool))) {
|
|
511 return ngx_http_gzip_error(ctx);
|
|
512 }
|
|
513 cl->buf = b;
|
|
514 cl->next = NULL;
|
0
|
515 ctx->out = cl;
|
|
516 ctx->last_out = &cl->next;
|
|
517
|
|
518 ctx->crc32 = crc32(0L, Z_NULL, 0);
|
|
519 ctx->flush = Z_NO_FLUSH;
|
|
520 }
|
|
521
|
|
522 if (in) {
|
|
523 if (ngx_chain_add_copy(r->pool, &ctx->in, in) == NGX_ERROR) {
|
|
524 return ngx_http_gzip_error(ctx);
|
|
525 }
|
|
526 }
|
|
527
|
|
528 last = NGX_NONE;
|
|
529
|
|
530 for ( ;; ) {
|
|
531
|
|
532 for ( ;; ) {
|
|
533
|
|
534 /* does zlib need a new data ? */
|
|
535
|
|
536 if (ctx->zstream.avail_in == 0
|
|
537 && ctx->flush == Z_NO_FLUSH
|
|
538 && !ctx->redo)
|
|
539 {
|
|
540 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
10
|
541 "gzip in: %p", ctx->in);
|
0
|
542
|
|
543 if (ctx->in == NULL) {
|
|
544 break;
|
|
545 }
|
|
546
|
|
547 ctx->in_buf = ctx->in->buf;
|
|
548 ctx->in = ctx->in->next;
|
|
549
|
|
550 ctx->zstream.next_in = ctx->in_buf->pos;
|
|
551 ctx->zstream.avail_in = ctx->in_buf->last - ctx->in_buf->pos;
|
|
552
|
|
553 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
10
|
554 "gzip in_buf:%p ni:%p ai:%ud",
|
0
|
555 ctx->in_buf,
|
|
556 ctx->zstream.next_in, ctx->zstream.avail_in);
|
|
557
|
|
558 /* STUB */
|
|
559 if (ctx->in_buf->last < ctx->in_buf->pos) {
|
|
560 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
|
|
561 "zstream.avail_in is huge");
|
|
562 ctx->done = 1;
|
|
563 return NGX_ERROR;
|
|
564 }
|
|
565 /**/
|
|
566
|
|
567 if (ctx->in_buf->last_buf) {
|
|
568 ctx->flush = Z_FINISH;
|
|
569
|
|
570 } else if (ctx->in_buf->flush) {
|
|
571 ctx->flush = Z_SYNC_FLUSH;
|
|
572 }
|
|
573
|
|
574 if (ctx->zstream.avail_in == 0) {
|
|
575 if (ctx->flush == Z_NO_FLUSH) {
|
|
576 continue;
|
|
577 }
|
|
578
|
|
579 } else {
|
|
580 ctx->crc32 = crc32(ctx->crc32, ctx->zstream.next_in,
|
|
581 ctx->zstream.avail_in);
|
|
582 }
|
|
583 }
|
|
584
|
|
585
|
|
586 /* is there a space for the gzipped data ? */
|
|
587
|
|
588 if (ctx->zstream.avail_out == 0) {
|
|
589
|
|
590 if (ctx->free) {
|
|
591 ctx->out_buf = ctx->free->buf;
|
|
592 ctx->free = ctx->free->next;
|
|
593
|
|
594 } else if (ctx->bufs < conf->bufs.num) {
|
|
595 ctx->out_buf = ngx_create_temp_buf(r->pool,
|
|
596 conf->bufs.size);
|
|
597 if (ctx->out_buf == NULL) {
|
|
598 return ngx_http_gzip_error(ctx);
|
|
599 }
|
|
600
|
|
601 ctx->out_buf->tag = (ngx_buf_tag_t)
|
|
602 &ngx_http_gzip_filter_module;
|
|
603 ctx->out_buf->recycled = 1;
|
|
604 ctx->bufs++;
|
|
605
|
|
606 } else {
|
|
607 break;
|
|
608 }
|
|
609
|
|
610 ctx->zstream.next_out = ctx->out_buf->pos;
|
|
611 ctx->zstream.avail_out = conf->bufs.size;
|
|
612 }
|
|
613
|
|
614 ngx_log_debug6(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
10
|
615 "deflate in: ni:%p no:%p ai:%ud ao:%ud fl:%d redo:%d",
|
|
616 ctx->zstream.next_in, ctx->zstream.next_out,
|
|
617 ctx->zstream.avail_in, ctx->zstream.avail_out,
|
|
618 ctx->flush, ctx->redo);
|
0
|
619
|
|
620 rc = deflate(&ctx->zstream, ctx->flush);
|
|
621
|
|
622 if (rc != Z_OK && rc != Z_STREAM_END) {
|
|
623 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
|
|
624 "deflate() failed: %d, %d", ctx->flush, rc);
|
|
625 return ngx_http_gzip_error(ctx);
|
|
626 }
|
|
627
|
|
628 ngx_log_debug5(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
10
|
629 "deflate out: ni:%p no:%p ai:%ud ao:%ud rc:%d",
|
0
|
630 ctx->zstream.next_in, ctx->zstream.next_out,
|
|
631 ctx->zstream.avail_in, ctx->zstream.avail_out,
|
|
632 rc);
|
|
633
|
|
634 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
10
|
635 "gzip in_buf:%p pos:%p",
|
0
|
636 ctx->in_buf, ctx->in_buf->pos);
|
|
637
|
|
638
|
|
639 if (ctx->zstream.next_in) {
|
|
640 ctx->in_buf->pos = ctx->zstream.next_in;
|
|
641
|
|
642 if (ctx->zstream.avail_in == 0) {
|
|
643 ctx->zstream.next_in = NULL;
|
|
644 }
|
|
645 }
|
|
646
|
|
647 ctx->out_buf->last = ctx->zstream.next_out;
|
|
648
|
|
649 if (ctx->zstream.avail_out == 0) {
|
|
650
|
|
651 /* zlib wants to output some more gzipped data */
|
|
652
|
10
|
653 if (!(cl = ngx_alloc_chain_link(r->pool))) {
|
|
654 return ngx_http_gzip_error(ctx);
|
|
655 }
|
|
656 cl->buf = ctx->out_buf;
|
|
657 cl->next = NULL;
|
0
|
658 *ctx->last_out = cl;
|
|
659 ctx->last_out = &cl->next;
|
|
660
|
|
661 ctx->redo = 1;
|
|
662
|
|
663 continue;
|
|
664 }
|
|
665
|
|
666 ctx->redo = 0;
|
|
667
|
|
668 if (ctx->flush == Z_SYNC_FLUSH) {
|
|
669
|
|
670 ctx->out_buf->flush = 0;
|
|
671 ctx->flush = Z_NO_FLUSH;
|
|
672
|
10
|
673 if (!(cl = ngx_alloc_chain_link(r->pool))) {
|
|
674 return ngx_http_gzip_error(ctx);
|
|
675 }
|
|
676 cl->buf = ctx->out_buf;
|
|
677 cl->next = NULL;
|
0
|
678 *ctx->last_out = cl;
|
|
679 ctx->last_out = &cl->next;
|
|
680
|
|
681 break;
|
|
682 }
|
|
683
|
|
684 if (rc == Z_STREAM_END) {
|
|
685
|
|
686 ctx->zin = ctx->zstream.total_in;
|
|
687 ctx->zout = 10 + ctx->zstream.total_out + 8;
|
|
688
|
|
689 rc = deflateEnd(&ctx->zstream);
|
|
690
|
|
691 if (rc != Z_OK) {
|
|
692 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
|
|
693 "deflateEnd() failed: %d", rc);
|
|
694 return ngx_http_gzip_error(ctx);
|
|
695 }
|
|
696
|
|
697 ngx_pfree(r->pool, ctx->preallocated);
|
|
698
|
10
|
699 if (!(cl = ngx_alloc_chain_link(r->pool))) {
|
|
700 return ngx_http_gzip_error(ctx);
|
|
701 }
|
|
702 cl->buf = ctx->out_buf;
|
|
703 cl->next = NULL;
|
0
|
704 *ctx->last_out = cl;
|
|
705 ctx->last_out = &cl->next;
|
|
706
|
|
707 if (ctx->zstream.avail_out >= 8) {
|
|
708 trailer = (struct gztrailer *) ctx->out_buf->last;
|
|
709 ctx->out_buf->last += 8;
|
|
710 ctx->out_buf->last_buf = 1;
|
|
711
|
|
712 } else {
|
|
713 if (!(b = ngx_create_temp_buf(r->pool, 8))) {
|
|
714 return ngx_http_gzip_error(ctx);
|
|
715 }
|
|
716
|
|
717 b->last_buf = 1;
|
|
718
|
10
|
719 if (!(cl = ngx_alloc_chain_link(r->pool))) {
|
|
720 return ngx_http_gzip_error(ctx);
|
|
721 }
|
|
722 cl->buf = b;
|
|
723 cl->next = NULL;
|
0
|
724 *ctx->last_out = cl;
|
|
725 ctx->last_out = &cl->next;
|
|
726 trailer = (struct gztrailer *) b->pos;
|
|
727 b->last += 8;
|
|
728 }
|
|
729
|
|
730 #if (HAVE_LITTLE_ENDIAN)
|
|
731 trailer->crc32 = ctx->crc32;
|
|
732 trailer->zlen = ctx->zin;
|
|
733 #else
|
|
734 trailer->crc32[0] = ctx->crc32 & 0xff;
|
|
735 trailer->crc32[1] = (ctx->crc32 >> 8) & 0xff;
|
|
736 trailer->crc32[2] = (ctx->crc32 >> 16) & 0xff;
|
|
737 trailer->crc32[3] = (ctx->crc32 >> 24) & 0xff;
|
|
738
|
|
739 trailer->zlen[0] = ctx->zin & 0xff;
|
|
740 trailer->zlen[1] = (ctx->zin >> 8) & 0xff;
|
|
741 trailer->zlen[2] = (ctx->zin >> 16) & 0xff;
|
|
742 trailer->zlen[3] = (ctx->zin >> 24) & 0xff;
|
|
743 #endif
|
|
744
|
|
745 ctx->zstream.avail_in = 0;
|
|
746 ctx->zstream.avail_out = 0;
|
|
747
|
|
748 ctx->done = 1;
|
|
749
|
|
750 break;
|
|
751 }
|
|
752
|
|
753 if (conf->no_buffer && ctx->in == NULL) {
|
10
|
754 if (!(cl = ngx_alloc_chain_link(r->pool))) {
|
|
755 return ngx_http_gzip_error(ctx);
|
|
756 }
|
|
757 cl->buf = ctx->out_buf;
|
|
758 cl->next = NULL;
|
0
|
759 *ctx->last_out = cl;
|
|
760 ctx->last_out = &cl->next;
|
|
761
|
|
762 break;
|
|
763 }
|
|
764 }
|
|
765
|
|
766 if (last == NGX_AGAIN) {
|
|
767 return NGX_AGAIN;
|
|
768 }
|
|
769
|
|
770 if (ctx->out == NULL && ctx->busy == NULL) {
|
|
771 return NGX_OK;
|
|
772 }
|
|
773
|
|
774 last = ngx_http_next_body_filter(r, ctx->out);
|
|
775
|
|
776 /*
|
|
777 * we do not check NGX_AGAIN here because the downstream filters
|
|
778 * may free some buffers and zlib may compress some data into them
|
|
779 */
|
|
780
|
|
781 if (last == NGX_ERROR) {
|
|
782 return ngx_http_gzip_error(ctx);
|
|
783 }
|
|
784
|
|
785 ngx_chain_update_chains(&ctx->free, &ctx->busy, &ctx->out,
|
|
786 (ngx_buf_tag_t) &ngx_http_gzip_filter_module);
|
|
787 ctx->last_out = &ctx->out;
|
|
788
|
|
789 if (ctx->done) {
|
|
790 return last;
|
|
791 }
|
|
792 }
|
|
793 }
|
|
794
|
|
795
|
|
796 static void *ngx_http_gzip_filter_alloc(void *opaque, u_int items, u_int size)
|
|
797 {
|
|
798 ngx_http_gzip_ctx_t *ctx = opaque;
|
|
799
|
|
800 void *p;
|
|
801 ngx_uint_t alloc;
|
|
802
|
|
803 alloc = items * size;
|
10
|
804
|
0
|
805 if (alloc % 512 != 0) {
|
|
806
|
|
807 /*
|
10
|
808 * The zlib deflate_state allocation, it takes about 6K,
|
|
809 * we allocate 8K. Other allocations are divisible by 512.
|
0
|
810 */
|
|
811
|
|
812 alloc = (alloc + ngx_pagesize - 1) & ~(ngx_pagesize - 1);
|
|
813 }
|
|
814
|
|
815 if (alloc <= ctx->allocated) {
|
|
816 p = ctx->free_mem;
|
|
817 ctx->free_mem += alloc;
|
|
818 ctx->allocated -= alloc;
|
|
819
|
|
820 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, ctx->request->connection->log, 0,
|
10
|
821 "gzip alloc: n:%ud s:%ud a:%ud p:%p",
|
0
|
822 items, size, alloc, p);
|
|
823
|
|
824 return p;
|
|
825 }
|
|
826
|
|
827 ngx_log_error(NGX_LOG_ALERT, ctx->request->connection->log, 0,
|
10
|
828 "gzip filter failed to use preallocated memory: %ud of %ud",
|
0
|
829 items * size, ctx->allocated);
|
|
830
|
|
831 p = ngx_palloc(ctx->request->pool, items * size);
|
|
832
|
|
833 return p;
|
|
834 }
|
|
835
|
|
836
|
|
837 static void ngx_http_gzip_filter_free(void *opaque, void *address)
|
|
838 {
|
|
839 #if 0
|
|
840 ngx_http_gzip_ctx_t *ctx = opaque;
|
|
841
|
|
842 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->request->connection->log, 0,
|
10
|
843 "gzip free: %p", address);
|
0
|
844 #endif
|
|
845 }
|
|
846
|
|
847
|
|
848 static u_char *ngx_http_gzip_log_ratio(ngx_http_request_t *r, u_char *buf,
|
|
849 uintptr_t data)
|
|
850 {
|
|
851 ngx_uint_t zint, zfrac;
|
|
852 ngx_http_gzip_ctx_t *ctx;
|
|
853
|
|
854 ctx = ngx_http_get_module_ctx(r, ngx_http_gzip_filter_module);
|
|
855
|
|
856 if (ctx == NULL || ctx->zout == 0) {
|
|
857 *buf = '-';
|
|
858 return buf + 1;
|
|
859 }
|
|
860
|
|
861 zint = (ngx_uint_t) (ctx->zin / ctx->zout);
|
|
862 zfrac = (ngx_uint_t) ((ctx->zin * 100 / ctx->zout) % 100);
|
|
863
|
4
|
864 if ((ctx->zin * 1000 / ctx->zout) % 10 > 4) {
|
|
865
|
|
866 /* the rounding, e.g., 2.125 to 2.13 */
|
|
867
|
|
868 zfrac++;
|
|
869
|
|
870 if (zfrac > 99) {
|
0
|
871 zint++;
|
|
872 zfrac = 0;
|
|
873 }
|
|
874 }
|
|
875
|
4
|
876 return ngx_sprintf(buf, "%ui.%02ui", zint, zfrac);
|
0
|
877 }
|
|
878
|
|
879
|
10
|
880 static int ngx_http_gzip_error(ngx_http_gzip_ctx_t *ctx)
|
0
|
881 {
|
|
882 deflateEnd(&ctx->zstream);
|
|
883
|
|
884 ngx_pfree(ctx->request->pool, ctx->preallocated);
|
|
885
|
|
886 ctx->zstream.avail_in = 0;
|
|
887 ctx->zstream.avail_out = 0;
|
|
888
|
|
889 ctx->done = 1;
|
|
890
|
|
891 return NGX_ERROR;
|
|
892 }
|
|
893
|
|
894
|
|
895 static ngx_int_t ngx_http_gzip_pre_conf(ngx_conf_t *cf)
|
|
896 {
|
|
897 ngx_http_log_op_name_t *op;
|
|
898
|
|
899 for (op = ngx_http_gzip_log_fmt_ops; op->name.len; op++) { /* void */ }
|
|
900 op->op = NULL;
|
|
901
|
|
902 op = ngx_http_log_fmt_ops;
|
|
903
|
|
904 for (op = ngx_http_log_fmt_ops; op->op; op++) {
|
|
905 if (op->name.len == 0) {
|
|
906 op = (ngx_http_log_op_name_t *) op->op;
|
|
907 }
|
|
908 }
|
|
909
|
|
910 op->op = (ngx_http_log_op_pt) ngx_http_gzip_log_fmt_ops;
|
|
911
|
|
912 return NGX_OK;
|
|
913 }
|
|
914
|
|
915
|
|
916 static ngx_int_t ngx_http_gzip_filter_init(ngx_cycle_t *cycle)
|
|
917 {
|
|
918 ngx_http_next_header_filter = ngx_http_top_header_filter;
|
|
919 ngx_http_top_header_filter = ngx_http_gzip_header_filter;
|
|
920
|
|
921 ngx_http_next_body_filter = ngx_http_top_body_filter;
|
|
922 ngx_http_top_body_filter = ngx_http_gzip_body_filter;
|
|
923
|
|
924 return NGX_OK;
|
|
925 }
|
|
926
|
|
927
|
|
928 static void *ngx_http_gzip_create_conf(ngx_conf_t *cf)
|
|
929 {
|
|
930 ngx_http_gzip_conf_t *conf;
|
|
931
|
|
932 if (!(conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_gzip_conf_t)))) {
|
|
933 return NGX_CONF_ERROR;
|
|
934 }
|
|
935
|
|
936 /*
|
|
937
|
|
938 set by ngx_pcalloc():
|
|
939
|
|
940 conf->bufs.num = 0;
|
|
941 conf->proxied = 0;
|
|
942
|
2
|
943 conf->types = NULL;
|
|
944
|
0
|
945 */
|
|
946
|
|
947 conf->enable = NGX_CONF_UNSET;
|
|
948 conf->no_buffer = NGX_CONF_UNSET;
|
|
949
|
|
950 conf->http_version = NGX_CONF_UNSET_UINT;
|
|
951
|
|
952 conf->level = NGX_CONF_UNSET;
|
|
953 conf->wbits = (size_t) NGX_CONF_UNSET;
|
|
954 conf->memlevel = (size_t) NGX_CONF_UNSET;
|
|
955 conf->min_length = NGX_CONF_UNSET;
|
|
956
|
|
957 return conf;
|
|
958 }
|
|
959
|
|
960
|
|
961 static char *ngx_http_gzip_merge_conf(ngx_conf_t *cf,
|
|
962 void *parent, void *child)
|
|
963 {
|
|
964 ngx_http_gzip_conf_t *prev = parent;
|
|
965 ngx_http_gzip_conf_t *conf = child;
|
|
966
|
2
|
967 ngx_http_gzip_type_t *type;
|
|
968
|
0
|
969 ngx_conf_merge_value(conf->enable, prev->enable, 0);
|
|
970
|
|
971 ngx_conf_merge_bufs_value(conf->bufs, prev->bufs, 4, ngx_pagesize);
|
|
972
|
|
973 ngx_conf_merge_unsigned_value(conf->http_version, prev->http_version,
|
|
974 NGX_HTTP_VERSION_11);
|
|
975 ngx_conf_merge_bitmask_value(conf->proxied, prev->proxied,
|
|
976 (NGX_CONF_BITMASK_SET
|
|
977 |NGX_HTTP_GZIP_PROXIED_OFF));
|
|
978
|
|
979 ngx_conf_merge_value(conf->level, prev->level, 1);
|
|
980 ngx_conf_merge_size_value(conf->wbits, prev->wbits, MAX_WBITS);
|
|
981 ngx_conf_merge_size_value(conf->memlevel, prev->memlevel,
|
|
982 MAX_MEM_LEVEL - 1);
|
|
983 ngx_conf_merge_value(conf->min_length, prev->min_length, 0);
|
|
984 ngx_conf_merge_value(conf->no_buffer, prev->no_buffer, 0);
|
|
985
|
2
|
986 if (conf->types == NULL) {
|
|
987 if (prev->types == NULL) {
|
|
988 conf->types = ngx_array_create(cf->pool, 1,
|
|
989 sizeof(ngx_http_gzip_type_t));
|
|
990 if (conf->types == NULL) {
|
|
991 return NGX_CONF_ERROR;
|
|
992 }
|
|
993
|
|
994 if (!(type = ngx_array_push(conf->types))) {
|
|
995 return NGX_CONF_ERROR;
|
|
996 }
|
|
997
|
|
998 type->name.len = sizeof("text/html") - 1;
|
|
999 type->name.data = (u_char *) "text/html";
|
|
1000 type->enable = 1;
|
|
1001
|
|
1002 } else {
|
|
1003 conf->types = prev->types;
|
|
1004 }
|
|
1005 }
|
|
1006
|
|
1007 return NGX_CONF_OK;
|
|
1008 }
|
|
1009
|
|
1010
|
|
1011 static char *ngx_http_gzip_set_types(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
1012 void *conf)
|
|
1013 {
|
|
1014 ngx_http_gzip_conf_t *gcf = conf;
|
|
1015
|
|
1016 ngx_str_t *value;
|
|
1017 ngx_uint_t i;
|
|
1018 ngx_http_gzip_type_t *type;
|
|
1019
|
|
1020 if (gcf->types == NULL) {
|
|
1021 gcf->types = ngx_array_create(cf->pool, 5,
|
|
1022 sizeof(ngx_http_gzip_type_t));
|
|
1023 if (gcf->types == NULL) {
|
|
1024 return NGX_CONF_ERROR;
|
|
1025 }
|
|
1026
|
|
1027 if (!(type = ngx_array_push(gcf->types))) {
|
|
1028 return NGX_CONF_ERROR;
|
|
1029 }
|
|
1030
|
|
1031 type->name.len = sizeof("text/html") - 1;
|
|
1032 type->name.data = (u_char *) "text/html";
|
|
1033 type->enable = 1;
|
|
1034 }
|
|
1035
|
|
1036 value = cf->args->elts;
|
|
1037
|
|
1038 for (i = 1; i < cf->args->nelts; i++) {
|
|
1039
|
|
1040 if (ngx_strcmp(value[i].data, "text/html") == 0) {
|
|
1041 continue;
|
|
1042 }
|
|
1043
|
|
1044 if (!(type = ngx_array_push(gcf->types))) {
|
|
1045 return NGX_CONF_ERROR;
|
|
1046 }
|
|
1047
|
|
1048 type->name.len = value[i].len;
|
|
1049
|
|
1050 if (!(type->name.data = ngx_palloc(cf->pool, type->name.len + 1))) {
|
|
1051 return NGX_CONF_ERROR;
|
|
1052 }
|
|
1053
|
|
1054 ngx_cpystrn(type->name.data, value[i].data, type->name.len + 1);
|
|
1055 }
|
|
1056
|
0
|
1057 return NGX_CONF_OK;
|
|
1058 }
|
|
1059
|
|
1060
|
|
1061 static char *ngx_http_gzip_set_window(ngx_conf_t *cf, void *post, void *data)
|
|
1062 {
|
|
1063 int *np = data;
|
|
1064
|
|
1065 int wbits, wsize;
|
|
1066
|
|
1067 wbits = 15;
|
|
1068
|
|
1069 for (wsize = 32 * 1024; wsize > 256; wsize >>= 1) {
|
|
1070
|
|
1071 if (wsize == *np) {
|
|
1072 *np = wbits;
|
|
1073
|
|
1074 return NGX_CONF_OK;
|
|
1075 }
|
|
1076
|
|
1077 wbits--;
|
|
1078 }
|
|
1079
|
|
1080 return "must be 512, 1k, 2k, 4k, 8k, 16k, or 32k";
|
|
1081 }
|
|
1082
|
|
1083
|
|
1084 static char *ngx_http_gzip_set_hash(ngx_conf_t *cf, void *post, void *data)
|
|
1085 {
|
|
1086 int *np = data;
|
|
1087
|
|
1088 int memlevel, hsize;
|
|
1089
|
|
1090 memlevel = 9;
|
|
1091
|
|
1092 for (hsize = 128 * 1024; hsize > 256; hsize >>= 1) {
|
|
1093
|
|
1094 if (hsize == *np) {
|
|
1095 *np = memlevel;
|
|
1096
|
|
1097 return NGX_CONF_OK;
|
|
1098 }
|
|
1099
|
|
1100 memlevel--;
|
|
1101 }
|
|
1102
|
|
1103 return "must be 512, 1k, 2k, 4k, 8k, 16k, 32k, 64k, or 128k";
|
|
1104 }
|