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