comparison src/http/ngx_http_file_cache.c @ 26:45fe5b98a9de NGINX_0_1_13

nginx 0.1.13 *) Feature: the server_names_hash and server_names_hash_threshold directives. *) Bugfix: the *.domain.tld names in the "server_name" directive did not work. *) Bugfix: the %request_length log parameter logged the incorrect length.
author Igor Sysoev <http://sysoev.ru>
date Tue, 21 Dec 2004 00:00:00 +0300
parents 6f8b0dc0f8dd
children 13710a1813ad
comparison
equal deleted inserted replaced
25:21488c53e135 26:45fe5b98a9de
20 #define MD5Update MD5_Update 20 #define MD5Update MD5_Update
21 #define MD5Final MD5_Final 21 #define MD5Final MD5_Final
22 #endif 22 #endif
23 23
24 24
25 #if 0 25 ngx_int_t ngx_http_file_cache_get(ngx_http_request_t *r,
26 26 ngx_http_cache_ctx_t *ctx)
27 int ngx_http_cache_get_file(ngx_http_request_t *r, ngx_http_cache_ctx_t *ctx) 27 {
28 { 28 ngx_uint_t i;
29 MD5_CTX md5; 29 ngx_str_t *key;
30 30 ngx_http_cache_t *c;
31 /* we use offsetof() because sizeof() pads struct size to int size */ 31 MD5_CTX md5;
32 ctx->header_size = offsetof(ngx_http_cache_header_t, key) 32
33 + ctx->key.len + 1; 33 c = r->cache;
34 34
35 ctx->file.name.len = ctx->path->name.len + 1 + ctx->path->len + 32; 35 c->file.name.len = ctx->path->name.len + 1 + ctx->path->len + 32;
36 if (!(ctx->file.name.data = ngx_palloc(r->pool, ctx->file.name.len + 1))) { 36 if (!(c->file.name.data = ngx_palloc(r->pool, c->file.name.len + 1))) {
37 return NGX_ERROR; 37 return NGX_ABORT;
38 } 38 }
39
40 ngx_memcpy(ctx->file.name.data, ctx->path->name.data, ctx->path->name.len);
41 39
42 MD5Init(&md5); 40 MD5Init(&md5);
43 MD5Update(&md5, (u_char *) ctx->key.data, ctx->key.len); 41
44 MD5Final(ctx->md5, &md5); 42 key = c->key.elts;
45 43 for (i = 0; i < c->key.nelts; i++) {
46 ngx_md5_text(ctx->file.name.data + ctx->path->name.len + 1 + ctx->path->len, 44 MD5Update(&md5, key[i].data, key[i].len);
47 ctx->md5); 45 }
46
47 MD5Update(&md5, ctx->key.data, ctx->key.len);
48
49 MD5Final(c->md5, &md5);
50
51 ngx_memcpy(c->file.name.data, ctx->path->name.data, ctx->path->name.len);
52
53 ngx_md5_text(c->file.name.data + ctx->path->name.len + 1 + ctx->path->len,
54 c->md5);
48 55
49 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 56 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
50 "file cache uri: %s, md5: %s", ctx->key.data, 57 "file cache key: %V, md5: %s", &ctx->key,
51 ctx->file.name.data + ctx->path->name.len + 1 + ctx->path->len); 58 c->file.name.data + ctx->path->name.len + 1 + ctx->path->len);
52 59
53 ngx_create_hashed_filename(&ctx->file, ctx->path); 60 ngx_create_hashed_filename(&c->file, ctx->path);
54 61
55 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 62 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
56 "file cache name: %s", ctx->file.name.data); 63 "file cache name: %s", c->file.name.data);
57 64
58 /* TODO: look open files cache */ 65 return ngx_http_file_cache_open(r->cache);
59 66 }
60 return ngx_http_cache_open_file(ctx, 0); 67
61 } 68
62 69 ngx_int_t ngx_http_file_cache_open(ngx_http_cache_t *c)
63
64 int ngx_http_cache_open_file(ngx_http_cache_ctx_t *ctx, ngx_file_uniq_t uniq)
65 { 70 {
66 ssize_t n; 71 ssize_t n;
67 ngx_err_t err; 72 ngx_err_t err;
68 ngx_http_cache_header_t *h; 73 ngx_http_cache_header_t *h;
69 74
70 ctx->file.fd = ngx_open_file(ctx->file.name.data, 75 c->file.fd = ngx_open_file(c->file.name.data,
71 NGX_FILE_RDONLY, NGX_FILE_OPEN); 76 NGX_FILE_RDONLY, NGX_FILE_OPEN);
72 77
73 if (ctx->file.fd == NGX_INVALID_FILE) { 78 if (c->file.fd == NGX_INVALID_FILE) {
74 err = ngx_errno; 79 err = ngx_errno;
75 80
76 if (err == NGX_ENOENT || err == NGX_ENOTDIR) { 81 if (err == NGX_ENOENT || err == NGX_ENOTDIR) {
77 return NGX_DECLINED; 82 return NGX_DECLINED;
78 } 83 }
79 84
80 ngx_log_error(NGX_LOG_CRIT, ctx->log, ngx_errno, 85 ngx_log_error(NGX_LOG_CRIT, c->log, ngx_errno,
81 ngx_open_file_n " \"%s\" failed", ctx->file.name.data); 86 ngx_open_file_n " \"%s\" failed", c->file.name.data);
82 return NGX_ERROR; 87 return NGX_ERROR;
83 } 88 }
84 89
85 if (uniq) { 90 if (c->uniq) {
86 if (ngx_fd_info(ctx->file.fd, &ctx->file.info) == NGX_FILE_ERROR) { 91 if (ngx_fd_info(c->file.fd, &c->file.info) == NGX_FILE_ERROR) {
87 ngx_log_error(NGX_LOG_CRIT, ctx->log, ngx_errno, 92 ngx_log_error(NGX_LOG_CRIT, c->log, ngx_errno,
88 ngx_fd_info_n " \"%s\" failed", ctx->file.name.data); 93 ngx_fd_info_n " \"%s\" failed", c->file.name.data);
89 94
90 return NGX_ERROR; 95 return NGX_ERROR;
91 } 96 }
92 97
93 if (ngx_file_uniq(&ctx->file.info) == uniq) { 98 if (ngx_file_uniq(&c->file.info) == c->uniq) {
94 if (ngx_close_file(ctx->file.fd) == NGX_FILE_ERROR) { 99 if (ngx_close_file(c->file.fd) == NGX_FILE_ERROR) {
95 ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno, 100 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
96 ngx_close_file_n " \"%s\" failed", 101 ngx_close_file_n " \"%s\" failed",
97 ctx->file.name.data); 102 c->file.name.data);
98 } 103 }
99 104
100 return NGX_HTTP_CACHE_THE_SAME; 105 return NGX_HTTP_CACHE_THE_SAME;
101 } 106 }
102 } 107 }
103 108
104 n = ngx_read_file(&ctx->file, ctx->buf->pos, 109 n = ngx_read_file(&c->file, c->buf->pos, c->buf->end - c->buf->last, 0);
105 ctx->buf->end - ctx->buf->last, 0);
106 110
107 if (n == NGX_ERROR || n == NGX_AGAIN) { 111 if (n == NGX_ERROR || n == NGX_AGAIN) {
108 return n; 112 return n;
109 } 113 }
110 114
111 if (n <= ctx->header_size) { 115 if (n <= c->header_size) {
112 ngx_log_error(NGX_LOG_CRIT, ctx->log, 0, 116 ngx_log_error(NGX_LOG_CRIT, c->log, 0,
113 "cache file \"%s\" is too small", ctx->file.name.data); 117 "cache file \"%s\" is too small", c->file.name.data);
114 return NGX_ERROR; 118 return NGX_ERROR;
115 } 119 }
116 120
117 h = (ngx_http_cache_header_t *) ctx->buf->pos; 121 h = (ngx_http_cache_header_t *) c->buf->pos;
118 ctx->expires = h->expires; 122 c->expires = h->expires;
119 ctx->last_modified= h->last_modified; 123 c->last_modified= h->last_modified;
120 ctx->date = h->date; 124 c->date = h->date;
121 ctx->length = h->length; 125 c->length = h->length;
122 126
123 if (h->key_len > (size_t) (ctx->buf->end - ctx->buf->pos)) { 127 if (h->key_len > (size_t) (c->buf->end - c->buf->pos)) {
124 ngx_log_error(NGX_LOG_ALERT, ctx->log, 0, 128 ngx_log_error(NGX_LOG_ALERT, c->log, 0,
125 "cache file \"%s\" is probably invalid", 129 "cache file \"%s\" is probably invalid",
126 ctx->file.name.data); 130 c->file.name.data);
127 return NGX_DECLINED; 131 return NGX_DECLINED;
128 } 132 }
129 133
130 if (ctx->key.len 134 #if 0
131 && (h->key_len != ctx->key.len 135
132 || ngx_strncmp(h->key, ctx->key.data, h->key_len) != 0)) 136 /* TODO */
133 { 137
138 if (c->key_len && h->key_len != c->key_len) {
139
140 ngx_strncmp(h->key, c->key_data, h->key_len) != 0))
141
134 h->key[h->key_len] = '\0'; 142 h->key[h->key_len] = '\0';
135 ngx_log_error(NGX_LOG_ALERT, ctx->log, 0, 143 ngx_log_error(NGX_LOG_ALERT, c->log, 0,
136 "md5 collision: \"%s\" and \"%s\"", 144 "md5 collision: \"%s\" and \"%s\"",
137 h->key, ctx->key.data); 145 h->key, c->key.data);
138 return NGX_DECLINED; 146 return NGX_DECLINED;
139 } 147 }
140 148
141 ctx->buf->last += n; 149 #endif
142 150
143 if (ctx->expires < ngx_time()) { 151 c->buf->last += n;
144 152
145 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ctx->log, 0, 153 if (c->expires < ngx_time()) {
154 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
146 "http file cache expired"); 155 "http file cache expired");
147
148 return NGX_HTTP_CACHE_STALE; 156 return NGX_HTTP_CACHE_STALE;
149 } 157 }
150 158
151 /* TODO: NGX_HTTP_CACHE_AGED */ 159 /* TODO: NGX_HTTP_CACHE_AGED */
152 160
161 /* STUB */ return NGX_DECLINED;
162
153 return NGX_OK; 163 return NGX_OK;
154 } 164 }
165
166
167 #if 0
155 168
156 169
157 int ngx_http_cache_update_file(ngx_http_request_t *r, ngx_http_cache_ctx_t *ctx, 170 int ngx_http_cache_update_file(ngx_http_request_t *r, ngx_http_cache_ctx_t *ctx,
158 ngx_str_t *temp_file) 171 ngx_str_t *temp_file)
159 { 172 {
194 retry = 1; 207 retry = 1;
195 } 208 }
196 } 209 }
197 210
198 211
199 int ngx_garbage_collector_http_cache_handler(ngx_gc_t *gc, ngx_str_t *name, 212 #endif
200 ngx_dir_t *dir) 213
201 { 214
202 int rc; 215 ngx_int_t ngx_http_cache_cleaner_handler(ngx_gc_t *gc, ngx_str_t *name,
203 char data[sizeof(ngx_http_cache_header_t)]; 216 ngx_dir_t *dir)
204 ngx_hunk_t buf; 217 {
205 ngx_http_cache_ctx_t ctx; 218 int rc;
206 219 ngx_buf_t buf;
207 ctx.file.fd = NGX_INVALID_FILE; 220 ngx_http_cache_t c;
208 ctx.file.name = *name; 221 u_char data[sizeof(ngx_http_cache_header_t)];
209 ctx.file.log = gc->log; 222
210 223 ngx_memzero(&c, sizeof(ngx_http_cache_t));
211 ctx.header_size = sizeof(ngx_http_cache_header_t); 224
212 ctx.buf = &buf; 225 c.file.fd = NGX_INVALID_FILE;
213 ctx.log = gc->log; 226 c.file.name = *name;
214 ctx.key.len = 0; 227 c.file.log = gc->log;
215 228
216 buf.type = NGX_HUNK_IN_MEMORY|NGX_HUNK_TEMP; 229 c.header_size = sizeof(ngx_http_cache_header_t);
230 c.buf = &buf;
231 c.log = gc->log;
232 c.key_len = 0;
233
234 buf.memory = 1;
235 buf.temporary = 1;
217 buf.pos = data; 236 buf.pos = data;
218 buf.last = data; 237 buf.last = data;
219 buf.start = data; 238 buf.start = data;
220 buf.end = data + sizeof(ngx_http_cache_header_t); 239 buf.end = data + sizeof(ngx_http_cache_header_t);
221 240
222 rc = ngx_http_cache_open_file(&ctx, 0); 241 rc = ngx_http_file_cache_open(&c);
223 242
224 /* TODO: NGX_AGAIN */ 243 /* TODO: NGX_AGAIN */
225 244
226 if (rc != NGX_ERROR && rc != NGX_DECLINED && rc != NGX_HTTP_CACHE_STALE) { 245 if (rc != NGX_ERROR&& rc != NGX_DECLINED && rc != NGX_HTTP_CACHE_STALE) {
227 return NGX_OK; 246 return NGX_OK;
228 } 247 }
229 248
230 if (ngx_delete_file(name->data) == NGX_FILE_ERROR) { 249 if (ngx_delete_file(name->data) == NGX_FILE_ERROR) {
231 ngx_log_error(NGX_LOG_CRIT, gc->log, ngx_errno, 250 ngx_log_error(NGX_LOG_CRIT, c.log, ngx_errno,
232 ngx_delete_file_n " \"%s\" failed", name->data); 251 ngx_delete_file_n " \"%s\" failed", name->data);
233 return NGX_ERROR; 252 return NGX_ERROR;
234 } 253 }
235 254
236 gc->deleted++; 255 gc->deleted++;
237 gc->freed += ngx_de_size(dir); 256 gc->freed += ngx_de_size(dir);
238 257
239 return NGX_OK; 258 return NGX_OK;
240 } 259 }
241
242 #endif