comparison src/http/ngx_http_file_cache.c @ 0:f0b350454894 NGINX_0_1_0

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