comparison src/http/ngx_http_cache.c @ 171:aff0e5d32af8

nginx-0.0.1-2003-11-03-20:33:31 import
author Igor Sysoev <igor@sysoev.ru>
date Mon, 03 Nov 2003 17:33:31 +0000
parents c42be4185301
children caa57ddf6d77
comparison
equal deleted inserted replaced
170:c42be4185301 171:aff0e5d32af8
6 #include <md5.h> 6 #include <md5.h>
7 7
8 8
9 int ngx_http_cache_get_file(ngx_http_request_t *r, ngx_http_cache_ctx_t *ctx) 9 int ngx_http_cache_get_file(ngx_http_request_t *r, ngx_http_cache_ctx_t *ctx)
10 { 10 {
11 int small; 11 ssize_t n;
12 ssize_t n, len; 12 MD5_CTX md5;
13 MD5_CTX md5; 13 ngx_err_t err;
14 ngx_err_t err; 14 ngx_http_cache_file_t *h;
15 ngx_str_t key; 15
16 ngx_http_bin_cache_t *h; 16 ctx->header_size = sizeof(ngx_http_cache_file_t) + ctx->key.len + 1;
17 17
18 ctx->file.name.len = ctx->path->name.len + 1 + ctx->path->len + 32; 18 ctx->file.name.len = ctx->path->name.len + 1 + ctx->path->len + 32;
19 if (!(ctx->file.name.data = ngx_palloc(r->pool, ctx->file.name.len + 1))) { 19 if (!(ctx->file.name.data = ngx_palloc(r->pool, ctx->file.name.len + 1))) {
20 return NGX_ERROR; 20 return NGX_ERROR;
21 } 21 }
41 41
42 if (ctx->file.fd == NGX_INVALID_FILE) { 42 if (ctx->file.fd == NGX_INVALID_FILE) {
43 err = ngx_errno; 43 err = ngx_errno;
44 44
45 if (err == NGX_ENOENT || err == NGX_ENOTDIR) { 45 if (err == NGX_ENOENT || err == NGX_ENOTDIR) {
46
47 /* TODO: text size */
48
49 ctx->header.size = 2 * sizeof(ssize_t)
50 + sizeof(ngx_http_cache_header_t)
51 + ctx->key.len + 1;
52
53 return NGX_DECLINED; 46 return NGX_DECLINED;
54 } 47 }
55 48
56 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno, 49 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
57 ngx_open_file_n " \"%s\" failed", ctx->file.name.data); 50 ngx_open_file_n " \"%s\" failed", ctx->file.name.data);
63 56
64 if (n == NGX_ERROR || n == NGX_AGAIN) { 57 if (n == NGX_ERROR || n == NGX_AGAIN) {
65 return n; 58 return n;
66 } 59 }
67 60
68 len = 0; 61 if (n <= ctx->header_size) {
69 small = 1;
70
71 if (n > 1) {
72 if (ctx->buf->pos[0] == 'T') {
73 /* STUB */
74 return NGX_ERROR;
75
76 } else if (ctx->buf->pos[0] == 'B') {
77
78 len = sizeof(ngx_http_bin_cache_t);
79
80 if (n > len) {
81 h = (ngx_http_bin_cache_t *) ctx->buf->pos;
82 key.len = h->key_len;
83
84 if (n >= len + (ssize_t) key.len + 1) {
85 ctx->header = h->header;
86 key.data = h->key;
87
88 small = 0;
89 }
90 }
91
92 } else {
93 ngx_log_error(NGX_LOG_CRIT, r->connection->log, 0,
94 "unknown type of cache file \"%s\"",
95 ctx->file.name.data);
96 return NGX_ERROR;
97 }
98
99 }
100
101 if (small) {
102 ngx_log_error(NGX_LOG_CRIT, r->connection->log, 0, 62 ngx_log_error(NGX_LOG_CRIT, r->connection->log, 0,
103 "cache file \"%s\" is to small", ctx->file.name.data); 63 "cache file \"%s\" is too small", ctx->file.name.data);
104 return NGX_ERROR; 64 return NGX_ERROR;
105 } 65 }
106 66
107 if (key.len != ctx->key.len 67 h = (ngx_http_cache_file_t *) ctx->buf->pos;
108 || ngx_strncmp(key.data, ctx->key.data, key.len) != 0) 68 ctx->header = h->header;
69
70 if (h->key_len != ctx->key.len
71 || ngx_strncmp(h->key, ctx->key.data, h->key_len) != 0)
109 { 72 {
110 key.data[key.len] = '\0'; 73 h->key[h->key_len] = '\0';
111 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, 74 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
112 "md5 collision: \"%s\" and \"%s\"", 75 "md5 collision: \"%s\" and \"%s\"",
113 key.data, ctx->key.data); 76 h->key, ctx->key.data);
114 return NGX_DECLINED; 77 return NGX_DECLINED;
115 } 78 }
116 79
117 ctx->header.size = len + key.len + 1;
118 ctx->buf->last += n; 80 ctx->buf->last += n;
81
82 if (ctx->header.expires < ngx_time()) {
83 return NGX_STALE;
84 }
119 85
120 return NGX_OK; 86 return NGX_OK;
121 } 87 }
122 88
123 89