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