3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
12 #if (NGX_HAVE_OPENSSL_MD5_H)
13 #include <openssl/md5.h>
19 #define MD5Init MD5_Init
20 #define MD5Update MD5_Update
21 #define MD5Final MD5_Final
25 ngx_int_t ngx_http_file_cache_get(ngx_http_request_t *r,
26 ngx_http_cache_ctx_t *ctx)
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))) {
43 for (i = 0; i < c->key.nelts; i++) {
44 MD5Update(&md5, key[i].data, key[i].len);
47 MD5Update(&md5, ctx->key.data, ctx->key.len);
49 MD5Final(c->md5, &md5);
51 ngx_memcpy(c->file.name.data, ctx->path->name.data, ctx->path->name.len);
53 ngx_md5_text(c->file.name.data + ctx->path->name.len + 1 + ctx->path->len,
56 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
57 "file cache key: %V, md5: %s", &ctx->key,
58 c->file.name.data + ctx->path->name.len + 1 + ctx->path->len);
60 ngx_create_hashed_filename(&c->file, ctx->path);
62 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
63 "file cache name: %s", c->file.name.data);
65 return ngx_http_file_cache_open(r->cache);
69 ngx_int_t ngx_http_file_cache_open(ngx_http_cache_t *c)
73 ngx_http_cache_header_t *h;
75 c->file.fd = ngx_open_file(c->file.name.data,
76 NGX_FILE_RDONLY, NGX_FILE_OPEN);
78 if (c->file.fd == NGX_INVALID_FILE) {
81 if (err == NGX_ENOENT || err == NGX_ENOTDIR) {
85 ngx_log_error(NGX_LOG_CRIT, c->log, ngx_errno,
86 ngx_open_file_n " \"%s\" failed", c->file.name.data);
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);
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,
101 ngx_close_file_n " \"%s\" failed",
105 return NGX_HTTP_CACHE_THE_SAME;
109 n = ngx_read_file(&c->file, c->buf->pos, c->buf->end - c->buf->last, 0);
111 if (n == NGX_ERROR || n == NGX_AGAIN) {
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);
121 h = (ngx_http_cache_header_t *) c->buf->pos;
122 c->expires = h->expires;
123 c->last_modified= h->last_modified;
125 c->length = h->length;
127 if (h->key_len > (size_t) (c->buf->end - c->buf->pos)) {
128 ngx_log_error(NGX_LOG_ALERT, c->log, 0,
129 "cache file \"%s\" is probably invalid",
138 if (c->key_len && h->key_len != c->key_len) {
140 ngx_strncmp(h->key, c->key_data, h->key_len) != 0))
142 h->key[h->key_len] = '\0';
143 ngx_log_error(NGX_LOG_ALERT, c->log, 0,
144 "md5 collision: \"%s\" and \"%s\"",
145 h->key, c->key.data);
153 if (c->expires < ngx_time()) {
154 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
155 "http file cache expired");
156 return NGX_HTTP_CACHE_STALE;
159 /* TODO: NGX_HTTP_CACHE_AGED */
161 /* STUB */ return NGX_DECLINED;
170 int ngx_http_cache_update_file(ngx_http_request_t *r, ngx_http_cache_ctx_t *ctx,
171 ngx_str_t *temp_file)
179 if (ngx_rename_file(temp_file->data, ctx->file.name.data) == NGX_OK) {
186 if (err == NGX_EEXIST) {
187 if (ngx_win32_rename_file(temp_file, &ctx->file.name, r->pool)
195 if (retry || (err != NGX_ENOENT && err != NGX_ENOTDIR)) {
196 ngx_log_error(NGX_LOG_CRIT, r->connection->log, err,
197 ngx_rename_file_n "(\"%s\", \"%s\") failed",
198 temp_file->data, ctx->file.name.data);
203 if (ngx_create_path(&ctx->file, ctx->path) == NGX_ERROR) {
215 ngx_int_t ngx_http_cache_cleaner_handler(ngx_gc_t *gc, ngx_str_t *name,
221 u_char data[sizeof(ngx_http_cache_header_t)];
223 ngx_memzero(&c, sizeof(ngx_http_cache_t));
225 c.file.fd = NGX_INVALID_FILE;
227 c.file.log = gc->log;
229 c.header_size = sizeof(ngx_http_cache_header_t);
239 buf.end = data + sizeof(ngx_http_cache_header_t);
241 rc = ngx_http_file_cache_open(&c);
243 /* TODO: NGX_AGAIN */
245 if (rc != NGX_ERROR&& rc != NGX_DECLINED && rc != NGX_HTTP_CACHE_STALE) {
249 if (ngx_delete_file(name->data) == NGX_FILE_ERROR) {
250 ngx_log_error(NGX_LOG_CRIT, c.log, ngx_errno,
251 ngx_delete_file_n " \"%s\" failed", name->data);
256 gc->freed += ngx_de_size(dir);