Igor Sysoev | 4fc368f | 2003-12-01 16:28:14 +0000 | [diff] [blame] | 1 | |
Igor Sysoev | d90282d | 2004-09-28 08:34:51 +0000 | [diff] [blame] | 2 | /* |
Igor Sysoev | ff8da91 | 2004-09-29 16:00:49 +0000 | [diff] [blame] | 3 | * Copyright (C) Igor Sysoev |
Igor Sysoev | d90282d | 2004-09-28 08:34:51 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | |
Igor Sysoev | 4fc368f | 2003-12-01 16:28:14 +0000 | [diff] [blame] | 7 | #include <ngx_config.h> |
| 8 | #include <ngx_core.h> |
| 9 | #include <ngx_http.h> |
| 10 | |
| 11 | |
Igor Sysoev | f690604 | 2004-11-25 16:17:31 +0000 | [diff] [blame] | 12 | #if (NGX_HAVE_OPENSSL_MD5_H) |
Igor Sysoev | 0ee5d3c | 2004-02-20 16:48:59 +0000 | [diff] [blame] | 13 | #include <openssl/md5.h> |
| 14 | #else |
Igor Sysoev | 4fc368f | 2003-12-01 16:28:14 +0000 | [diff] [blame] | 15 | #include <md5.h> |
Igor Sysoev | 0ee5d3c | 2004-02-20 16:48:59 +0000 | [diff] [blame] | 16 | #endif |
Igor Sysoev | 4fc368f | 2003-12-01 16:28:14 +0000 | [diff] [blame] | 17 | |
Igor Sysoev | f690604 | 2004-11-25 16:17:31 +0000 | [diff] [blame] | 18 | #if (NGX_OPENSSL_MD5) |
Igor Sysoev | 4fc368f | 2003-12-01 16:28:14 +0000 | [diff] [blame] | 19 | #define MD5Init MD5_Init |
| 20 | #define MD5Update MD5_Update |
| 21 | #define MD5Final MD5_Final |
| 22 | #endif |
| 23 | |
| 24 | |
Igor Sysoev | b1dfe47 | 2004-12-21 12:30:30 +0000 | [diff] [blame] | 25 | ngx_int_t ngx_http_file_cache_get(ngx_http_request_t *r, |
| 26 | ngx_http_cache_ctx_t *ctx) |
Igor Sysoev | 4fc368f | 2003-12-01 16:28:14 +0000 | [diff] [blame] | 27 | { |
Igor Sysoev | b1dfe47 | 2004-12-21 12:30:30 +0000 | [diff] [blame] | 28 | ngx_uint_t i; |
| 29 | ngx_str_t *key; |
| 30 | ngx_http_cache_t *c; |
| 31 | MD5_CTX md5; |
Igor Sysoev | 4fc368f | 2003-12-01 16:28:14 +0000 | [diff] [blame] | 32 | |
Igor Sysoev | b1dfe47 | 2004-12-21 12:30:30 +0000 | [diff] [blame] | 33 | c = r->cache; |
Igor Sysoev | 4fc368f | 2003-12-01 16:28:14 +0000 | [diff] [blame] | 34 | |
Igor Sysoev | b1dfe47 | 2004-12-21 12:30:30 +0000 | [diff] [blame] | 35 | c->file.name.len = ctx->path->name.len + 1 + ctx->path->len + 32; |
| 36 | if (!(c->file.name.data = ngx_palloc(r->pool, c->file.name.len + 1))) { |
| 37 | return NGX_ABORT; |
Igor Sysoev | 4fc368f | 2003-12-01 16:28:14 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Igor Sysoev | 4fc368f | 2003-12-01 16:28:14 +0000 | [diff] [blame] | 40 | MD5Init(&md5); |
Igor Sysoev | 4fc368f | 2003-12-01 16:28:14 +0000 | [diff] [blame] | 41 | |
Igor Sysoev | b1dfe47 | 2004-12-21 12:30:30 +0000 | [diff] [blame] | 42 | key = c->key.elts; |
| 43 | for (i = 0; i < c->key.nelts; i++) { |
| 44 | MD5Update(&md5, key[i].data, key[i].len); |
| 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); |
Igor Sysoev | 4fc368f | 2003-12-01 16:28:14 +0000 | [diff] [blame] | 55 | |
Igor Sysoev | 54498db | 2004-02-11 17:08:49 +0000 | [diff] [blame] | 56 | ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
Igor Sysoev | b1dfe47 | 2004-12-21 12:30:30 +0000 | [diff] [blame] | 57 | "file cache key: %V, md5: %s", &ctx->key, |
| 58 | c->file.name.data + ctx->path->name.len + 1 + ctx->path->len); |
Igor Sysoev | 4fc368f | 2003-12-01 16:28:14 +0000 | [diff] [blame] | 59 | |
Igor Sysoev | b1dfe47 | 2004-12-21 12:30:30 +0000 | [diff] [blame] | 60 | ngx_create_hashed_filename(&c->file, ctx->path); |
Igor Sysoev | 4fc368f | 2003-12-01 16:28:14 +0000 | [diff] [blame] | 61 | |
Igor Sysoev | 54498db | 2004-02-11 17:08:49 +0000 | [diff] [blame] | 62 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
Igor Sysoev | b1dfe47 | 2004-12-21 12:30:30 +0000 | [diff] [blame] | 63 | "file cache name: %s", c->file.name.data); |
Igor Sysoev | 4fc368f | 2003-12-01 16:28:14 +0000 | [diff] [blame] | 64 | |
Igor Sysoev | b1dfe47 | 2004-12-21 12:30:30 +0000 | [diff] [blame] | 65 | return ngx_http_file_cache_open(r->cache); |
Igor Sysoev | 4fc368f | 2003-12-01 16:28:14 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | |
Igor Sysoev | b1dfe47 | 2004-12-21 12:30:30 +0000 | [diff] [blame] | 69 | ngx_int_t ngx_http_file_cache_open(ngx_http_cache_t *c) |
Igor Sysoev | 4fc368f | 2003-12-01 16:28:14 +0000 | [diff] [blame] | 70 | { |
| 71 | ssize_t n; |
| 72 | ngx_err_t err; |
| 73 | ngx_http_cache_header_t *h; |
| 74 | |
Igor Sysoev | b1dfe47 | 2004-12-21 12:30:30 +0000 | [diff] [blame] | 75 | c->file.fd = ngx_open_file(c->file.name.data, |
| 76 | NGX_FILE_RDONLY, NGX_FILE_OPEN); |
Igor Sysoev | 4fc368f | 2003-12-01 16:28:14 +0000 | [diff] [blame] | 77 | |
Igor Sysoev | b1dfe47 | 2004-12-21 12:30:30 +0000 | [diff] [blame] | 78 | if (c->file.fd == NGX_INVALID_FILE) { |
Igor Sysoev | 4fc368f | 2003-12-01 16:28:14 +0000 | [diff] [blame] | 79 | err = ngx_errno; |
| 80 | |
| 81 | if (err == NGX_ENOENT || err == NGX_ENOTDIR) { |
| 82 | return NGX_DECLINED; |
| 83 | } |
| 84 | |
Igor Sysoev | b1dfe47 | 2004-12-21 12:30:30 +0000 | [diff] [blame] | 85 | ngx_log_error(NGX_LOG_CRIT, c->log, ngx_errno, |
| 86 | ngx_open_file_n " \"%s\" failed", c->file.name.data); |
Igor Sysoev | 4fc368f | 2003-12-01 16:28:14 +0000 | [diff] [blame] | 87 | return NGX_ERROR; |
| 88 | } |
| 89 | |
Igor Sysoev | b1dfe47 | 2004-12-21 12:30:30 +0000 | [diff] [blame] | 90 | if (c->uniq) { |
| 91 | if (ngx_fd_info(c->file.fd, &c->file.info) == NGX_FILE_ERROR) { |
| 92 | ngx_log_error(NGX_LOG_CRIT, c->log, ngx_errno, |
| 93 | ngx_fd_info_n " \"%s\" failed", c->file.name.data); |
Igor Sysoev | 4fc368f | 2003-12-01 16:28:14 +0000 | [diff] [blame] | 94 | |
| 95 | return NGX_ERROR; |
| 96 | } |
| 97 | |
Igor Sysoev | b1dfe47 | 2004-12-21 12:30:30 +0000 | [diff] [blame] | 98 | if (ngx_file_uniq(&c->file.info) == c->uniq) { |
| 99 | if (ngx_close_file(c->file.fd) == NGX_FILE_ERROR) { |
| 100 | ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno, |
Igor Sysoev | 4fc368f | 2003-12-01 16:28:14 +0000 | [diff] [blame] | 101 | ngx_close_file_n " \"%s\" failed", |
Igor Sysoev | b1dfe47 | 2004-12-21 12:30:30 +0000 | [diff] [blame] | 102 | c->file.name.data); |
Igor Sysoev | 4fc368f | 2003-12-01 16:28:14 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | return NGX_HTTP_CACHE_THE_SAME; |
| 106 | } |
| 107 | } |
| 108 | |
Igor Sysoev | b1dfe47 | 2004-12-21 12:30:30 +0000 | [diff] [blame] | 109 | n = ngx_read_file(&c->file, c->buf->pos, c->buf->end - c->buf->last, 0); |
Igor Sysoev | 4fc368f | 2003-12-01 16:28:14 +0000 | [diff] [blame] | 110 | |
| 111 | if (n == NGX_ERROR || n == NGX_AGAIN) { |
| 112 | return n; |
| 113 | } |
| 114 | |
Igor Sysoev | b1dfe47 | 2004-12-21 12:30:30 +0000 | [diff] [blame] | 115 | if (n <= c->header_size) { |
| 116 | ngx_log_error(NGX_LOG_CRIT, c->log, 0, |
| 117 | "cache file \"%s\" is too small", c->file.name.data); |
Igor Sysoev | 4fc368f | 2003-12-01 16:28:14 +0000 | [diff] [blame] | 118 | return NGX_ERROR; |
| 119 | } |
| 120 | |
Igor Sysoev | b1dfe47 | 2004-12-21 12:30:30 +0000 | [diff] [blame] | 121 | h = (ngx_http_cache_header_t *) c->buf->pos; |
| 122 | c->expires = h->expires; |
| 123 | c->last_modified= h->last_modified; |
| 124 | c->date = h->date; |
| 125 | c->length = h->length; |
Igor Sysoev | 4fc368f | 2003-12-01 16:28:14 +0000 | [diff] [blame] | 126 | |
Igor Sysoev | b1dfe47 | 2004-12-21 12:30:30 +0000 | [diff] [blame] | 127 | if (h->key_len > (size_t) (c->buf->end - c->buf->pos)) { |
| 128 | ngx_log_error(NGX_LOG_ALERT, c->log, 0, |
Igor Sysoev | 4fc368f | 2003-12-01 16:28:14 +0000 | [diff] [blame] | 129 | "cache file \"%s\" is probably invalid", |
Igor Sysoev | b1dfe47 | 2004-12-21 12:30:30 +0000 | [diff] [blame] | 130 | c->file.name.data); |
Igor Sysoev | 4fc368f | 2003-12-01 16:28:14 +0000 | [diff] [blame] | 131 | return NGX_DECLINED; |
| 132 | } |
| 133 | |
Igor Sysoev | b1dfe47 | 2004-12-21 12:30:30 +0000 | [diff] [blame] | 134 | #if 0 |
| 135 | |
| 136 | /* TODO */ |
| 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 | |
Igor Sysoev | 4fc368f | 2003-12-01 16:28:14 +0000 | [diff] [blame] | 142 | h->key[h->key_len] = '\0'; |
Igor Sysoev | b1dfe47 | 2004-12-21 12:30:30 +0000 | [diff] [blame] | 143 | ngx_log_error(NGX_LOG_ALERT, c->log, 0, |
Igor Sysoev | 4fc368f | 2003-12-01 16:28:14 +0000 | [diff] [blame] | 144 | "md5 collision: \"%s\" and \"%s\"", |
Igor Sysoev | b1dfe47 | 2004-12-21 12:30:30 +0000 | [diff] [blame] | 145 | h->key, c->key.data); |
Igor Sysoev | 4fc368f | 2003-12-01 16:28:14 +0000 | [diff] [blame] | 146 | return NGX_DECLINED; |
| 147 | } |
| 148 | |
Igor Sysoev | b1dfe47 | 2004-12-21 12:30:30 +0000 | [diff] [blame] | 149 | #endif |
Igor Sysoev | 4fc368f | 2003-12-01 16:28:14 +0000 | [diff] [blame] | 150 | |
Igor Sysoev | b1dfe47 | 2004-12-21 12:30:30 +0000 | [diff] [blame] | 151 | c->buf->last += n; |
Igor Sysoev | 54498db | 2004-02-11 17:08:49 +0000 | [diff] [blame] | 152 | |
Igor Sysoev | b1dfe47 | 2004-12-21 12:30:30 +0000 | [diff] [blame] | 153 | if (c->expires < ngx_time()) { |
| 154 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, |
Igor Sysoev | 54498db | 2004-02-11 17:08:49 +0000 | [diff] [blame] | 155 | "http file cache expired"); |
Igor Sysoev | 4fc368f | 2003-12-01 16:28:14 +0000 | [diff] [blame] | 156 | return NGX_HTTP_CACHE_STALE; |
| 157 | } |
| 158 | |
| 159 | /* TODO: NGX_HTTP_CACHE_AGED */ |
| 160 | |
Igor Sysoev | b1dfe47 | 2004-12-21 12:30:30 +0000 | [diff] [blame] | 161 | /* STUB */ return NGX_DECLINED; |
| 162 | |
Igor Sysoev | 4fc368f | 2003-12-01 16:28:14 +0000 | [diff] [blame] | 163 | return NGX_OK; |
| 164 | } |
| 165 | |
| 166 | |
Igor Sysoev | b1dfe47 | 2004-12-21 12:30:30 +0000 | [diff] [blame] | 167 | #if 0 |
| 168 | |
| 169 | |
Igor Sysoev | 4fc368f | 2003-12-01 16:28:14 +0000 | [diff] [blame] | 170 | int ngx_http_cache_update_file(ngx_http_request_t *r, ngx_http_cache_ctx_t *ctx, |
| 171 | ngx_str_t *temp_file) |
| 172 | { |
| 173 | int retry; |
| 174 | ngx_err_t err; |
| 175 | |
| 176 | retry = 0; |
| 177 | |
| 178 | for ( ;; ) { |
| 179 | if (ngx_rename_file(temp_file->data, ctx->file.name.data) == NGX_OK) { |
| 180 | return NGX_OK; |
| 181 | } |
| 182 | |
| 183 | err = ngx_errno; |
| 184 | |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 185 | #if (NGX_WIN32) |
Igor Sysoev | 4fc368f | 2003-12-01 16:28:14 +0000 | [diff] [blame] | 186 | if (err == NGX_EEXIST) { |
| 187 | if (ngx_win32_rename_file(temp_file, &ctx->file.name, r->pool) |
| 188 | == NGX_ERROR) |
| 189 | { |
| 190 | return NGX_ERROR; |
| 191 | } |
| 192 | } |
| 193 | #endif |
| 194 | |
| 195 | if (retry || (err != NGX_ENOENT && err != NGX_ENOTDIR)) { |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 196 | ngx_log_error(NGX_LOG_CRIT, r->connection->log, err, |
Igor Sysoev | 4fc368f | 2003-12-01 16:28:14 +0000 | [diff] [blame] | 197 | ngx_rename_file_n "(\"%s\", \"%s\") failed", |
| 198 | temp_file->data, ctx->file.name.data); |
| 199 | |
| 200 | return NGX_ERROR; |
| 201 | } |
| 202 | |
| 203 | if (ngx_create_path(&ctx->file, ctx->path) == NGX_ERROR) { |
| 204 | return NGX_ERROR; |
| 205 | } |
| 206 | |
| 207 | retry = 1; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | |
Igor Sysoev | b1dfe47 | 2004-12-21 12:30:30 +0000 | [diff] [blame] | 212 | #endif |
| 213 | |
| 214 | |
| 215 | ngx_int_t ngx_http_cache_cleaner_handler(ngx_gc_t *gc, ngx_str_t *name, |
| 216 | ngx_dir_t *dir) |
Igor Sysoev | 4fc368f | 2003-12-01 16:28:14 +0000 | [diff] [blame] | 217 | { |
Igor Sysoev | b1dfe47 | 2004-12-21 12:30:30 +0000 | [diff] [blame] | 218 | int rc; |
| 219 | ngx_buf_t buf; |
| 220 | ngx_http_cache_t c; |
| 221 | u_char data[sizeof(ngx_http_cache_header_t)]; |
Igor Sysoev | 4fc368f | 2003-12-01 16:28:14 +0000 | [diff] [blame] | 222 | |
Igor Sysoev | b1dfe47 | 2004-12-21 12:30:30 +0000 | [diff] [blame] | 223 | ngx_memzero(&c, sizeof(ngx_http_cache_t)); |
Igor Sysoev | 4fc368f | 2003-12-01 16:28:14 +0000 | [diff] [blame] | 224 | |
Igor Sysoev | b1dfe47 | 2004-12-21 12:30:30 +0000 | [diff] [blame] | 225 | c.file.fd = NGX_INVALID_FILE; |
| 226 | c.file.name = *name; |
| 227 | c.file.log = gc->log; |
Igor Sysoev | 4fc368f | 2003-12-01 16:28:14 +0000 | [diff] [blame] | 228 | |
Igor Sysoev | b1dfe47 | 2004-12-21 12:30:30 +0000 | [diff] [blame] | 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; |
Igor Sysoev | 4fc368f | 2003-12-01 16:28:14 +0000 | [diff] [blame] | 236 | buf.pos = data; |
| 237 | buf.last = data; |
| 238 | buf.start = data; |
| 239 | buf.end = data + sizeof(ngx_http_cache_header_t); |
| 240 | |
Igor Sysoev | b1dfe47 | 2004-12-21 12:30:30 +0000 | [diff] [blame] | 241 | rc = ngx_http_file_cache_open(&c); |
Igor Sysoev | 4fc368f | 2003-12-01 16:28:14 +0000 | [diff] [blame] | 242 | |
| 243 | /* TODO: NGX_AGAIN */ |
| 244 | |
Igor Sysoev | b1dfe47 | 2004-12-21 12:30:30 +0000 | [diff] [blame] | 245 | if (rc != NGX_ERROR&& rc != NGX_DECLINED && rc != NGX_HTTP_CACHE_STALE) { |
Igor Sysoev | 4fc368f | 2003-12-01 16:28:14 +0000 | [diff] [blame] | 246 | return NGX_OK; |
| 247 | } |
| 248 | |
| 249 | if (ngx_delete_file(name->data) == NGX_FILE_ERROR) { |
Igor Sysoev | b1dfe47 | 2004-12-21 12:30:30 +0000 | [diff] [blame] | 250 | ngx_log_error(NGX_LOG_CRIT, c.log, ngx_errno, |
Igor Sysoev | 4fc368f | 2003-12-01 16:28:14 +0000 | [diff] [blame] | 251 | ngx_delete_file_n " \"%s\" failed", name->data); |
| 252 | return NGX_ERROR; |
| 253 | } |
| 254 | |
| 255 | gc->deleted++; |
| 256 | gc->freed += ngx_de_size(dir); |
| 257 | |
| 258 | return NGX_OK; |
| 259 | } |