aboutsummaryrefslogtreecommitdiff
path: root/src/http/modules/ngx_http_static_module.c
diff options
context:
space:
mode:
authorIgor Sysoev <igor@sysoev.ru>2007-09-01 12:12:48 +0000
committerIgor Sysoev <igor@sysoev.ru>2007-09-01 12:12:48 +0000
commit140c7556a27050f5059e2edd9b038835ff6cd338 (patch)
treed176eeb05f2e7ea2222979e828e568d6de0cdcb2 /src/http/modules/ngx_http_static_module.c
parentd92bee51ea4db438a79c075a45937d5789210ab2 (diff)
downloadnginx-140c7556a27050f5059e2edd9b038835ff6cd338.tar.gz
nginx-140c7556a27050f5059e2edd9b038835ff6cd338.zip
open_file_cache in HTTP
Diffstat (limited to 'src/http/modules/ngx_http_static_module.c')
-rw-r--r--src/http/modules/ngx_http_static_module.c155
1 files changed, 39 insertions, 116 deletions
diff --git a/src/http/modules/ngx_http_static_module.c b/src/http/modules/ngx_http_static_module.c
index b94ebf230..aa5e72eb9 100644
--- a/src/http/modules/ngx_http_static_module.c
+++ b/src/http/modules/ngx_http_static_module.c
@@ -9,35 +9,10 @@
#include <ngx_http.h>
-typedef struct {
- ngx_http_cache_hash_t *redirect_cache;
-} ngx_http_static_loc_conf_t;
-
-
static ngx_int_t ngx_http_static_handler(ngx_http_request_t *r);
-static void *ngx_http_static_create_loc_conf(ngx_conf_t *cf);
-static char *ngx_http_static_merge_loc_conf(ngx_conf_t *cf,
- void *parent, void *child);
static ngx_int_t ngx_http_static_init(ngx_conf_t *cf);
-static ngx_command_t ngx_http_static_commands[] = {
-
-#if (NGX_HTTP_CACHE)
-
- { ngx_string("redirect_cache"),
- NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE3,
- ngx_http_set_cache_slot,
- NGX_HTTP_LOC_CONF_OFFSET,
- offsetof(ngx_http_static_loc_conf_t, redirect_cache),
- NULL },
-
-#endif
-
- ngx_null_command
-};
-
-
ngx_http_module_t ngx_http_static_module_ctx = {
NULL, /* preconfiguration */
ngx_http_static_init, /* postconfiguration */
@@ -48,15 +23,15 @@ ngx_http_module_t ngx_http_static_module_ctx = {
NULL, /* create server configuration */
NULL, /* merge server configuration */
- ngx_http_static_create_loc_conf, /* create location configuration */
- ngx_http_static_merge_loc_conf /* merge location configuration */
+ NULL, /* create location configuration */
+ NULL /* merge location configuration */
};
ngx_module_t ngx_http_static_module = {
NGX_MODULE_V1,
&ngx_http_static_module_ctx, /* module context */
- ngx_http_static_commands, /* module directives */
+ NULL, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
@@ -75,16 +50,13 @@ ngx_http_static_handler(ngx_http_request_t *r)
u_char *last, *location;
size_t root;
ngx_fd_t fd;
+ ngx_str_t path;
ngx_int_t rc;
ngx_uint_t level;
- ngx_str_t path;
- ngx_err_t err;
ngx_log_t *log;
ngx_buf_t *b;
ngx_chain_t out;
- ngx_file_info_t fi;
- ngx_pool_cleanup_t *cln;
- ngx_pool_cleanup_file_t *clnf;
+ ngx_open_file_info_t of;
ngx_http_core_loc_conf_t *clcf;
if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) {
@@ -118,75 +90,68 @@ ngx_http_static_handler(ngx_http_request_t *r)
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
+ path.len = last - path.data;
+
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
"http filename: \"%s\"", path.data);
- cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_pool_cleanup_file_t));
- if (cln == NULL) {
- return NGX_HTTP_INTERNAL_SERVER_ERROR;
- }
+ clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
+
+ of.test_dir = 0;
+ of.retest = clcf->open_file_cache_retest;
+ of.errors = clcf->open_file_cache_errors;
- fd = ngx_open_file(path.data, NGX_FILE_RDONLY, NGX_FILE_OPEN, 0);
+ rc = ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool);
- if (fd == NGX_INVALID_FILE) {
- err = ngx_errno;
+ if (rc == NGX_ERROR) {
+
+ switch (of.err) {
+
+ case 0:
+ return NGX_HTTP_INTERNAL_SERVER_ERROR;
+
+ case NGX_ENOENT:
+ case NGX_ENOTDIR:
+ case NGX_ENAMETOOLONG:
- if (err == NGX_ENOENT
- || err == NGX_ENOTDIR
- || err == NGX_ENAMETOOLONG)
- {
level = NGX_LOG_ERR;
rc = NGX_HTTP_NOT_FOUND;
+ break;
+
+ case NGX_EACCES:
- } else if (err == NGX_EACCES) {
level = NGX_LOG_ERR;
rc = NGX_HTTP_FORBIDDEN;
+ break;
+
+ default:
- } else {
level = NGX_LOG_CRIT;
rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
+ break;
}
- clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
-
if (rc != NGX_HTTP_NOT_FOUND || clcf->log_not_found) {
- ngx_log_error(level, log, err,
+ ngx_log_error(level, log, of.err,
ngx_open_file_n " \"%s\" failed", path.data);
}
return rc;
}
- ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0, "http static fd: %d", fd);
-
- if (ngx_fd_info(fd, &fi) == NGX_FILE_ERROR) {
- ngx_log_error(NGX_LOG_CRIT, log, ngx_errno,
- ngx_fd_info_n " \"%s\" failed", path.data);
-
- if (ngx_close_file(fd) == NGX_FILE_ERROR) {
- ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
- ngx_close_file_n " \"%s\" failed", path.data);
- }
+ fd = of.fd;
- return NGX_HTTP_INTERNAL_SERVER_ERROR;
- }
+ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0, "http static fd: %d", fd);
- if (ngx_is_dir(&fi)) {
+ if (of.is_dir) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0, "http dir");
- if (ngx_close_file(fd) == NGX_FILE_ERROR) {
- ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
- ngx_close_file_n " \"%s\" failed", path.data);
- }
-
r->headers_out.location = ngx_palloc(r->pool, sizeof(ngx_table_elt_t));
if (r->headers_out.location == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
- clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
-
if (!clcf->alias && clcf->root_lengths == NULL) {
location = path.data + clcf->root.len;
@@ -214,15 +179,10 @@ ngx_http_static_handler(ngx_http_request_t *r)
#if !(NGX_WIN32) /* the not regular files are probably Unix specific */
- if (!ngx_is_file(&fi)) {
+ if (!of.is_file) {
ngx_log_error(NGX_LOG_CRIT, log, ngx_errno,
"\"%s\" is not a regular file", path.data);
- if (ngx_close_file(fd) == NGX_FILE_ERROR) {
- ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
- ngx_close_file_n " \"%s\" failed", path.data);
- }
-
return NGX_HTTP_NOT_FOUND;
}
@@ -230,22 +190,15 @@ ngx_http_static_handler(ngx_http_request_t *r)
log->action = "sending response to client";
- cln->handler = ngx_pool_cleanup_file;
- clnf = cln->data;
-
- clnf->fd = fd;
- clnf->name = path.data;
- clnf->log = r->pool->log;
-
r->headers_out.status = NGX_HTTP_OK;
- r->headers_out.content_length_n = ngx_file_size(&fi);
- r->headers_out.last_modified_time = ngx_file_mtime(&fi);
+ r->headers_out.content_length_n = of.size;
+ r->headers_out.last_modified_time = of.mtime;
if (ngx_http_set_content_type(r) != NGX_OK) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
- if (r != r->main && ngx_file_size(&fi) == 0) {
+ if (r != r->main && of.size == 0) {
return ngx_http_send_header(r);
}
@@ -270,7 +223,7 @@ ngx_http_static_handler(ngx_http_request_t *r)
}
b->file_pos = 0;
- b->file_last = ngx_file_size(&fi);
+ b->file_last = of.size;
b->in_file = b->file_last ? 1: 0;
b->last_buf = (r == r->main) ? 1: 0;
@@ -287,36 +240,6 @@ ngx_http_static_handler(ngx_http_request_t *r)
}
-static void *
-ngx_http_static_create_loc_conf(ngx_conf_t *cf)
-{
- ngx_http_static_loc_conf_t *conf;
-
- conf = ngx_palloc(cf->pool, sizeof(ngx_http_static_loc_conf_t));
- if (conf == NULL) {
- return NGX_CONF_ERROR;
- }
-
- conf->redirect_cache = NULL;
-
- return conf;
-}
-
-
-static char *
-ngx_http_static_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
-{
- ngx_http_static_loc_conf_t *prev = parent;
- ngx_http_static_loc_conf_t *conf = child;
-
- if (conf->redirect_cache == NULL) {
- conf->redirect_cache = prev->redirect_cache;
- }
-
- return NGX_CONF_OK;
-}
-
-
static ngx_int_t
ngx_http_static_init(ngx_conf_t *cf)
{