comparison 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
comparison
equal deleted inserted replaced
87:5f6d848dcbef 88:674d333f4296
4 #include <ngx_string.h> 4 #include <ngx_string.h>
5 #include <ngx_file.h> 5 #include <ngx_file.h>
6 #include <ngx_hunk.h> 6 #include <ngx_hunk.h>
7 #include <ngx_http.h> 7 #include <ngx_http.h>
8 #include <ngx_http_config.h> 8 #include <ngx_http_config.h>
9 #include <ngx_http_core_module.h>
9 #include <ngx_http_output_filter.h> 10 #include <ngx_http_output_filter.h>
10 11
11 12
12 ngx_http_module_t ngx_http_static_module; 13 ngx_http_module_t ngx_http_static_module;
13 14
14 15
15 int ngx_http_static_handler(ngx_http_request_t *r) 16 int ngx_http_static_handler(ngx_http_request_t *r)
16 { 17 {
17 int rc; 18 int rc, key, i;
19 ngx_log_e level;
18 ngx_err_t err; 20 ngx_err_t err;
19 ngx_hunk_t *h; 21 ngx_hunk_t *h;
22 ngx_http_type_t *type;
20 ngx_http_log_ctx_t *ctx; 23 ngx_http_log_ctx_t *ctx;
24 ngx_http_core_loc_conf_t *core_lcf;
25
26 core_lcf = (ngx_http_core_loc_conf_t *)
27 ngx_http_get_module_loc_conf(r, ngx_http_core_module_ctx);
21 28
22 #if 0 29 #if 0
23 ngx_http_event_static_handler_loc_conf_t *cf; 30 ngx_http_event_static_handler_loc_conf_t *lcf;
24 31
25 cf = (ngx_http_event_static_handler_loc_conf_t *) 32 lcf = (ngx_http_event_static_handler_loc_conf_t *)
26 ngx_get_module_loc_conf(r, &ngx_http_event_static_handler_module); 33 ngx_get_module_loc_conf(r, &ngx_http_event_static_handler_module_ctx);
27 34
28 #endif 35 #endif
29 36
30 ngx_http_discard_body(r); 37 ngx_http_discard_body(r);
31 ctx = r->connection->log->data; 38 ctx = r->connection->log->data;
32 ctx->action = "sending response"; 39 ctx->action = "sending response";
33 40
34 if (r->file.fd == NGX_INVALID_FILE) 41 if (r->file.fd == NGX_INVALID_FILE) {
35 r->file.fd = ngx_open_file(r->file.name.data, NGX_FILE_RDONLY); 42 r->file.fd = ngx_open_file(r->file.name.data, NGX_FILE_RDONLY);
36 43
37 if (r->file.fd == NGX_INVALID_FILE) { 44 if (r->file.fd == NGX_INVALID_FILE) {
38 err = ngx_errno; 45 err = ngx_errno;
39 ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_errno,
40 "ngx_http_static_handler: "
41 ngx_open_file_n " %s failed", r->file.name.data);
42 46
43 if (err == NGX_ENOENT || err == NGX_ENOTDIR) { 47 if (err == NGX_ENOENT || err == NGX_ENOTDIR) {
44 return NGX_HTTP_NOT_FOUND; 48 level = NGX_LOG_ERR;
49 rc = NGX_HTTP_NOT_FOUND;
45 50
46 } else { 51 } else {
47 return NGX_HTTP_INTERNAL_SERVER_ERROR; 52 level = NGX_LOG_CRIT;
53 rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
54 }
55
56 ngx_log_error(level, r->connection->log, ngx_errno,
57 ngx_open_file_n " %s failed", r->file.name.data);
58 return rc;
48 } 59 }
49 } 60 }
50 61
51 if (!r->file.info_valid) { 62 if (!r->file.info_valid) {
52 if (ngx_stat_fd(r->file.fd, &r->file.info) == NGX_FILE_ERROR) { 63 if (ngx_stat_fd(r->file.fd, &r->file.info) == NGX_FILE_ERROR) {
53 ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_errno, 64 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
54 "ngx_http_static_handler: "
55 ngx_stat_fd_n " %s failed", r->file.name.data); 65 ngx_stat_fd_n " %s failed", r->file.name.data);
56 66
57 if (ngx_close_file(r->file.fd) == NGX_FILE_ERROR) 67 if (ngx_close_file(r->file.fd) == NGX_FILE_ERROR)
58 ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_errno, 68 ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno,
59 "ngx_http_static_handler: "
60 ngx_close_file_n " %s failed", r->file.name.data); 69 ngx_close_file_n " %s failed", r->file.name.data);
61 70
62 return NGX_HTTP_INTERNAL_SERVER_ERROR; 71 return NGX_HTTP_INTERNAL_SERVER_ERROR;
63 } 72 }
64 73
65 r->file.info_valid = 1; 74 r->file.info_valid = 1;
66 } 75 }
67 76
68 #if !(WIN32) /* it's probably Unix specific */ 77 #if !(WIN32) /* not regular files is probably Unix specific */
69 78
70 if (!ngx_is_file(r->file.info)) { 79 if (!ngx_is_file(r->file.info)) {
71 ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_errno, 80 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
72 "ngx_http_static_handler: "
73 "%s is not regular file", r->file.name.data); 81 "%s is not regular file", r->file.name.data);
74 82
75 if (ngx_close_file(r->file.fd) == NGX_FILE_ERROR) 83 if (ngx_close_file(r->file.fd) == NGX_FILE_ERROR)
76 ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_errno, 84 ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno,
77 "ngx_http_static_handler: "
78 ngx_close_file_n " %s failed", r->file.name.data); 85 ngx_close_file_n " %s failed", r->file.name.data);
79 86
80 return NGX_HTTP_NOT_FOUND; 87 return NGX_HTTP_NOT_FOUND;
81 } 88 }
82 89
91 NGX_HTTP_INTERNAL_SERVER_ERROR); 98 NGX_HTTP_INTERNAL_SERVER_ERROR);
92 99
93 r->headers_out.content_type->key.len = 12; 100 r->headers_out.content_type->key.len = 12;
94 r->headers_out.content_type->key.data = "Content-Type"; 101 r->headers_out.content_type->key.data = "Content-Type";
95 102
96 /* STUB */
97 if (r->exten.len) { 103 if (r->exten.len) {
98 if (ngx_strcasecmp(r->exten.data, "html") == 0) { 104 ngx_http_types_hash_key(key, r->exten);
99 r->headers_out.content_type->value.len = 25; 105
100 r->headers_out.content_type->value.data = 106 type = (ngx_http_type_t *) core_lcf->types[key].elts;
101 "text/html; charset=koi8-r"; 107 for (i = 0; i < core_lcf->types[key].nelts; i++) {
102 } else if (ngx_strcasecmp(r->exten.data, "gif") == 0) { 108 if (r->exten.len != type[i].exten.len) {
103 r->headers_out.content_type->value.len = 9; 109 continue;
104 r->headers_out.content_type->value.data = "image/gif"; 110 }
105 } else if (ngx_strcasecmp(r->exten.data, "jpg") == 0) { 111
106 r->headers_out.content_type->value.len = 10; 112 if (ngx_strcasecmp(r->exten.data, type[i].exten.data) == 0) {
107 r->headers_out.content_type->value.data = "image/jpeg"; 113 r->headers_out.content_type->value.len = type[i].type.len;
108 } else if (ngx_strcasecmp(r->exten.data, "pdf") == 0) { 114 r->headers_out.content_type->value.data = type[i].type.data;
109 r->headers_out.content_type->value.len = 15; 115 }
110 r->headers_out.content_type->value.data = "application/pdf";
111 } 116 }
117 }
112 118
113 } else { 119 if (r->headers_out.content_type->value.len == 0) {
120 /* STUB: default type */
114 r->headers_out.content_type->value.len = 25; 121 r->headers_out.content_type->value.len = 25;
115 r->headers_out.content_type->value.data = "text/html; charset=koi8-r"; 122 r->headers_out.content_type->value.data = "text/html; charset=koi8-r";
116 } 123 }
117 /**/
118 124
119 /* we need to allocate them before header would be sent */ 125 /* we need to allocate all before the header would be sent */
120 ngx_test_null(h, ngx_pcalloc(r->pool, sizeof(ngx_hunk_t)), 126 ngx_test_null(h, ngx_pcalloc(r->pool, sizeof(ngx_hunk_t)),
121 NGX_HTTP_INTERNAL_SERVER_ERROR); 127 NGX_HTTP_INTERNAL_SERVER_ERROR);
122 128
123 ngx_test_null(h->file, ngx_pcalloc(r->pool, sizeof(ngx_file_t)), 129 ngx_test_null(h->file, ngx_pcalloc(r->pool, sizeof(ngx_file_t)),
124 NGX_HTTP_INTERNAL_SERVER_ERROR); 130 NGX_HTTP_INTERNAL_SERVER_ERROR);