diff src/http/modules/ngx_http_static_handler.c @ 88:674d333f4296

nginx-0.0.1-2003-05-14-21:13:13 import
author Igor Sysoev <igor@sysoev.ru>
date Wed, 14 May 2003 17:13:13 +0000
parents 5f6d848dcbef
children 29bf798b583f
line wrap: on
line diff
--- a/src/http/modules/ngx_http_static_handler.c
+++ b/src/http/modules/ngx_http_static_handler.c
@@ -6,6 +6,7 @@
 #include <ngx_hunk.h>
 #include <ngx_http.h>
 #include <ngx_http_config.h>
+#include <ngx_http_core_module.h>
 #include <ngx_http_output_filter.h>
 
 
@@ -14,16 +15,22 @@ ngx_http_module_t  ngx_http_static_modul
 
 int ngx_http_static_handler(ngx_http_request_t *r)
 {
-    int                  rc;
+    int                  rc, key, i;
+    ngx_log_e            level;
     ngx_err_t            err;
     ngx_hunk_t          *h;
+    ngx_http_type_t     *type;
     ngx_http_log_ctx_t  *ctx;
+    ngx_http_core_loc_conf_t  *core_lcf; 
+
+    core_lcf = (ngx_http_core_loc_conf_t *)
+                     ngx_http_get_module_loc_conf(r, ngx_http_core_module_ctx);
 
 #if 0
-    ngx_http_event_static_handler_loc_conf_t  *cf;
+    ngx_http_event_static_handler_loc_conf_t  *lcf;
 
-    cf = (ngx_http_event_static_handler_loc_conf_t *)
-             ngx_get_module_loc_conf(r, &ngx_http_event_static_handler_module);
+    lcf = (ngx_http_event_static_handler_loc_conf_t *)
+         ngx_get_module_loc_conf(r, &ngx_http_event_static_handler_module_ctx);
 
 #endif
 
@@ -31,32 +38,34 @@ int ngx_http_static_handler(ngx_http_req
     ctx = r->connection->log->data;
     ctx->action = "sending response";
 
-    if (r->file.fd == NGX_INVALID_FILE)
+    if (r->file.fd == NGX_INVALID_FILE) {
         r->file.fd = ngx_open_file(r->file.name.data, NGX_FILE_RDONLY);
 
-    if (r->file.fd == NGX_INVALID_FILE) {
-        err = ngx_errno;
-        ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_errno,
-                      "ngx_http_static_handler: "
-                      ngx_open_file_n " %s failed", r->file.name.data);
+        if (r->file.fd == NGX_INVALID_FILE) {
+            err = ngx_errno;
+
+            if (err == NGX_ENOENT || err == NGX_ENOTDIR) {
+                level = NGX_LOG_ERR;
+                rc = NGX_HTTP_NOT_FOUND;
 
-        if (err == NGX_ENOENT || err == NGX_ENOTDIR) {
-            return NGX_HTTP_NOT_FOUND;
+            } else {
+                level = NGX_LOG_CRIT;
+                rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
+            }
 
-        } else {
-            return NGX_HTTP_INTERNAL_SERVER_ERROR;
+            ngx_log_error(level, r->connection->log, ngx_errno,
+                          ngx_open_file_n " %s failed", r->file.name.data);
+            return rc;
         }
     }
 
     if (!r->file.info_valid) {
         if (ngx_stat_fd(r->file.fd, &r->file.info) == NGX_FILE_ERROR) {
-            ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_errno,
-                          "ngx_http_static_handler: "
+            ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
                           ngx_stat_fd_n " %s failed", r->file.name.data);
 
             if (ngx_close_file(r->file.fd) == NGX_FILE_ERROR)
-                ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_errno,
-                              "ngx_http_static_handler: "
+                ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno,
                               ngx_close_file_n " %s failed", r->file.name.data);
 
             return NGX_HTTP_INTERNAL_SERVER_ERROR;
@@ -65,16 +74,14 @@ int ngx_http_static_handler(ngx_http_req
         r->file.info_valid = 1;
     }
 
-#if !(WIN32) /* it's probably Unix specific */
+#if !(WIN32) /* not regular files is probably Unix specific */
 
     if (!ngx_is_file(r->file.info)) {
-        ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_errno,
-                      "ngx_http_static_handler: "
+        ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
                       "%s is not regular file", r->file.name.data);
 
         if (ngx_close_file(r->file.fd) == NGX_FILE_ERROR)
-            ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_errno,
-                          "ngx_http_static_handler: "
+            ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno,
                           ngx_close_file_n " %s failed", r->file.name.data);
 
         return NGX_HTTP_NOT_FOUND;
@@ -93,30 +100,29 @@ int ngx_http_static_handler(ngx_http_req
     r->headers_out.content_type->key.len = 12;
     r->headers_out.content_type->key.data = "Content-Type";
 
-    /* STUB */
     if (r->exten.len) {
-        if (ngx_strcasecmp(r->exten.data, "html") == 0) {
-            r->headers_out.content_type->value.len = 25;
-            r->headers_out.content_type->value.data =
-                                                   "text/html; charset=koi8-r";
-        } else if (ngx_strcasecmp(r->exten.data, "gif") == 0) {
-            r->headers_out.content_type->value.len = 9;
-            r->headers_out.content_type->value.data = "image/gif";
-        } else if (ngx_strcasecmp(r->exten.data, "jpg") == 0) {
-            r->headers_out.content_type->value.len = 10;
-            r->headers_out.content_type->value.data = "image/jpeg";
-        } else if (ngx_strcasecmp(r->exten.data, "pdf") == 0) {
-            r->headers_out.content_type->value.len = 15;
-            r->headers_out.content_type->value.data = "application/pdf";
+        ngx_http_types_hash_key(key, r->exten);
+
+        type = (ngx_http_type_t *) core_lcf->types[key].elts;
+        for (i = 0; i < core_lcf->types[key].nelts; i++) {
+            if (r->exten.len != type[i].exten.len) {
+                continue;
+            }
+
+            if (ngx_strcasecmp(r->exten.data, type[i].exten.data) == 0) {
+                r->headers_out.content_type->value.len = type[i].type.len;
+                r->headers_out.content_type->value.data = type[i].type.data;
+            }
         }
+    }
 
-    } else {
+    if (r->headers_out.content_type->value.len == 0) {
+        /* STUB: default type */
         r->headers_out.content_type->value.len = 25;
         r->headers_out.content_type->value.data = "text/html; charset=koi8-r";
     }
-    /**/
 
-    /* we need to allocate them before header would be sent */
+    /* we need to allocate all before the header would be sent */
     ngx_test_null(h, ngx_pcalloc(r->pool, sizeof(ngx_hunk_t)),
                   NGX_HTTP_INTERNAL_SERVER_ERROR);