blob: 10cb3edcf72897127f2a438be009b6a7a8dbebf2 [file] [log] [blame]
Igor Sysoev4fc368f2003-12-01 16:28:14 +00001
2#include <ngx_config.h>
3#include <ngx_core.h>
4#include <ngx_http.h>
5
6
Igor Sysoev0ee5d3c2004-02-20 16:48:59 +00007#if (HAVE_OPENSSL_MD5_H)
8#include <openssl/md5.h>
9#else
Igor Sysoev4fc368f2003-12-01 16:28:14 +000010#include <md5.h>
Igor Sysoev0ee5d3c2004-02-20 16:48:59 +000011#endif
Igor Sysoev4fc368f2003-12-01 16:28:14 +000012
13#if (HAVE_OPENSSL_MD5)
14#define MD5Init MD5_Init
15#define MD5Update MD5_Update
16#define MD5Final MD5_Final
17#endif
18
19
20
21int ngx_http_cache_get_file(ngx_http_request_t *r, ngx_http_cache_ctx_t *ctx)
22{
23 MD5_CTX md5;
24
25 /* we use offsetof() because sizeof() pads struct size to int size */
26 ctx->header_size = offsetof(ngx_http_cache_header_t, key)
27 + ctx->key.len + 1;
28
29 ctx->file.name.len = ctx->path->name.len + 1 + ctx->path->len + 32;
30 if (!(ctx->file.name.data = ngx_palloc(r->pool, ctx->file.name.len + 1))) {
31 return NGX_ERROR;
32 }
33
34 ngx_memcpy(ctx->file.name.data, ctx->path->name.data, ctx->path->name.len);
35
36 MD5Init(&md5);
37 MD5Update(&md5, (u_char *) ctx->key.data, ctx->key.len);
38 MD5Final(ctx->md5, &md5);
39
40 ngx_md5_text(ctx->file.name.data + ctx->path->name.len + 1 + ctx->path->len,
41 ctx->md5);
42
Igor Sysoev54498db2004-02-11 17:08:49 +000043 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
44 "file cache uri: %s, md5: %s", ctx->key.data,
45 ctx->file.name.data + ctx->path->name.len + 1 + ctx->path->len);
Igor Sysoev4fc368f2003-12-01 16:28:14 +000046
47 ngx_create_hashed_filename(&ctx->file, ctx->path);
48
Igor Sysoev54498db2004-02-11 17:08:49 +000049 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
50 "file cache name: %s", ctx->file.name.data);
Igor Sysoev4fc368f2003-12-01 16:28:14 +000051
52 /* TODO: look open files cache */
53
54 return ngx_http_cache_open_file(ctx, 0);
55}
56
57
58int ngx_http_cache_open_file(ngx_http_cache_ctx_t *ctx, ngx_file_uniq_t uniq)
59{
60 ssize_t n;
61 ngx_err_t err;
62 ngx_http_cache_header_t *h;
63
64 ctx->file.fd = ngx_open_file(ctx->file.name.data,
65 NGX_FILE_RDONLY, NGX_FILE_OPEN);
66
67 if (ctx->file.fd == NGX_INVALID_FILE) {
68 err = ngx_errno;
69
70 if (err == NGX_ENOENT || err == NGX_ENOTDIR) {
71 return NGX_DECLINED;
72 }
73
74 ngx_log_error(NGX_LOG_CRIT, ctx->log, ngx_errno,
75 ngx_open_file_n " \"%s\" failed", ctx->file.name.data);
76 return NGX_ERROR;
77 }
78
79 if (uniq) {
80 if (ngx_fd_info(ctx->file.fd, &ctx->file.info) == NGX_FILE_ERROR) {
81 ngx_log_error(NGX_LOG_CRIT, ctx->log, ngx_errno,
82 ngx_fd_info_n " \"%s\" failed", ctx->file.name.data);
83
84 return NGX_ERROR;
85 }
86
87 if (ngx_file_uniq(&ctx->file.info) == uniq) {
88 if (ngx_close_file(ctx->file.fd) == NGX_FILE_ERROR) {
89 ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno,
90 ngx_close_file_n " \"%s\" failed",
91 ctx->file.name.data);
92 }
93
94 return NGX_HTTP_CACHE_THE_SAME;
95 }
96 }
97
98 n = ngx_read_file(&ctx->file, ctx->buf->pos,
99 ctx->buf->end - ctx->buf->last, 0);
100
101 if (n == NGX_ERROR || n == NGX_AGAIN) {
102 return n;
103 }
104
105 if (n <= ctx->header_size) {
106 ngx_log_error(NGX_LOG_CRIT, ctx->log, 0,
107 "cache file \"%s\" is too small", ctx->file.name.data);
108 return NGX_ERROR;
109 }
110
111 h = (ngx_http_cache_header_t *) ctx->buf->pos;
112 ctx->expires = h->expires;
113 ctx->last_modified= h->last_modified;
114 ctx->date = h->date;
115 ctx->length = h->length;
116
117 if (h->key_len > (size_t) (ctx->buf->end - ctx->buf->pos)) {
118 ngx_log_error(NGX_LOG_ALERT, ctx->log, 0,
119 "cache file \"%s\" is probably invalid",
120 ctx->file.name.data);
121 return NGX_DECLINED;
122 }
123
124 if (ctx->key.len
125 && (h->key_len != ctx->key.len
126 || ngx_strncmp(h->key, ctx->key.data, h->key_len) != 0))
127 {
128 h->key[h->key_len] = '\0';
129 ngx_log_error(NGX_LOG_ALERT, ctx->log, 0,
130 "md5 collision: \"%s\" and \"%s\"",
131 h->key, ctx->key.data);
132 return NGX_DECLINED;
133 }
134
135 ctx->buf->last += n;
136
137 if (ctx->expires < ngx_time()) {
Igor Sysoev54498db2004-02-11 17:08:49 +0000138
139 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ctx->log, 0,
140 "http file cache expired");
141
Igor Sysoev4fc368f2003-12-01 16:28:14 +0000142 return NGX_HTTP_CACHE_STALE;
143 }
144
145 /* TODO: NGX_HTTP_CACHE_AGED */
146
147 return NGX_OK;
148}
149
150
151int ngx_http_cache_update_file(ngx_http_request_t *r, ngx_http_cache_ctx_t *ctx,
152 ngx_str_t *temp_file)
153{
154 int retry;
155 ngx_err_t err;
156
157 retry = 0;
158
159 for ( ;; ) {
160 if (ngx_rename_file(temp_file->data, ctx->file.name.data) == NGX_OK) {
161 return NGX_OK;
162 }
163
164 err = ngx_errno;
165
166#if (WIN32)
167 if (err == NGX_EEXIST) {
168 if (ngx_win32_rename_file(temp_file, &ctx->file.name, r->pool)
169 == NGX_ERROR)
170 {
171 return NGX_ERROR;
172 }
173 }
174#endif
175
176 if (retry || (err != NGX_ENOENT && err != NGX_ENOTDIR)) {
177 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
178 ngx_rename_file_n "(\"%s\", \"%s\") failed",
179 temp_file->data, ctx->file.name.data);
180
181 return NGX_ERROR;
182 }
183
184 if (ngx_create_path(&ctx->file, ctx->path) == NGX_ERROR) {
185 return NGX_ERROR;
186 }
187
188 retry = 1;
189 }
190}
191
192
193int ngx_garbage_collector_http_cache_handler(ngx_gc_t *gc, ngx_str_t *name,
194 ngx_dir_t *dir)
195{
196 int rc;
197 char data[sizeof(ngx_http_cache_header_t)];
198 ngx_hunk_t buf;
199 ngx_http_cache_ctx_t ctx;
200
201 ctx.file.fd = NGX_INVALID_FILE;
202 ctx.file.name = *name;
203 ctx.file.log = gc->log;
204
205 ctx.header_size = sizeof(ngx_http_cache_header_t);
206 ctx.buf = &buf;
207 ctx.log = gc->log;
208 ctx.key.len = 0;
209
210 buf.type = NGX_HUNK_IN_MEMORY|NGX_HUNK_TEMP;
211 buf.pos = data;
212 buf.last = data;
213 buf.start = data;
214 buf.end = data + sizeof(ngx_http_cache_header_t);
215
216 rc = ngx_http_cache_open_file(&ctx, 0);
217
218 /* TODO: NGX_AGAIN */
219
220 if (rc != NGX_ERROR && rc != NGX_DECLINED && rc != NGX_HTTP_CACHE_STALE) {
221 return NGX_OK;
222 }
223
224 if (ngx_delete_file(name->data) == NGX_FILE_ERROR) {
225 ngx_log_error(NGX_LOG_CRIT, gc->log, ngx_errno,
226 ngx_delete_file_n " \"%s\" failed", name->data);
227 return NGX_ERROR;
228 }
229
230 gc->deleted++;
231 gc->freed += ngx_de_size(dir);
232
233 return NGX_OK;
234}