0
|
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 child */
|
|
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, *redirect_cleanup;
|
|
79 ngx_http_core_loc_conf_t *clcf;
|
|
80 ngx_http_static_loc_conf_t *slcf;
|
|
81 #if (NGX_HTTP_CACHE)
|
|
82 uint32_t file_crc, redirect_crc;
|
|
83 ngx_http_cache_t *file, *redirect;
|
|
84 #endif
|
|
85
|
|
86 if (r->uri.data[r->uri.len - 1] == '/') {
|
|
87 return NGX_DECLINED;
|
|
88 }
|
|
89
|
38
|
90 /* TODO: Win32 */
|
|
91 if (r->zero_in_uri) {
|
|
92 return NGX_DECLINED;
|
|
93 }
|
|
94
|
0
|
95 if (r->method != NGX_HTTP_GET && r->method != NGX_HTTP_HEAD) {
|
|
96 return NGX_HTTP_NOT_ALLOWED;
|
|
97 }
|
|
98
|
|
99 rc = ngx_http_discard_body(r);
|
|
100
|
|
101 if (rc != NGX_OK && rc != NGX_AGAIN) {
|
|
102 return rc;
|
|
103 }
|
|
104
|
|
105 #if (NGX_HTTP_CACHE)
|
|
106
|
|
107 /*
|
|
108 * there is a valid cached open file, i.e by the index handler,
|
|
109 * and it should be already registered in r->cleanup
|
|
110 */
|
|
111
|
|
112 if (r->cache && !r->cache->expired) {
|
|
113 return ngx_http_send_cached(r);
|
|
114 }
|
|
115
|
|
116 #endif
|
|
117
|
|
118 log = r->connection->log;
|
|
119
|
|
120 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
121
|
|
122 /*
|
|
123 * make a file name, reserve 2 bytes for a trailing '/'
|
|
124 * in a possible redirect and for the last '\0'
|
|
125 */
|
|
126
|
|
127 if (clcf->alias) {
|
|
128 name.data = ngx_palloc(r->pool, clcf->root.len + r->uri.len + 2
|
|
129 - clcf->name.len);
|
|
130 if (name.data == NULL) {
|
|
131 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
132 }
|
|
133
|
|
134 last = ngx_cpymem(name.data, clcf->root.data, clcf->root.len);
|
|
135 last = ngx_cpystrn(last, r->uri.data + clcf->name.len,
|
|
136 r->uri.len + 1 - clcf->name.len);
|
|
137
|
|
138 name.len = last - name.data;
|
|
139
|
|
140 location.data = ngx_palloc(r->pool, r->uri.len + 2);
|
|
141 if (location.data == NULL) {
|
|
142 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
143 }
|
|
144
|
|
145 last = ngx_cpystrn(location.data, r->uri.data, r->uri.len + 1);
|
|
146
|
|
147 #if 0
|
|
148 /*
|
|
149 * aliases usually have trailling "/",
|
|
150 * set it in the start of the possible redirect
|
|
151 */
|
|
152
|
|
153 if (*location.data != '/') {
|
|
154 location.data--;
|
|
155 }
|
|
156 #endif
|
|
157
|
|
158 location.len = last - location.data + 1;
|
|
159
|
|
160 } else {
|
|
161 name.data = ngx_palloc(r->pool, clcf->root.len + r->uri.len + 2);
|
|
162 if (name.data == NULL) {
|
|
163 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
164 }
|
|
165
|
|
166 location.data = ngx_cpymem(name.data, clcf->root.data, clcf->root.len);
|
|
167 last = ngx_cpystrn(location.data, r->uri.data, r->uri.len + 1);
|
|
168
|
|
169 name.len = last - name.data;
|
|
170 location.len = last - location.data + 1;
|
|
171 }
|
|
172
|
|
173 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
|
|
174 "http filename: \"%s\"", name.data);
|
|
175
|
|
176
|
|
177 /* allocate cleanups */
|
|
178
|
|
179 if (!(file_cleanup = ngx_push_array(&r->cleanup))) {
|
|
180 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
181 }
|
|
182 file_cleanup->valid = 0;
|
|
183
|
|
184 slcf = ngx_http_get_module_loc_conf(r, ngx_http_static_module);
|
|
185 if (slcf->redirect_cache) {
|
|
186 if (!(redirect_cleanup = ngx_push_array(&r->cleanup))) {
|
|
187 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
188 }
|
|
189 redirect_cleanup->valid = 0;
|
|
190
|
|
191 } else {
|
|
192 redirect_cleanup = NULL;
|
|
193 }
|
|
194
|
|
195 #if (NGX_HTTP_CACHE)
|
|
196
|
|
197 /* look up an open files cache */
|
|
198
|
|
199 if (clcf->open_files) {
|
|
200 file = ngx_http_cache_get(clcf->open_files, file_cleanup,
|
|
201 &name, &file_crc);
|
|
202
|
|
203 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
|
10
|
204 "http open file cache get: %p", file);
|
0
|
205
|
|
206 if (file && !file->expired) {
|
|
207 r->cache = file;
|
|
208 return ngx_http_send_cached(r);
|
|
209 }
|
|
210
|
|
211 } else {
|
|
212 file = NULL;
|
|
213 }
|
|
214
|
|
215
|
|
216 /* look up an redirect cache */
|
|
217
|
|
218 if (slcf->redirect_cache) {
|
|
219 redirect = ngx_http_cache_get(slcf->redirect_cache, redirect_cleanup,
|
|
220 &name, &redirect_crc);
|
|
221
|
|
222 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
|
10
|
223 "http redirect cache get: %p", redirect);
|
0
|
224
|
|
225 if (redirect && !redirect->expired) {
|
|
226
|
|
227 /*
|
|
228 * We do not copy a cached value so the cache entry is locked
|
|
229 * until the end of the request. In a single threaded model
|
|
230 * the redirected request should complete before other event
|
|
231 * will be processed. In a multithreaded model this locking
|
|
232 * should keep more popular redirects in cache.
|
|
233 */
|
|
234
|
|
235 if (!(r->headers_out.location =
|
|
236 ngx_http_add_header(&r->headers_out, ngx_http_headers_out)))
|
|
237 {
|
|
238 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
239 }
|
|
240
|
|
241 r->headers_out.location->value = redirect->data.value;
|
|
242
|
|
243 return NGX_HTTP_MOVED_PERMANENTLY;
|
|
244 }
|
|
245
|
|
246 } else {
|
|
247 redirect = NULL;
|
|
248 }
|
|
249
|
|
250 #endif
|
|
251
|
|
252 /* open file */
|
|
253
|
10
|
254 #if (NGX_WIN9X)
|
0
|
255
|
|
256 /* TODO: redirect cache */
|
|
257
|
|
258 if (ngx_win32_version < NGX_WIN_NT) {
|
|
259
|
|
260 /*
|
|
261 * there is no way to open a file or a directory in Win9X with
|
|
262 * one syscall because Win9X has no FILE_FLAG_BACKUP_SEMANTICS flag
|
|
263 * so we need to check its type before the opening
|
|
264 */
|
|
265
|
|
266 if (ngx_file_info(name.data, &fi) == NGX_FILE_ERROR) {
|
|
267 err = ngx_errno;
|
|
268 ngx_log_error(NGX_LOG_ERR, log, err,
|
|
269 ngx_file_info_n " \"%s\" failed", name.data);
|
|
270
|
|
271 if (err == NGX_ENOENT || err == NGX_ENOTDIR) {
|
|
272 return NGX_HTTP_NOT_FOUND;
|
|
273
|
|
274 } else if (err == NGX_EACCES) {
|
|
275 return NGX_HTTP_FORBIDDEN;
|
|
276
|
|
277 } else {
|
|
278 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
279 }
|
|
280 }
|
|
281
|
|
282 if (ngx_is_dir(&fi)) {
|
10
|
283 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
|
|
284 "HTTP DIR: \"%s\"", name.data);
|
0
|
285
|
|
286 if (!(r->headers_out.location =
|
|
287 ngx_http_add_header(&r->headers_out, ngx_http_headers_out)))
|
|
288 {
|
|
289 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
290 }
|
|
291
|
|
292 *last++ = '/';
|
|
293 *last = '\0';
|
|
294 r->headers_out.location->value.len = last - location;
|
|
295 r->headers_out.location->value.data = location;
|
|
296
|
|
297 return NGX_HTTP_MOVED_PERMANENTLY;
|
|
298 }
|
|
299 }
|
|
300
|
|
301 #endif
|
|
302
|
|
303
|
|
304 fd = ngx_open_file(name.data, NGX_FILE_RDONLY, NGX_FILE_OPEN);
|
|
305
|
|
306 if (fd == NGX_INVALID_FILE) {
|
|
307 err = ngx_errno;
|
|
308
|
|
309 if (err == NGX_ENOENT || err == NGX_ENOTDIR) {
|
|
310 level = NGX_LOG_ERR;
|
|
311 rc = NGX_HTTP_NOT_FOUND;
|
|
312
|
|
313 } else if (err == NGX_EACCES) {
|
|
314 level = NGX_LOG_ERR;
|
|
315 rc = NGX_HTTP_FORBIDDEN;
|
|
316
|
|
317 } else {
|
|
318 level = NGX_LOG_CRIT;
|
|
319 rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
320 }
|
|
321
|
|
322 ngx_log_error(level, log, err,
|
|
323 ngx_open_file_n " \"%s\" failed", name.data);
|
|
324
|
|
325 return rc;
|
|
326 }
|
|
327
|
|
328 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0, "http static fd: %d", fd);
|
|
329
|
|
330 if (ngx_fd_info(fd, &fi) == NGX_FILE_ERROR) {
|
|
331 ngx_log_error(NGX_LOG_CRIT, log, ngx_errno,
|
|
332 ngx_fd_info_n " \"%s\" failed", name.data);
|
|
333
|
|
334 if (ngx_close_file(fd) == NGX_FILE_ERROR) {
|
|
335 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
|
|
336 ngx_close_file_n " \"%s\" failed", name.data);
|
|
337 }
|
|
338
|
|
339 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
340 }
|
|
341
|
|
342 if (ngx_is_dir(&fi)) {
|
|
343
|
|
344 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0, "http dir");
|
|
345
|
|
346 if (ngx_close_file(fd) == NGX_FILE_ERROR) {
|
|
347 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
|
|
348 ngx_close_file_n " \"%s\" failed", name.data);
|
|
349 }
|
|
350
|
|
351 *last++ = '/';
|
|
352 *last = '\0';
|
|
353
|
|
354 r->headers_out.location = ngx_list_push(&r->headers_out.headers);
|
|
355 if (r->headers_out.location == NULL) {
|
|
356 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
357 }
|
|
358
|
|
359 r->headers_out.location->value = location;
|
|
360
|
|
361 #if (NGX_HTTP_CACHE)
|
|
362
|
|
363 if (slcf->redirect_cache) {
|
|
364 if (redirect) {
|
|
365 if (location.len == redirect->data.value.len
|
|
366 && ngx_memcmp(redirect->data.value.data, location.data,
|
|
367 location.len) == 0)
|
|
368 {
|
|
369 redirect->accessed = ngx_cached_time;
|
|
370 redirect->updated = ngx_cached_time;
|
|
371
|
|
372 /*
|
|
373 * we can unlock the cache entry because
|
|
374 * we have the local copy anyway
|
|
375 */
|
|
376
|
|
377 ngx_http_cache_unlock(slcf->redirect_cache, redirect, log);
|
|
378 redirect_cleanup->valid = 0;
|
|
379
|
|
380 return NGX_HTTP_MOVED_PERMANENTLY;
|
|
381 }
|
|
382 }
|
|
383
|
|
384 location.len++;
|
|
385 redirect = ngx_http_cache_alloc(slcf->redirect_cache, redirect,
|
|
386 redirect_cleanup,
|
|
387 &name, redirect_crc,
|
|
388 &location, log);
|
|
389 location.len--;
|
|
390
|
|
391 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
|
10
|
392 "http redirect cache alloc: %p", redirect);
|
0
|
393
|
|
394 if (redirect) {
|
|
395 redirect->fd = NGX_INVALID_FILE;
|
|
396 redirect->accessed = ngx_cached_time;
|
|
397 redirect->last_modified = 0;
|
|
398 redirect->updated = ngx_cached_time;
|
|
399 redirect->memory = 1;
|
|
400 ngx_http_cache_unlock(slcf->redirect_cache, redirect, log);
|
|
401 redirect_cleanup->valid = 0;
|
|
402 }
|
|
403
|
|
404 }
|
|
405
|
|
406 #endif
|
|
407
|
|
408 return NGX_HTTP_MOVED_PERMANENTLY;
|
|
409 }
|
|
410
|
10
|
411 #if !(NGX_WIN32) /* the not regular files are probably Unix specific */
|
0
|
412
|
|
413 if (!ngx_is_file(&fi)) {
|
|
414 ngx_log_error(NGX_LOG_CRIT, log, ngx_errno,
|
10
|
415 "\"%s\" is not a regular file", name.data);
|
0
|
416
|
|
417 if (ngx_close_file(fd) == NGX_FILE_ERROR) {
|
|
418 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
|
|
419 ngx_close_file_n " \"%s\" failed", name.data);
|
|
420 }
|
|
421
|
|
422 return NGX_HTTP_NOT_FOUND;
|
|
423 }
|
|
424
|
|
425 #endif
|
|
426
|
|
427
|
|
428 #if (NGX_HTTP_CACHE)
|
|
429
|
|
430 if (clcf->open_files) {
|
|
431
|
|
432 #if (NGX_USE_HTTP_FILE_CACHE_UNIQ)
|
|
433
|
|
434 if (file && file->uniq == ngx_file_uniq(&fi)) {
|
|
435 if (ngx_close_file(fd) == NGX_FILE_ERROR) {
|
|
436 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
|
|
437 ngx_close_file_n " \"%s\" failed", name.data);
|
|
438 }
|
|
439 file->accessed = ngx_cached_time;
|
|
440 file->updated = ngx_cached_time;
|
|
441 file->expired = 0;
|
|
442 r->cache = file;
|
|
443
|
|
444 return ngx_http_send_cached(r);
|
|
445
|
|
446 } else {
|
|
447 if (file) {
|
|
448 ngx_http_cache_unlock(clcf->open_files, file, log);
|
|
449 file = NULL;
|
|
450 }
|
|
451
|
|
452 file = ngx_http_cache_alloc(clcf->open_files, file,
|
|
453 file_cleanup,
|
|
454 &name, file_crc, NULL, log);
|
|
455 if (file) {
|
|
456 file->uniq = ngx_file_uniq(&fi);
|
|
457 }
|
|
458 }
|
|
459
|
|
460 #else
|
|
461 file = ngx_http_cache_alloc(clcf->open_files, file,
|
|
462 file_cleanup,
|
|
463 &name, file_crc, NULL, log);
|
|
464 #endif
|
|
465
|
|
466 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
|
10
|
467 "http open file cache alloc: %p", file);
|
0
|
468
|
|
469 if (file) {
|
|
470 file->fd = fd;
|
|
471 file->data.size = ngx_file_size(&fi);
|
|
472 file->accessed = ngx_cached_time;
|
|
473 file->last_modified = ngx_file_mtime(&fi);
|
|
474 file->updated = ngx_cached_time;
|
|
475 r->cache = file;
|
|
476 }
|
|
477
|
|
478 return ngx_http_send_cached(r);
|
|
479 }
|
|
480
|
|
481 #endif
|
|
482
|
32
|
483 log->action = "sending response to client";
|
0
|
484
|
|
485 file_cleanup->data.file.fd = fd;
|
|
486 file_cleanup->data.file.name = name.data;
|
|
487 file_cleanup->valid = 1;
|
|
488 file_cleanup->cache = 0;
|
|
489
|
|
490 r->headers_out.status = NGX_HTTP_OK;
|
|
491 r->headers_out.content_length_n = ngx_file_size(&fi);
|
|
492 r->headers_out.last_modified_time = ngx_file_mtime(&fi);
|
|
493
|
|
494 if (r->headers_out.content_length_n == 0) {
|
|
495 r->header_only = 1;
|
|
496 }
|
|
497
|
|
498 if (ngx_http_set_content_type(r) != NGX_OK) {
|
|
499 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
500 }
|
|
501
|
|
502 #if (NGX_SUPPRESS_WARN)
|
|
503 b = NULL;
|
|
504 #endif
|
|
505
|
|
506 if (!r->header_only) {
|
|
507 /* we need to allocate all before the header would be sent */
|
|
508
|
|
509 if (!(b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)))) {
|
|
510 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
511 }
|
|
512
|
|
513 if (!(b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t)))) {
|
|
514 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
515 }
|
|
516
|
|
517 r->filter_allow_ranges = 1;
|
|
518 }
|
|
519
|
|
520 rc = ngx_http_send_header(r);
|
|
521
|
|
522 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
|
|
523 return rc;
|
|
524 }
|
|
525
|
|
526 b->in_file = 1;
|
|
527
|
|
528 if (!r->main) {
|
|
529 b->last_buf = 1;
|
|
530 }
|
|
531
|
|
532 b->file_pos = 0;
|
|
533 b->file_last = ngx_file_size(&fi);
|
|
534
|
|
535 b->file->fd = fd;
|
14
|
536 b->file->name = name;
|
0
|
537 b->file->log = log;
|
|
538
|
|
539 out.buf = b;
|
|
540 out.next = NULL;
|
|
541
|
|
542 return ngx_http_output_filter(r, &out);
|
|
543 }
|
|
544
|
|
545
|
|
546 static void *ngx_http_static_create_loc_conf(ngx_conf_t *cf)
|
|
547 {
|
|
548 ngx_http_static_loc_conf_t *conf;
|
|
549
|
|
550 if (!(conf = ngx_palloc(cf->pool, sizeof(ngx_http_static_loc_conf_t)))) {
|
|
551 return NGX_CONF_ERROR;
|
|
552 }
|
|
553
|
|
554 conf->redirect_cache = NULL;
|
|
555
|
|
556 return conf;
|
|
557 }
|
|
558
|
|
559
|
|
560 static char *ngx_http_static_merge_loc_conf(ngx_conf_t *cf,
|
|
561 void *parent, void *child)
|
|
562 {
|
|
563 ngx_http_static_loc_conf_t *prev = parent;
|
|
564 ngx_http_static_loc_conf_t *conf = child;
|
|
565
|
|
566 if (conf->redirect_cache == NULL) {
|
|
567 conf->redirect_cache = prev->redirect_cache;
|
|
568 }
|
|
569
|
|
570 return NGX_CONF_OK;
|
|
571 }
|
|
572
|
|
573
|
|
574 static ngx_int_t ngx_http_static_init(ngx_cycle_t *cycle)
|
|
575 {
|
|
576 ngx_http_handler_pt *h;
|
|
577 ngx_http_core_main_conf_t *cmcf;
|
|
578
|
|
579 cmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_core_module);
|
|
580
|
|
581 h = ngx_push_array(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
|
|
582 if (h == NULL) {
|
|
583 return NGX_ERROR;
|
|
584 }
|
|
585
|
|
586 *h = ngx_http_static_handler;
|
|
587
|
|
588 return NGX_OK;
|
|
589 }
|