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