Mercurial > hg > nginx
comparison src/http/ngx_http_cache.c @ 190:02a715e85df1
nginx-0.0.1-2003-11-19-00:34:08 import
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Tue, 18 Nov 2003 21:34:08 +0000 |
parents | c966c09be66b |
children | 71ce40b3c37b |
comparison
equal
deleted
inserted
replaced
189:c966c09be66b | 190:02a715e85df1 |
---|---|
1 | 1 |
2 #include <ngx_config.h> | 2 #include <ngx_config.h> |
3 #include <ngx_core.h> | 3 #include <ngx_core.h> |
4 #include <ngx_http.h> | 4 #include <ngx_http.h> |
5 | |
5 | 6 |
6 #include <md5.h> | 7 #include <md5.h> |
7 | 8 |
8 #if (HAVE_OPENSSL_MD5) | 9 #if (HAVE_OPENSSL_MD5) |
9 #define MD5Init MD5_Init | 10 #define MD5Init MD5_Init |
39 | 40 |
40 ngx_log_debug(r->connection->log, "FILE: %s" _ ctx->file.name.data); | 41 ngx_log_debug(r->connection->log, "FILE: %s" _ ctx->file.name.data); |
41 | 42 |
42 /* TODO: look open files cache */ | 43 /* TODO: look open files cache */ |
43 | 44 |
44 return ngx_http_cache_open_file(r, ctx, 0); | 45 return ngx_http_cache_open_file(ctx, 0); |
45 } | 46 } |
46 | 47 |
47 | 48 |
48 /* TODO: Win32 inode analogy */ | 49 /* TODO: Win32 inode analogy */ |
49 | 50 |
50 int ngx_http_cache_open_file(ngx_http_request_t *r, ngx_http_cache_ctx_t *ctx, | 51 int ngx_http_cache_open_file(ngx_http_cache_ctx_t *ctx, ngx_file_uniq_t uniq) |
51 ngx_file_uniq_t uniq) | |
52 { | 52 { |
53 ssize_t n; | 53 ssize_t n; |
54 ngx_err_t err; | 54 ngx_err_t err; |
55 ngx_http_cache_header_t *h; | 55 ngx_http_cache_header_t *h; |
56 | 56 |
62 | 62 |
63 if (err == NGX_ENOENT || err == NGX_ENOTDIR) { | 63 if (err == NGX_ENOENT || err == NGX_ENOTDIR) { |
64 return NGX_DECLINED; | 64 return NGX_DECLINED; |
65 } | 65 } |
66 | 66 |
67 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno, | 67 ngx_log_error(NGX_LOG_CRIT, ctx->log, ngx_errno, |
68 ngx_open_file_n " \"%s\" failed", ctx->file.name.data); | 68 ngx_open_file_n " \"%s\" failed", ctx->file.name.data); |
69 return NGX_ERROR; | 69 return NGX_ERROR; |
70 } | 70 } |
71 | 71 |
72 if (uniq) { | 72 if (uniq) { |
73 if (ngx_fd_info(ctx->file.fd, &ctx->file.info) == NGX_FILE_ERROR) { | 73 if (ngx_fd_info(ctx->file.fd, &ctx->file.info) == NGX_FILE_ERROR) { |
74 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno, | 74 ngx_log_error(NGX_LOG_CRIT, ctx->log, ngx_errno, |
75 ngx_fd_info_n " \"%s\" failed", ctx->file.name.data); | 75 ngx_fd_info_n " \"%s\" failed", ctx->file.name.data); |
76 | 76 |
77 return NGX_ERROR; | 77 return NGX_ERROR; |
78 } | 78 } |
79 | 79 |
80 if (ngx_file_uniq(&ctx->file.info) == uniq) { | 80 if (ngx_file_uniq(&ctx->file.info) == uniq) { |
81 if (ngx_close_file(ctx->file.fd) == NGX_FILE_ERROR) { | 81 if (ngx_close_file(ctx->file.fd) == NGX_FILE_ERROR) { |
82 ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno, | 82 ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno, |
83 ngx_close_file_n " \"%s\" failed", | 83 ngx_close_file_n " \"%s\" failed", |
84 ctx->file.name.data); | 84 ctx->file.name.data); |
85 } | 85 } |
86 | 86 |
87 return NGX_HTTP_CACHE_THE_SAME; | 87 return NGX_HTTP_CACHE_THE_SAME; |
94 if (n == NGX_ERROR || n == NGX_AGAIN) { | 94 if (n == NGX_ERROR || n == NGX_AGAIN) { |
95 return n; | 95 return n; |
96 } | 96 } |
97 | 97 |
98 if (n <= ctx->header_size) { | 98 if (n <= ctx->header_size) { |
99 ngx_log_error(NGX_LOG_CRIT, r->connection->log, 0, | 99 ngx_log_error(NGX_LOG_CRIT, ctx->log, 0, |
100 "cache file \"%s\" is too small", ctx->file.name.data); | 100 "cache file \"%s\" is too small", ctx->file.name.data); |
101 return NGX_ERROR; | 101 return NGX_ERROR; |
102 } | 102 } |
103 | 103 |
104 h = (ngx_http_cache_header_t *) ctx->buf->pos; | 104 h = (ngx_http_cache_header_t *) ctx->buf->pos; |
106 ctx->last_modified= h->last_modified; | 106 ctx->last_modified= h->last_modified; |
107 ctx->date = h->date; | 107 ctx->date = h->date; |
108 ctx->length = h->length; | 108 ctx->length = h->length; |
109 | 109 |
110 if (h->key_len > (size_t) (ctx->buf->last - ctx->buf->pos)) { | 110 if (h->key_len > (size_t) (ctx->buf->last - ctx->buf->pos)) { |
111 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, | 111 ngx_log_error(NGX_LOG_ALERT, ctx->log, 0, |
112 "cache file \"%s\" is probably invalid", | 112 "cache file \"%s\" is probably invalid", |
113 ctx->file.name.data); | 113 ctx->file.name.data); |
114 return NGX_DECLINED; | 114 return NGX_DECLINED; |
115 } | 115 } |
116 | 116 |
117 if (h->key_len != ctx->key.len | 117 if (ctx->key.len |
118 || ngx_strncmp(h->key, ctx->key.data, h->key_len) != 0) | 118 && (h->key_len != ctx->key.len |
119 || ngx_strncmp(h->key, ctx->key.data, h->key_len) != 0)) | |
119 { | 120 { |
120 h->key[h->key_len] = '\0'; | 121 h->key[h->key_len] = '\0'; |
121 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, | 122 ngx_log_error(NGX_LOG_ALERT, ctx->log, 0, |
122 "md5 collision: \"%s\" and \"%s\"", | 123 "md5 collision: \"%s\" and \"%s\"", |
123 h->key, ctx->key.data); | 124 h->key, ctx->key.data); |
124 return NGX_DECLINED; | 125 return NGX_DECLINED; |
125 } | 126 } |
126 | 127 |
127 ctx->buf->last += n; | 128 ctx->buf->last += n; |
128 | 129 |
129 if (ctx->expires < ngx_time()) { | 130 if (ctx->expires < ngx_time()) { |
130 ngx_log_debug(r->connection->log, "EXPIRED"); | 131 ngx_log_debug(ctx->log, "EXPIRED"); |
131 return NGX_HTTP_CACHE_STALE; | 132 return NGX_HTTP_CACHE_STALE; |
132 } | 133 } |
133 | 134 |
134 /* TODO: NGX_HTTP_CACHE_AGED */ | 135 /* TODO: NGX_HTTP_CACHE_AGED */ |
136 | |
137 return NGX_OK; | |
138 } | |
139 | |
140 | |
141 int ngx_garbage_collector_http_cache_handler(ngx_gc_t *gc, ngx_str_t *name, | |
142 ngx_dir_t *dir) | |
143 { | |
144 int rc; | |
145 char data[sizeof(ngx_http_cache_header_t)]; | |
146 ngx_hunk_t buf; | |
147 ngx_http_cache_ctx_t ctx; | |
148 | |
149 ctx.file.fd = NGX_INVALID_FILE; | |
150 ctx.file.name = *name; | |
151 ctx.file.log = gc->log; | |
152 | |
153 ctx.header_size = sizeof(ngx_http_cache_header_t); | |
154 ctx.buf = &buf; | |
155 ctx.log = gc->log; | |
156 ctx.key.len = 0; | |
157 | |
158 buf.type = NGX_HUNK_IN_MEMORY|NGX_HUNK_TEMP; | |
159 buf.pos = data; | |
160 buf.last = data; | |
161 buf.start = data; | |
162 buf.end = data + sizeof(ngx_http_cache_header_t); | |
163 | |
164 rc = ngx_http_cache_open_file(&ctx, 0); | |
165 | |
166 /* TODO: NGX_AGAIN */ | |
167 | |
168 if (rc != NGX_ERROR && rc != NGX_DECLINED && rc != NGX_HTTP_CACHE_STALE) { | |
169 return NGX_OK; | |
170 } | |
171 | |
172 if (ngx_delete_file(name->data) == NGX_FILE_ERROR) { | |
173 ngx_log_error(NGX_LOG_CRIT, gc->log, ngx_errno, | |
174 ngx_delete_file_n " \"%s\" failed", name->data); | |
175 return NGX_ERROR; | |
176 } | |
177 | |
178 gc->deleted++; | |
179 gc->freed += ngx_de_size(dir); | |
135 | 180 |
136 return NGX_OK; | 181 return NGX_OK; |
137 } | 182 } |
138 | 183 |
139 | 184 |