comparison 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
comparison
equal deleted inserted replaced
199:a65b630b3a66 200:abeaebe0a33c
39 }; 39 };
40 40
41 41
42 ngx_int_t ngx_http_static_translate_handler(ngx_http_request_t *r) 42 ngx_int_t ngx_http_static_translate_handler(ngx_http_request_t *r)
43 { 43 {
44 char *location, *last, *path;
45 uint32_t crc;
44 ngx_int_t rc, level; 46 ngx_int_t rc, level;
45 uint32_t crc; 47 ngx_str_t name;
46 char *location, *last;
47 ngx_err_t err; 48 ngx_err_t err;
48 ngx_http_cache_t *cache; 49 ngx_http_cache_t *cache;
49 ngx_http_cache_conf_t *ccf; 50 ngx_http_cache_conf_t *ccf;
50 ngx_http_core_loc_conf_t *clcf; 51 ngx_http_core_loc_conf_t *clcf;
51 52
53 return NGX_HTTP_NOT_ALLOWED; 54 return NGX_HTTP_NOT_ALLOWED;
54 } 55 }
55 56
56 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); 57 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
57 58
59
58 if (r->uri.data[r->uri.len - 1] == '/') { 60 if (r->uri.data[r->uri.len - 1] == '/') {
59 if (r->path.data == NULL) { 61
60 ngx_test_null(r->path.data, 62 /* there is no index handler */
61 ngx_palloc(r->pool, 63
62 clcf->doc_root.len + r->uri.len), 64 if (!(path = ngx_palloc(r->pool, clcf->doc_root.len + r->uri.len))) {
63 NGX_HTTP_INTERNAL_SERVER_ERROR); 65 return NGX_HTTP_INTERNAL_SERVER_ERROR;
64 66 }
65 ngx_cpystrn(ngx_cpymem(r->path.data, clcf->doc_root.data, 67
66 clcf->doc_root.len), 68 ngx_cpystrn(ngx_cpymem(path, clcf->doc_root.data, clcf->doc_root.len),
67 r->uri.data, r->uri.len + 1); 69 r->uri.data, r->uri.len + 1);
68
69 } else {
70 r->path.data[r->path.len] = '\0';
71 }
72 70
73 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, 71 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
74 "directory index of \"%s\" is forbidden", r->path.data); 72 "directory index of \"%s\" is forbidden", path);
75 73
76 return NGX_HTTP_FORBIDDEN; 74 return NGX_HTTP_FORBIDDEN;
77 } 75 }
78 76
79 /* "+ 2" is for trailing '/' in possible redirect and '\0' */ 77
80 ngx_test_null(r->file.name.data, 78 /* "+ 2" is for a trailing '/' in a possible redirect and '\0' */
79 ngx_test_null(name.data,
81 ngx_palloc(r->pool, clcf->doc_root.len + r->uri.len + 2), 80 ngx_palloc(r->pool, clcf->doc_root.len + r->uri.len + 2),
82 NGX_HTTP_INTERNAL_SERVER_ERROR); 81 NGX_HTTP_INTERNAL_SERVER_ERROR);
83 82
84 location = ngx_cpymem(r->file.name.data, clcf->doc_root.data, 83 location = ngx_cpymem(name.data, clcf->doc_root.data, clcf->doc_root.len);
85 clcf->doc_root.len),
86
87 last = ngx_cpystrn(location, r->uri.data, r->uri.len + 1); 84 last = ngx_cpystrn(location, r->uri.data, r->uri.len + 1);
88 85
89 ngx_log_debug(r->connection->log, "HTTP filename: '%s'" _ r->file.name.data); 86 ngx_log_debug(r->connection->log, "HTTP filename: '%s'" _ data);
90 87
91 ccf = ngx_http_get_module_loc_conf(r, ngx_http_cache_module); 88
92 89 if (r->cache == NULL) {
93 if (ccf->open_files) { 90
94 cache = ngx_http_cache_get(ccf->open_files, &r->file.name, &crc); 91 /* look up an open files cache */
95 92
96 ngx_log_debug(r->connection->log, "cache get: %x" _ cache); 93 ccf = ngx_http_get_module_loc_conf(r, ngx_http_cache_module);
97 94
98 if (cache 95 if (ccf->open_files) {
99 && ((ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) 96 cache = ngx_http_cache_get(ccf->open_files, &name, &crc);
100 || ccf->open_files->update >= ngx_cached_time - cache->updated)) 97
101 { 98 ngx_log_debug(r->connection->log, "cache get: %x" _ cache);
102 cache->refs++; 99
103 r->file.fd = cache->fd; 100 if (cache
104 r->file.name = cache->key; 101 && ((ngx_event_flags & NGX_HAVE_KQUEUE_EVENT)
105 r->content_handler = ngx_http_static_handler; 102 || ccf->open_files->update
106 103 >= ngx_cached_time - cache->updated))
107 return NGX_OK; 104 {
105 r->cache = cache;
106 r->content_handler = ngx_http_static_handler;
107
108 return NGX_OK;
109 }
110
111 } else {
112 cache = NULL;
108 } 113 }
109 114
110 } else { 115 } else {
111 cache = NULL; 116 cache = r->cache;
112 } 117 }
118
113 119
114 #if (WIN9X) 120 #if (WIN9X)
115 121
116 if (ngx_win32_version < NGX_WIN_NT) { 122 if (ngx_win32_version < NGX_WIN_NT) {
117 123
119 * there is no way to open a file or a directory in Win9X with 125 * there is no way to open a file or a directory in Win9X with
120 * one syscall because Win9X has no FILE_FLAG_BACKUP_SEMANTICS flag 126 * one syscall because Win9X has no FILE_FLAG_BACKUP_SEMANTICS flag
121 * so we need to check its type before the opening 127 * so we need to check its type before the opening
122 */ 128 */
123 129
124 if (ngx_file_info(r->file.name.data, &r->file.info) == NGX_FILE_ERROR) { 130 if (ngx_file_info(name.data, &r->file.info) == NGX_FILE_ERROR) {
125 err = ngx_errno; 131 err = ngx_errno;
126 ngx_log_error(NGX_LOG_ERR, r->connection->log, err, 132 ngx_log_error(NGX_LOG_ERR, r->connection->log, err,
127 ngx_file_info_n " \"%s\" failed", r->file.name.data); 133 ngx_file_info_n " \"%s\" failed", name.data);
128 134
129 if (err == NGX_ENOENT || err == NGX_ENOTDIR) { 135 if (err == NGX_ENOENT || err == NGX_ENOTDIR) {
130 return NGX_HTTP_NOT_FOUND; 136 return NGX_HTTP_NOT_FOUND;
131 137
132 } else if (err == NGX_EACCES) { 138 } else if (err == NGX_EACCES) {
136 return NGX_HTTP_INTERNAL_SERVER_ERROR; 142 return NGX_HTTP_INTERNAL_SERVER_ERROR;
137 } 143 }
138 } 144 }
139 145
140 if (ngx_is_dir(&r->file.info)) { 146 if (ngx_is_dir(&r->file.info)) {
141 ngx_log_debug(r->connection->log, "HTTP DIR: '%s'" _ r->file.name.data); 147 ngx_log_debug(r->connection->log, "HTTP DIR: '%s'" _ name.data);
142 148
143 if (!(r->headers_out.location = 149 if (!(r->headers_out.location =
144 ngx_http_add_header(&r->headers_out, ngx_http_headers_out))) 150 ngx_http_add_header(&r->headers_out, ngx_http_headers_out)))
145 { 151 {
146 return NGX_HTTP_INTERNAL_SERVER_ERROR; 152 return NGX_HTTP_INTERNAL_SERVER_ERROR;
156 return NGX_HTTP_MOVED_PERMANENTLY; 162 return NGX_HTTP_MOVED_PERMANENTLY;
157 } 163 }
158 } 164 }
159 165
160 #endif 166 #endif
167
161 168
162 if (r->file.fd == NGX_INVALID_FILE) { 169 if (r->file.fd == NGX_INVALID_FILE) {
163 r->file.fd = ngx_open_file(r->file.name.data, 170 r->file.fd = ngx_open_file(r->file.name.data,
164 NGX_FILE_RDONLY, NGX_FILE_OPEN); 171 NGX_FILE_RDONLY, NGX_FILE_OPEN);
165 } 172 }
280 ngx_log_e level; 287 ngx_log_e level;
281 ngx_err_t err; 288 ngx_err_t err;
282 ngx_hunk_t *h; 289 ngx_hunk_t *h;
283 ngx_chain_t out; 290 ngx_chain_t out;
284 ngx_http_type_t *type; 291 ngx_http_type_t *type;
292 ngx_http_cleanup_t *cleanup;
285 ngx_http_log_ctx_t *ctx; 293 ngx_http_log_ctx_t *ctx;
286 ngx_http_core_loc_conf_t *clcf; 294 ngx_http_core_loc_conf_t *clcf;
287 295
288 rc = ngx_http_discard_body(r); 296 rc = ngx_http_discard_body(r);
289 297
291 return rc; 299 return rc;
292 } 300 }
293 301
294 ctx = r->connection->log->data; 302 ctx = r->connection->log->data;
295 ctx->action = "sending response to client"; 303 ctx->action = "sending response to client";
304
305 if (!(cleanup = ngx_push_array(&r->cleanup))) {
306 return NGX_HTTP_INTERNAL_SERVER_ERROR;
307 }
296 308
297 if (r->file.fd == NGX_INVALID_FILE) { 309 if (r->file.fd == NGX_INVALID_FILE) {
298 r->file.fd = ngx_open_file(r->file.name.data, 310 r->file.fd = ngx_open_file(r->file.name.data,
299 NGX_FILE_RDONLY, NGX_FILE_OPEN); 311 NGX_FILE_RDONLY, NGX_FILE_OPEN);
300 312
313 ngx_log_error(level, r->connection->log, ngx_errno, 325 ngx_log_error(level, r->connection->log, ngx_errno,
314 ngx_open_file_n " %s failed", r->file.name.data); 326 ngx_open_file_n " %s failed", r->file.name.data);
315 return rc; 327 return rc;
316 } 328 }
317 } 329 }
330
331 cleanup->data.file.fd = r->file.fd;
332 cleanup->data.file.name = r->file.name.data;
333 cleanup->cache = 0;
318 334
319 if (!r->file.info_valid) { 335 if (!r->file.info_valid) {
320 if (ngx_fd_info(r->file.fd, &r->file.info) == NGX_FILE_ERROR) { 336 if (ngx_fd_info(r->file.fd, &r->file.info) == NGX_FILE_ERROR) {
321 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno, 337 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
322 ngx_fd_info_n " %s failed", r->file.name.data); 338 ngx_fd_info_n " %s failed", r->file.name.data);
376 ngx_test_null(h, ngx_pcalloc(r->pool, sizeof(ngx_hunk_t)), 392 ngx_test_null(h, ngx_pcalloc(r->pool, sizeof(ngx_hunk_t)),
377 NGX_HTTP_INTERNAL_SERVER_ERROR); 393 NGX_HTTP_INTERNAL_SERVER_ERROR);
378 394
379 ngx_test_null(h->file, ngx_pcalloc(r->pool, sizeof(ngx_file_t)), 395 ngx_test_null(h->file, ngx_pcalloc(r->pool, sizeof(ngx_file_t)),
380 NGX_HTTP_INTERNAL_SERVER_ERROR); 396 NGX_HTTP_INTERNAL_SERVER_ERROR);
381
382 397
383 rc = ngx_http_send_header(r); 398 rc = ngx_http_send_header(r);
384 399
385 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) { 400 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
386 return rc; 401 return rc;