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