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