1
2 /*
3 * Copyright (C) Igor Sysoev
4 */
5
6
7 #include <ngx_config.h>
8 #include <ngx_core.h>
9 #include <ngx_http.h>
10
11
12 #if (NGX_HAVE_OPENSSL_MD5_H)
13 #include <openssl/md5.h>
14 #else
15 #include <md5.h>
16 #endif
17
18 #if (NGX_OPENSSL_MD5)
19 #define MD5Init MD5_Init
20 #define MD5Update MD5_Update
21 #define MD5Final MD5_Final
22 #endif
23
24
25 ngx_int_t ngx_http_file_cache_get(ngx_http_request_t *r,
26 ngx_http_cache_ctx_t *ctx)
27 {
28 ngx_uint_t i;
29 ngx_str_t *key;
30 ngx_http_cache_t *c;
31 MD5_CTX md5;
32
33 c = r->cache;
34
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;
38 }
39
40 MD5Init(&md5);
41
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);
55
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);
59
60 ngx_create_hashed_filename(&c->file, ctx->path);
61
62 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
63 "file cache name: %s", c->file.name.data);
64
65 return ngx_http_file_cache_open(r->cache);
66 }
67
68
69 ngx_int_t ngx_http_file_cache_open(ngx_http_cache_t *c)
70 {
71 ssize_t n;
72 ngx_err_t err;
73 ngx_http_cache_header_t *h;
74
75 c->file.fd = ngx_open_file(c->file.name.data,
76 NGX_FILE_RDONLY, NGX_FILE_OPEN);
77
78 if (c->file.fd == NGX_INVALID_FILE) {
79 err = ngx_errno;
80
81 if (err == NGX_ENOENT || err == NGX_ENOTDIR) {
82 return NGX_DECLINED;
83 }
84
85 ngx_log_error(NGX_LOG_CRIT, c->log, ngx_errno,
86 ngx_open_file_n " \"%s\" failed", c->file.name.data);
87 return NGX_ERROR;
88 }
89
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);
94
95 return NGX_ERROR;
96 }
97
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",
102 c->file.name.data);
103 }
104
105 return NGX_HTTP_CACHE_THE_SAME;
106 }
107 }
108
109 n = ngx_read_file(&c->file, c->buf->pos, c->buf->end - c->buf->last, 0);
110
111 if (n == NGX_ERROR || n == NGX_AGAIN) {
112 return n;
113 }
114
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);
118 return NGX_ERROR;
119 }
120
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;
126
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",
130 c->file.name.data);
131 return NGX_DECLINED;
132 }
133
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
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);
146 return NGX_DECLINED;
147 }
148
149 #endif
150
151 c->buf->last += n;
152
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;
157 }
158
159 /* TODO: NGX_HTTP_CACHE_AGED */
160
161 /* STUB */ return NGX_DECLINED;
162
163 return NGX_OK;
164 }
165
166
167 #if 0
168
169
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
185 #if (NGX_WIN32)
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)) {
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);
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
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)
217 {
218 int rc;
219 ngx_buf_t buf;
220 ngx_http_cache_t c;
221 u_char data[sizeof(ngx_http_cache_header_t)];
222
223 ngx_memzero(&c, sizeof(ngx_http_cache_t));
224
225 c.file.fd = NGX_INVALID_FILE;
226 c.file.name = *name;
227 c.file.log = gc->log;
228
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;
236 buf.pos = data;
237 buf.last = data;
238 buf.start = data;
239 buf.end = data + sizeof(ngx_http_cache_header_t);
240
241 rc = ngx_http_file_cache_open(&c);
242
243 /* TODO: NGX_AGAIN */
244
245 if (rc != NGX_ERROR&& rc != NGX_DECLINED && rc != NGX_HTTP_CACHE_STALE) {
246 return NGX_OK;
247 }
248
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);
252 return NGX_ERROR;
253 }
254
255 gc->deleted++;
256 gc->freed += ngx_de_size(dir);
257
258 return NGX_OK;
259 }
260
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.