diff src/http/modules/ngx_http_static_handler.c @ 200:abeaebe0a33c

nginx-0.0.1-2003-11-28-20:41:47 import
author Igor Sysoev <igor@sysoev.ru>
date Fri, 28 Nov 2003 17:41:47 +0000
parents a65b630b3a66
children 267ea1d98683
line wrap: on
line diff
--- a/src/http/modules/ngx_http_static_handler.c
+++ b/src/http/modules/ngx_http_static_handler.c
@@ -41,9 +41,10 @@ ngx_module_t  ngx_http_static_module = {
 
 ngx_int_t ngx_http_static_translate_handler(ngx_http_request_t *r)
 {
-    ngx_int_t                  rc, level;
+    char                      *location, *last, *path;
     uint32_t                   crc;
-    char                      *location, *last;
+    ngx_int_t                  rc, level;
+    ngx_str_t                  name;
     ngx_err_t                  err;
     ngx_http_cache_t          *cache;
     ngx_http_cache_conf_t     *ccf;
@@ -55,62 +56,67 @@ ngx_int_t ngx_http_static_translate_hand
 
     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
 
+
     if (r->uri.data[r->uri.len - 1] == '/') {
-        if (r->path.data == NULL) {
-            ngx_test_null(r->path.data,
-                          ngx_palloc(r->pool,
-                                     clcf->doc_root.len + r->uri.len),
-                          NGX_HTTP_INTERNAL_SERVER_ERROR);
 
-            ngx_cpystrn(ngx_cpymem(r->path.data, clcf->doc_root.data,
-                                   clcf->doc_root.len),
-                        r->uri.data, r->uri.len + 1);
+        /* there is no index handler */
 
-        } else {
-            r->path.data[r->path.len] = '\0';
+        if (!(path = ngx_palloc(r->pool, clcf->doc_root.len + r->uri.len))) {
+            return NGX_HTTP_INTERNAL_SERVER_ERROR;
         }
 
+        ngx_cpystrn(ngx_cpymem(path, clcf->doc_root.data, clcf->doc_root.len),
+                    r->uri.data, r->uri.len + 1);
+
         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
-                      "directory index of \"%s\" is forbidden", r->path.data);
+                      "directory index of \"%s\" is forbidden", path);
 
         return NGX_HTTP_FORBIDDEN;
     }
 
-    /* "+ 2" is for trailing '/' in possible redirect and '\0' */
-    ngx_test_null(r->file.name.data,
+
+    /* "+ 2" is for a trailing '/' in a possible redirect and '\0' */
+    ngx_test_null(name.data,
                   ngx_palloc(r->pool, clcf->doc_root.len + r->uri.len + 2),
                   NGX_HTTP_INTERNAL_SERVER_ERROR);
 
-    location = ngx_cpymem(r->file.name.data, clcf->doc_root.data,
-                          clcf->doc_root.len),
-
+    location = ngx_cpymem(name.data, clcf->doc_root.data, clcf->doc_root.len);
     last = ngx_cpystrn(location, r->uri.data, r->uri.len + 1);
 
-ngx_log_debug(r->connection->log, "HTTP filename: '%s'" _ r->file.name.data);
+    ngx_log_debug(r->connection->log, "HTTP filename: '%s'" _ data);
+
 
-    ccf = ngx_http_get_module_loc_conf(r, ngx_http_cache_module);
+    if (r->cache == NULL) {
+
+        /* look up an open files cache */
 
-    if (ccf->open_files) {
-        cache = ngx_http_cache_get(ccf->open_files, &r->file.name, &crc);
+        ccf = ngx_http_get_module_loc_conf(r, ngx_http_cache_module);
 
-ngx_log_debug(r->connection->log, "cache get: %x" _ cache);
+        if (ccf->open_files) {
+            cache = ngx_http_cache_get(ccf->open_files, &name, &crc);
+
+            ngx_log_debug(r->connection->log, "cache get: %x" _ cache);
 
-        if (cache
-            && ((ngx_event_flags & NGX_HAVE_KQUEUE_EVENT)
-                || ccf->open_files->update >= ngx_cached_time - cache->updated))
-        {
-            cache->refs++;
-            r->file.fd = cache->fd;
-            r->file.name = cache->key;
-            r->content_handler = ngx_http_static_handler;
+            if (cache
+                && ((ngx_event_flags & NGX_HAVE_KQUEUE_EVENT)
+                    || ccf->open_files->update
+                                          >= ngx_cached_time - cache->updated))
+            {
+                r->cache = cache;
+                r->content_handler = ngx_http_static_handler;
 
-            return NGX_OK;
+                return NGX_OK;
+            }
+
+        } else {
+            cache = NULL;
         }
 
     } else {
-        cache = NULL;
+        cache = r->cache;
     }
 
+
 #if (WIN9X)
 
     if (ngx_win32_version < NGX_WIN_NT) {
@@ -121,10 +127,10 @@ ngx_log_debug(r->connection->log, "cache
          * so we need to check its type before the opening
          */
 
-        if (ngx_file_info(r->file.name.data, &r->file.info) == NGX_FILE_ERROR) {
+        if (ngx_file_info(name.data, &r->file.info) == NGX_FILE_ERROR) {
             err = ngx_errno;
             ngx_log_error(NGX_LOG_ERR, r->connection->log, err,
-                          ngx_file_info_n " \"%s\" failed", r->file.name.data);
+                          ngx_file_info_n " \"%s\" failed", name.data);
 
             if (err == NGX_ENOENT || err == NGX_ENOTDIR) {
                 return NGX_HTTP_NOT_FOUND;
@@ -138,7 +144,7 @@ ngx_log_debug(r->connection->log, "cache
         }
 
         if (ngx_is_dir(&r->file.info)) {
-ngx_log_debug(r->connection->log, "HTTP DIR: '%s'" _ r->file.name.data);
+            ngx_log_debug(r->connection->log, "HTTP DIR: '%s'" _ name.data);
 
             if (!(r->headers_out.location =
                    ngx_http_add_header(&r->headers_out, ngx_http_headers_out)))
@@ -159,6 +165,7 @@ ngx_log_debug(r->connection->log, "HTTP 
 
 #endif
 
+
     if (r->file.fd == NGX_INVALID_FILE) {
         r->file.fd = ngx_open_file(r->file.name.data,
                                    NGX_FILE_RDONLY, NGX_FILE_OPEN);
@@ -282,6 +289,7 @@ static int ngx_http_static_handler(ngx_h
     ngx_hunk_t                *h;
     ngx_chain_t                out;
     ngx_http_type_t           *type;
+    ngx_http_cleanup_t        *cleanup;
     ngx_http_log_ctx_t        *ctx;
     ngx_http_core_loc_conf_t  *clcf;
 
@@ -294,6 +302,10 @@ static int ngx_http_static_handler(ngx_h
     ctx = r->connection->log->data;
     ctx->action = "sending response to client";
 
+    if (!(cleanup = ngx_push_array(&r->cleanup))) {
+        return NGX_HTTP_INTERNAL_SERVER_ERROR;
+    }
+
     if (r->file.fd == NGX_INVALID_FILE) {
         r->file.fd = ngx_open_file(r->file.name.data,
                                    NGX_FILE_RDONLY, NGX_FILE_OPEN);
@@ -316,6 +328,10 @@ static int ngx_http_static_handler(ngx_h
         }
     }
 
+    cleanup->data.file.fd = r->file.fd;
+    cleanup->data.file.name = r->file.name.data;
+    cleanup->cache = 0;
+
     if (!r->file.info_valid) {
         if (ngx_fd_info(r->file.fd, &r->file.info) == NGX_FILE_ERROR) {
             ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
@@ -379,7 +395,6 @@ static int ngx_http_static_handler(ngx_h
     ngx_test_null(h->file, ngx_pcalloc(r->pool, sizeof(ngx_file_t)),
                   NGX_HTTP_INTERNAL_SERVER_ERROR);
 
-
     rc = ngx_http_send_header(r);
 
     if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {