diff src/http/modules/ngx_http_static_module.c @ 332:3a91bfeffaba NGINX_0_6_10

nginx 0.6.10 *) Feature: the "open_file_cache", "open_file_cache_retest", and "open_file_cache_errors" directives. *) Bugfix: socket leak; bug appeared in 0.6.7. *) Bugfix: a charset set by the "charset" directive was not appended to the "Content-Type" header set by $r->send_http_header(). *) Bugfix: a segmentation fault might occur in worker process if /dev/poll method was used.
author Igor Sysoev <http://sysoev.ru>
date Mon, 03 Sep 2007 00:00:00 +0400
parents 9fc4ab6673f9
children b743d290eb3b
line wrap: on
line diff
--- 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_modul
     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
     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,69 @@ ngx_http_static_handler(ngx_http_request
         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);
 
-    fd = ngx_open_file(path.data, NGX_FILE_RDONLY, NGX_FILE_OPEN, 0);
+    of.test_dir = 0;
+    of.retest = clcf->open_file_cache_retest;
+    of.errors = clcf->open_file_cache_errors;
+    of.events = clcf->open_file_cache_events;
+
+    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) {
 
-        if (err == NGX_ENOENT
-            || err == NGX_ENOTDIR
-            || err == NGX_ENAMETOOLONG)
-        {
+        case 0:
+            return NGX_HTTP_INTERNAL_SERVER_ERROR;
+
+        case NGX_ENOENT:
+        case NGX_ENOTDIR:
+        case NGX_ENAMETOOLONG:
+
             level = NGX_LOG_ERR;
             rc = NGX_HTTP_NOT_FOUND;
+            break;
 
-        } else if (err == NGX_EACCES) {
+        case NGX_EACCES:
+
             level = NGX_LOG_ERR;
             rc = NGX_HTTP_FORBIDDEN;
+            break;
 
-        } else {
+        default:
+
             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;
     }
 
+    fd = of.fd;
+
     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);
-        }
-
-        return NGX_HTTP_INTERNAL_SERVER_ERROR;
-    }
-
-    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 +180,10 @@ ngx_http_static_handler(ngx_http_request
 
 #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 +191,15 @@ ngx_http_static_handler(ngx_http_request
 
     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 +224,7 @@ ngx_http_static_handler(ngx_http_request
     }
 
     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 +241,6 @@ ngx_http_static_handler(ngx_http_request
 }
 
 
-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)
 {