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
|
464
|
389 c->buf->last += n;
|
26
|
390
|
464
|
391 c->valid_sec = h->valid_sec;
|
|
392 c->last_modified = h->last_modified;
|
|
393 c->date = h->date;
|
|
394 c->valid_msec = h->valid_msec;
|
564
|
395 c->header_start = h->header_start;
|
464
|
396 c->body_start = h->body_start;
|
26
|
397
|
464
|
398 r->cached = 1;
|
|
399
|
518
|
400 cache = c->file_cache;
|
|
401
|
|
402 if (cache->sh->cold) {
|
464
|
403
|
|
404 ngx_shmtx_lock(&cache->shpool->mutex);
|
26
|
405
|
468
|
406 if (!c->node->exists) {
|
|
407 c->node->uses = 1;
|
|
408 c->node->body_start = c->body_start;
|
|
409 c->node->exists = 1;
|
518
|
410 c->node->uniq = c->uniq;
|
636
|
411 c->node->fs_size = c->fs_size;
|
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
|
642
|
856 if (r != r->main && c->length - c->body_start == 0) {
|
|
857 return ngx_http_send_header(r);
|
|
858 }
|
|
859
|
464
|
860 /* we need to allocate all before the header would be sent */
|
|
861
|
|
862 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
|
|
863 if (b == NULL) {
|
|
864 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
865 }
|
|
866
|
|
867 b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t));
|
|
868 if (b->file == NULL) {
|
|
869 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
870 }
|
|
871
|
|
872 rc = ngx_http_send_header(r);
|
|
873
|
|
874 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
|
|
875 return rc;
|
|
876 }
|
|
877
|
|
878 b->file_pos = c->body_start;
|
|
879 b->file_last = c->length;
|
|
880
|
642
|
881 b->in_file = (c->length - c->body_start) ? 1: 0;
|
464
|
882 b->last_buf = (r == r->main) ? 1: 0;
|
|
883 b->last_in_chain = 1;
|
|
884
|
|
885 b->file->fd = c->file.fd;
|
|
886 b->file->name = c->file.name;
|
|
887 b->file->log = r->connection->log;
|
|
888
|
|
889 out.buf = b;
|
|
890 out.next = NULL;
|
|
891
|
|
892 return ngx_http_output_filter(r, &out);
|
|
893 }
|
|
894
|
|
895
|
|
896 void
|
590
|
897 ngx_http_file_cache_free(ngx_http_cache_t *c, ngx_temp_file_t *tf)
|
464
|
898 {
|
588
|
899 ngx_http_file_cache_t *cache;
|
|
900 ngx_http_file_cache_node_t *fcn;
|
464
|
901
|
626
|
902 if (c->updated || c->node == NULL) {
|
464
|
903 return;
|
|
904 }
|
|
905
|
|
906 cache = c->file_cache;
|
|
907
|
590
|
908 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->file.log, 0,
|
|
909 "http file cache free, fd: %d", c->file.fd);
|
464
|
910
|
|
911 ngx_shmtx_lock(&cache->shpool->mutex);
|
|
912
|
588
|
913 fcn = c->node;
|
|
914 fcn->count--;
|
590
|
915
|
|
916 if (c->updating) {
|
|
917 fcn->updating = 0;
|
|
918 }
|
464
|
919
|
|
920 if (c->error) {
|
588
|
921 fcn->error = c->error;
|
|
922
|
594
|
923 if (c->valid_sec) {
|
|
924 fcn->valid_sec = c->valid_sec;
|
|
925 fcn->valid_msec = c->valid_msec;
|
|
926 }
|
|
927
|
592
|
928 } else if (!fcn->exists && fcn->count == 0 && c->min_uses == 1) {
|
588
|
929 ngx_queue_remove(&fcn->queue);
|
|
930 ngx_rbtree_delete(&cache->sh->rbtree, &fcn->node);
|
|
931 ngx_slab_free_locked(cache->shpool, fcn);
|
|
932 c->node = NULL;
|
464
|
933 }
|
|
934
|
|
935 ngx_shmtx_unlock(&cache->shpool->mutex);
|
|
936
|
590
|
937 c->updated = 1;
|
|
938 c->updating = 0;
|
|
939
|
464
|
940 if (c->temp_file) {
|
|
941 if (tf && tf->file.fd != NGX_INVALID_FILE) {
|
590
|
942 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->file.log, 0,
|
464
|
943 "http file cache incomplete: \"%s\"",
|
|
944 tf->file.name.data);
|
|
945
|
|
946 if (ngx_delete_file(tf->file.name.data) == NGX_FILE_ERROR) {
|
590
|
947 ngx_log_error(NGX_LOG_CRIT, c->file.log, ngx_errno,
|
464
|
948 ngx_delete_file_n " \"%s\" failed",
|
|
949 tf->file.name.data);
|
|
950 }
|
|
951 }
|
|
952 }
|
|
953 }
|
|
954
|
|
955
|
|
956 static void
|
|
957 ngx_http_file_cache_cleanup(void *data)
|
|
958 {
|
|
959 ngx_http_cache_t *c = data;
|
|
960
|
|
961 if (c->updated) {
|
|
962 return;
|
|
963 }
|
|
964
|
|
965 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->file.log, 0,
|
|
966 "http file cache cleanup");
|
|
967
|
590
|
968 if (c->updating) {
|
|
969 ngx_log_error(NGX_LOG_ALERT, c->file.log, 0,
|
|
970 "stalled cache updating, error:%ui", c->error);
|
464
|
971 }
|
|
972
|
590
|
973 ngx_http_file_cache_free(c, NULL);
|
464
|
974 }
|
|
975
|
|
976
|
|
977 static time_t
|
468
|
978 ngx_http_file_cache_forced_expire(ngx_http_file_cache_t *cache)
|
|
979 {
|
|
980 u_char *name;
|
|
981 size_t len;
|
|
982 time_t wait;
|
|
983 ngx_uint_t tries;
|
|
984 ngx_path_t *path;
|
|
985 ngx_queue_t *q;
|
|
986 ngx_http_file_cache_node_t *fcn;
|
|
987
|
|
988 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
|
|
989 "http file cache forced expire");
|
|
990
|
|
991 path = cache->path;
|
|
992 len = path->name.len + 1 + path->len + 2 * NGX_HTTP_CACHE_KEY_LEN;
|
|
993
|
|
994 name = ngx_alloc(len + 1, ngx_cycle->log);
|
|
995 if (name == NULL) {
|
478
|
996 return 10;
|
468
|
997 }
|
|
998
|
|
999 ngx_memcpy(name, path->name.data, path->name.len);
|
|
1000
|
478
|
1001 wait = 10;
|
592
|
1002 tries = 20;
|
468
|
1003
|
|
1004 ngx_shmtx_lock(&cache->shpool->mutex);
|
|
1005
|
480
|
1006 for (q = ngx_queue_last(&cache->sh->queue);
|
|
1007 q != ngx_queue_sentinel(&cache->sh->queue);
|
468
|
1008 q = ngx_queue_prev(q))
|
|
1009 {
|
|
1010 fcn = ngx_queue_data(q, ngx_http_file_cache_node_t, queue);
|
|
1011
|
|
1012 ngx_log_debug6(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
|
|
1013 "http file cache forced expire: #%d %d %02xd%02xd%02xd%02xd",
|
|
1014 fcn->count, fcn->exists,
|
|
1015 fcn->key[0], fcn->key[1], fcn->key[2], fcn->key[3]);
|
|
1016
|
592
|
1017 if (fcn->count == 0) {
|
|
1018 ngx_http_file_cache_delete(cache, q, name);
|
620
|
1019 wait = 0;
|
468
|
1020
|
592
|
1021 } else {
|
|
1022 if (--tries) {
|
468
|
1023 continue;
|
|
1024 }
|
|
1025
|
|
1026 wait = 1;
|
|
1027 }
|
|
1028
|
|
1029 break;
|
|
1030 }
|
|
1031
|
|
1032 ngx_shmtx_unlock(&cache->shpool->mutex);
|
|
1033
|
|
1034 ngx_free(name);
|
|
1035
|
|
1036 return wait;
|
|
1037 }
|
|
1038
|
|
1039
|
|
1040 static time_t
|
|
1041 ngx_http_file_cache_expire(ngx_http_file_cache_t *cache)
|
464
|
1042 {
|
|
1043 u_char *name, *p;
|
|
1044 size_t len;
|
|
1045 time_t now, wait;
|
|
1046 ngx_path_t *path;
|
|
1047 ngx_queue_t *q;
|
|
1048 ngx_http_file_cache_node_t *fcn;
|
|
1049 u_char key[2 * NGX_HTTP_CACHE_KEY_LEN];
|
|
1050
|
|
1051 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
|
|
1052 "http file cache expire");
|
|
1053
|
|
1054 path = cache->path;
|
|
1055 len = path->name.len + 1 + path->len + 2 * NGX_HTTP_CACHE_KEY_LEN;
|
|
1056
|
468
|
1057 name = ngx_alloc(len + 1, ngx_cycle->log);
|
|
1058 if (name == NULL) {
|
478
|
1059 return 10;
|
468
|
1060 }
|
|
1061
|
|
1062 ngx_memcpy(name, path->name.data, path->name.len);
|
|
1063
|
464
|
1064 now = ngx_time();
|
|
1065
|
|
1066 ngx_shmtx_lock(&cache->shpool->mutex);
|
|
1067
|
|
1068 for ( ;; ) {
|
|
1069
|
480
|
1070 if (ngx_queue_empty(&cache->sh->queue)) {
|
478
|
1071 wait = 10;
|
464
|
1072 break;
|
|
1073 }
|
|
1074
|
480
|
1075 q = ngx_queue_last(&cache->sh->queue);
|
464
|
1076
|
|
1077 fcn = ngx_queue_data(q, ngx_http_file_cache_node_t, queue);
|
|
1078
|
468
|
1079 wait = fcn->expire - now;
|
464
|
1080
|
468
|
1081 if (wait > 0) {
|
478
|
1082 wait = wait > 10 ? 10 : wait;
|
468
|
1083 break;
|
464
|
1084 }
|
|
1085
|
|
1086 ngx_log_debug6(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
|
|
1087 "http file cache expire: #%d %d %02xd%02xd%02xd%02xd",
|
|
1088 fcn->count, fcn->exists,
|
|
1089 fcn->key[0], fcn->key[1], fcn->key[2], fcn->key[3]);
|
|
1090
|
592
|
1091 if (fcn->count == 0) {
|
|
1092 ngx_http_file_cache_delete(cache, q, name);
|
468
|
1093 continue;
|
464
|
1094 }
|
|
1095
|
596
|
1096 if (fcn->deleting) {
|
630
|
1097 wait = 1;
|
|
1098 break;
|
596
|
1099 }
|
|
1100
|
592
|
1101 p = ngx_hex_dump(key, (u_char *) &fcn->node.key,
|
|
1102 sizeof(ngx_rbtree_key_t));
|
|
1103 len = NGX_HTTP_CACHE_KEY_LEN - sizeof(ngx_rbtree_key_t);
|
|
1104 (void) ngx_hex_dump(p, fcn->key, len);
|
464
|
1105
|
592
|
1106 /*
|
|
1107 * abnormally exited workers may leave locked cache entries,
|
|
1108 * and although it may be safe to remove them completely,
|
|
1109 * we prefer to remove them from inactive queue and rbtree
|
|
1110 * only, and to allow other leaks
|
|
1111 */
|
464
|
1112
|
592
|
1113 ngx_queue_remove(q);
|
|
1114 ngx_rbtree_delete(&cache->sh->rbtree, &fcn->node);
|
464
|
1115
|
592
|
1116 ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, 0,
|
|
1117 "ignore long locked inactive cache entry %*s, count:%d",
|
|
1118 2 * NGX_HTTP_CACHE_KEY_LEN, key, fcn->count);
|
464
|
1119 }
|
|
1120
|
|
1121 ngx_shmtx_unlock(&cache->shpool->mutex);
|
|
1122
|
468
|
1123 ngx_free(name);
|
|
1124
|
464
|
1125 return wait;
|
|
1126 }
|
|
1127
|
|
1128
|
468
|
1129 static void
|
|
1130 ngx_http_file_cache_delete(ngx_http_file_cache_t *cache, ngx_queue_t *q,
|
|
1131 u_char *name)
|
|
1132 {
|
|
1133 u_char *p;
|
|
1134 size_t len;
|
|
1135 ngx_path_t *path;
|
|
1136 ngx_http_file_cache_node_t *fcn;
|
|
1137
|
|
1138 fcn = ngx_queue_data(q, ngx_http_file_cache_node_t, queue);
|
|
1139
|
592
|
1140 if (fcn->exists) {
|
622
|
1141 cache->sh->size -= fcn->fs_size;
|
468
|
1142
|
592
|
1143 path = cache->path;
|
|
1144 p = name + path->name.len + 1 + path->len;
|
|
1145 p = ngx_hex_dump(p, (u_char *) &fcn->node.key,
|
|
1146 sizeof(ngx_rbtree_key_t));
|
|
1147 len = NGX_HTTP_CACHE_KEY_LEN - sizeof(ngx_rbtree_key_t);
|
|
1148 p = ngx_hex_dump(p, fcn->key, len);
|
|
1149 *p = '\0';
|
468
|
1150
|
592
|
1151 fcn->count++;
|
596
|
1152 fcn->deleting = 1;
|
592
|
1153 ngx_shmtx_unlock(&cache->shpool->mutex);
|
468
|
1154
|
592
|
1155 len = path->name.len + 1 + path->len + 2 * NGX_HTTP_CACHE_KEY_LEN;
|
|
1156 ngx_create_hashed_filename(path, name, len);
|
468
|
1157
|
592
|
1158 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
|
|
1159 "http file cache expire: \"%s\"", name);
|
468
|
1160
|
592
|
1161 if (ngx_delete_file(name) == NGX_FILE_ERROR) {
|
|
1162 ngx_log_error(NGX_LOG_CRIT, ngx_cycle->log, ngx_errno,
|
|
1163 ngx_delete_file_n " \"%s\" failed", name);
|
|
1164 }
|
468
|
1165
|
592
|
1166 ngx_shmtx_lock(&cache->shpool->mutex);
|
|
1167 fcn->count--;
|
596
|
1168 fcn->deleting = 0;
|
468
|
1169 }
|
|
1170
|
592
|
1171 if (fcn->count == 0) {
|
|
1172 ngx_queue_remove(q);
|
|
1173 ngx_rbtree_delete(&cache->sh->rbtree, &fcn->node);
|
|
1174 ngx_slab_free_locked(cache->shpool, fcn);
|
|
1175 }
|
468
|
1176 }
|
|
1177
|
|
1178
|
|
1179 static time_t
|
|
1180 ngx_http_file_cache_manager(void *data)
|
464
|
1181 {
|
|
1182 ngx_http_file_cache_t *cache = data;
|
|
1183
|
514
|
1184 off_t size;
|
620
|
1185 time_t next, wait;
|
464
|
1186
|
468
|
1187 next = ngx_http_file_cache_expire(cache);
|
|
1188
|
|
1189 cache->last = ngx_current_msec;
|
|
1190 cache->files = 0;
|
|
1191
|
|
1192 for ( ;; ) {
|
|
1193 ngx_shmtx_lock(&cache->shpool->mutex);
|
|
1194
|
480
|
1195 size = cache->sh->size;
|
464
|
1196
|
468
|
1197 ngx_shmtx_unlock(&cache->shpool->mutex);
|
|
1198
|
470
|
1199 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
|
|
1200 "http file cache size: %O", size);
|
|
1201
|
468
|
1202 if (size < cache->max_size) {
|
|
1203 return next;
|
|
1204 }
|
464
|
1205
|
620
|
1206 wait = ngx_http_file_cache_forced_expire(cache);
|
|
1207
|
|
1208 if (wait > 0) {
|
|
1209 return wait;
|
|
1210 }
|
468
|
1211
|
632
|
1212 if (ngx_quit || ngx_terminate) {
|
468
|
1213 return next;
|
|
1214 }
|
|
1215 }
|
464
|
1216 }
|
|
1217
|
|
1218
|
514
|
1219 static void
|
|
1220 ngx_http_file_cache_loader(void *data)
|
|
1221 {
|
|
1222 ngx_http_file_cache_t *cache = data;
|
|
1223
|
|
1224 ngx_tree_ctx_t tree;
|
|
1225
|
|
1226 if (!cache->sh->cold || cache->sh->loading) {
|
|
1227 return;
|
|
1228 }
|
|
1229
|
|
1230 if (!ngx_atomic_cmp_set(&cache->sh->loading, 0, ngx_pid)) {
|
|
1231 return;
|
|
1232 }
|
|
1233
|
|
1234 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
|
|
1235 "http file cache loader");
|
|
1236
|
|
1237 tree.init_handler = NULL;
|
|
1238 tree.file_handler = ngx_http_file_cache_manage_file;
|
|
1239 tree.pre_tree_handler = ngx_http_file_cache_noop;
|
|
1240 tree.post_tree_handler = ngx_http_file_cache_noop;
|
|
1241 tree.spec_handler = ngx_http_file_cache_delete_file;
|
|
1242 tree.data = cache;
|
|
1243 tree.alloc = 0;
|
|
1244 tree.log = ngx_cycle->log;
|
|
1245
|
|
1246 cache->last = ngx_current_msec;
|
|
1247 cache->files = 0;
|
|
1248
|
|
1249 if (ngx_walk_tree(&tree, &cache->path->name) == NGX_ABORT) {
|
|
1250 cache->sh->loading = 0;
|
|
1251 return;
|
|
1252 }
|
|
1253
|
|
1254 cache->sh->cold = 0;
|
|
1255 cache->sh->loading = 0;
|
|
1256
|
|
1257 ngx_log_error(NGX_LOG_NOTICE, ngx_cycle->log, 0,
|
|
1258 "http file cache: %V %.3fM, bsize: %uz",
|
|
1259 &cache->path->name,
|
|
1260 ((double) cache->sh->size * cache->bsize) / (1024 * 1024),
|
|
1261 cache->bsize);
|
|
1262 }
|
|
1263
|
|
1264
|
464
|
1265 static ngx_int_t
|
468
|
1266 ngx_http_file_cache_noop(ngx_tree_ctx_t *ctx, ngx_str_t *path)
|
464
|
1267 {
|
|
1268 return NGX_OK;
|
|
1269 }
|
|
1270
|
|
1271
|
|
1272 static ngx_int_t
|
468
|
1273 ngx_http_file_cache_manage_file(ngx_tree_ctx_t *ctx, ngx_str_t *path)
|
464
|
1274 {
|
634
|
1275 ngx_msec_t elapsed;
|
468
|
1276 ngx_http_file_cache_t *cache;
|
|
1277
|
|
1278 cache = ctx->data;
|
|
1279
|
|
1280 if (ngx_http_file_cache_add_file(ctx, path) != NGX_OK) {
|
|
1281 (void) ngx_http_file_cache_delete_file(ctx, path);
|
|
1282 }
|
|
1283
|
634
|
1284 if (++cache->files >= cache->loader_files) {
|
|
1285 ngx_http_file_cache_loader_sleep(cache);
|
|
1286
|
|
1287 } else {
|
|
1288 ngx_time_update();
|
|
1289
|
|
1290 elapsed = ngx_abs((ngx_msec_int_t) (ngx_current_msec - cache->last));
|
|
1291
|
|
1292 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
|
|
1293 "http file cache loader time elapsed: %M", elapsed);
|
|
1294
|
|
1295 if (elapsed >= cache->loader_threshold) {
|
|
1296 ngx_http_file_cache_loader_sleep(cache);
|
|
1297 }
|
|
1298 }
|
|
1299
|
|
1300 return (ngx_quit || ngx_terminate) ? NGX_ABORT : NGX_OK;
|
|
1301 }
|
|
1302
|
|
1303
|
|
1304 static void
|
|
1305 ngx_http_file_cache_loader_sleep(ngx_http_file_cache_t *cache)
|
|
1306 {
|
|
1307 ngx_msleep(cache->loader_sleep);
|
|
1308
|
|
1309 ngx_time_update();
|
|
1310
|
|
1311 cache->last = ngx_current_msec;
|
|
1312 cache->files = 0;
|
468
|
1313 }
|
|
1314
|
464
|
1315
|
468
|
1316 static ngx_int_t
|
|
1317 ngx_http_file_cache_add_file(ngx_tree_ctx_t *ctx, ngx_str_t *name)
|
|
1318 {
|
632
|
1319 u_char *p;
|
|
1320 ngx_int_t n;
|
|
1321 ngx_uint_t i;
|
|
1322 ngx_http_cache_t c;
|
|
1323 ngx_http_file_cache_t *cache;
|
468
|
1324
|
|
1325 if (name->len < 2 * NGX_HTTP_CACHE_KEY_LEN) {
|
|
1326 return NGX_ERROR;
|
464
|
1327 }
|
|
1328
|
632
|
1329 if (ctx->size < (off_t) sizeof(ngx_http_file_cache_header_t)) {
|
468
|
1330 ngx_log_error(NGX_LOG_CRIT, ctx->log, 0,
|
|
1331 "cache file \"%s\" is too small", name->data);
|
|
1332 return NGX_ERROR;
|
|
1333 }
|
|
1334
|
632
|
1335 ngx_memzero(&c, sizeof(ngx_http_cache_t));
|
622
|
1336 cache = ctx->data;
|
|
1337
|
632
|
1338 c.length = ctx->size;
|
|
1339 c.fs_size = (ctx->fs_size + cache->bsize - 1) / cache->bsize;
|
468
|
1340
|
|
1341 p = &name->data[name->len - 2 * NGX_HTTP_CACHE_KEY_LEN];
|
464
|
1342
|
|
1343 for (i = 0; i < NGX_HTTP_CACHE_KEY_LEN; i++) {
|
|
1344 n = ngx_hextoi(p, 2);
|
|
1345
|
|
1346 if (n == NGX_ERROR) {
|
468
|
1347 return NGX_ERROR;
|
464
|
1348 }
|
|
1349
|
|
1350 p += 2;
|
|
1351
|
468
|
1352 c.key[i] = (u_char) n;
|
464
|
1353 }
|
|
1354
|
468
|
1355 return ngx_http_file_cache_add(cache, &c);
|
|
1356 }
|
|
1357
|
|
1358
|
|
1359 static ngx_int_t
|
|
1360 ngx_http_file_cache_add(ngx_http_file_cache_t *cache, ngx_http_cache_t *c)
|
|
1361 {
|
|
1362 ngx_http_file_cache_node_t *fcn;
|
|
1363
|
464
|
1364 ngx_shmtx_lock(&cache->shpool->mutex);
|
|
1365
|
468
|
1366 fcn = ngx_http_file_cache_lookup(cache, c->key);
|
|
1367
|
|
1368 if (fcn == NULL) {
|
|
1369
|
|
1370 fcn = ngx_slab_alloc_locked(cache->shpool,
|
|
1371 sizeof(ngx_http_file_cache_node_t));
|
|
1372 if (fcn == NULL) {
|
|
1373 ngx_shmtx_unlock(&cache->shpool->mutex);
|
|
1374 return NGX_ERROR;
|
|
1375 }
|
|
1376
|
|
1377 ngx_memcpy((u_char *) &fcn->node.key, c->key, sizeof(ngx_rbtree_key_t));
|
|
1378
|
|
1379 ngx_memcpy(fcn->key, &c->key[sizeof(ngx_rbtree_key_t)],
|
|
1380 NGX_HTTP_CACHE_KEY_LEN - sizeof(ngx_rbtree_key_t));
|
|
1381
|
480
|
1382 ngx_rbtree_insert(&cache->sh->rbtree, &fcn->node);
|
468
|
1383
|
|
1384 fcn->uses = 1;
|
|
1385 fcn->count = 0;
|
632
|
1386 fcn->valid_msec = 0;
|
468
|
1387 fcn->error = 0;
|
|
1388 fcn->exists = 1;
|
594
|
1389 fcn->updating = 0;
|
596
|
1390 fcn->deleting = 0;
|
632
|
1391 fcn->uniq = 0;
|
|
1392 fcn->valid_sec = 0;
|
|
1393 fcn->body_start = 0;
|
622
|
1394 fcn->fs_size = c->fs_size;
|
468
|
1395
|
622
|
1396 cache->sh->size += c->fs_size;
|
468
|
1397
|
|
1398 } else {
|
|
1399 ngx_queue_remove(&fcn->queue);
|
|
1400 }
|
|
1401
|
|
1402 fcn->expire = ngx_time() + cache->inactive;
|
|
1403
|
480
|
1404 ngx_queue_insert_head(&cache->sh->queue, &fcn->queue);
|
464
|
1405
|
|
1406 ngx_shmtx_unlock(&cache->shpool->mutex);
|
|
1407
|
468
|
1408 return NGX_OK;
|
|
1409 }
|
|
1410
|
464
|
1411
|
468
|
1412 static ngx_int_t
|
|
1413 ngx_http_file_cache_delete_file(ngx_tree_ctx_t *ctx, ngx_str_t *path)
|
|
1414 {
|
464
|
1415 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0,
|
468
|
1416 "http file cache delete: \"%s\"", path->data);
|
464
|
1417
|
|
1418 if (ngx_delete_file(path->data) == NGX_FILE_ERROR) {
|
|
1419 ngx_log_error(NGX_LOG_CRIT, ctx->log, ngx_errno,
|
|
1420 ngx_delete_file_n " \"%s\" failed", path->data);
|
|
1421 }
|
26
|
1422
|
0
|
1423 return NGX_OK;
|
|
1424 }
|
|
1425
|
|
1426
|
464
|
1427 time_t
|
|
1428 ngx_http_file_cache_valid(ngx_array_t *cache_valid, ngx_uint_t status)
|
0
|
1429 {
|
464
|
1430 ngx_uint_t i;
|
|
1431 ngx_http_cache_valid_t *valid;
|
0
|
1432
|
470
|
1433 if (cache_valid == NULL) {
|
|
1434 return 0;
|
|
1435 }
|
|
1436
|
464
|
1437 valid = cache_valid->elts;
|
|
1438 for (i = 0; i < cache_valid->nelts; i++) {
|
0
|
1439
|
464
|
1440 if (valid[i].status == 0) {
|
|
1441 return valid[i].valid;
|
0
|
1442 }
|
|
1443
|
464
|
1444 if (valid[i].status == status) {
|
|
1445 return valid[i].valid;
|
0
|
1446 }
|
464
|
1447 }
|
0
|
1448
|
464
|
1449 return 0;
|
0
|
1450 }
|
|
1451
|
|
1452
|
464
|
1453 char *
|
|
1454 ngx_http_file_cache_set_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
1455 {
|
468
|
1456 off_t max_size;
|
464
|
1457 u_char *last, *p;
|
468
|
1458 time_t inactive;
|
464
|
1459 ssize_t size;
|
|
1460 ngx_str_t s, name, *value;
|
632
|
1461 ngx_int_t loader_files, loader_sleep, loader_threshold;
|
464
|
1462 ngx_uint_t i, n;
|
|
1463 ngx_http_file_cache_t *cache;
|
|
1464
|
|
1465 cache = ngx_pcalloc(cf->pool, sizeof(ngx_http_file_cache_t));
|
|
1466 if (cache == NULL) {
|
|
1467 return NGX_CONF_ERROR;
|
|
1468 }
|
|
1469
|
|
1470 cache->path = ngx_pcalloc(cf->pool, sizeof(ngx_path_t));
|
|
1471 if (cache->path == NULL) {
|
|
1472 return NGX_CONF_ERROR;
|
|
1473 }
|
|
1474
|
|
1475 inactive = 600;
|
632
|
1476 loader_files = 100;
|
|
1477 loader_sleep = 50;
|
|
1478 loader_threshold = 200;
|
464
|
1479
|
|
1480 name.len = 0;
|
|
1481 size = 0;
|
468
|
1482 max_size = NGX_MAX_OFF_T_VALUE;
|
464
|
1483
|
|
1484 value = cf->args->elts;
|
|
1485
|
|
1486 cache->path->name = value[1];
|
|
1487
|
|
1488 if (cache->path->name.data[cache->path->name.len - 1] == '/') {
|
|
1489 cache->path->name.len--;
|
|
1490 }
|
|
1491
|
|
1492 if (ngx_conf_full_name(cf->cycle, &cache->path->name, 0) != NGX_OK) {
|
|
1493 return NGX_CONF_ERROR;
|
|
1494 }
|
|
1495
|
|
1496 for (i = 2; i < cf->args->nelts; i++) {
|
|
1497
|
|
1498 if (ngx_strncmp(value[i].data, "levels=", 7) == 0) {
|
|
1499
|
|
1500 p = value[i].data + 7;
|
|
1501 last = value[i].data + value[i].len;
|
|
1502
|
472
|
1503 for (n = 0; n < 3 && p < last; n++) {
|
464
|
1504
|
472
|
1505 if (*p > '0' && *p < '3') {
|
464
|
1506
|
|
1507 cache->path->level[n] = *p++ - '0';
|
|
1508 cache->path->len += cache->path->level[n] + 1;
|
|
1509
|
|
1510 if (p == last) {
|
|
1511 break;
|
|
1512 }
|
|
1513
|
472
|
1514 if (*p++ == ':' && n < 2 && p != last) {
|
464
|
1515 continue;
|
|
1516 }
|
472
|
1517
|
|
1518 goto invalid_levels;
|
464
|
1519 }
|
|
1520
|
|
1521 goto invalid_levels;
|
|
1522 }
|
|
1523
|
|
1524 if (cache->path->len < 10 + 3) {
|
|
1525 continue;
|
|
1526 }
|
|
1527
|
|
1528 invalid_levels:
|
|
1529
|
|
1530 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1531 "invalid \"levels\" \"%V\"", &value[i]);
|
|
1532 return NGX_CONF_ERROR;
|
|
1533 }
|
|
1534
|
|
1535 if (ngx_strncmp(value[i].data, "keys_zone=", 10) == 0) {
|
|
1536
|
|
1537 name.data = value[i].data + 10;
|
|
1538
|
|
1539 p = (u_char *) ngx_strchr(name.data, ':');
|
|
1540
|
|
1541 if (p) {
|
480
|
1542 *p = '\0';
|
|
1543
|
464
|
1544 name.len = p - name.data;
|
|
1545
|
|
1546 p++;
|
|
1547
|
|
1548 s.len = value[i].data + value[i].len - p;
|
|
1549 s.data = p;
|
|
1550
|
|
1551 size = ngx_parse_size(&s);
|
|
1552 if (size > 8191) {
|
|
1553 continue;
|
|
1554 }
|
|
1555 }
|
|
1556
|
|
1557 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1558 "invalid keys zone size \"%V\"", &value[i]);
|
|
1559 return NGX_CONF_ERROR;
|
|
1560 }
|
|
1561
|
|
1562 if (ngx_strncmp(value[i].data, "inactive=", 9) == 0) {
|
|
1563
|
|
1564 s.len = value[i].len - 9;
|
|
1565 s.data = value[i].data + 9;
|
|
1566
|
|
1567 inactive = ngx_parse_time(&s, 1);
|
|
1568 if (inactive < 0) {
|
|
1569 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1570 "invalid inactive value \"%V\"", &value[i]);
|
|
1571 return NGX_CONF_ERROR;
|
|
1572 }
|
|
1573
|
|
1574 continue;
|
|
1575 }
|
|
1576
|
468
|
1577 if (ngx_strncmp(value[i].data, "max_size=", 9) == 0) {
|
464
|
1578
|
468
|
1579 s.len = value[i].len - 9;
|
|
1580 s.data = value[i].data + 9;
|
464
|
1581
|
468
|
1582 max_size = ngx_parse_offset(&s);
|
|
1583 if (max_size < 0) {
|
464
|
1584 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
468
|
1585 "invalid max_size value \"%V\"", &value[i]);
|
464
|
1586 return NGX_CONF_ERROR;
|
|
1587 }
|
|
1588
|
|
1589 continue;
|
|
1590 }
|
|
1591
|
632
|
1592 if (ngx_strncmp(value[i].data, "loader_files=", 13) == 0) {
|
|
1593
|
|
1594 loader_files = ngx_atoi(value[i].data + 13, value[i].len - 13);
|
|
1595 if (loader_files == NGX_ERROR) {
|
|
1596 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1597 "invalid loader_files value \"%V\"", &value[i]);
|
|
1598 return NGX_CONF_ERROR;
|
|
1599 }
|
|
1600
|
|
1601 continue;
|
|
1602 }
|
|
1603
|
|
1604 if (ngx_strncmp(value[i].data, "loader_sleep=", 13) == 0) {
|
|
1605
|
|
1606 s.len = value[i].len - 13;
|
|
1607 s.data = value[i].data + 13;
|
|
1608
|
|
1609 loader_sleep = ngx_parse_time(&s, 0);
|
|
1610 if (loader_sleep < 0) {
|
|
1611 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1612 "invalid loader_sleep value \"%V\"", &value[i]);
|
|
1613 return NGX_CONF_ERROR;
|
|
1614 }
|
|
1615
|
|
1616 continue;
|
|
1617 }
|
|
1618
|
|
1619 if (ngx_strncmp(value[i].data, "loader_threshold=", 17) == 0) {
|
|
1620
|
|
1621 s.len = value[i].len - 17;
|
|
1622 s.data = value[i].data + 17;
|
|
1623
|
|
1624 loader_threshold = ngx_parse_time(&s, 0);
|
|
1625 if (loader_threshold < 0) {
|
|
1626 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1627 "invalid loader_threshold value \"%V\"", &value[i]);
|
|
1628 return NGX_CONF_ERROR;
|
|
1629 }
|
|
1630
|
|
1631 continue;
|
|
1632 }
|
|
1633
|
464
|
1634 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1635 "invalid parameter \"%V\"", &value[i]);
|
|
1636 return NGX_CONF_ERROR;
|
|
1637 }
|
|
1638
|
|
1639 if (name.len == 0 || size == 0) {
|
|
1640 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1641 "\"%V\" must have \"keys_zone\" parameter",
|
|
1642 &cmd->name);
|
|
1643 return NGX_CONF_ERROR;
|
|
1644 }
|
|
1645
|
468
|
1646 cache->path->manager = ngx_http_file_cache_manager;
|
514
|
1647 cache->path->loader = ngx_http_file_cache_loader;
|
464
|
1648 cache->path->data = cache;
|
632
|
1649 cache->path->conf_file = cf->conf_file->file.name.data;
|
|
1650 cache->path->line = cf->conf_file->line;
|
|
1651 cache->loader_files = loader_files;
|
|
1652 cache->loader_sleep = (ngx_msec_t) loader_sleep;
|
|
1653 cache->loader_threshold = (ngx_msec_t) loader_threshold;
|
464
|
1654
|
|
1655 if (ngx_add_path(cf, &cache->path) != NGX_OK) {
|
|
1656 return NGX_CONF_ERROR;
|
|
1657 }
|
|
1658
|
|
1659 cache->shm_zone = ngx_shared_memory_add(cf, &name, size, cmd->post);
|
|
1660 if (cache->shm_zone == NULL) {
|
|
1661 return NGX_CONF_ERROR;
|
|
1662 }
|
|
1663
|
|
1664 if (cache->shm_zone->data) {
|
|
1665 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1666 "duplicate zone \"%V\"", &name);
|
|
1667 return NGX_CONF_ERROR;
|
|
1668 }
|
|
1669
|
|
1670
|
|
1671 cache->shm_zone->init = ngx_http_file_cache_init;
|
|
1672 cache->shm_zone->data = cache;
|
|
1673
|
|
1674 cache->inactive = inactive;
|
468
|
1675 cache->max_size = max_size;
|
464
|
1676
|
|
1677 return NGX_CONF_OK;
|
|
1678 }
|
26
|
1679
|
|
1680
|
464
|
1681 char *
|
|
1682 ngx_http_file_cache_valid_set_slot(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
1683 void *conf)
|
0
|
1684 {
|
464
|
1685 char *p = conf;
|
0
|
1686
|
464
|
1687 time_t valid;
|
|
1688 ngx_str_t *value;
|
|
1689 ngx_uint_t i, n, status;
|
|
1690 ngx_array_t **a;
|
|
1691 ngx_http_cache_valid_t *v;
|
|
1692 static ngx_uint_t statuses[] = { 200, 301, 302 };
|
26
|
1693
|
464
|
1694 a = (ngx_array_t **) (p + cmd->offset);
|
0
|
1695
|
464
|
1696 if (*a == NGX_CONF_UNSET_PTR) {
|
|
1697 *a = ngx_array_create(cf->pool, 1, sizeof(ngx_http_cache_valid_t));
|
|
1698 if (*a == NULL) {
|
|
1699 return NGX_CONF_ERROR;
|
|
1700 }
|
|
1701 }
|
0
|
1702
|
464
|
1703 value = cf->args->elts;
|
|
1704 n = cf->args->nelts - 1;
|
0
|
1705
|
464
|
1706 valid = ngx_parse_time(&value[n], 1);
|
|
1707 if (valid < 0) {
|
|
1708 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1709 "invalid time value \"%V\"", &value[n]);
|
|
1710 return NGX_CONF_ERROR;
|
0
|
1711 }
|
|
1712
|
464
|
1713 if (n == 1) {
|
|
1714
|
|
1715 for (i = 0; i < 3; i++) {
|
|
1716 v = ngx_array_push(*a);
|
|
1717 if (v == NULL) {
|
|
1718 return NGX_CONF_ERROR;
|
|
1719 }
|
|
1720
|
|
1721 v->status = statuses[i];
|
|
1722 v->valid = valid;
|
|
1723 }
|
|
1724
|
|
1725 return NGX_CONF_OK;
|
0
|
1726 }
|
|
1727
|
464
|
1728 for (i = 1; i < n; i++) {
|
|
1729
|
|
1730 if (ngx_strcmp(value[i].data, "any") == 0) {
|
|
1731
|
|
1732 status = 0;
|
|
1733
|
|
1734 } else {
|
0
|
1735
|
464
|
1736 status = ngx_atoi(value[i].data, value[i].len);
|
|
1737 if (status < 100) {
|
|
1738 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1739 "invalid status \"%V\"", &value[i]);
|
|
1740 return NGX_CONF_ERROR;
|
|
1741 }
|
|
1742 }
|
|
1743
|
|
1744 v = ngx_array_push(*a);
|
|
1745 if (v == NULL) {
|
|
1746 return NGX_CONF_ERROR;
|
|
1747 }
|
|
1748
|
|
1749 v->status = status;
|
|
1750 v->valid = valid;
|
|
1751 }
|
|
1752
|
|
1753 return NGX_CONF_OK;
|
0
|
1754 }
|
572
|
1755
|
|
1756
|
|
1757 ngx_int_t
|
|
1758 ngx_http_cache(ngx_http_request_t *r, ngx_array_t *no_cache)
|
|
1759 {
|
|
1760 ngx_str_t val;
|
|
1761 ngx_uint_t i;
|
|
1762 ngx_http_complex_value_t *cv;
|
|
1763
|
|
1764 cv = no_cache->elts;
|
|
1765
|
|
1766 for (i = 0; i < no_cache->nelts; i++) {
|
|
1767 if (ngx_http_complex_value(r, &cv[i], &val) != NGX_OK) {
|
|
1768 return NGX_ERROR;
|
|
1769 }
|
|
1770
|
|
1771 if (val.len && val.data[0] != '0') {
|
|
1772 return NGX_DECLINED;
|
|
1773 }
|
|
1774 }
|
|
1775
|
|
1776 return NGX_OK;
|
|
1777 }
|
|
1778
|
|
1779
|
|
1780 char *
|
|
1781 ngx_http_no_cache_set_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
1782 {
|
|
1783 char *p = conf;
|
|
1784
|
|
1785 ngx_str_t *value;
|
|
1786 ngx_uint_t i;
|
|
1787 ngx_array_t **a;
|
|
1788 ngx_http_complex_value_t *cv;
|
|
1789 ngx_http_compile_complex_value_t ccv;
|
|
1790
|
|
1791 a = (ngx_array_t **) (p + cmd->offset);
|
|
1792
|
|
1793 if (*a == NGX_CONF_UNSET_PTR) {
|
|
1794 *a = ngx_array_create(cf->pool, 1, sizeof(ngx_http_complex_value_t));
|
|
1795 if (*a == NULL) {
|
|
1796 return NGX_CONF_ERROR;
|
|
1797 }
|
|
1798 }
|
|
1799
|
|
1800 value = cf->args->elts;
|
|
1801
|
|
1802 for (i = 1; i < cf->args->nelts; i++) {
|
|
1803 cv = ngx_array_push(*a);
|
|
1804 if (cv == NULL) {
|
|
1805 return NGX_CONF_ERROR;
|
|
1806 }
|
|
1807
|
|
1808 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
|
|
1809
|
|
1810 ccv.cf = cf;
|
|
1811 ccv.value = &value[i];
|
|
1812 ccv.complex_value = cv;
|
|
1813
|
|
1814 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
|
|
1815 return NGX_CONF_ERROR;
|
|
1816 }
|
|
1817 }
|
|
1818
|
|
1819 return NGX_CONF_OK;
|
|
1820 }
|