Mercurial > hg > nginx-vendor-0-8
comparison src/http/modules/ngx_http_static_module.c @ 50:72eb30262aac NGINX_0_1_25
nginx 0.1.25
*) Bugfix: nginx did run on Linux parisc.
*) Feature: nginx now does not start under FreeBSD if the sysctl
kern.ipc.somaxconn value is too big.
*) Bugfix: if a request was internally redirected by the
ngx_http_index_module module to the ngx_http_proxy_module or
ngx_http_fastcgi_module modules, then the index file was not closed
after request completion.
*) Feature: the "proxy_pass" can be used in location with regular
expression.
*) Feature: the ngx_http_rewrite_filter_module module supports the
condition like "if ($HTTP_USER_AGENT ~ MSIE)".
*) Bugfix: nginx started too slow if the large number of addresses and
text values were used in the "geo" directive.
*) Change: a variable name must be declared as "$name" in the "geo"
directive. The previous variant without "$" is still supported, but
will be removed soon.
*) Feature: the "%{VARIABLE}v" logging parameter.
*) Feature: the "set $name value" directive.
*) Bugfix: gcc 4.0 compatibility.
*) Feature: the --with-openssl-opt=OPTIONS autoconfiguration directive.
author | Igor Sysoev <http://sysoev.ru> |
---|---|
date | Sat, 19 Mar 2005 00:00:00 +0300 |
parents | |
children | b55cbf18157e |
comparison
equal
deleted
inserted
replaced
49:93dabbc9efb9 | 50:72eb30262aac |
---|---|
1 | |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4 */ | |
5 | |
6 | |
7 #include <ngx_config.h> | |
8 #include <ngx_core.h> | |
9 #include <ngx_http.h> | |
10 | |
11 | |
12 typedef struct { | |
13 ngx_http_cache_hash_t *redirect_cache; | |
14 } ngx_http_static_loc_conf_t; | |
15 | |
16 | |
17 static ngx_int_t ngx_http_static_handler(ngx_http_request_t *r); | |
18 static void *ngx_http_static_create_loc_conf(ngx_conf_t *cf); | |
19 static char *ngx_http_static_merge_loc_conf(ngx_conf_t *cf, | |
20 void *parent, void *child); | |
21 static ngx_int_t ngx_http_static_init(ngx_cycle_t *cycle); | |
22 | |
23 | |
24 static ngx_command_t ngx_http_static_commands[] = { | |
25 | |
26 #if (NGX_HTTP_CACHE) | |
27 | |
28 { ngx_string("redirect_cache"), | |
29 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE3, | |
30 ngx_http_set_cache_slot, | |
31 NGX_HTTP_LOC_CONF_OFFSET, | |
32 offsetof(ngx_http_static_loc_conf_t, redirect_cache), | |
33 NULL }, | |
34 | |
35 #endif | |
36 | |
37 ngx_null_command | |
38 }; | |
39 | |
40 | |
41 | |
42 ngx_http_module_t ngx_http_static_module_ctx = { | |
43 NULL, /* pre conf */ | |
44 | |
45 NULL, /* create main configuration */ | |
46 NULL, /* init main configuration */ | |
47 | |
48 NULL, /* create server configuration */ | |
49 NULL, /* merge server configuration */ | |
50 | |
51 ngx_http_static_create_loc_conf, /* create location configuration */ | |
52 ngx_http_static_merge_loc_conf /* merge location configuration */ | |
53 }; | |
54 | |
55 | |
56 ngx_module_t ngx_http_static_module = { | |
57 NGX_MODULE, | |
58 &ngx_http_static_module_ctx, /* module context */ | |
59 ngx_http_static_commands, /* module directives */ | |
60 NGX_HTTP_MODULE, /* module type */ | |
61 ngx_http_static_init, /* init module */ | |
62 NULL /* init process */ | |
63 }; | |
64 | |
65 | |
66 static ngx_int_t ngx_http_static_handler(ngx_http_request_t *r) | |
67 { | |
68 u_char *last; | |
69 ngx_fd_t fd; | |
70 ngx_int_t rc; | |
71 ngx_uint_t level; | |
72 ngx_str_t name, location; | |
73 ngx_err_t err; | |
74 ngx_log_t *log; | |
75 ngx_buf_t *b; | |
76 ngx_chain_t out; | |
77 ngx_file_info_t fi; | |
78 ngx_http_cleanup_t *file_cleanup; | |
79 #if (NGX_HTTP_CACHE) | |
80 ngx_http_cleanup_t *redirect_cleanup; | |
81 #endif | |
82 ngx_http_core_loc_conf_t *clcf; | |
83 #if (NGX_HTTP_CACHE) | |
84 ngx_http_static_loc_conf_t *slcf; | |
85 uint32_t file_crc, redirect_crc; | |
86 ngx_http_cache_t *file, *redirect; | |
87 #endif | |
88 | |
89 if (r->uri.data[r->uri.len - 1] == '/') { | |
90 return NGX_DECLINED; | |
91 } | |
92 | |
93 /* TODO: Win32 */ | |
94 if (r->zero_in_uri) { | |
95 return NGX_DECLINED; | |
96 } | |
97 | |
98 if (r->method != NGX_HTTP_GET && r->method != NGX_HTTP_HEAD) { | |
99 return NGX_HTTP_NOT_ALLOWED; | |
100 } | |
101 | |
102 rc = ngx_http_discard_body(r); | |
103 | |
104 if (rc != NGX_OK && rc != NGX_AGAIN) { | |
105 return rc; | |
106 } | |
107 | |
108 #if (NGX_HTTP_CACHE) | |
109 | |
110 /* | |
111 * there is a valid cached open file, i.e by the index handler, | |
112 * and it should be already registered in r->cleanup | |
113 */ | |
114 | |
115 if (r->cache && !r->cache->expired) { | |
116 return ngx_http_send_cached(r); | |
117 } | |
118 | |
119 #endif | |
120 | |
121 log = r->connection->log; | |
122 | |
123 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); | |
124 | |
125 /* | |
126 * make a file name, reserve 2 bytes for a trailing '/' | |
127 * in a possible redirect and for the last '\0' | |
128 */ | |
129 | |
130 if (clcf->alias) { | |
131 name.data = ngx_palloc(r->pool, clcf->root.len + r->uri.len + 2 | |
132 - clcf->name.len); | |
133 if (name.data == NULL) { | |
134 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
135 } | |
136 | |
137 last = ngx_cpymem(name.data, clcf->root.data, clcf->root.len); | |
138 last = ngx_cpystrn(last, r->uri.data + clcf->name.len, | |
139 r->uri.len + 1 - clcf->name.len); | |
140 | |
141 name.len = last - name.data; | |
142 | |
143 location.data = ngx_palloc(r->pool, r->uri.len + 2); | |
144 if (location.data == NULL) { | |
145 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
146 } | |
147 | |
148 last = ngx_cpystrn(location.data, r->uri.data, r->uri.len + 1); | |
149 | |
150 #if 0 | |
151 /* | |
152 * aliases usually have trailling "/", | |
153 * set it in the start of the possible redirect | |
154 */ | |
155 | |
156 if (*location.data != '/') { | |
157 location.data--; | |
158 } | |
159 #endif | |
160 | |
161 location.len = last - location.data + 1; | |
162 | |
163 } else { | |
164 name.data = ngx_palloc(r->pool, clcf->root.len + r->uri.len + 2); | |
165 if (name.data == NULL) { | |
166 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
167 } | |
168 | |
169 location.data = ngx_cpymem(name.data, clcf->root.data, clcf->root.len); | |
170 last = ngx_cpystrn(location.data, r->uri.data, r->uri.len + 1); | |
171 | |
172 name.len = last - name.data; | |
173 location.len = last - location.data + 1; | |
174 } | |
175 | |
176 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0, | |
177 "http filename: \"%s\"", name.data); | |
178 | |
179 | |
180 /* allocate cleanups */ | |
181 | |
182 file_cleanup = ngx_array_push(&r->cleanup); | |
183 if (file_cleanup == NULL) { | |
184 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
185 } | |
186 file_cleanup->valid = 0; | |
187 | |
188 #if (NGX_HTTP_CACHE) | |
189 | |
190 slcf = ngx_http_get_module_loc_conf(r, ngx_http_static_module); | |
191 if (slcf->redirect_cache) { | |
192 redirect_cleanup = ngx_array_push(&r->cleanup); | |
193 if (redirect_cleanup == NULL) { | |
194 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
195 } | |
196 redirect_cleanup->valid = 0; | |
197 | |
198 } else { | |
199 redirect_cleanup = NULL; | |
200 } | |
201 | |
202 /* look up an open files cache */ | |
203 | |
204 if (clcf->open_files) { | |
205 file = ngx_http_cache_get(clcf->open_files, file_cleanup, | |
206 &name, &file_crc); | |
207 | |
208 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0, | |
209 "http open file cache get: %p", file); | |
210 | |
211 if (file && !file->expired) { | |
212 r->cache = file; | |
213 return ngx_http_send_cached(r); | |
214 } | |
215 | |
216 } else { | |
217 file = NULL; | |
218 } | |
219 | |
220 | |
221 /* look up an redirect cache */ | |
222 | |
223 if (slcf->redirect_cache) { | |
224 redirect = ngx_http_cache_get(slcf->redirect_cache, redirect_cleanup, | |
225 &name, &redirect_crc); | |
226 | |
227 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0, | |
228 "http redirect cache get: %p", redirect); | |
229 | |
230 if (redirect && !redirect->expired) { | |
231 | |
232 /* | |
233 * We do not copy a cached value so the cache entry is locked | |
234 * until the end of the request. In a single threaded model | |
235 * the redirected request should complete before other event | |
236 * will be processed. In a multithreaded model this locking | |
237 * should keep more popular redirects in cache. | |
238 */ | |
239 | |
240 r->headers_out.location = ngx_http_add_header(&r->headers_out, | |
241 ngx_http_headers_out); | |
242 if (r->headers_out.location == NULL) { | |
243 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
244 } | |
245 | |
246 r->headers_out.location->value = redirect->data.value; | |
247 | |
248 return NGX_HTTP_MOVED_PERMANENTLY; | |
249 } | |
250 | |
251 } else { | |
252 redirect = NULL; | |
253 } | |
254 | |
255 #endif | |
256 | |
257 /* open file */ | |
258 | |
259 #if (NGX_WIN9X) | |
260 | |
261 /* TODO: redirect cache */ | |
262 | |
263 if (ngx_win32_version < NGX_WIN_NT) { | |
264 | |
265 /* | |
266 * there is no way to open a file or a directory in Win9X with | |
267 * one syscall because Win9X has no FILE_FLAG_BACKUP_SEMANTICS flag | |
268 * so we need to check its type before the opening | |
269 */ | |
270 | |
271 if (ngx_file_info(name.data, &fi) == NGX_FILE_ERROR) { | |
272 err = ngx_errno; | |
273 ngx_log_error(NGX_LOG_ERR, log, err, | |
274 ngx_file_info_n " \"%s\" failed", name.data); | |
275 | |
276 if (err == NGX_ENOENT || err == NGX_ENOTDIR) { | |
277 return NGX_HTTP_NOT_FOUND; | |
278 | |
279 } else if (err == NGX_EACCES) { | |
280 return NGX_HTTP_FORBIDDEN; | |
281 | |
282 } else { | |
283 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
284 } | |
285 } | |
286 | |
287 if (ngx_is_dir(&fi)) { | |
288 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0, | |
289 "HTTP DIR: \"%s\"", name.data); | |
290 | |
291 r->headers_out.location = ngx_http_add_header(&r->headers_out, | |
292 ngx_http_headers_out); | |
293 if (r->headers_out.location == NULL) { | |
294 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
295 } | |
296 | |
297 *last++ = '/'; | |
298 *last = '\0'; | |
299 r->headers_out.location->value.len = last - location; | |
300 r->headers_out.location->value.data = location; | |
301 | |
302 return NGX_HTTP_MOVED_PERMANENTLY; | |
303 } | |
304 } | |
305 | |
306 #endif | |
307 | |
308 | |
309 fd = ngx_open_file(name.data, NGX_FILE_RDONLY, NGX_FILE_OPEN); | |
310 | |
311 if (fd == NGX_INVALID_FILE) { | |
312 err = ngx_errno; | |
313 | |
314 if (err == NGX_ENOENT || err == NGX_ENOTDIR) { | |
315 level = NGX_LOG_ERR; | |
316 rc = NGX_HTTP_NOT_FOUND; | |
317 | |
318 } else if (err == NGX_EACCES) { | |
319 level = NGX_LOG_ERR; | |
320 rc = NGX_HTTP_FORBIDDEN; | |
321 | |
322 } else { | |
323 level = NGX_LOG_CRIT; | |
324 rc = NGX_HTTP_INTERNAL_SERVER_ERROR; | |
325 } | |
326 | |
327 ngx_log_error(level, log, err, | |
328 ngx_open_file_n " \"%s\" failed", name.data); | |
329 | |
330 return rc; | |
331 } | |
332 | |
333 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0, "http static fd: %d", fd); | |
334 | |
335 if (ngx_fd_info(fd, &fi) == NGX_FILE_ERROR) { | |
336 ngx_log_error(NGX_LOG_CRIT, log, ngx_errno, | |
337 ngx_fd_info_n " \"%s\" failed", name.data); | |
338 | |
339 if (ngx_close_file(fd) == NGX_FILE_ERROR) { | |
340 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, | |
341 ngx_close_file_n " \"%s\" failed", name.data); | |
342 } | |
343 | |
344 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
345 } | |
346 | |
347 if (ngx_is_dir(&fi)) { | |
348 | |
349 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0, "http dir"); | |
350 | |
351 if (ngx_close_file(fd) == NGX_FILE_ERROR) { | |
352 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, | |
353 ngx_close_file_n " \"%s\" failed", name.data); | |
354 } | |
355 | |
356 *last++ = '/'; | |
357 *last = '\0'; | |
358 | |
359 r->headers_out.location = ngx_list_push(&r->headers_out.headers); | |
360 if (r->headers_out.location == NULL) { | |
361 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
362 } | |
363 | |
364 r->headers_out.location->value = location; | |
365 | |
366 #if (NGX_HTTP_CACHE) | |
367 | |
368 if (slcf->redirect_cache) { | |
369 if (redirect) { | |
370 if (location.len == redirect->data.value.len | |
371 && ngx_memcmp(redirect->data.value.data, location.data, | |
372 location.len) == 0) | |
373 { | |
374 redirect->accessed = ngx_cached_time; | |
375 redirect->updated = ngx_cached_time; | |
376 | |
377 /* | |
378 * we can unlock the cache entry because | |
379 * we have the local copy anyway | |
380 */ | |
381 | |
382 ngx_http_cache_unlock(slcf->redirect_cache, redirect, log); | |
383 redirect_cleanup->valid = 0; | |
384 | |
385 return NGX_HTTP_MOVED_PERMANENTLY; | |
386 } | |
387 } | |
388 | |
389 location.len++; | |
390 redirect = ngx_http_cache_alloc(slcf->redirect_cache, redirect, | |
391 redirect_cleanup, | |
392 &name, redirect_crc, | |
393 &location, log); | |
394 location.len--; | |
395 | |
396 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0, | |
397 "http redirect cache alloc: %p", redirect); | |
398 | |
399 if (redirect) { | |
400 redirect->fd = NGX_INVALID_FILE; | |
401 redirect->accessed = ngx_cached_time; | |
402 redirect->last_modified = 0; | |
403 redirect->updated = ngx_cached_time; | |
404 redirect->memory = 1; | |
405 ngx_http_cache_unlock(slcf->redirect_cache, redirect, log); | |
406 redirect_cleanup->valid = 0; | |
407 } | |
408 | |
409 } | |
410 | |
411 #endif | |
412 | |
413 return NGX_HTTP_MOVED_PERMANENTLY; | |
414 } | |
415 | |
416 #if !(NGX_WIN32) /* the not regular files are probably Unix specific */ | |
417 | |
418 if (!ngx_is_file(&fi)) { | |
419 ngx_log_error(NGX_LOG_CRIT, log, ngx_errno, | |
420 "\"%s\" is not a regular file", name.data); | |
421 | |
422 if (ngx_close_file(fd) == NGX_FILE_ERROR) { | |
423 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, | |
424 ngx_close_file_n " \"%s\" failed", name.data); | |
425 } | |
426 | |
427 return NGX_HTTP_NOT_FOUND; | |
428 } | |
429 | |
430 #endif | |
431 | |
432 | |
433 #if (NGX_HTTP_CACHE) | |
434 | |
435 if (clcf->open_files) { | |
436 | |
437 #if (NGX_USE_HTTP_FILE_CACHE_UNIQ) | |
438 | |
439 if (file && file->uniq == ngx_file_uniq(&fi)) { | |
440 if (ngx_close_file(fd) == NGX_FILE_ERROR) { | |
441 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, | |
442 ngx_close_file_n " \"%s\" failed", name.data); | |
443 } | |
444 file->accessed = ngx_cached_time; | |
445 file->updated = ngx_cached_time; | |
446 file->expired = 0; | |
447 r->cache = file; | |
448 | |
449 return ngx_http_send_cached(r); | |
450 | |
451 } else { | |
452 if (file) { | |
453 ngx_http_cache_unlock(clcf->open_files, file, log); | |
454 file = NULL; | |
455 } | |
456 | |
457 file = ngx_http_cache_alloc(clcf->open_files, file, | |
458 file_cleanup, | |
459 &name, file_crc, NULL, log); | |
460 if (file) { | |
461 file->uniq = ngx_file_uniq(&fi); | |
462 } | |
463 } | |
464 | |
465 #else | |
466 file = ngx_http_cache_alloc(clcf->open_files, file, | |
467 file_cleanup, | |
468 &name, file_crc, NULL, log); | |
469 #endif | |
470 | |
471 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0, | |
472 "http open file cache alloc: %p", file); | |
473 | |
474 if (file) { | |
475 file->fd = fd; | |
476 file->data.size = ngx_file_size(&fi); | |
477 file->accessed = ngx_cached_time; | |
478 file->last_modified = ngx_file_mtime(&fi); | |
479 file->updated = ngx_cached_time; | |
480 r->cache = file; | |
481 } | |
482 | |
483 return ngx_http_send_cached(r); | |
484 } | |
485 | |
486 #endif | |
487 | |
488 log->action = "sending response to client"; | |
489 | |
490 file_cleanup->data.file.fd = fd; | |
491 file_cleanup->data.file.name = name.data; | |
492 file_cleanup->valid = 1; | |
493 file_cleanup->cache = 0; | |
494 | |
495 r->headers_out.status = NGX_HTTP_OK; | |
496 r->headers_out.content_length_n = ngx_file_size(&fi); | |
497 r->headers_out.last_modified_time = ngx_file_mtime(&fi); | |
498 | |
499 if (r->headers_out.content_length_n == 0) { | |
500 r->header_only = 1; | |
501 } | |
502 | |
503 if (ngx_http_set_content_type(r) != NGX_OK) { | |
504 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
505 } | |
506 | |
507 #if (NGX_SUPPRESS_WARN) | |
508 b = NULL; | |
509 #endif | |
510 | |
511 if (!r->header_only) { | |
512 /* we need to allocate all before the header would be sent */ | |
513 | |
514 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)); | |
515 if (b == NULL) { | |
516 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
517 } | |
518 | |
519 b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t)); | |
520 if (b->file == NULL) { | |
521 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
522 } | |
523 | |
524 r->filter_allow_ranges = 1; | |
525 } | |
526 | |
527 rc = ngx_http_send_header(r); | |
528 | |
529 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) { | |
530 return rc; | |
531 } | |
532 | |
533 b->in_file = 1; | |
534 | |
535 if (!r->main) { | |
536 b->last_buf = 1; | |
537 } | |
538 | |
539 b->file_pos = 0; | |
540 b->file_last = ngx_file_size(&fi); | |
541 | |
542 b->file->fd = fd; | |
543 b->file->name = name; | |
544 b->file->log = log; | |
545 | |
546 out.buf = b; | |
547 out.next = NULL; | |
548 | |
549 return ngx_http_output_filter(r, &out); | |
550 } | |
551 | |
552 | |
553 static void *ngx_http_static_create_loc_conf(ngx_conf_t *cf) | |
554 { | |
555 ngx_http_static_loc_conf_t *conf; | |
556 | |
557 conf = ngx_palloc(cf->pool, sizeof(ngx_http_static_loc_conf_t)); | |
558 if (conf == NULL) { | |
559 return NGX_CONF_ERROR; | |
560 } | |
561 | |
562 conf->redirect_cache = NULL; | |
563 | |
564 return conf; | |
565 } | |
566 | |
567 | |
568 static char *ngx_http_static_merge_loc_conf(ngx_conf_t *cf, | |
569 void *parent, void *child) | |
570 { | |
571 ngx_http_static_loc_conf_t *prev = parent; | |
572 ngx_http_static_loc_conf_t *conf = child; | |
573 | |
574 if (conf->redirect_cache == NULL) { | |
575 conf->redirect_cache = prev->redirect_cache; | |
576 } | |
577 | |
578 return NGX_CONF_OK; | |
579 } | |
580 | |
581 | |
582 static ngx_int_t ngx_http_static_init(ngx_cycle_t *cycle) | |
583 { | |
584 ngx_http_handler_pt *h; | |
585 ngx_http_core_main_conf_t *cmcf; | |
586 | |
587 cmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_core_module); | |
588 | |
589 h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers); | |
590 if (h == NULL) { | |
591 return NGX_ERROR; | |
592 } | |
593 | |
594 *h = ngx_http_static_handler; | |
595 | |
596 return NGX_OK; | |
597 } |