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