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