nginx-0.0.1-2003-12-01-19:28:14 import

This commit is contained in:
Igor Sysoev 2003-12-01 16:28:14 +00:00
parent 865c150cb4
commit 4fc368fa30
6 changed files with 239 additions and 238 deletions

View File

@ -169,6 +169,7 @@ HTTP_SRCS="src/http/ngx_http.c \
src/http/ngx_http_special_response.c \
src/http/ngx_http_request.c \
src/http/ngx_http_cache.c \
src/http/ngx_http_file_cache.c \
src/http/ngx_http_busy_lock.c \
src/http/ngx_http_parse.c \
src/http/ngx_http_headers.c \

View File

@ -132,7 +132,7 @@ int ngx_http_index_handler(ngx_http_request_t *r)
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
"http index cache get: " PTR_FMT, ctx->cache);
if (ctx->cache && ctx->cache->valid) {
if (ctx->cache && !ctx->cache->expired) {
ctx->cache->accessed = ngx_cached_time;
@ -266,7 +266,6 @@ int ngx_http_index_handler(ngx_http_request_t *r)
ctx->cache->accessed = ngx_cached_time;
ctx->cache->last_modified = 0;
ctx->cache->updated = ngx_cached_time;
ctx->cache->valid = 1;
ctx->cache->memory = 1;
ngx_http_cache_unlock(ilcf->index_cache, ctx->cache, log);
}

View File

@ -91,7 +91,7 @@ static ngx_int_t ngx_http_static_handler(ngx_http_request_t *r)
* and it must be already registered in r->cleanup
*/
if (r->cache && r->cache->valid) {
if (r->cache && !r->cache->expired) {
return ngx_http_send_cached(r);
}
@ -146,7 +146,7 @@ static ngx_int_t ngx_http_static_handler(ngx_http_request_t *r)
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
"http open file cache get: " PTR_FMT, file);
if (file && file->valid) {
if (file && !file->expired) {
r->cache = file;
return ngx_http_send_cached(r);
}
@ -165,7 +165,7 @@ static ngx_int_t ngx_http_static_handler(ngx_http_request_t *r)
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
"http redirect cache get: " PTR_FMT, redirect);
if (redirect && redirect->valid) {
if (redirect && !redirect->expired) {
/*
* We do not copy a cached value so the cache entry is locked
@ -336,7 +336,6 @@ static ngx_int_t ngx_http_static_handler(ngx_http_request_t *r)
redirect->accessed = ngx_cached_time;
redirect->last_modified = 0;
redirect->updated = ngx_cached_time;
redirect->valid = 1;
redirect->memory = 1;
ngx_http_cache_unlock(slcf->redirect_cache, redirect, log);
redirect_cleanup->valid = 0;
@ -375,7 +374,7 @@ static ngx_int_t ngx_http_static_handler(ngx_http_request_t *r)
}
file->accessed = ngx_cached_time;
file->updated = ngx_cached_time;
file->valid = 1;
file->expired = 0;
r->cache = file;
return ngx_http_send_cached(r);
@ -409,7 +408,6 @@ static ngx_int_t ngx_http_static_handler(ngx_http_request_t *r)
file->accessed = ngx_cached_time;
file->last_modified = ngx_file_mtime(&fi);
file->updated = ngx_cached_time;
file->valid = 1;
r->cache = file;
}

View File

@ -4,14 +4,6 @@
#include <ngx_http.h>
#include <md5.h>
#if (HAVE_OPENSSL_MD5)
#define MD5Init MD5_Init
#define MD5Update MD5_Update
#define MD5Final MD5_Final
#endif
static ngx_http_module_t ngx_http_cache_module_ctx = {
NULL, /* pre conf */
@ -58,13 +50,10 @@ ngx_http_cache_t *ngx_http_cache_get(ngx_http_cache_hash_t *hash,
c[i].refs++;
ngx_mutex_unlock(&hash->mutex);
if (c[i].notify) {
if (!(ngx_event_flags & NGX_HAVE_KQUEUE_EVENT)) {
c[i].valid = 0;
}
} else if (ngx_cached_time - c[i].updated >= hash->update) {
c[i].valid = 0;
if ((!(c[i].notify && (ngx_event_flags & NGX_HAVE_KQUEUE_EVENT)))
&& (ngx_cached_time - c[i].updated >= hash->update))
{
c[i].expired = 1;
}
if (cleanup) {
@ -168,8 +157,8 @@ ngx_http_cache_t *ngx_http_cache_alloc(ngx_http_cache_hash_t *hash,
cache->refs = 1;
cache->count = 0;
cache->valid = 1;
cache->deleted = 0;
cache->expired = 0;
cache->memory = 0;
cache->mmap = 0;
cache->notify = 0;
@ -253,7 +242,7 @@ ngx_add_file_event(ngx_fd_t, ngx_event_handler_pt *handler, void *data)
ngx_memzero(ev, sizeof(ngx_event_t);
ev->data = data;
ev->event_handler = handler;
ev->event_handler = ngx_http_cache_invalidate;
return ngx_add_event(ev, NGX_VNODE_EVENT, 0);
}
@ -280,173 +269,6 @@ void ngx_http_cache_invalidate(ngx_event_t *ev)
#endif
int ngx_http_cache_get_file(ngx_http_request_t *r, ngx_http_cache_ctx_t *ctx)
{
MD5_CTX md5;
/* we use offsetof() because sizeof() pads struct size to int size */
ctx->header_size = offsetof(ngx_http_cache_header_t, key)
+ ctx->key.len + 1;
ctx->file.name.len = ctx->path->name.len + 1 + ctx->path->len + 32;
if (!(ctx->file.name.data = ngx_palloc(r->pool, ctx->file.name.len + 1))) {
return NGX_ERROR;
}
ngx_memcpy(ctx->file.name.data, ctx->path->name.data, ctx->path->name.len);
MD5Init(&md5);
MD5Update(&md5, (u_char *) ctx->key.data, ctx->key.len);
MD5Final(ctx->md5, &md5);
ngx_md5_text(ctx->file.name.data + ctx->path->name.len + 1 + ctx->path->len,
ctx->md5);
ngx_log_debug(r->connection->log, "URL: %s, md5: %s" _ ctx->key.data _
ctx->file.name.data + ctx->path->name.len + 1 + ctx->path->len);
ngx_create_hashed_filename(&ctx->file, ctx->path);
ngx_log_debug(r->connection->log, "FILE: %s" _ ctx->file.name.data);
/* TODO: look open files cache */
return ngx_http_cache_open_file(ctx, 0);
}
int ngx_http_cache_open_file(ngx_http_cache_ctx_t *ctx, ngx_file_uniq_t uniq)
{
ssize_t n;
ngx_err_t err;
ngx_http_cache_header_t *h;
ctx->file.fd = ngx_open_file(ctx->file.name.data,
NGX_FILE_RDONLY, NGX_FILE_OPEN);
if (ctx->file.fd == NGX_INVALID_FILE) {
err = ngx_errno;
if (err == NGX_ENOENT || err == NGX_ENOTDIR) {
return NGX_DECLINED;
}
ngx_log_error(NGX_LOG_CRIT, ctx->log, ngx_errno,
ngx_open_file_n " \"%s\" failed", ctx->file.name.data);
return NGX_ERROR;
}
if (uniq) {
if (ngx_fd_info(ctx->file.fd, &ctx->file.info) == NGX_FILE_ERROR) {
ngx_log_error(NGX_LOG_CRIT, ctx->log, ngx_errno,
ngx_fd_info_n " \"%s\" failed", ctx->file.name.data);
return NGX_ERROR;
}
if (ngx_file_uniq(&ctx->file.info) == uniq) {
if (ngx_close_file(ctx->file.fd) == NGX_FILE_ERROR) {
ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno,
ngx_close_file_n " \"%s\" failed",
ctx->file.name.data);
}
return NGX_HTTP_CACHE_THE_SAME;
}
}
n = ngx_read_file(&ctx->file, ctx->buf->pos,
ctx->buf->end - ctx->buf->last, 0);
if (n == NGX_ERROR || n == NGX_AGAIN) {
return n;
}
if (n <= ctx->header_size) {
ngx_log_error(NGX_LOG_CRIT, ctx->log, 0,
"cache file \"%s\" is too small", ctx->file.name.data);
return NGX_ERROR;
}
h = (ngx_http_cache_header_t *) ctx->buf->pos;
ctx->expires = h->expires;
ctx->last_modified= h->last_modified;
ctx->date = h->date;
ctx->length = h->length;
if (h->key_len > (size_t) (ctx->buf->end - ctx->buf->pos)) {
ngx_log_error(NGX_LOG_ALERT, ctx->log, 0,
"cache file \"%s\" is probably invalid",
ctx->file.name.data);
return NGX_DECLINED;
}
if (ctx->key.len
&& (h->key_len != ctx->key.len
|| ngx_strncmp(h->key, ctx->key.data, h->key_len) != 0))
{
h->key[h->key_len] = '\0';
ngx_log_error(NGX_LOG_ALERT, ctx->log, 0,
"md5 collision: \"%s\" and \"%s\"",
h->key, ctx->key.data);
return NGX_DECLINED;
}
ctx->buf->last += n;
if (ctx->expires < ngx_time()) {
ngx_log_debug(ctx->log, "EXPIRED");
return NGX_HTTP_CACHE_STALE;
}
/* TODO: NGX_HTTP_CACHE_AGED */
return NGX_OK;
}
int ngx_http_cache_update_file(ngx_http_request_t *r, ngx_http_cache_ctx_t *ctx,
ngx_str_t *temp_file)
{
int retry;
ngx_err_t err;
retry = 0;
for ( ;; ) {
if (ngx_rename_file(temp_file->data, ctx->file.name.data) == NGX_OK) {
return NGX_OK;
}
err = ngx_errno;
#if (WIN32)
if (err == NGX_EEXIST) {
if (ngx_win32_rename_file(temp_file, &ctx->file.name, r->pool)
== NGX_ERROR)
{
return NGX_ERROR;
}
}
#endif
if (retry || (err != NGX_ENOENT && err != NGX_ENOTDIR)) {
ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
ngx_rename_file_n "(\"%s\", \"%s\") failed",
temp_file->data, ctx->file.name.data);
return NGX_ERROR;
}
if (ngx_create_path(&ctx->file, ctx->path) == NGX_ERROR) {
return NGX_ERROR;
}
retry = 1;
}
}
/* TODO: currently fd only */
ngx_int_t ngx_http_send_cached(ngx_http_request_t *r)
@ -486,7 +308,7 @@ ngx_int_t ngx_http_send_cached(ngx_http_request_t *r)
h->type = r->main ? NGX_HUNK_FILE : NGX_HUNK_FILE|NGX_HUNK_LAST;
h->file_pos = 0;
h->file_last = ngx_file_size(&r->file.info);
h->file_last = r->cache->data.size;
h->file->fd = r->cache->fd;
h->file->log = r->connection->log;
@ -498,50 +320,6 @@ ngx_int_t ngx_http_send_cached(ngx_http_request_t *r)
}
int ngx_garbage_collector_http_cache_handler(ngx_gc_t *gc, ngx_str_t *name,
ngx_dir_t *dir)
{
int rc;
char data[sizeof(ngx_http_cache_header_t)];
ngx_hunk_t buf;
ngx_http_cache_ctx_t ctx;
ctx.file.fd = NGX_INVALID_FILE;
ctx.file.name = *name;
ctx.file.log = gc->log;
ctx.header_size = sizeof(ngx_http_cache_header_t);
ctx.buf = &buf;
ctx.log = gc->log;
ctx.key.len = 0;
buf.type = NGX_HUNK_IN_MEMORY|NGX_HUNK_TEMP;
buf.pos = data;
buf.last = data;
buf.start = data;
buf.end = data + sizeof(ngx_http_cache_header_t);
rc = ngx_http_cache_open_file(&ctx, 0);
/* TODO: NGX_AGAIN */
if (rc != NGX_ERROR && rc != NGX_DECLINED && rc != NGX_HTTP_CACHE_STALE) {
return NGX_OK;
}
if (ngx_delete_file(name->data) == NGX_FILE_ERROR) {
ngx_log_error(NGX_LOG_CRIT, gc->log, ngx_errno,
ngx_delete_file_n " \"%s\" failed", name->data);
return NGX_ERROR;
}
gc->deleted++;
gc->freed += ngx_de_size(dir);
return NGX_OK;
}
char *ngx_http_set_cache_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
char *p = conf;

View File

@ -22,8 +22,8 @@ typedef struct {
unsigned count:NGX_HTTP_CACHE_LAZY_ALLOCATION_BITS;
unsigned valid:1;
unsigned deleted:1;
unsigned expired:1;
unsigned memory:1;
unsigned mmap:1;
unsigned notify:1;

View File

@ -0,0 +1,225 @@
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <md5.h>
#if (HAVE_OPENSSL_MD5)
#define MD5Init MD5_Init
#define MD5Update MD5_Update
#define MD5Final MD5_Final
#endif
int ngx_http_cache_get_file(ngx_http_request_t *r, ngx_http_cache_ctx_t *ctx)
{
MD5_CTX md5;
/* we use offsetof() because sizeof() pads struct size to int size */
ctx->header_size = offsetof(ngx_http_cache_header_t, key)
+ ctx->key.len + 1;
ctx->file.name.len = ctx->path->name.len + 1 + ctx->path->len + 32;
if (!(ctx->file.name.data = ngx_palloc(r->pool, ctx->file.name.len + 1))) {
return NGX_ERROR;
}
ngx_memcpy(ctx->file.name.data, ctx->path->name.data, ctx->path->name.len);
MD5Init(&md5);
MD5Update(&md5, (u_char *) ctx->key.data, ctx->key.len);
MD5Final(ctx->md5, &md5);
ngx_md5_text(ctx->file.name.data + ctx->path->name.len + 1 + ctx->path->len,
ctx->md5);
ngx_log_debug(r->connection->log, "URL: %s, md5: %s" _ ctx->key.data _
ctx->file.name.data + ctx->path->name.len + 1 + ctx->path->len);
ngx_create_hashed_filename(&ctx->file, ctx->path);
ngx_log_debug(r->connection->log, "FILE: %s" _ ctx->file.name.data);
/* TODO: look open files cache */
return ngx_http_cache_open_file(ctx, 0);
}
int ngx_http_cache_open_file(ngx_http_cache_ctx_t *ctx, ngx_file_uniq_t uniq)
{
ssize_t n;
ngx_err_t err;
ngx_http_cache_header_t *h;
ctx->file.fd = ngx_open_file(ctx->file.name.data,
NGX_FILE_RDONLY, NGX_FILE_OPEN);
if (ctx->file.fd == NGX_INVALID_FILE) {
err = ngx_errno;
if (err == NGX_ENOENT || err == NGX_ENOTDIR) {
return NGX_DECLINED;
}
ngx_log_error(NGX_LOG_CRIT, ctx->log, ngx_errno,
ngx_open_file_n " \"%s\" failed", ctx->file.name.data);
return NGX_ERROR;
}
if (uniq) {
if (ngx_fd_info(ctx->file.fd, &ctx->file.info) == NGX_FILE_ERROR) {
ngx_log_error(NGX_LOG_CRIT, ctx->log, ngx_errno,
ngx_fd_info_n " \"%s\" failed", ctx->file.name.data);
return NGX_ERROR;
}
if (ngx_file_uniq(&ctx->file.info) == uniq) {
if (ngx_close_file(ctx->file.fd) == NGX_FILE_ERROR) {
ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno,
ngx_close_file_n " \"%s\" failed",
ctx->file.name.data);
}
return NGX_HTTP_CACHE_THE_SAME;
}
}
n = ngx_read_file(&ctx->file, ctx->buf->pos,
ctx->buf->end - ctx->buf->last, 0);
if (n == NGX_ERROR || n == NGX_AGAIN) {
return n;
}
if (n <= ctx->header_size) {
ngx_log_error(NGX_LOG_CRIT, ctx->log, 0,
"cache file \"%s\" is too small", ctx->file.name.data);
return NGX_ERROR;
}
h = (ngx_http_cache_header_t *) ctx->buf->pos;
ctx->expires = h->expires;
ctx->last_modified= h->last_modified;
ctx->date = h->date;
ctx->length = h->length;
if (h->key_len > (size_t) (ctx->buf->end - ctx->buf->pos)) {
ngx_log_error(NGX_LOG_ALERT, ctx->log, 0,
"cache file \"%s\" is probably invalid",
ctx->file.name.data);
return NGX_DECLINED;
}
if (ctx->key.len
&& (h->key_len != ctx->key.len
|| ngx_strncmp(h->key, ctx->key.data, h->key_len) != 0))
{
h->key[h->key_len] = '\0';
ngx_log_error(NGX_LOG_ALERT, ctx->log, 0,
"md5 collision: \"%s\" and \"%s\"",
h->key, ctx->key.data);
return NGX_DECLINED;
}
ctx->buf->last += n;
if (ctx->expires < ngx_time()) {
ngx_log_debug(ctx->log, "EXPIRED");
return NGX_HTTP_CACHE_STALE;
}
/* TODO: NGX_HTTP_CACHE_AGED */
return NGX_OK;
}
int ngx_http_cache_update_file(ngx_http_request_t *r, ngx_http_cache_ctx_t *ctx,
ngx_str_t *temp_file)
{
int retry;
ngx_err_t err;
retry = 0;
for ( ;; ) {
if (ngx_rename_file(temp_file->data, ctx->file.name.data) == NGX_OK) {
return NGX_OK;
}
err = ngx_errno;
#if (WIN32)
if (err == NGX_EEXIST) {
if (ngx_win32_rename_file(temp_file, &ctx->file.name, r->pool)
== NGX_ERROR)
{
return NGX_ERROR;
}
}
#endif
if (retry || (err != NGX_ENOENT && err != NGX_ENOTDIR)) {
ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
ngx_rename_file_n "(\"%s\", \"%s\") failed",
temp_file->data, ctx->file.name.data);
return NGX_ERROR;
}
if (ngx_create_path(&ctx->file, ctx->path) == NGX_ERROR) {
return NGX_ERROR;
}
retry = 1;
}
}
int ngx_garbage_collector_http_cache_handler(ngx_gc_t *gc, ngx_str_t *name,
ngx_dir_t *dir)
{
int rc;
char data[sizeof(ngx_http_cache_header_t)];
ngx_hunk_t buf;
ngx_http_cache_ctx_t ctx;
ctx.file.fd = NGX_INVALID_FILE;
ctx.file.name = *name;
ctx.file.log = gc->log;
ctx.header_size = sizeof(ngx_http_cache_header_t);
ctx.buf = &buf;
ctx.log = gc->log;
ctx.key.len = 0;
buf.type = NGX_HUNK_IN_MEMORY|NGX_HUNK_TEMP;
buf.pos = data;
buf.last = data;
buf.start = data;
buf.end = data + sizeof(ngx_http_cache_header_t);
rc = ngx_http_cache_open_file(&ctx, 0);
/* TODO: NGX_AGAIN */
if (rc != NGX_ERROR && rc != NGX_DECLINED && rc != NGX_HTTP_CACHE_STALE) {
return NGX_OK;
}
if (ngx_delete_file(name->data) == NGX_FILE_ERROR) {
ngx_log_error(NGX_LOG_CRIT, gc->log, ngx_errno,
ngx_delete_file_n " \"%s\" failed", name->data);
return NGX_ERROR;
}
gc->deleted++;
gc->freed += ngx_de_size(dir);
return NGX_OK;
}