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>
|
464
|
10 #include <ngx_md5.h>
|
0
|
11
|
|
12
|
518
|
13 static ngx_int_t ngx_http_file_cache_read(ngx_http_request_t *r,
|
|
14 ngx_http_cache_t *c);
|
542
|
15 static ssize_t ngx_http_file_cache_aio_read(ngx_http_request_t *r,
|
|
16 ngx_http_cache_t *c);
|
518
|
17 #if (NGX_HAVE_FILE_AIO)
|
|
18 static void ngx_http_cache_aio_event_handler(ngx_event_t *ev);
|
|
19 #endif
|
468
|
20 static ngx_int_t ngx_http_file_cache_exists(ngx_http_file_cache_t *cache,
|
|
21 ngx_http_cache_t *c);
|
588
|
22 static ngx_int_t ngx_http_file_cache_name(ngx_http_request_t *r,
|
|
23 ngx_path_t *path);
|
464
|
24 static ngx_http_file_cache_node_t *
|
|
25 ngx_http_file_cache_lookup(ngx_http_file_cache_t *cache, u_char *key);
|
|
26 static void ngx_http_file_cache_rbtree_insert_value(ngx_rbtree_node_t *temp,
|
|
27 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel);
|
|
28 static void ngx_http_file_cache_cleanup(void *data);
|
468
|
29 static time_t ngx_http_file_cache_forced_expire(ngx_http_file_cache_t *cache);
|
|
30 static time_t ngx_http_file_cache_expire(ngx_http_file_cache_t *cache);
|
|
31 static void ngx_http_file_cache_delete(ngx_http_file_cache_t *cache,
|
|
32 ngx_queue_t *q, u_char *name);
|
634
|
33 static void ngx_http_file_cache_loader_sleep(ngx_http_file_cache_t *cache);
|
468
|
34 static ngx_int_t ngx_http_file_cache_noop(ngx_tree_ctx_t *ctx,
|
464
|
35 ngx_str_t *path);
|
468
|
36 static ngx_int_t ngx_http_file_cache_manage_file(ngx_tree_ctx_t *ctx,
|
|
37 ngx_str_t *path);
|
|
38 static ngx_int_t ngx_http_file_cache_add_file(ngx_tree_ctx_t *ctx,
|
|
39 ngx_str_t *path);
|
|
40 static ngx_int_t ngx_http_file_cache_add(ngx_http_file_cache_t *cache,
|
|
41 ngx_http_cache_t *c);
|
|
42 static ngx_int_t ngx_http_file_cache_delete_file(ngx_tree_ctx_t *ctx,
|
464
|
43 ngx_str_t *path);
|
0
|
44
|
|
45
|
502
|
46 ngx_str_t ngx_http_cache_status[] = {
|
|
47 ngx_string("MISS"),
|
588
|
48 ngx_string("BYPASS"),
|
502
|
49 ngx_string("EXPIRED"),
|
|
50 ngx_string("STALE"),
|
|
51 ngx_string("UPDATING"),
|
|
52 ngx_string("HIT")
|
|
53 };
|
|
54
|
|
55
|
464
|
56 static u_char ngx_http_file_cache_key[] = { LF, 'K', 'E', 'Y', ':', ' ' };
|
0
|
57
|
|
58
|
464
|
59 static ngx_int_t
|
|
60 ngx_http_file_cache_init(ngx_shm_zone_t *shm_zone, void *data)
|
0
|
61 {
|
464
|
62 ngx_http_file_cache_t *ocache = data;
|
0
|
63
|
468
|
64 size_t len;
|
514
|
65 ngx_uint_t n;
|
464
|
66 ngx_http_file_cache_t *cache;
|
0
|
67
|
464
|
68 cache = shm_zone->data;
|
0
|
69
|
464
|
70 if (ocache) {
|
|
71 if (ngx_strcmp(cache->path->name.data, ocache->path->name.data) != 0) {
|
|
72 ngx_log_error(NGX_LOG_EMERG, shm_zone->shm.log, 0,
|
|
73 "cache \"%V\" uses the \"%V\" cache path "
|
|
74 "while previously it used the \"%V\" cache path",
|
480
|
75 &shm_zone->shm.name, &cache->path->name,
|
464
|
76 &ocache->path->name);
|
0
|
77
|
|
78 return NGX_ERROR;
|
|
79 }
|
|
80
|
514
|
81 for (n = 0; n < 3; n++) {
|
|
82 if (cache->path->level[n] != ocache->path->level[n]) {
|
|
83 ngx_log_error(NGX_LOG_EMERG, shm_zone->shm.log, 0,
|
|
84 "cache \"%V\" had previously different levels",
|
|
85 &shm_zone->shm.name);
|
|
86 return NGX_ERROR;
|
|
87 }
|
|
88 }
|
|
89
|
480
|
90 cache->sh = ocache->sh;
|
|
91
|
464
|
92 cache->shpool = ocache->shpool;
|
468
|
93 cache->bsize = ocache->bsize;
|
|
94
|
|
95 cache->max_size /= cache->bsize;
|
464
|
96
|
514
|
97 if (!cache->sh->cold || cache->sh->loading) {
|
|
98 cache->path->loader = NULL;
|
|
99 }
|
|
100
|
464
|
101 return NGX_OK;
|
|
102 }
|
|
103
|
|
104 cache->shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
|
|
105
|
480
|
106 if (shm_zone->shm.exists) {
|
|
107 cache->sh = cache->shpool->data;
|
|
108 cache->bsize = ngx_fs_bsize(cache->path->name.data);
|
|
109
|
|
110 return NGX_OK;
|
464
|
111 }
|
|
112
|
480
|
113 cache->sh = ngx_slab_alloc(cache->shpool, sizeof(ngx_http_file_cache_sh_t));
|
|
114 if (cache->sh == NULL) {
|
464
|
115 return NGX_ERROR;
|
|
116 }
|
|
117
|
480
|
118 cache->shpool->data = cache->sh;
|
|
119
|
|
120 ngx_rbtree_init(&cache->sh->rbtree, &cache->sh->sentinel,
|
464
|
121 ngx_http_file_cache_rbtree_insert_value);
|
|
122
|
480
|
123 ngx_queue_init(&cache->sh->queue);
|
464
|
124
|
480
|
125 cache->sh->cold = 1;
|
514
|
126 cache->sh->loading = 0;
|
480
|
127 cache->sh->size = 0;
|
468
|
128
|
|
129 cache->bsize = ngx_fs_bsize(cache->path->name.data);
|
|
130
|
|
131 cache->max_size /= cache->bsize;
|
|
132
|
480
|
133 len = sizeof(" in cache keys zone \"\"") + shm_zone->shm.name.len;
|
468
|
134
|
|
135 cache->shpool->log_ctx = ngx_slab_alloc(cache->shpool, len);
|
|
136 if (cache->shpool->log_ctx == NULL) {
|
|
137 return NGX_ERROR;
|
|
138 }
|
|
139
|
|
140 ngx_sprintf(cache->shpool->log_ctx, " in cache keys zone \"%V\"%Z",
|
480
|
141 &shm_zone->shm.name);
|
464
|
142
|
|
143 return NGX_OK;
|
|
144 }
|
|
145
|
|
146
|
588
|
147 ngx_int_t
|
|
148 ngx_http_file_cache_new(ngx_http_request_t *r)
|
|
149 {
|
|
150 ngx_http_cache_t *c;
|
|
151
|
|
152 c = ngx_pcalloc(r->pool, sizeof(ngx_http_cache_t));
|
|
153 if (c == NULL) {
|
|
154 return NGX_ERROR;
|
|
155 }
|
|
156
|
|
157 if (ngx_array_init(&c->keys, r->pool, 4, sizeof(ngx_str_t)) != NGX_OK) {
|
|
158 return NGX_ERROR;
|
|
159 }
|
|
160
|
|
161 r->cache = c;
|
|
162 c->file.log = r->connection->log;
|
590
|
163 c->file.fd = NGX_INVALID_FILE;
|
588
|
164
|
|
165 return NGX_OK;
|
|
166 }
|
|
167
|
|
168
|
|
169 ngx_int_t
|
|
170 ngx_http_file_cache_create(ngx_http_request_t *r)
|
|
171 {
|
|
172 ngx_http_cache_t *c;
|
|
173 ngx_pool_cleanup_t *cln;
|
|
174 ngx_http_file_cache_t *cache;
|
|
175
|
|
176 c = r->cache;
|
|
177 cache = c->file_cache;
|
|
178
|
|
179 cln = ngx_pool_cleanup_add(r->pool, 0);
|
|
180 if (cln == NULL) {
|
|
181 return NGX_ERROR;
|
|
182 }
|
|
183
|
|
184 if (ngx_http_file_cache_exists(cache, c) == NGX_ERROR) {
|
|
185 return NGX_ERROR;
|
|
186 }
|
|
187
|
|
188 cln->handler = ngx_http_file_cache_cleanup;
|
|
189 cln->data = c;
|
|
190
|
|
191 if (ngx_http_file_cache_name(r, cache->path) != NGX_OK) {
|
|
192 return NGX_ERROR;
|
|
193 }
|
|
194
|
|
195 return NGX_OK;
|
|
196 }
|
|
197
|
|
198
|
464
|
199 void
|
|
200 ngx_http_file_cache_create_key(ngx_http_request_t *r)
|
|
201 {
|
|
202 size_t len;
|
|
203 ngx_str_t *key;
|
|
204 ngx_uint_t i;
|
|
205 ngx_md5_t md5;
|
|
206 ngx_http_cache_t *c;
|
|
207
|
|
208 c = r->cache;
|
|
209
|
|
210 len = 0;
|
|
211
|
|
212 ngx_crc32_init(c->crc32);
|
|
213 ngx_md5_init(&md5);
|
|
214
|
|
215 key = c->keys.elts;
|
|
216 for (i = 0; i < c->keys.nelts; i++) {
|
|
217 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
218 "http cache key: \"%V\"", &key[i]);
|
|
219
|
|
220 len += key[i].len;
|
|
221
|
|
222 ngx_crc32_update(&c->crc32, key[i].data, key[i].len);
|
|
223 ngx_md5_update(&md5, key[i].data, key[i].len);
|
|
224 }
|
|
225
|
|
226 c->header_start = sizeof(ngx_http_file_cache_header_t)
|
|
227 + sizeof(ngx_http_file_cache_key) + len + 1;
|
|
228
|
|
229 ngx_crc32_final(c->crc32);
|
|
230 ngx_md5_final(c->key, &md5);
|
|
231 }
|
|
232
|
|
233
|
|
234 ngx_int_t
|
|
235 ngx_http_file_cache_open(ngx_http_request_t *r)
|
|
236 {
|
518
|
237 ngx_int_t rc, rv;
|
|
238 ngx_uint_t cold, test;
|
|
239 ngx_http_cache_t *c;
|
|
240 ngx_pool_cleanup_t *cln;
|
|
241 ngx_open_file_info_t of;
|
|
242 ngx_http_file_cache_t *cache;
|
|
243 ngx_http_core_loc_conf_t *clcf;
|
464
|
244
|
|
245 c = r->cache;
|
518
|
246
|
|
247 if (c->buf) {
|
|
248 return ngx_http_file_cache_read(r, c);
|
|
249 }
|
|
250
|
464
|
251 cache = c->file_cache;
|
|
252
|
|
253 cln = ngx_pool_cleanup_add(r->pool, 0);
|
|
254 if (cln == NULL) {
|
|
255 return NGX_ERROR;
|
|
256 }
|
|
257
|
468
|
258 rc = ngx_http_file_cache_exists(cache, c);
|
464
|
259
|
498
|
260 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
261 "http file cache exists: %i e:%d", rc, c->exists);
|
464
|
262
|
|
263 if (rc == NGX_ERROR) {
|
|
264 return rc;
|
|
265 }
|
|
266
|
|
267 cln->handler = ngx_http_file_cache_cleanup;
|
|
268 cln->data = c;
|
|
269
|
|
270 if (rc == NGX_AGAIN) {
|
518
|
271 return NGX_HTTP_CACHE_SCARCE;
|
464
|
272 }
|
|
273
|
480
|
274 cold = cache->sh->cold;
|
464
|
275
|
|
276 if (rc == NGX_OK) {
|
|
277
|
|
278 if (c->error) {
|
|
279 return c->error;
|
|
280 }
|
|
281
|
|
282 c->temp_file = 1;
|
|
283 test = c->exists ? 1 : 0;
|
|
284 rv = NGX_DECLINED;
|
|
285
|
|
286 } else { /* rc == NGX_DECLINED */
|
|
287
|
|
288 if (c->min_uses > 1) {
|
|
289
|
|
290 if (!cold) {
|
518
|
291 return NGX_HTTP_CACHE_SCARCE;
|
0
|
292 }
|
|
293
|
464
|
294 test = 1;
|
518
|
295 rv = NGX_HTTP_CACHE_SCARCE;
|
464
|
296
|
|
297 } else {
|
|
298 c->temp_file = 1;
|
|
299 test = cold ? 1 : 0;
|
|
300 rv = NGX_DECLINED;
|
0
|
301 }
|
|
302 }
|
|
303
|
588
|
304 if (ngx_http_file_cache_name(r, cache->path) != NGX_OK) {
|
464
|
305 return NGX_ERROR;
|
|
306 }
|
|
307
|
|
308 if (!test) {
|
|
309 return NGX_DECLINED;
|
|
310 }
|
|
311
|
|
312 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
313
|
|
314 ngx_memzero(&of, sizeof(ngx_open_file_info_t));
|
0
|
315
|
464
|
316 of.uniq = c->uniq;
|
|
317 of.valid = clcf->open_file_cache_valid;
|
|
318 of.min_uses = clcf->open_file_cache_min_uses;
|
|
319 of.events = clcf->open_file_cache_events;
|
|
320 of.directio = NGX_OPEN_FILE_DIRECTIO_OFF;
|
532
|
321 of.read_ahead = clcf->read_ahead;
|
464
|
322
|
|
323 if (ngx_open_cached_file(clcf->open_file_cache, &c->file.name, &of, r->pool)
|
|
324 != NGX_OK)
|
|
325 {
|
|
326 switch (of.err) {
|
|
327
|
|
328 case 0:
|
|
329 return NGX_ERROR;
|
|
330
|
|
331 case NGX_ENOENT:
|
|
332 case NGX_ENOTDIR:
|
|
333 return rv;
|
|
334
|
|
335 default:
|
|
336 ngx_log_error(NGX_LOG_CRIT, r->connection->log, of.err,
|
|
337 ngx_open_file_n " \"%s\" failed", c->file.name.data);
|
|
338 return NGX_ERROR;
|
|
339 }
|
|
340 }
|
|
341
|
|
342 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
343 "http file cache fd: %d", of.fd);
|
|
344
|
|
345 c->file.fd = of.fd;
|
466
|
346 c->file.log = r->connection->log;
|
518
|
347 c->uniq = of.uniq;
|
|
348 c->length = of.size;
|
622
|
349 c->fs_size = (of.fs_size + cache->bsize - 1) / cache->bsize;
|
464
|
350
|
|
351 c->buf = ngx_create_temp_buf(r->pool, c->body_start);
|
|
352 if (c->buf == NULL) {
|
|
353 return NGX_ERROR;
|
|
354 }
|
|
355
|
518
|
356 return ngx_http_file_cache_read(r, c);
|
|
357 }
|
|
358
|
|
359
|
|
360 static ngx_int_t
|
|
361 ngx_http_file_cache_read(ngx_http_request_t *r, ngx_http_cache_t *c)
|
|
362 {
|
|
363 time_t now;
|
|
364 ssize_t n;
|
|
365 ngx_int_t rc;
|
|
366 ngx_http_file_cache_t *cache;
|
|
367 ngx_http_file_cache_header_t *h;
|
|
368
|
542
|
369 n = ngx_http_file_cache_aio_read(r, c);
|
518
|
370
|
542
|
371 if (n < 0) {
|
0
|
372 return n;
|
|
373 }
|
|
374
|
554
|
375 if ((size_t) n < c->header_start) {
|
464
|
376 ngx_log_error(NGX_LOG_CRIT, r->connection->log, 0,
|
26
|
377 "cache file \"%s\" is too small", c->file.name.data);
|
632
|
378 return NGX_DECLINED;
|
0
|
379 }
|
|
380
|
464
|
381 h = (ngx_http_file_cache_header_t *) c->buf->pos;
|
0
|
382
|
564
|
383 if (h->crc32 != c->crc32) {
|
464
|
384 ngx_log_error(NGX_LOG_CRIT, r->connection->log, 0,
|
|
385 "cache file \"%s\" has md5 collision", c->file.name.data);
|
0
|
386 return NGX_DECLINED;
|
|
387 }
|
|
388
|
654
|
389 if (h->body_start > c->body_start) {
|
|
390 ngx_log_error(NGX_LOG_CRIT, r->connection->log, 0,
|
|
391 "cache file \"%s\" has too long header",
|
|
392 c->file.name.data);
|
|
393 return NGX_DECLINED;
|
|
394 }
|
|
395
|
464
|
396 c->buf->last += n;
|
26
|
397
|
464
|
398 c->valid_sec = h->valid_sec;
|
|
399 c->last_modified = h->last_modified;
|
|
400 c->date = h->date;
|
|
401 c->valid_msec = h->valid_msec;
|
564
|
402 c->header_start = h->header_start;
|
464
|
403 c->body_start = h->body_start;
|
26
|
404
|
464
|
405 r->cached = 1;
|
|
406
|
518
|
407 cache = c->file_cache;
|
|
408
|
|
409 if (cache->sh->cold) {
|
464
|
410
|
|
411 ngx_shmtx_lock(&cache->shpool->mutex);
|
26
|
412
|
468
|
413 if (!c->node->exists) {
|
|
414 c->node->uses = 1;
|
|
415 c->node->body_start = c->body_start;
|
|
416 c->node->exists = 1;
|
518
|
417 c->node->uniq = c->uniq;
|
636
|
418 c->node->fs_size = c->fs_size;
|
468
|
419
|
622
|
420 cache->sh->size += c->fs_size;
|
468
|
421 }
|
464
|
422
|
|
423 ngx_shmtx_unlock(&cache->shpool->mutex);
|
0
|
424 }
|
|
425
|
468
|
426 now = ngx_time();
|
|
427
|
464
|
428 if (c->valid_sec < now) {
|
0
|
429
|
498
|
430 ngx_shmtx_lock(&cache->shpool->mutex);
|
0
|
431
|
498
|
432 if (c->node->updating) {
|
|
433 rc = NGX_HTTP_CACHE_UPDATING;
|
464
|
434
|
498
|
435 } else {
|
|
436 c->node->updating = 1;
|
590
|
437 c->updating = 1;
|
498
|
438 rc = NGX_HTTP_CACHE_STALE;
|
|
439 }
|
|
440
|
|
441 ngx_shmtx_unlock(&cache->shpool->mutex);
|
|
442
|
|
443 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
444 "http file cache expired: %i %T %T",
|
|
445 rc, c->valid_sec, now);
|
|
446
|
|
447 return rc;
|
0
|
448 }
|
|
449
|
464
|
450 return NGX_OK;
|
|
451 }
|
|
452
|
|
453
|
542
|
454 static ssize_t
|
|
455 ngx_http_file_cache_aio_read(ngx_http_request_t *r, ngx_http_cache_t *c)
|
|
456 {
|
518
|
457 #if (NGX_HAVE_FILE_AIO)
|
542
|
458 ssize_t n;
|
|
459 ngx_http_core_loc_conf_t *clcf;
|
518
|
460
|
542
|
461 if (!ngx_file_aio) {
|
|
462 goto noaio;
|
|
463 }
|
|
464
|
|
465 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
466
|
|
467 if (!clcf->aio) {
|
|
468 goto noaio;
|
|
469 }
|
|
470
|
|
471 n = ngx_file_aio_read(&c->file, c->buf->pos, c->body_start, 0, r->pool);
|
|
472
|
|
473 if (n != NGX_AGAIN) {
|
|
474 return n;
|
|
475 }
|
|
476
|
|
477 c->file.aio->data = r;
|
|
478 c->file.aio->handler = ngx_http_cache_aio_event_handler;
|
|
479
|
|
480 r->main->blocked++;
|
|
481 r->aio = 1;
|
|
482
|
|
483 return NGX_AGAIN;
|
|
484
|
|
485 noaio:
|
|
486
|
|
487 #endif
|
|
488
|
|
489 return ngx_read_file(&c->file, c->buf->pos, c->body_start, 0);
|
|
490 }
|
|
491
|
|
492
|
|
493 #if (NGX_HAVE_FILE_AIO)
|
518
|
494
|
|
495 static void
|
|
496 ngx_http_cache_aio_event_handler(ngx_event_t *ev)
|
|
497 {
|
|
498 ngx_event_aio_t *aio;
|
|
499 ngx_http_request_t *r;
|
|
500
|
|
501 aio = ev->data;
|
|
502 r = aio->data;
|
|
503
|
|
504 r->main->blocked--;
|
|
505 r->aio = 0;
|
|
506
|
|
507 r->connection->write->handler(r->connection->write);
|
|
508 }
|
|
509
|
|
510 #endif
|
|
511
|
|
512
|
464
|
513 static ngx_int_t
|
468
|
514 ngx_http_file_cache_exists(ngx_http_file_cache_t *cache, ngx_http_cache_t *c)
|
464
|
515 {
|
|
516 ngx_int_t rc;
|
|
517 ngx_http_file_cache_node_t *fcn;
|
|
518
|
|
519 ngx_shmtx_lock(&cache->shpool->mutex);
|
|
520
|
468
|
521 fcn = ngx_http_file_cache_lookup(cache, c->key);
|
464
|
522
|
|
523 if (fcn) {
|
|
524 ngx_queue_remove(&fcn->queue);
|
|
525
|
592
|
526 fcn->uses++;
|
|
527 fcn->count++;
|
|
528
|
464
|
529 if (fcn->error) {
|
|
530
|
|
531 if (fcn->valid_sec < ngx_time()) {
|
|
532 goto renew;
|
|
533 }
|
|
534
|
|
535 rc = NGX_OK;
|
|
536
|
|
537 goto done;
|
|
538 }
|
|
539
|
632
|
540 if (fcn->exists || fcn->uses >= c->min_uses) {
|
464
|
541
|
468
|
542 c->exists = fcn->exists;
|
632
|
543 if (fcn->body_start) {
|
|
544 c->body_start = fcn->body_start;
|
|
545 }
|
464
|
546
|
|
547 rc = NGX_OK;
|
|
548
|
|
549 goto done;
|
|
550 }
|
|
551
|
632
|
552 rc = NGX_AGAIN;
|
464
|
553
|
|
554 goto done;
|
|
555 }
|
|
556
|
|
557 fcn = ngx_slab_alloc_locked(cache->shpool,
|
|
558 sizeof(ngx_http_file_cache_node_t));
|
|
559 if (fcn == NULL) {
|
|
560 ngx_shmtx_unlock(&cache->shpool->mutex);
|
|
561
|
478
|
562 (void) ngx_http_file_cache_forced_expire(cache);
|
464
|
563
|
|
564 ngx_shmtx_lock(&cache->shpool->mutex);
|
|
565
|
|
566 fcn = ngx_slab_alloc_locked(cache->shpool,
|
|
567 sizeof(ngx_http_file_cache_node_t));
|
|
568 if (fcn == NULL) {
|
|
569 rc = NGX_ERROR;
|
|
570 goto failed;
|
|
571 }
|
|
572 }
|
|
573
|
468
|
574 ngx_memcpy((u_char *) &fcn->node.key, c->key, sizeof(ngx_rbtree_key_t));
|
464
|
575
|
468
|
576 ngx_memcpy(fcn->key, &c->key[sizeof(ngx_rbtree_key_t)],
|
464
|
577 NGX_HTTP_CACHE_KEY_LEN - sizeof(ngx_rbtree_key_t));
|
|
578
|
480
|
579 ngx_rbtree_insert(&cache->sh->rbtree, &fcn->node);
|
464
|
580
|
592
|
581 fcn->uses = 1;
|
|
582 fcn->count = 1;
|
594
|
583 fcn->updating = 0;
|
596
|
584 fcn->deleting = 0;
|
592
|
585
|
464
|
586 renew:
|
|
587
|
|
588 rc = NGX_DECLINED;
|
|
589
|
|
590 fcn->valid_msec = 0;
|
|
591 fcn->error = 0;
|
|
592 fcn->exists = 0;
|
|
593 fcn->valid_sec = 0;
|
|
594 fcn->uniq = 0;
|
|
595 fcn->body_start = 0;
|
622
|
596 fcn->fs_size = 0;
|
464
|
597
|
|
598 done:
|
|
599
|
|
600 fcn->expire = ngx_time() + cache->inactive;
|
|
601
|
480
|
602 ngx_queue_insert_head(&cache->sh->queue, &fcn->queue);
|
464
|
603
|
468
|
604 c->uniq = fcn->uniq;
|
|
605 c->error = fcn->error;
|
|
606 c->node = fcn;
|
464
|
607
|
|
608 failed:
|
|
609
|
|
610 ngx_shmtx_unlock(&cache->shpool->mutex);
|
|
611
|
|
612 return rc;
|
|
613 }
|
|
614
|
|
615
|
588
|
616 static ngx_int_t
|
|
617 ngx_http_file_cache_name(ngx_http_request_t *r, ngx_path_t *path)
|
|
618 {
|
|
619 u_char *p;
|
|
620 ngx_http_cache_t *c;
|
|
621
|
|
622 c = r->cache;
|
|
623
|
|
624 c->file.name.len = path->name.len + 1 + path->len
|
|
625 + 2 * NGX_HTTP_CACHE_KEY_LEN;
|
|
626
|
|
627 c->file.name.data = ngx_pnalloc(r->pool, c->file.name.len + 1);
|
|
628 if (c->file.name.data == NULL) {
|
|
629 return NGX_ERROR;
|
|
630 }
|
|
631
|
|
632 ngx_memcpy(c->file.name.data, path->name.data, path->name.len);
|
|
633
|
|
634 p = c->file.name.data + path->name.len + 1 + path->len;
|
|
635 p = ngx_hex_dump(p, c->key, NGX_HTTP_CACHE_KEY_LEN);
|
|
636 *p = '\0';
|
|
637
|
|
638 ngx_create_hashed_filename(path, c->file.name.data, c->file.name.len);
|
|
639
|
|
640 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
641 "cache file: \"%s\"", c->file.name.data);
|
|
642
|
|
643 return NGX_OK;
|
|
644 }
|
|
645
|
|
646
|
464
|
647 static ngx_http_file_cache_node_t *
|
|
648 ngx_http_file_cache_lookup(ngx_http_file_cache_t *cache, u_char *key)
|
|
649 {
|
|
650 ngx_int_t rc;
|
|
651 ngx_rbtree_key_t node_key;
|
|
652 ngx_rbtree_node_t *node, *sentinel;
|
|
653 ngx_http_file_cache_node_t *fcn;
|
|
654
|
|
655 ngx_memcpy((u_char *) &node_key, key, sizeof(ngx_rbtree_key_t));
|
|
656
|
480
|
657 node = cache->sh->rbtree.root;
|
|
658 sentinel = cache->sh->rbtree.sentinel;
|
464
|
659
|
|
660 while (node != sentinel) {
|
|
661
|
|
662 if (node_key < node->key) {
|
|
663 node = node->left;
|
|
664 continue;
|
|
665 }
|
|
666
|
|
667 if (node_key > node->key) {
|
|
668 node = node->right;
|
|
669 continue;
|
|
670 }
|
|
671
|
|
672 /* node_key == node->key */
|
|
673
|
|
674 do {
|
|
675 fcn = (ngx_http_file_cache_node_t *) node;
|
|
676
|
|
677 rc = ngx_memcmp(&key[sizeof(ngx_rbtree_key_t)], fcn->key,
|
|
678 NGX_HTTP_CACHE_KEY_LEN - sizeof(ngx_rbtree_key_t));
|
|
679
|
|
680 if (rc == 0) {
|
|
681 return fcn;
|
|
682 }
|
|
683
|
|
684 node = (rc < 0) ? node->left : node->right;
|
|
685
|
|
686 } while (node != sentinel && node_key == node->key);
|
|
687
|
|
688 break;
|
|
689 }
|
|
690
|
|
691 /* not found */
|
|
692
|
|
693 return NULL;
|
|
694 }
|
|
695
|
|
696
|
|
697 static void
|
|
698 ngx_http_file_cache_rbtree_insert_value(ngx_rbtree_node_t *temp,
|
|
699 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel)
|
|
700 {
|
|
701 ngx_rbtree_node_t **p;
|
|
702 ngx_http_file_cache_node_t *cn, *cnt;
|
|
703
|
|
704 for ( ;; ) {
|
|
705
|
|
706 if (node->key < temp->key) {
|
|
707
|
|
708 p = &temp->left;
|
|
709
|
|
710 } else if (node->key > temp->key) {
|
|
711
|
|
712 p = &temp->right;
|
|
713
|
|
714 } else { /* node->key == temp->key */
|
|
715
|
|
716 cn = (ngx_http_file_cache_node_t *) node;
|
|
717 cnt = (ngx_http_file_cache_node_t *) temp;
|
|
718
|
|
719 p = (ngx_memcmp(cn->key, cnt->key,
|
|
720 NGX_HTTP_CACHE_KEY_LEN - sizeof(ngx_rbtree_key_t))
|
|
721 < 0)
|
|
722 ? &temp->left : &temp->right;
|
|
723 }
|
|
724
|
|
725 if (*p == sentinel) {
|
|
726 break;
|
|
727 }
|
|
728
|
|
729 temp = *p;
|
|
730 }
|
|
731
|
|
732 *p = node;
|
|
733 node->parent = temp;
|
|
734 node->left = sentinel;
|
|
735 node->right = sentinel;
|
|
736 ngx_rbt_red(node);
|
|
737 }
|
|
738
|
|
739
|
|
740 void
|
|
741 ngx_http_file_cache_set_header(ngx_http_request_t *r, u_char *buf)
|
|
742 {
|
|
743 ngx_http_file_cache_header_t *h = (ngx_http_file_cache_header_t *) buf;
|
|
744
|
|
745 u_char *p;
|
|
746 ngx_str_t *key;
|
|
747 ngx_uint_t i;
|
|
748 ngx_http_cache_t *c;
|
|
749
|
|
750 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
751 "http file cache set header");
|
|
752
|
|
753 c = r->cache;
|
|
754
|
|
755 h->valid_sec = c->valid_sec;
|
|
756 h->last_modified = c->last_modified;
|
|
757 h->date = c->date;
|
|
758 h->crc32 = c->crc32;
|
|
759 h->valid_msec = (u_short) c->valid_msec;
|
|
760 h->header_start = (u_short) c->header_start;
|
|
761 h->body_start = (u_short) c->body_start;
|
|
762
|
|
763 p = buf + sizeof(ngx_http_file_cache_header_t);
|
|
764
|
|
765 p = ngx_cpymem(p, ngx_http_file_cache_key, sizeof(ngx_http_file_cache_key));
|
|
766
|
|
767 key = c->keys.elts;
|
|
768 for (i = 0; i < c->keys.nelts; i++) {
|
|
769 p = ngx_copy(p, key[i].data, key[i].len);
|
|
770 }
|
|
771
|
|
772 *p = LF;
|
|
773 }
|
|
774
|
|
775
|
|
776 void
|
|
777 ngx_http_file_cache_update(ngx_http_request_t *r, ngx_temp_file_t *tf)
|
|
778 {
|
622
|
779 off_t fs_size;
|
464
|
780 ngx_int_t rc;
|
|
781 ngx_file_uniq_t uniq;
|
|
782 ngx_file_info_t fi;
|
|
783 ngx_http_cache_t *c;
|
|
784 ngx_ext_rename_file_t ext;
|
|
785 ngx_http_file_cache_t *cache;
|
|
786
|
|
787 c = r->cache;
|
|
788
|
|
789 if (c->updated) {
|
|
790 return;
|
|
791 }
|
|
792
|
|
793 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
794 "http file cache update");
|
|
795
|
|
796 c->updated = 1;
|
590
|
797 c->updating = 0;
|
464
|
798
|
|
799 cache = c->file_cache;
|
|
800
|
|
801 uniq = 0;
|
622
|
802 fs_size = 0;
|
464
|
803
|
|
804 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
805 "http file cache rename: \"%s\" to \"%s\"",
|
|
806 tf->file.name.data, c->file.name.data);
|
|
807
|
|
808 ext.access = NGX_FILE_OWNER_ACCESS;
|
|
809 ext.path_access = NGX_FILE_OWNER_ACCESS;
|
|
810 ext.time = -1;
|
|
811 ext.create_path = 1;
|
|
812 ext.delete_file = 1;
|
|
813 ext.log = r->connection->log;
|
|
814
|
|
815 rc = ngx_ext_rename_file(&tf->file.name, &c->file.name, &ext);
|
|
816
|
|
817 if (rc == NGX_OK) {
|
|
818
|
|
819 if (ngx_fd_info(tf->file.fd, &fi) == NGX_FILE_ERROR) {
|
|
820 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
|
|
821 ngx_fd_info_n " \"%s\" failed", tf->file.name.data);
|
|
822
|
|
823 rc = NGX_ERROR;
|
|
824
|
|
825 } else {
|
|
826 uniq = ngx_file_uniq(&fi);
|
622
|
827 fs_size = (ngx_file_fs_size(&fi) + cache->bsize - 1) / cache->bsize;
|
464
|
828 }
|
|
829 }
|
|
830
|
|
831 ngx_shmtx_lock(&cache->shpool->mutex);
|
|
832
|
|
833 c->node->count--;
|
|
834 c->node->uniq = uniq;
|
|
835 c->node->body_start = c->body_start;
|
|
836
|
622
|
837 cache->sh->size += fs_size - c->node->fs_size;
|
|
838 c->node->fs_size = fs_size;
|
468
|
839
|
464
|
840 if (rc == NGX_OK) {
|
|
841 c->node->exists = 1;
|
|
842 }
|
|
843
|
498
|
844 c->node->updating = 0;
|
|
845
|
464
|
846 ngx_shmtx_unlock(&cache->shpool->mutex);
|
|
847 }
|
|
848
|
|
849
|
|
850 ngx_int_t
|
|
851 ngx_http_cache_send(ngx_http_request_t *r)
|
|
852 {
|
|
853 ngx_int_t rc;
|
|
854 ngx_buf_t *b;
|
|
855 ngx_chain_t out;
|
|
856 ngx_http_cache_t *c;
|
|
857
|
|
858 c = r->cache;
|
|
859
|
|
860 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
632
|
861 "http file cache send: %s", c->file.name.data);
|
464
|
862
|
642
|
863 if (r != r->main && c->length - c->body_start == 0) {
|
|
864 return ngx_http_send_header(r);
|
|
865 }
|
|
866
|
464
|
867 /* we need to allocate all before the header would be sent */
|
|
868
|
|
869 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
|
|
870 if (b == NULL) {
|
|
871 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
872 }
|
|
873
|
|
874 b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t));
|
|
875 if (b->file == NULL) {
|
|
876 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
877 }
|
|
878
|
|
879 rc = ngx_http_send_header(r);
|
|
880
|
|
881 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
|
|
882 return rc;
|
|
883 }
|
|
884
|
|
885 b->file_pos = c->body_start;
|
|
886 b->file_last = c->length;
|
|
887
|
642
|
888 b->in_file = (c->length - c->body_start) ? 1: 0;
|
464
|
889 b->last_buf = (r == r->main) ? 1: 0;
|
|
890 b->last_in_chain = 1;
|
|
891
|
|
892 b->file->fd = c->file.fd;
|
|
893 b->file->name = c->file.name;
|
|
894 b->file->log = r->connection->log;
|
|
895
|
|
896 out.buf = b;
|
|
897 out.next = NULL;
|
|
898
|
|
899 return ngx_http_output_filter(r, &out);
|
|
900 }
|
|
901
|
|
902
|
|
903 void
|
590
|
904 ngx_http_file_cache_free(ngx_http_cache_t *c, ngx_temp_file_t *tf)
|
464
|
905 {
|
588
|
906 ngx_http_file_cache_t *cache;
|
|
907 ngx_http_file_cache_node_t *fcn;
|
464
|
908
|
626
|
909 if (c->updated || c->node == NULL) {
|
464
|
910 return;
|
|
911 }
|
|
912
|
|
913 cache = c->file_cache;
|
|
914
|
590
|
915 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->file.log, 0,
|
|
916 "http file cache free, fd: %d", c->file.fd);
|
464
|
917
|
|
918 ngx_shmtx_lock(&cache->shpool->mutex);
|
|
919
|
588
|
920 fcn = c->node;
|
|
921 fcn->count--;
|
590
|
922
|
|
923 if (c->updating) {
|
|
924 fcn->updating = 0;
|
|
925 }
|
464
|
926
|
|
927 if (c->error) {
|
588
|
928 fcn->error = c->error;
|
|
929
|
594
|
930 if (c->valid_sec) {
|
|
931 fcn->valid_sec = c->valid_sec;
|
|
932 fcn->valid_msec = c->valid_msec;
|
|
933 }
|
|
934
|
592
|
935 } else if (!fcn->exists && fcn->count == 0 && c->min_uses == 1) {
|
588
|
936 ngx_queue_remove(&fcn->queue);
|
|
937 ngx_rbtree_delete(&cache->sh->rbtree, &fcn->node);
|
|
938 ngx_slab_free_locked(cache->shpool, fcn);
|
|
939 c->node = NULL;
|
464
|
940 }
|
|
941
|
|
942 ngx_shmtx_unlock(&cache->shpool->mutex);
|
|
943
|
590
|
944 c->updated = 1;
|
|
945 c->updating = 0;
|
|
946
|
464
|
947 if (c->temp_file) {
|
|
948 if (tf && tf->file.fd != NGX_INVALID_FILE) {
|
590
|
949 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->file.log, 0,
|
464
|
950 "http file cache incomplete: \"%s\"",
|
|
951 tf->file.name.data);
|
|
952
|
|
953 if (ngx_delete_file(tf->file.name.data) == NGX_FILE_ERROR) {
|
590
|
954 ngx_log_error(NGX_LOG_CRIT, c->file.log, ngx_errno,
|
464
|
955 ngx_delete_file_n " \"%s\" failed",
|
|
956 tf->file.name.data);
|
|
957 }
|
|
958 }
|
|
959 }
|
|
960 }
|
|
961
|
|
962
|
|
963 static void
|
|
964 ngx_http_file_cache_cleanup(void *data)
|
|
965 {
|
|
966 ngx_http_cache_t *c = data;
|
|
967
|
|
968 if (c->updated) {
|
|
969 return;
|
|
970 }
|
|
971
|
|
972 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->file.log, 0,
|
|
973 "http file cache cleanup");
|
|
974
|
590
|
975 if (c->updating) {
|
|
976 ngx_log_error(NGX_LOG_ALERT, c->file.log, 0,
|
|
977 "stalled cache updating, error:%ui", c->error);
|
464
|
978 }
|
|
979
|
590
|
980 ngx_http_file_cache_free(c, NULL);
|
464
|
981 }
|
|
982
|
|
983
|
|
984 static time_t
|
468
|
985 ngx_http_file_cache_forced_expire(ngx_http_file_cache_t *cache)
|
|
986 {
|
|
987 u_char *name;
|
|
988 size_t len;
|
|
989 time_t wait;
|
|
990 ngx_uint_t tries;
|
|
991 ngx_path_t *path;
|
|
992 ngx_queue_t *q;
|
|
993 ngx_http_file_cache_node_t *fcn;
|
|
994
|
|
995 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
|
|
996 "http file cache forced expire");
|
|
997
|
|
998 path = cache->path;
|
|
999 len = path->name.len + 1 + path->len + 2 * NGX_HTTP_CACHE_KEY_LEN;
|
|
1000
|
|
1001 name = ngx_alloc(len + 1, ngx_cycle->log);
|
|
1002 if (name == NULL) {
|
478
|
1003 return 10;
|
468
|
1004 }
|
|
1005
|
|
1006 ngx_memcpy(name, path->name.data, path->name.len);
|
|
1007
|
478
|
1008 wait = 10;
|
592
|
1009 tries = 20;
|
468
|
1010
|
|
1011 ngx_shmtx_lock(&cache->shpool->mutex);
|
|
1012
|
480
|
1013 for (q = ngx_queue_last(&cache->sh->queue);
|
|
1014 q != ngx_queue_sentinel(&cache->sh->queue);
|
468
|
1015 q = ngx_queue_prev(q))
|
|
1016 {
|
|
1017 fcn = ngx_queue_data(q, ngx_http_file_cache_node_t, queue);
|
|
1018
|
|
1019 ngx_log_debug6(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
|
|
1020 "http file cache forced expire: #%d %d %02xd%02xd%02xd%02xd",
|
|
1021 fcn->count, fcn->exists,
|
|
1022 fcn->key[0], fcn->key[1], fcn->key[2], fcn->key[3]);
|
|
1023
|
592
|
1024 if (fcn->count == 0) {
|
|
1025 ngx_http_file_cache_delete(cache, q, name);
|
620
|
1026 wait = 0;
|
468
|
1027
|
592
|
1028 } else {
|
|
1029 if (--tries) {
|
468
|
1030 continue;
|
|
1031 }
|
|
1032
|
|
1033 wait = 1;
|
|
1034 }
|
|
1035
|
|
1036 break;
|
|
1037 }
|
|
1038
|
|
1039 ngx_shmtx_unlock(&cache->shpool->mutex);
|
|
1040
|
|
1041 ngx_free(name);
|
|
1042
|
|
1043 return wait;
|
|
1044 }
|
|
1045
|
|
1046
|
|
1047 static time_t
|
|
1048 ngx_http_file_cache_expire(ngx_http_file_cache_t *cache)
|
464
|
1049 {
|
|
1050 u_char *name, *p;
|
|
1051 size_t len;
|
|
1052 time_t now, wait;
|
|
1053 ngx_path_t *path;
|
|
1054 ngx_queue_t *q;
|
|
1055 ngx_http_file_cache_node_t *fcn;
|
|
1056 u_char key[2 * NGX_HTTP_CACHE_KEY_LEN];
|
|
1057
|
|
1058 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
|
|
1059 "http file cache expire");
|
|
1060
|
|
1061 path = cache->path;
|
|
1062 len = path->name.len + 1 + path->len + 2 * NGX_HTTP_CACHE_KEY_LEN;
|
|
1063
|
468
|
1064 name = ngx_alloc(len + 1, ngx_cycle->log);
|
|
1065 if (name == NULL) {
|
478
|
1066 return 10;
|
468
|
1067 }
|
|
1068
|
|
1069 ngx_memcpy(name, path->name.data, path->name.len);
|
|
1070
|
464
|
1071 now = ngx_time();
|
|
1072
|
|
1073 ngx_shmtx_lock(&cache->shpool->mutex);
|
|
1074
|
|
1075 for ( ;; ) {
|
|
1076
|
480
|
1077 if (ngx_queue_empty(&cache->sh->queue)) {
|
478
|
1078 wait = 10;
|
464
|
1079 break;
|
|
1080 }
|
|
1081
|
480
|
1082 q = ngx_queue_last(&cache->sh->queue);
|
464
|
1083
|
|
1084 fcn = ngx_queue_data(q, ngx_http_file_cache_node_t, queue);
|
|
1085
|
468
|
1086 wait = fcn->expire - now;
|
464
|
1087
|
468
|
1088 if (wait > 0) {
|
478
|
1089 wait = wait > 10 ? 10 : wait;
|
468
|
1090 break;
|
464
|
1091 }
|
|
1092
|
|
1093 ngx_log_debug6(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
|
|
1094 "http file cache expire: #%d %d %02xd%02xd%02xd%02xd",
|
|
1095 fcn->count, fcn->exists,
|
|
1096 fcn->key[0], fcn->key[1], fcn->key[2], fcn->key[3]);
|
|
1097
|
592
|
1098 if (fcn->count == 0) {
|
|
1099 ngx_http_file_cache_delete(cache, q, name);
|
468
|
1100 continue;
|
464
|
1101 }
|
|
1102
|
596
|
1103 if (fcn->deleting) {
|
630
|
1104 wait = 1;
|
|
1105 break;
|
596
|
1106 }
|
|
1107
|
592
|
1108 p = ngx_hex_dump(key, (u_char *) &fcn->node.key,
|
|
1109 sizeof(ngx_rbtree_key_t));
|
|
1110 len = NGX_HTTP_CACHE_KEY_LEN - sizeof(ngx_rbtree_key_t);
|
|
1111 (void) ngx_hex_dump(p, fcn->key, len);
|
464
|
1112
|
592
|
1113 /*
|
|
1114 * abnormally exited workers may leave locked cache entries,
|
|
1115 * and although it may be safe to remove them completely,
|
654
|
1116 * we prefer to just move them to the top of the inactive queue
|
592
|
1117 */
|
464
|
1118
|
592
|
1119 ngx_queue_remove(q);
|
654
|
1120 fcn->expire = ngx_time() + cache->inactive;
|
|
1121 ngx_queue_insert_head(&cache->sh->queue, &fcn->queue);
|
464
|
1122
|
592
|
1123 ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, 0,
|
|
1124 "ignore long locked inactive cache entry %*s, count:%d",
|
|
1125 2 * NGX_HTTP_CACHE_KEY_LEN, key, fcn->count);
|
464
|
1126 }
|
|
1127
|
|
1128 ngx_shmtx_unlock(&cache->shpool->mutex);
|
|
1129
|
468
|
1130 ngx_free(name);
|
|
1131
|
464
|
1132 return wait;
|
|
1133 }
|
|
1134
|
|
1135
|
468
|
1136 static void
|
|
1137 ngx_http_file_cache_delete(ngx_http_file_cache_t *cache, ngx_queue_t *q,
|
|
1138 u_char *name)
|
|
1139 {
|
|
1140 u_char *p;
|
|
1141 size_t len;
|
|
1142 ngx_path_t *path;
|
|
1143 ngx_http_file_cache_node_t *fcn;
|
|
1144
|
|
1145 fcn = ngx_queue_data(q, ngx_http_file_cache_node_t, queue);
|
|
1146
|
592
|
1147 if (fcn->exists) {
|
622
|
1148 cache->sh->size -= fcn->fs_size;
|
468
|
1149
|
592
|
1150 path = cache->path;
|
|
1151 p = name + path->name.len + 1 + path->len;
|
|
1152 p = ngx_hex_dump(p, (u_char *) &fcn->node.key,
|
|
1153 sizeof(ngx_rbtree_key_t));
|
|
1154 len = NGX_HTTP_CACHE_KEY_LEN - sizeof(ngx_rbtree_key_t);
|
|
1155 p = ngx_hex_dump(p, fcn->key, len);
|
|
1156 *p = '\0';
|
468
|
1157
|
592
|
1158 fcn->count++;
|
596
|
1159 fcn->deleting = 1;
|
592
|
1160 ngx_shmtx_unlock(&cache->shpool->mutex);
|
468
|
1161
|
592
|
1162 len = path->name.len + 1 + path->len + 2 * NGX_HTTP_CACHE_KEY_LEN;
|
|
1163 ngx_create_hashed_filename(path, name, len);
|
468
|
1164
|
592
|
1165 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
|
|
1166 "http file cache expire: \"%s\"", name);
|
468
|
1167
|
592
|
1168 if (ngx_delete_file(name) == NGX_FILE_ERROR) {
|
|
1169 ngx_log_error(NGX_LOG_CRIT, ngx_cycle->log, ngx_errno,
|
|
1170 ngx_delete_file_n " \"%s\" failed", name);
|
|
1171 }
|
468
|
1172
|
592
|
1173 ngx_shmtx_lock(&cache->shpool->mutex);
|
|
1174 fcn->count--;
|
596
|
1175 fcn->deleting = 0;
|
468
|
1176 }
|
|
1177
|
592
|
1178 if (fcn->count == 0) {
|
|
1179 ngx_queue_remove(q);
|
|
1180 ngx_rbtree_delete(&cache->sh->rbtree, &fcn->node);
|
|
1181 ngx_slab_free_locked(cache->shpool, fcn);
|
|
1182 }
|
468
|
1183 }
|
|
1184
|
|
1185
|
|
1186 static time_t
|
|
1187 ngx_http_file_cache_manager(void *data)
|
464
|
1188 {
|
|
1189 ngx_http_file_cache_t *cache = data;
|
|
1190
|
514
|
1191 off_t size;
|
620
|
1192 time_t next, wait;
|
464
|
1193
|
468
|
1194 next = ngx_http_file_cache_expire(cache);
|
|
1195
|
|
1196 cache->last = ngx_current_msec;
|
|
1197 cache->files = 0;
|
|
1198
|
|
1199 for ( ;; ) {
|
|
1200 ngx_shmtx_lock(&cache->shpool->mutex);
|
|
1201
|
480
|
1202 size = cache->sh->size;
|
464
|
1203
|
468
|
1204 ngx_shmtx_unlock(&cache->shpool->mutex);
|
|
1205
|
470
|
1206 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
|
|
1207 "http file cache size: %O", size);
|
|
1208
|
468
|
1209 if (size < cache->max_size) {
|
|
1210 return next;
|
|
1211 }
|
464
|
1212
|
620
|
1213 wait = ngx_http_file_cache_forced_expire(cache);
|
|
1214
|
|
1215 if (wait > 0) {
|
|
1216 return wait;
|
|
1217 }
|
468
|
1218
|
632
|
1219 if (ngx_quit || ngx_terminate) {
|
468
|
1220 return next;
|
|
1221 }
|
|
1222 }
|
464
|
1223 }
|
|
1224
|
|
1225
|
514
|
1226 static void
|
|
1227 ngx_http_file_cache_loader(void *data)
|
|
1228 {
|
|
1229 ngx_http_file_cache_t *cache = data;
|
|
1230
|
|
1231 ngx_tree_ctx_t tree;
|
|
1232
|
|
1233 if (!cache->sh->cold || cache->sh->loading) {
|
|
1234 return;
|
|
1235 }
|
|
1236
|
|
1237 if (!ngx_atomic_cmp_set(&cache->sh->loading, 0, ngx_pid)) {
|
|
1238 return;
|
|
1239 }
|
|
1240
|
|
1241 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
|
|
1242 "http file cache loader");
|
|
1243
|
|
1244 tree.init_handler = NULL;
|
|
1245 tree.file_handler = ngx_http_file_cache_manage_file;
|
|
1246 tree.pre_tree_handler = ngx_http_file_cache_noop;
|
|
1247 tree.post_tree_handler = ngx_http_file_cache_noop;
|
|
1248 tree.spec_handler = ngx_http_file_cache_delete_file;
|
|
1249 tree.data = cache;
|
|
1250 tree.alloc = 0;
|
|
1251 tree.log = ngx_cycle->log;
|
|
1252
|
|
1253 cache->last = ngx_current_msec;
|
|
1254 cache->files = 0;
|
|
1255
|
|
1256 if (ngx_walk_tree(&tree, &cache->path->name) == NGX_ABORT) {
|
|
1257 cache->sh->loading = 0;
|
|
1258 return;
|
|
1259 }
|
|
1260
|
|
1261 cache->sh->cold = 0;
|
|
1262 cache->sh->loading = 0;
|
|
1263
|
|
1264 ngx_log_error(NGX_LOG_NOTICE, ngx_cycle->log, 0,
|
|
1265 "http file cache: %V %.3fM, bsize: %uz",
|
|
1266 &cache->path->name,
|
|
1267 ((double) cache->sh->size * cache->bsize) / (1024 * 1024),
|
|
1268 cache->bsize);
|
|
1269 }
|
|
1270
|
|
1271
|
464
|
1272 static ngx_int_t
|
468
|
1273 ngx_http_file_cache_noop(ngx_tree_ctx_t *ctx, ngx_str_t *path)
|
464
|
1274 {
|
|
1275 return NGX_OK;
|
|
1276 }
|
|
1277
|
|
1278
|
|
1279 static ngx_int_t
|
468
|
1280 ngx_http_file_cache_manage_file(ngx_tree_ctx_t *ctx, ngx_str_t *path)
|
464
|
1281 {
|
634
|
1282 ngx_msec_t elapsed;
|
468
|
1283 ngx_http_file_cache_t *cache;
|
|
1284
|
|
1285 cache = ctx->data;
|
|
1286
|
|
1287 if (ngx_http_file_cache_add_file(ctx, path) != NGX_OK) {
|
|
1288 (void) ngx_http_file_cache_delete_file(ctx, path);
|
|
1289 }
|
|
1290
|
634
|
1291 if (++cache->files >= cache->loader_files) {
|
|
1292 ngx_http_file_cache_loader_sleep(cache);
|
|
1293
|
|
1294 } else {
|
|
1295 ngx_time_update();
|
|
1296
|
|
1297 elapsed = ngx_abs((ngx_msec_int_t) (ngx_current_msec - cache->last));
|
|
1298
|
|
1299 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
|
|
1300 "http file cache loader time elapsed: %M", elapsed);
|
|
1301
|
|
1302 if (elapsed >= cache->loader_threshold) {
|
|
1303 ngx_http_file_cache_loader_sleep(cache);
|
|
1304 }
|
|
1305 }
|
|
1306
|
|
1307 return (ngx_quit || ngx_terminate) ? NGX_ABORT : NGX_OK;
|
|
1308 }
|
|
1309
|
|
1310
|
|
1311 static void
|
|
1312 ngx_http_file_cache_loader_sleep(ngx_http_file_cache_t *cache)
|
|
1313 {
|
|
1314 ngx_msleep(cache->loader_sleep);
|
|
1315
|
|
1316 ngx_time_update();
|
|
1317
|
|
1318 cache->last = ngx_current_msec;
|
|
1319 cache->files = 0;
|
468
|
1320 }
|
|
1321
|
464
|
1322
|
468
|
1323 static ngx_int_t
|
|
1324 ngx_http_file_cache_add_file(ngx_tree_ctx_t *ctx, ngx_str_t *name)
|
|
1325 {
|
632
|
1326 u_char *p;
|
|
1327 ngx_int_t n;
|
|
1328 ngx_uint_t i;
|
|
1329 ngx_http_cache_t c;
|
|
1330 ngx_http_file_cache_t *cache;
|
468
|
1331
|
|
1332 if (name->len < 2 * NGX_HTTP_CACHE_KEY_LEN) {
|
|
1333 return NGX_ERROR;
|
464
|
1334 }
|
|
1335
|
632
|
1336 if (ctx->size < (off_t) sizeof(ngx_http_file_cache_header_t)) {
|
468
|
1337 ngx_log_error(NGX_LOG_CRIT, ctx->log, 0,
|
|
1338 "cache file \"%s\" is too small", name->data);
|
|
1339 return NGX_ERROR;
|
|
1340 }
|
|
1341
|
632
|
1342 ngx_memzero(&c, sizeof(ngx_http_cache_t));
|
622
|
1343 cache = ctx->data;
|
|
1344
|
632
|
1345 c.length = ctx->size;
|
|
1346 c.fs_size = (ctx->fs_size + cache->bsize - 1) / cache->bsize;
|
468
|
1347
|
|
1348 p = &name->data[name->len - 2 * NGX_HTTP_CACHE_KEY_LEN];
|
464
|
1349
|
|
1350 for (i = 0; i < NGX_HTTP_CACHE_KEY_LEN; i++) {
|
|
1351 n = ngx_hextoi(p, 2);
|
|
1352
|
|
1353 if (n == NGX_ERROR) {
|
468
|
1354 return NGX_ERROR;
|
464
|
1355 }
|
|
1356
|
|
1357 p += 2;
|
|
1358
|
468
|
1359 c.key[i] = (u_char) n;
|
464
|
1360 }
|
|
1361
|
468
|
1362 return ngx_http_file_cache_add(cache, &c);
|
|
1363 }
|
|
1364
|
|
1365
|
|
1366 static ngx_int_t
|
|
1367 ngx_http_file_cache_add(ngx_http_file_cache_t *cache, ngx_http_cache_t *c)
|
|
1368 {
|
|
1369 ngx_http_file_cache_node_t *fcn;
|
|
1370
|
464
|
1371 ngx_shmtx_lock(&cache->shpool->mutex);
|
|
1372
|
468
|
1373 fcn = ngx_http_file_cache_lookup(cache, c->key);
|
|
1374
|
|
1375 if (fcn == NULL) {
|
|
1376
|
|
1377 fcn = ngx_slab_alloc_locked(cache->shpool,
|
|
1378 sizeof(ngx_http_file_cache_node_t));
|
|
1379 if (fcn == NULL) {
|
|
1380 ngx_shmtx_unlock(&cache->shpool->mutex);
|
|
1381 return NGX_ERROR;
|
|
1382 }
|
|
1383
|
|
1384 ngx_memcpy((u_char *) &fcn->node.key, c->key, sizeof(ngx_rbtree_key_t));
|
|
1385
|
|
1386 ngx_memcpy(fcn->key, &c->key[sizeof(ngx_rbtree_key_t)],
|
|
1387 NGX_HTTP_CACHE_KEY_LEN - sizeof(ngx_rbtree_key_t));
|
|
1388
|
480
|
1389 ngx_rbtree_insert(&cache->sh->rbtree, &fcn->node);
|
468
|
1390
|
|
1391 fcn->uses = 1;
|
|
1392 fcn->count = 0;
|
632
|
1393 fcn->valid_msec = 0;
|
468
|
1394 fcn->error = 0;
|
|
1395 fcn->exists = 1;
|
594
|
1396 fcn->updating = 0;
|
596
|
1397 fcn->deleting = 0;
|
632
|
1398 fcn->uniq = 0;
|
|
1399 fcn->valid_sec = 0;
|
|
1400 fcn->body_start = 0;
|
622
|
1401 fcn->fs_size = c->fs_size;
|
468
|
1402
|
622
|
1403 cache->sh->size += c->fs_size;
|
468
|
1404
|
|
1405 } else {
|
|
1406 ngx_queue_remove(&fcn->queue);
|
|
1407 }
|
|
1408
|
|
1409 fcn->expire = ngx_time() + cache->inactive;
|
|
1410
|
480
|
1411 ngx_queue_insert_head(&cache->sh->queue, &fcn->queue);
|
464
|
1412
|
|
1413 ngx_shmtx_unlock(&cache->shpool->mutex);
|
|
1414
|
468
|
1415 return NGX_OK;
|
|
1416 }
|
|
1417
|
464
|
1418
|
468
|
1419 static ngx_int_t
|
|
1420 ngx_http_file_cache_delete_file(ngx_tree_ctx_t *ctx, ngx_str_t *path)
|
|
1421 {
|
464
|
1422 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0,
|
468
|
1423 "http file cache delete: \"%s\"", path->data);
|
464
|
1424
|
|
1425 if (ngx_delete_file(path->data) == NGX_FILE_ERROR) {
|
|
1426 ngx_log_error(NGX_LOG_CRIT, ctx->log, ngx_errno,
|
|
1427 ngx_delete_file_n " \"%s\" failed", path->data);
|
|
1428 }
|
26
|
1429
|
0
|
1430 return NGX_OK;
|
|
1431 }
|
|
1432
|
|
1433
|
464
|
1434 time_t
|
|
1435 ngx_http_file_cache_valid(ngx_array_t *cache_valid, ngx_uint_t status)
|
0
|
1436 {
|
464
|
1437 ngx_uint_t i;
|
|
1438 ngx_http_cache_valid_t *valid;
|
0
|
1439
|
470
|
1440 if (cache_valid == NULL) {
|
|
1441 return 0;
|
|
1442 }
|
|
1443
|
464
|
1444 valid = cache_valid->elts;
|
|
1445 for (i = 0; i < cache_valid->nelts; i++) {
|
0
|
1446
|
464
|
1447 if (valid[i].status == 0) {
|
|
1448 return valid[i].valid;
|
0
|
1449 }
|
|
1450
|
464
|
1451 if (valid[i].status == status) {
|
|
1452 return valid[i].valid;
|
0
|
1453 }
|
464
|
1454 }
|
0
|
1455
|
464
|
1456 return 0;
|
0
|
1457 }
|
|
1458
|
|
1459
|
464
|
1460 char *
|
|
1461 ngx_http_file_cache_set_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
1462 {
|
468
|
1463 off_t max_size;
|
464
|
1464 u_char *last, *p;
|
468
|
1465 time_t inactive;
|
464
|
1466 ssize_t size;
|
|
1467 ngx_str_t s, name, *value;
|
632
|
1468 ngx_int_t loader_files, loader_sleep, loader_threshold;
|
464
|
1469 ngx_uint_t i, n;
|
|
1470 ngx_http_file_cache_t *cache;
|
|
1471
|
|
1472 cache = ngx_pcalloc(cf->pool, sizeof(ngx_http_file_cache_t));
|
|
1473 if (cache == NULL) {
|
|
1474 return NGX_CONF_ERROR;
|
|
1475 }
|
|
1476
|
|
1477 cache->path = ngx_pcalloc(cf->pool, sizeof(ngx_path_t));
|
|
1478 if (cache->path == NULL) {
|
|
1479 return NGX_CONF_ERROR;
|
|
1480 }
|
|
1481
|
|
1482 inactive = 600;
|
632
|
1483 loader_files = 100;
|
|
1484 loader_sleep = 50;
|
|
1485 loader_threshold = 200;
|
464
|
1486
|
|
1487 name.len = 0;
|
|
1488 size = 0;
|
468
|
1489 max_size = NGX_MAX_OFF_T_VALUE;
|
464
|
1490
|
|
1491 value = cf->args->elts;
|
|
1492
|
|
1493 cache->path->name = value[1];
|
|
1494
|
|
1495 if (cache->path->name.data[cache->path->name.len - 1] == '/') {
|
|
1496 cache->path->name.len--;
|
|
1497 }
|
|
1498
|
|
1499 if (ngx_conf_full_name(cf->cycle, &cache->path->name, 0) != NGX_OK) {
|
|
1500 return NGX_CONF_ERROR;
|
|
1501 }
|
|
1502
|
|
1503 for (i = 2; i < cf->args->nelts; i++) {
|
|
1504
|
|
1505 if (ngx_strncmp(value[i].data, "levels=", 7) == 0) {
|
|
1506
|
|
1507 p = value[i].data + 7;
|
|
1508 last = value[i].data + value[i].len;
|
|
1509
|
472
|
1510 for (n = 0; n < 3 && p < last; n++) {
|
464
|
1511
|
472
|
1512 if (*p > '0' && *p < '3') {
|
464
|
1513
|
|
1514 cache->path->level[n] = *p++ - '0';
|
|
1515 cache->path->len += cache->path->level[n] + 1;
|
|
1516
|
|
1517 if (p == last) {
|
|
1518 break;
|
|
1519 }
|
|
1520
|
472
|
1521 if (*p++ == ':' && n < 2 && p != last) {
|
464
|
1522 continue;
|
|
1523 }
|
472
|
1524
|
|
1525 goto invalid_levels;
|
464
|
1526 }
|
|
1527
|
|
1528 goto invalid_levels;
|
|
1529 }
|
|
1530
|
|
1531 if (cache->path->len < 10 + 3) {
|
|
1532 continue;
|
|
1533 }
|
|
1534
|
|
1535 invalid_levels:
|
|
1536
|
|
1537 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1538 "invalid \"levels\" \"%V\"", &value[i]);
|
|
1539 return NGX_CONF_ERROR;
|
|
1540 }
|
|
1541
|
|
1542 if (ngx_strncmp(value[i].data, "keys_zone=", 10) == 0) {
|
|
1543
|
|
1544 name.data = value[i].data + 10;
|
|
1545
|
|
1546 p = (u_char *) ngx_strchr(name.data, ':');
|
|
1547
|
|
1548 if (p) {
|
480
|
1549 *p = '\0';
|
|
1550
|
464
|
1551 name.len = p - name.data;
|
|
1552
|
|
1553 p++;
|
|
1554
|
|
1555 s.len = value[i].data + value[i].len - p;
|
|
1556 s.data = p;
|
|
1557
|
|
1558 size = ngx_parse_size(&s);
|
|
1559 if (size > 8191) {
|
|
1560 continue;
|
|
1561 }
|
|
1562 }
|
|
1563
|
|
1564 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1565 "invalid keys zone size \"%V\"", &value[i]);
|
|
1566 return NGX_CONF_ERROR;
|
|
1567 }
|
|
1568
|
|
1569 if (ngx_strncmp(value[i].data, "inactive=", 9) == 0) {
|
|
1570
|
|
1571 s.len = value[i].len - 9;
|
|
1572 s.data = value[i].data + 9;
|
|
1573
|
|
1574 inactive = ngx_parse_time(&s, 1);
|
|
1575 if (inactive < 0) {
|
|
1576 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1577 "invalid inactive value \"%V\"", &value[i]);
|
|
1578 return NGX_CONF_ERROR;
|
|
1579 }
|
|
1580
|
|
1581 continue;
|
|
1582 }
|
|
1583
|
468
|
1584 if (ngx_strncmp(value[i].data, "max_size=", 9) == 0) {
|
464
|
1585
|
468
|
1586 s.len = value[i].len - 9;
|
|
1587 s.data = value[i].data + 9;
|
464
|
1588
|
468
|
1589 max_size = ngx_parse_offset(&s);
|
|
1590 if (max_size < 0) {
|
464
|
1591 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
468
|
1592 "invalid max_size value \"%V\"", &value[i]);
|
464
|
1593 return NGX_CONF_ERROR;
|
|
1594 }
|
|
1595
|
|
1596 continue;
|
|
1597 }
|
|
1598
|
632
|
1599 if (ngx_strncmp(value[i].data, "loader_files=", 13) == 0) {
|
|
1600
|
|
1601 loader_files = ngx_atoi(value[i].data + 13, value[i].len - 13);
|
|
1602 if (loader_files == NGX_ERROR) {
|
|
1603 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1604 "invalid loader_files value \"%V\"", &value[i]);
|
|
1605 return NGX_CONF_ERROR;
|
|
1606 }
|
|
1607
|
|
1608 continue;
|
|
1609 }
|
|
1610
|
|
1611 if (ngx_strncmp(value[i].data, "loader_sleep=", 13) == 0) {
|
|
1612
|
|
1613 s.len = value[i].len - 13;
|
|
1614 s.data = value[i].data + 13;
|
|
1615
|
|
1616 loader_sleep = ngx_parse_time(&s, 0);
|
|
1617 if (loader_sleep < 0) {
|
|
1618 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1619 "invalid loader_sleep value \"%V\"", &value[i]);
|
|
1620 return NGX_CONF_ERROR;
|
|
1621 }
|
|
1622
|
|
1623 continue;
|
|
1624 }
|
|
1625
|
|
1626 if (ngx_strncmp(value[i].data, "loader_threshold=", 17) == 0) {
|
|
1627
|
|
1628 s.len = value[i].len - 17;
|
|
1629 s.data = value[i].data + 17;
|
|
1630
|
|
1631 loader_threshold = ngx_parse_time(&s, 0);
|
|
1632 if (loader_threshold < 0) {
|
|
1633 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1634 "invalid loader_threshold value \"%V\"", &value[i]);
|
|
1635 return NGX_CONF_ERROR;
|
|
1636 }
|
|
1637
|
|
1638 continue;
|
|
1639 }
|
|
1640
|
464
|
1641 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1642 "invalid parameter \"%V\"", &value[i]);
|
|
1643 return NGX_CONF_ERROR;
|
|
1644 }
|
|
1645
|
|
1646 if (name.len == 0 || size == 0) {
|
|
1647 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1648 "\"%V\" must have \"keys_zone\" parameter",
|
|
1649 &cmd->name);
|
|
1650 return NGX_CONF_ERROR;
|
|
1651 }
|
|
1652
|
468
|
1653 cache->path->manager = ngx_http_file_cache_manager;
|
514
|
1654 cache->path->loader = ngx_http_file_cache_loader;
|
464
|
1655 cache->path->data = cache;
|
632
|
1656 cache->path->conf_file = cf->conf_file->file.name.data;
|
|
1657 cache->path->line = cf->conf_file->line;
|
|
1658 cache->loader_files = loader_files;
|
|
1659 cache->loader_sleep = (ngx_msec_t) loader_sleep;
|
|
1660 cache->loader_threshold = (ngx_msec_t) loader_threshold;
|
464
|
1661
|
|
1662 if (ngx_add_path(cf, &cache->path) != NGX_OK) {
|
|
1663 return NGX_CONF_ERROR;
|
|
1664 }
|
|
1665
|
|
1666 cache->shm_zone = ngx_shared_memory_add(cf, &name, size, cmd->post);
|
|
1667 if (cache->shm_zone == NULL) {
|
|
1668 return NGX_CONF_ERROR;
|
|
1669 }
|
|
1670
|
|
1671 if (cache->shm_zone->data) {
|
|
1672 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1673 "duplicate zone \"%V\"", &name);
|
|
1674 return NGX_CONF_ERROR;
|
|
1675 }
|
|
1676
|
|
1677
|
|
1678 cache->shm_zone->init = ngx_http_file_cache_init;
|
|
1679 cache->shm_zone->data = cache;
|
|
1680
|
|
1681 cache->inactive = inactive;
|
468
|
1682 cache->max_size = max_size;
|
464
|
1683
|
|
1684 return NGX_CONF_OK;
|
|
1685 }
|
26
|
1686
|
|
1687
|
464
|
1688 char *
|
|
1689 ngx_http_file_cache_valid_set_slot(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
1690 void *conf)
|
0
|
1691 {
|
464
|
1692 char *p = conf;
|
0
|
1693
|
464
|
1694 time_t valid;
|
|
1695 ngx_str_t *value;
|
|
1696 ngx_uint_t i, n, status;
|
|
1697 ngx_array_t **a;
|
|
1698 ngx_http_cache_valid_t *v;
|
|
1699 static ngx_uint_t statuses[] = { 200, 301, 302 };
|
26
|
1700
|
464
|
1701 a = (ngx_array_t **) (p + cmd->offset);
|
0
|
1702
|
464
|
1703 if (*a == NGX_CONF_UNSET_PTR) {
|
|
1704 *a = ngx_array_create(cf->pool, 1, sizeof(ngx_http_cache_valid_t));
|
|
1705 if (*a == NULL) {
|
|
1706 return NGX_CONF_ERROR;
|
|
1707 }
|
|
1708 }
|
0
|
1709
|
464
|
1710 value = cf->args->elts;
|
|
1711 n = cf->args->nelts - 1;
|
0
|
1712
|
464
|
1713 valid = ngx_parse_time(&value[n], 1);
|
|
1714 if (valid < 0) {
|
|
1715 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1716 "invalid time value \"%V\"", &value[n]);
|
|
1717 return NGX_CONF_ERROR;
|
0
|
1718 }
|
|
1719
|
464
|
1720 if (n == 1) {
|
|
1721
|
|
1722 for (i = 0; i < 3; i++) {
|
|
1723 v = ngx_array_push(*a);
|
|
1724 if (v == NULL) {
|
|
1725 return NGX_CONF_ERROR;
|
|
1726 }
|
|
1727
|
|
1728 v->status = statuses[i];
|
|
1729 v->valid = valid;
|
|
1730 }
|
|
1731
|
|
1732 return NGX_CONF_OK;
|
0
|
1733 }
|
|
1734
|
464
|
1735 for (i = 1; i < n; i++) {
|
|
1736
|
|
1737 if (ngx_strcmp(value[i].data, "any") == 0) {
|
|
1738
|
|
1739 status = 0;
|
|
1740
|
|
1741 } else {
|
0
|
1742
|
464
|
1743 status = ngx_atoi(value[i].data, value[i].len);
|
|
1744 if (status < 100) {
|
|
1745 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1746 "invalid status \"%V\"", &value[i]);
|
|
1747 return NGX_CONF_ERROR;
|
|
1748 }
|
|
1749 }
|
|
1750
|
|
1751 v = ngx_array_push(*a);
|
|
1752 if (v == NULL) {
|
|
1753 return NGX_CONF_ERROR;
|
|
1754 }
|
|
1755
|
|
1756 v->status = status;
|
|
1757 v->valid = valid;
|
|
1758 }
|
|
1759
|
|
1760 return NGX_CONF_OK;
|
0
|
1761 }
|