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
|
|
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
|
468
|
1264 ngx_http_file_cache_noop(ngx_tree_ctx_t *ctx, ngx_str_t *path)
|
464
|
1265 {
|
|
1266 return NGX_OK;
|
|
1267 }
|
|
1268
|
|
1269
|
|
1270 static ngx_int_t
|
468
|
1271 ngx_http_file_cache_manage_file(ngx_tree_ctx_t *ctx, ngx_str_t *path)
|
464
|
1272 {
|
634
|
1273 ngx_msec_t elapsed;
|
468
|
1274 ngx_http_file_cache_t *cache;
|
|
1275
|
|
1276 cache = ctx->data;
|
|
1277
|
|
1278 if (ngx_http_file_cache_add_file(ctx, path) != NGX_OK) {
|
|
1279 (void) ngx_http_file_cache_delete_file(ctx, path);
|
|
1280 }
|
|
1281
|
634
|
1282 if (++cache->files >= cache->loader_files) {
|
|
1283 ngx_http_file_cache_loader_sleep(cache);
|
|
1284
|
|
1285 } else {
|
|
1286 ngx_time_update();
|
|
1287
|
|
1288 elapsed = ngx_abs((ngx_msec_int_t) (ngx_current_msec - cache->last));
|
|
1289
|
|
1290 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
|
|
1291 "http file cache loader time elapsed: %M", elapsed);
|
|
1292
|
|
1293 if (elapsed >= cache->loader_threshold) {
|
|
1294 ngx_http_file_cache_loader_sleep(cache);
|
|
1295 }
|
|
1296 }
|
|
1297
|
|
1298 return (ngx_quit || ngx_terminate) ? NGX_ABORT : NGX_OK;
|
|
1299 }
|
|
1300
|
|
1301
|
|
1302 static void
|
|
1303 ngx_http_file_cache_loader_sleep(ngx_http_file_cache_t *cache)
|
|
1304 {
|
|
1305 ngx_msleep(cache->loader_sleep);
|
|
1306
|
|
1307 ngx_time_update();
|
|
1308
|
|
1309 cache->last = ngx_current_msec;
|
|
1310 cache->files = 0;
|
468
|
1311 }
|
|
1312
|
464
|
1313
|
468
|
1314 static ngx_int_t
|
|
1315 ngx_http_file_cache_add_file(ngx_tree_ctx_t *ctx, ngx_str_t *name)
|
|
1316 {
|
632
|
1317 u_char *p;
|
|
1318 ngx_int_t n;
|
|
1319 ngx_uint_t i;
|
|
1320 ngx_http_cache_t c;
|
|
1321 ngx_http_file_cache_t *cache;
|
468
|
1322
|
|
1323 if (name->len < 2 * NGX_HTTP_CACHE_KEY_LEN) {
|
|
1324 return NGX_ERROR;
|
464
|
1325 }
|
|
1326
|
632
|
1327 if (ctx->size < (off_t) sizeof(ngx_http_file_cache_header_t)) {
|
468
|
1328 ngx_log_error(NGX_LOG_CRIT, ctx->log, 0,
|
|
1329 "cache file \"%s\" is too small", name->data);
|
|
1330 return NGX_ERROR;
|
|
1331 }
|
|
1332
|
632
|
1333 ngx_memzero(&c, sizeof(ngx_http_cache_t));
|
622
|
1334 cache = ctx->data;
|
|
1335
|
632
|
1336 c.length = ctx->size;
|
|
1337 c.fs_size = (ctx->fs_size + cache->bsize - 1) / cache->bsize;
|
468
|
1338
|
|
1339 p = &name->data[name->len - 2 * NGX_HTTP_CACHE_KEY_LEN];
|
464
|
1340
|
|
1341 for (i = 0; i < NGX_HTTP_CACHE_KEY_LEN; i++) {
|
|
1342 n = ngx_hextoi(p, 2);
|
|
1343
|
|
1344 if (n == NGX_ERROR) {
|
468
|
1345 return NGX_ERROR;
|
464
|
1346 }
|
|
1347
|
|
1348 p += 2;
|
|
1349
|
468
|
1350 c.key[i] = (u_char) n;
|
464
|
1351 }
|
|
1352
|
468
|
1353 return ngx_http_file_cache_add(cache, &c);
|
|
1354 }
|
|
1355
|
|
1356
|
|
1357 static ngx_int_t
|
|
1358 ngx_http_file_cache_add(ngx_http_file_cache_t *cache, ngx_http_cache_t *c)
|
|
1359 {
|
|
1360 ngx_http_file_cache_node_t *fcn;
|
|
1361
|
464
|
1362 ngx_shmtx_lock(&cache->shpool->mutex);
|
|
1363
|
468
|
1364 fcn = ngx_http_file_cache_lookup(cache, c->key);
|
|
1365
|
|
1366 if (fcn == NULL) {
|
|
1367
|
|
1368 fcn = ngx_slab_alloc_locked(cache->shpool,
|
|
1369 sizeof(ngx_http_file_cache_node_t));
|
|
1370 if (fcn == NULL) {
|
|
1371 ngx_shmtx_unlock(&cache->shpool->mutex);
|
|
1372 return NGX_ERROR;
|
|
1373 }
|
|
1374
|
|
1375 ngx_memcpy((u_char *) &fcn->node.key, c->key, sizeof(ngx_rbtree_key_t));
|
|
1376
|
|
1377 ngx_memcpy(fcn->key, &c->key[sizeof(ngx_rbtree_key_t)],
|
|
1378 NGX_HTTP_CACHE_KEY_LEN - sizeof(ngx_rbtree_key_t));
|
|
1379
|
480
|
1380 ngx_rbtree_insert(&cache->sh->rbtree, &fcn->node);
|
468
|
1381
|
|
1382 fcn->uses = 1;
|
|
1383 fcn->count = 0;
|
632
|
1384 fcn->valid_msec = 0;
|
468
|
1385 fcn->error = 0;
|
|
1386 fcn->exists = 1;
|
594
|
1387 fcn->updating = 0;
|
596
|
1388 fcn->deleting = 0;
|
632
|
1389 fcn->uniq = 0;
|
|
1390 fcn->valid_sec = 0;
|
|
1391 fcn->body_start = 0;
|
622
|
1392 fcn->fs_size = c->fs_size;
|
468
|
1393
|
622
|
1394 cache->sh->size += c->fs_size;
|
468
|
1395
|
|
1396 } else {
|
|
1397 ngx_queue_remove(&fcn->queue);
|
|
1398 }
|
|
1399
|
|
1400 fcn->expire = ngx_time() + cache->inactive;
|
|
1401
|
480
|
1402 ngx_queue_insert_head(&cache->sh->queue, &fcn->queue);
|
464
|
1403
|
|
1404 ngx_shmtx_unlock(&cache->shpool->mutex);
|
|
1405
|
468
|
1406 return NGX_OK;
|
|
1407 }
|
|
1408
|
464
|
1409
|
468
|
1410 static ngx_int_t
|
|
1411 ngx_http_file_cache_delete_file(ngx_tree_ctx_t *ctx, ngx_str_t *path)
|
|
1412 {
|
464
|
1413 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0,
|
468
|
1414 "http file cache delete: \"%s\"", path->data);
|
464
|
1415
|
|
1416 if (ngx_delete_file(path->data) == NGX_FILE_ERROR) {
|
|
1417 ngx_log_error(NGX_LOG_CRIT, ctx->log, ngx_errno,
|
|
1418 ngx_delete_file_n " \"%s\" failed", path->data);
|
|
1419 }
|
26
|
1420
|
0
|
1421 return NGX_OK;
|
|
1422 }
|
|
1423
|
|
1424
|
464
|
1425 time_t
|
|
1426 ngx_http_file_cache_valid(ngx_array_t *cache_valid, ngx_uint_t status)
|
0
|
1427 {
|
464
|
1428 ngx_uint_t i;
|
|
1429 ngx_http_cache_valid_t *valid;
|
0
|
1430
|
470
|
1431 if (cache_valid == NULL) {
|
|
1432 return 0;
|
|
1433 }
|
|
1434
|
464
|
1435 valid = cache_valid->elts;
|
|
1436 for (i = 0; i < cache_valid->nelts; i++) {
|
0
|
1437
|
464
|
1438 if (valid[i].status == 0) {
|
|
1439 return valid[i].valid;
|
0
|
1440 }
|
|
1441
|
464
|
1442 if (valid[i].status == status) {
|
|
1443 return valid[i].valid;
|
0
|
1444 }
|
464
|
1445 }
|
0
|
1446
|
464
|
1447 return 0;
|
0
|
1448 }
|
|
1449
|
|
1450
|
464
|
1451 char *
|
|
1452 ngx_http_file_cache_set_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
1453 {
|
468
|
1454 off_t max_size;
|
464
|
1455 u_char *last, *p;
|
468
|
1456 time_t inactive;
|
464
|
1457 ssize_t size;
|
|
1458 ngx_str_t s, name, *value;
|
632
|
1459 ngx_int_t loader_files, loader_sleep, loader_threshold;
|
464
|
1460 ngx_uint_t i, n;
|
|
1461 ngx_http_file_cache_t *cache;
|
|
1462
|
|
1463 cache = ngx_pcalloc(cf->pool, sizeof(ngx_http_file_cache_t));
|
|
1464 if (cache == NULL) {
|
|
1465 return NGX_CONF_ERROR;
|
|
1466 }
|
|
1467
|
|
1468 cache->path = ngx_pcalloc(cf->pool, sizeof(ngx_path_t));
|
|
1469 if (cache->path == NULL) {
|
|
1470 return NGX_CONF_ERROR;
|
|
1471 }
|
|
1472
|
|
1473 inactive = 600;
|
632
|
1474 loader_files = 100;
|
|
1475 loader_sleep = 50;
|
|
1476 loader_threshold = 200;
|
464
|
1477
|
|
1478 name.len = 0;
|
|
1479 size = 0;
|
468
|
1480 max_size = NGX_MAX_OFF_T_VALUE;
|
464
|
1481
|
|
1482 value = cf->args->elts;
|
|
1483
|
|
1484 cache->path->name = value[1];
|
|
1485
|
|
1486 if (cache->path->name.data[cache->path->name.len - 1] == '/') {
|
|
1487 cache->path->name.len--;
|
|
1488 }
|
|
1489
|
|
1490 if (ngx_conf_full_name(cf->cycle, &cache->path->name, 0) != NGX_OK) {
|
|
1491 return NGX_CONF_ERROR;
|
|
1492 }
|
|
1493
|
|
1494 for (i = 2; i < cf->args->nelts; i++) {
|
|
1495
|
|
1496 if (ngx_strncmp(value[i].data, "levels=", 7) == 0) {
|
|
1497
|
|
1498 p = value[i].data + 7;
|
|
1499 last = value[i].data + value[i].len;
|
|
1500
|
472
|
1501 for (n = 0; n < 3 && p < last; n++) {
|
464
|
1502
|
472
|
1503 if (*p > '0' && *p < '3') {
|
464
|
1504
|
|
1505 cache->path->level[n] = *p++ - '0';
|
|
1506 cache->path->len += cache->path->level[n] + 1;
|
|
1507
|
|
1508 if (p == last) {
|
|
1509 break;
|
|
1510 }
|
|
1511
|
472
|
1512 if (*p++ == ':' && n < 2 && p != last) {
|
464
|
1513 continue;
|
|
1514 }
|
472
|
1515
|
|
1516 goto invalid_levels;
|
464
|
1517 }
|
|
1518
|
|
1519 goto invalid_levels;
|
|
1520 }
|
|
1521
|
|
1522 if (cache->path->len < 10 + 3) {
|
|
1523 continue;
|
|
1524 }
|
|
1525
|
|
1526 invalid_levels:
|
|
1527
|
|
1528 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1529 "invalid \"levels\" \"%V\"", &value[i]);
|
|
1530 return NGX_CONF_ERROR;
|
|
1531 }
|
|
1532
|
|
1533 if (ngx_strncmp(value[i].data, "keys_zone=", 10) == 0) {
|
|
1534
|
|
1535 name.data = value[i].data + 10;
|
|
1536
|
|
1537 p = (u_char *) ngx_strchr(name.data, ':');
|
|
1538
|
|
1539 if (p) {
|
480
|
1540 *p = '\0';
|
|
1541
|
464
|
1542 name.len = p - name.data;
|
|
1543
|
|
1544 p++;
|
|
1545
|
|
1546 s.len = value[i].data + value[i].len - p;
|
|
1547 s.data = p;
|
|
1548
|
|
1549 size = ngx_parse_size(&s);
|
|
1550 if (size > 8191) {
|
|
1551 continue;
|
|
1552 }
|
|
1553 }
|
|
1554
|
|
1555 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1556 "invalid keys zone size \"%V\"", &value[i]);
|
|
1557 return NGX_CONF_ERROR;
|
|
1558 }
|
|
1559
|
|
1560 if (ngx_strncmp(value[i].data, "inactive=", 9) == 0) {
|
|
1561
|
|
1562 s.len = value[i].len - 9;
|
|
1563 s.data = value[i].data + 9;
|
|
1564
|
|
1565 inactive = ngx_parse_time(&s, 1);
|
|
1566 if (inactive < 0) {
|
|
1567 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1568 "invalid inactive value \"%V\"", &value[i]);
|
|
1569 return NGX_CONF_ERROR;
|
|
1570 }
|
|
1571
|
|
1572 continue;
|
|
1573 }
|
|
1574
|
468
|
1575 if (ngx_strncmp(value[i].data, "max_size=", 9) == 0) {
|
464
|
1576
|
468
|
1577 s.len = value[i].len - 9;
|
|
1578 s.data = value[i].data + 9;
|
464
|
1579
|
468
|
1580 max_size = ngx_parse_offset(&s);
|
|
1581 if (max_size < 0) {
|
464
|
1582 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
468
|
1583 "invalid max_size value \"%V\"", &value[i]);
|
464
|
1584 return NGX_CONF_ERROR;
|
|
1585 }
|
|
1586
|
|
1587 continue;
|
|
1588 }
|
|
1589
|
632
|
1590 if (ngx_strncmp(value[i].data, "loader_files=", 13) == 0) {
|
|
1591
|
|
1592 loader_files = ngx_atoi(value[i].data + 13, value[i].len - 13);
|
|
1593 if (loader_files == NGX_ERROR) {
|
|
1594 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1595 "invalid loader_files value \"%V\"", &value[i]);
|
|
1596 return NGX_CONF_ERROR;
|
|
1597 }
|
|
1598
|
|
1599 continue;
|
|
1600 }
|
|
1601
|
|
1602 if (ngx_strncmp(value[i].data, "loader_sleep=", 13) == 0) {
|
|
1603
|
|
1604 s.len = value[i].len - 13;
|
|
1605 s.data = value[i].data + 13;
|
|
1606
|
|
1607 loader_sleep = ngx_parse_time(&s, 0);
|
|
1608 if (loader_sleep < 0) {
|
|
1609 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1610 "invalid loader_sleep value \"%V\"", &value[i]);
|
|
1611 return NGX_CONF_ERROR;
|
|
1612 }
|
|
1613
|
|
1614 continue;
|
|
1615 }
|
|
1616
|
|
1617 if (ngx_strncmp(value[i].data, "loader_threshold=", 17) == 0) {
|
|
1618
|
|
1619 s.len = value[i].len - 17;
|
|
1620 s.data = value[i].data + 17;
|
|
1621
|
|
1622 loader_threshold = ngx_parse_time(&s, 0);
|
|
1623 if (loader_threshold < 0) {
|
|
1624 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1625 "invalid loader_threshold value \"%V\"", &value[i]);
|
|
1626 return NGX_CONF_ERROR;
|
|
1627 }
|
|
1628
|
|
1629 continue;
|
|
1630 }
|
|
1631
|
464
|
1632 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1633 "invalid parameter \"%V\"", &value[i]);
|
|
1634 return NGX_CONF_ERROR;
|
|
1635 }
|
|
1636
|
|
1637 if (name.len == 0 || size == 0) {
|
|
1638 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1639 "\"%V\" must have \"keys_zone\" parameter",
|
|
1640 &cmd->name);
|
|
1641 return NGX_CONF_ERROR;
|
|
1642 }
|
|
1643
|
468
|
1644 cache->path->manager = ngx_http_file_cache_manager;
|
514
|
1645 cache->path->loader = ngx_http_file_cache_loader;
|
464
|
1646 cache->path->data = cache;
|
632
|
1647 cache->path->conf_file = cf->conf_file->file.name.data;
|
|
1648 cache->path->line = cf->conf_file->line;
|
|
1649 cache->loader_files = loader_files;
|
|
1650 cache->loader_sleep = (ngx_msec_t) loader_sleep;
|
|
1651 cache->loader_threshold = (ngx_msec_t) loader_threshold;
|
464
|
1652
|
|
1653 if (ngx_add_path(cf, &cache->path) != NGX_OK) {
|
|
1654 return NGX_CONF_ERROR;
|
|
1655 }
|
|
1656
|
|
1657 cache->shm_zone = ngx_shared_memory_add(cf, &name, size, cmd->post);
|
|
1658 if (cache->shm_zone == NULL) {
|
|
1659 return NGX_CONF_ERROR;
|
|
1660 }
|
|
1661
|
|
1662 if (cache->shm_zone->data) {
|
|
1663 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1664 "duplicate zone \"%V\"", &name);
|
|
1665 return NGX_CONF_ERROR;
|
|
1666 }
|
|
1667
|
|
1668
|
|
1669 cache->shm_zone->init = ngx_http_file_cache_init;
|
|
1670 cache->shm_zone->data = cache;
|
|
1671
|
|
1672 cache->inactive = inactive;
|
468
|
1673 cache->max_size = max_size;
|
464
|
1674
|
|
1675 return NGX_CONF_OK;
|
|
1676 }
|
26
|
1677
|
|
1678
|
464
|
1679 char *
|
|
1680 ngx_http_file_cache_valid_set_slot(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
1681 void *conf)
|
0
|
1682 {
|
464
|
1683 char *p = conf;
|
0
|
1684
|
464
|
1685 time_t valid;
|
|
1686 ngx_str_t *value;
|
|
1687 ngx_uint_t i, n, status;
|
|
1688 ngx_array_t **a;
|
|
1689 ngx_http_cache_valid_t *v;
|
|
1690 static ngx_uint_t statuses[] = { 200, 301, 302 };
|
26
|
1691
|
464
|
1692 a = (ngx_array_t **) (p + cmd->offset);
|
0
|
1693
|
464
|
1694 if (*a == NGX_CONF_UNSET_PTR) {
|
|
1695 *a = ngx_array_create(cf->pool, 1, sizeof(ngx_http_cache_valid_t));
|
|
1696 if (*a == NULL) {
|
|
1697 return NGX_CONF_ERROR;
|
|
1698 }
|
|
1699 }
|
0
|
1700
|
464
|
1701 value = cf->args->elts;
|
|
1702 n = cf->args->nelts - 1;
|
0
|
1703
|
464
|
1704 valid = ngx_parse_time(&value[n], 1);
|
|
1705 if (valid < 0) {
|
|
1706 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1707 "invalid time value \"%V\"", &value[n]);
|
|
1708 return NGX_CONF_ERROR;
|
0
|
1709 }
|
|
1710
|
464
|
1711 if (n == 1) {
|
|
1712
|
|
1713 for (i = 0; i < 3; i++) {
|
|
1714 v = ngx_array_push(*a);
|
|
1715 if (v == NULL) {
|
|
1716 return NGX_CONF_ERROR;
|
|
1717 }
|
|
1718
|
|
1719 v->status = statuses[i];
|
|
1720 v->valid = valid;
|
|
1721 }
|
|
1722
|
|
1723 return NGX_CONF_OK;
|
0
|
1724 }
|
|
1725
|
464
|
1726 for (i = 1; i < n; i++) {
|
|
1727
|
|
1728 if (ngx_strcmp(value[i].data, "any") == 0) {
|
|
1729
|
|
1730 status = 0;
|
|
1731
|
|
1732 } else {
|
0
|
1733
|
464
|
1734 status = ngx_atoi(value[i].data, value[i].len);
|
|
1735 if (status < 100) {
|
|
1736 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1737 "invalid status \"%V\"", &value[i]);
|
|
1738 return NGX_CONF_ERROR;
|
|
1739 }
|
|
1740 }
|
|
1741
|
|
1742 v = ngx_array_push(*a);
|
|
1743 if (v == NULL) {
|
|
1744 return NGX_CONF_ERROR;
|
|
1745 }
|
|
1746
|
|
1747 v->status = status;
|
|
1748 v->valid = valid;
|
|
1749 }
|
|
1750
|
|
1751 return NGX_CONF_OK;
|
0
|
1752 }
|
572
|
1753
|
|
1754
|
|
1755 ngx_int_t
|
|
1756 ngx_http_cache(ngx_http_request_t *r, ngx_array_t *no_cache)
|
|
1757 {
|
|
1758 ngx_str_t val;
|
|
1759 ngx_uint_t i;
|
|
1760 ngx_http_complex_value_t *cv;
|
|
1761
|
|
1762 cv = no_cache->elts;
|
|
1763
|
|
1764 for (i = 0; i < no_cache->nelts; i++) {
|
|
1765 if (ngx_http_complex_value(r, &cv[i], &val) != NGX_OK) {
|
|
1766 return NGX_ERROR;
|
|
1767 }
|
|
1768
|
|
1769 if (val.len && val.data[0] != '0') {
|
|
1770 return NGX_DECLINED;
|
|
1771 }
|
|
1772 }
|
|
1773
|
|
1774 return NGX_OK;
|
|
1775 }
|
|
1776
|
|
1777
|
|
1778 char *
|
|
1779 ngx_http_no_cache_set_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
1780 {
|
|
1781 char *p = conf;
|
|
1782
|
|
1783 ngx_str_t *value;
|
|
1784 ngx_uint_t i;
|
|
1785 ngx_array_t **a;
|
|
1786 ngx_http_complex_value_t *cv;
|
|
1787 ngx_http_compile_complex_value_t ccv;
|
|
1788
|
|
1789 a = (ngx_array_t **) (p + cmd->offset);
|
|
1790
|
|
1791 if (*a == NGX_CONF_UNSET_PTR) {
|
|
1792 *a = ngx_array_create(cf->pool, 1, sizeof(ngx_http_complex_value_t));
|
|
1793 if (*a == NULL) {
|
|
1794 return NGX_CONF_ERROR;
|
|
1795 }
|
|
1796 }
|
|
1797
|
|
1798 value = cf->args->elts;
|
|
1799
|
|
1800 for (i = 1; i < cf->args->nelts; i++) {
|
|
1801 cv = ngx_array_push(*a);
|
|
1802 if (cv == NULL) {
|
|
1803 return NGX_CONF_ERROR;
|
|
1804 }
|
|
1805
|
|
1806 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
|
|
1807
|
|
1808 ccv.cf = cf;
|
|
1809 ccv.value = &value[i];
|
|
1810 ccv.complex_value = cv;
|
|
1811
|
|
1812 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
|
|
1813 return NGX_CONF_ERROR;
|
|
1814 }
|
|
1815 }
|
|
1816
|
|
1817 return NGX_CONF_OK;
|
|
1818 }
|