2002-08-16 01:20:26 +08:00
|
|
|
|
|
|
|
#include <ngx_config.h>
|
2003-05-14 00:02:32 +08:00
|
|
|
#include <ngx_core.h>
|
2002-08-20 22:48:28 +08:00
|
|
|
#include <ngx_http.h>
|
|
|
|
|
2002-08-16 01:20:26 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
typedef struct {
|
|
|
|
ngx_http_cache_hash_t *redirect_cache;
|
|
|
|
} ngx_http_static_loc_conf_t;
|
|
|
|
|
|
|
|
|
|
|
|
static ngx_int_t ngx_http_static_handler(ngx_http_request_t *r);
|
|
|
|
static void *ngx_http_static_create_loc_conf(ngx_conf_t *cf);
|
|
|
|
static char *ngx_http_static_merge_loc_conf(ngx_conf_t *cf,
|
|
|
|
void *parent, void *child);
|
|
|
|
static ngx_int_t ngx_http_static_init(ngx_cycle_t *cycle);
|
2003-10-17 04:19:16 +08:00
|
|
|
|
|
|
|
|
|
|
|
static ngx_command_t ngx_http_static_commands[] = {
|
|
|
|
|
2004-03-13 00:57:08 +08:00
|
|
|
#if (NGX_HTTP_CACHE)
|
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
{ ngx_string("redirect_cache"),
|
|
|
|
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE3,
|
|
|
|
ngx_http_set_cache_slot,
|
|
|
|
NGX_HTTP_LOC_CONF_OFFSET,
|
|
|
|
offsetof(ngx_http_static_loc_conf_t, redirect_cache),
|
|
|
|
NULL },
|
|
|
|
|
2004-03-13 00:57:08 +08:00
|
|
|
#endif
|
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
ngx_null_command
|
2003-10-17 04:19:16 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ngx_http_module_t ngx_http_static_module_ctx = {
|
2003-11-11 01:17:31 +08:00
|
|
|
NULL, /* pre conf */
|
|
|
|
|
2003-10-17 04:19:16 +08:00
|
|
|
NULL, /* create main configuration */
|
|
|
|
NULL, /* init main configuration */
|
|
|
|
|
|
|
|
NULL, /* create server configuration */
|
|
|
|
NULL, /* merge server configuration */
|
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
ngx_http_static_create_loc_conf, /* create location configuration */
|
|
|
|
ngx_http_static_merge_loc_conf /* merge location configuration */
|
2003-10-17 04:19:16 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
ngx_module_t ngx_http_static_module = {
|
|
|
|
NGX_MODULE,
|
|
|
|
&ngx_http_static_module_ctx, /* module context */
|
|
|
|
ngx_http_static_commands, /* module directives */
|
|
|
|
NGX_HTTP_MODULE, /* module type */
|
|
|
|
ngx_http_static_init, /* init module */
|
|
|
|
NULL /* init child */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
static ngx_int_t ngx_http_static_handler(ngx_http_request_t *r)
|
2003-10-17 04:19:16 +08:00
|
|
|
{
|
2004-03-16 15:10:12 +08:00
|
|
|
u_char *last;
|
2003-12-01 04:03:18 +08:00
|
|
|
ngx_fd_t fd;
|
2004-02-12 01:08:49 +08:00
|
|
|
ngx_int_t rc;
|
|
|
|
ngx_uint_t level;
|
2003-12-01 04:03:18 +08:00
|
|
|
ngx_str_t name, location;
|
|
|
|
ngx_err_t err;
|
|
|
|
ngx_log_t *log;
|
|
|
|
ngx_hunk_t *h;
|
|
|
|
ngx_chain_t out;
|
|
|
|
ngx_file_info_t fi;
|
|
|
|
ngx_http_cleanup_t *file_cleanup, *redirect_cleanup;
|
|
|
|
ngx_http_log_ctx_t *ctx;
|
|
|
|
ngx_http_core_loc_conf_t *clcf;
|
|
|
|
ngx_http_static_loc_conf_t *slcf;
|
2004-03-15 04:46:25 +08:00
|
|
|
#if (NGX_HTTP_CACHE)
|
|
|
|
uint32_t file_crc, redirect_crc;
|
|
|
|
ngx_http_cache_t *file, *redirect;
|
|
|
|
#endif
|
2003-12-01 04:03:18 +08:00
|
|
|
|
|
|
|
if (r->uri.data[r->uri.len - 1] == '/') {
|
|
|
|
return NGX_DECLINED;
|
|
|
|
}
|
2003-10-17 04:19:16 +08:00
|
|
|
|
|
|
|
if (r->method != NGX_HTTP_GET && r->method != NGX_HTTP_HEAD) {
|
|
|
|
return NGX_HTTP_NOT_ALLOWED;
|
|
|
|
}
|
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
rc = ngx_http_discard_body(r);
|
|
|
|
|
2004-03-23 14:01:52 +08:00
|
|
|
if (rc != NGX_OK && rc != NGX_AGAIN) {
|
2003-12-01 04:03:18 +08:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2004-03-13 00:57:08 +08:00
|
|
|
#if (NGX_HTTP_CACHE)
|
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
/*
|
2004-02-09 15:46:43 +08:00
|
|
|
* there is a valid cached open file, i.e by the index handler,
|
|
|
|
* and it should be already registered in r->cleanup
|
2003-12-01 04:03:18 +08:00
|
|
|
*/
|
|
|
|
|
2003-12-02 00:28:14 +08:00
|
|
|
if (r->cache && !r->cache->expired) {
|
2003-12-01 04:03:18 +08:00
|
|
|
return ngx_http_send_cached(r);
|
|
|
|
}
|
|
|
|
|
2004-03-13 00:57:08 +08:00
|
|
|
#endif
|
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
log = r->connection->log;
|
|
|
|
|
2003-10-17 04:19:16 +08:00
|
|
|
clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
/*
|
2004-02-09 15:46:43 +08:00
|
|
|
* make a file name, reserve 2 bytes for a trailing '/'
|
|
|
|
* in a possible redirect and for the last '\0'
|
2003-12-01 04:03:18 +08:00
|
|
|
*/
|
2003-11-29 01:41:47 +08:00
|
|
|
|
2004-03-16 15:10:12 +08:00
|
|
|
name.data = ngx_palloc(r->pool, clcf->root.len + r->uri.len + 2
|
|
|
|
- clcf->alias * clcf->name.len);
|
2004-02-09 15:46:43 +08:00
|
|
|
if (name.data == NULL) {
|
|
|
|
return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
|
|
}
|
2003-12-01 04:03:18 +08:00
|
|
|
|
2004-03-16 15:10:12 +08:00
|
|
|
location.data = ngx_cpymem(name.data, clcf->root.data, clcf->root.len);
|
|
|
|
|
|
|
|
if (clcf->alias) {
|
|
|
|
last = ngx_cpystrn(location.data, r->uri.data + clcf->name.len,
|
|
|
|
r->uri.len + 1 - clcf->name.len);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* aliases usually have trailling "/",
|
|
|
|
* set it in the start of the possible redirect
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (*location.data != '/') {
|
|
|
|
location.data--;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
last = ngx_cpystrn(location.data, r->uri.data, r->uri.len + 1);
|
|
|
|
}
|
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
name.len = last - name.data;
|
|
|
|
location.len = last - location.data + 1;
|
|
|
|
|
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
|
|
|
|
"http filename: \"%s\"", name.data);
|
2003-10-17 04:19:16 +08:00
|
|
|
|
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
/* allocate cleanups */
|
|
|
|
|
|
|
|
if (!(file_cleanup = ngx_push_array(&r->cleanup))) {
|
|
|
|
return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
|
|
}
|
|
|
|
file_cleanup->valid = 0;
|
|
|
|
|
|
|
|
slcf = ngx_http_get_module_loc_conf(r, ngx_http_static_module);
|
|
|
|
if (slcf->redirect_cache) {
|
|
|
|
if (!(redirect_cleanup = ngx_push_array(&r->cleanup))) {
|
2003-11-29 01:41:47 +08:00
|
|
|
return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
2003-10-17 04:19:16 +08:00
|
|
|
}
|
2003-12-01 04:03:18 +08:00
|
|
|
redirect_cleanup->valid = 0;
|
2003-10-17 04:19:16 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
} else {
|
|
|
|
redirect_cleanup = NULL;
|
|
|
|
}
|
2003-11-29 01:41:47 +08:00
|
|
|
|
2004-03-13 00:57:08 +08:00
|
|
|
#if (NGX_HTTP_CACHE)
|
2003-10-17 04:19:16 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
/* look up an open files cache */
|
2003-10-17 04:19:16 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
if (clcf->open_files) {
|
|
|
|
file = ngx_http_cache_get(clcf->open_files, file_cleanup,
|
|
|
|
&name, &file_crc);
|
2003-11-29 01:41:47 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
|
|
|
|
"http open file cache get: " PTR_FMT, file);
|
2003-10-17 04:19:16 +08:00
|
|
|
|
2003-12-02 00:28:14 +08:00
|
|
|
if (file && !file->expired) {
|
2003-12-01 04:03:18 +08:00
|
|
|
r->cache = file;
|
|
|
|
return ngx_http_send_cached(r);
|
|
|
|
}
|
2003-10-17 04:19:16 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
} else {
|
|
|
|
file = NULL;
|
|
|
|
}
|
2003-10-17 04:19:16 +08:00
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
/* look up an redirect cache */
|
2003-11-26 04:44:56 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
if (slcf->redirect_cache) {
|
|
|
|
redirect = ngx_http_cache_get(slcf->redirect_cache, redirect_cleanup,
|
|
|
|
&name, &redirect_crc);
|
2003-11-26 04:44:56 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
|
|
|
|
"http redirect cache get: " PTR_FMT, redirect);
|
2003-11-26 04:44:56 +08:00
|
|
|
|
2003-12-02 00:28:14 +08:00
|
|
|
if (redirect && !redirect->expired) {
|
2003-11-29 01:41:47 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
/*
|
|
|
|
* We do not copy a cached value so the cache entry is locked
|
|
|
|
* until the end of the request. In a single threaded model
|
|
|
|
* the redirected request should complete before other event
|
|
|
|
* will be processed. In a multithreaded model this locking
|
|
|
|
* should keep more popular redirects in cache.
|
|
|
|
*/
|
2003-11-29 01:41:47 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
if (!(r->headers_out.location =
|
|
|
|
ngx_http_add_header(&r->headers_out, ngx_http_headers_out)))
|
2003-11-29 01:41:47 +08:00
|
|
|
{
|
2003-12-01 04:03:18 +08:00
|
|
|
return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
2003-11-29 01:41:47 +08:00
|
|
|
}
|
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
r->headers_out.location->value = redirect->data.value;
|
|
|
|
|
|
|
|
return NGX_HTTP_MOVED_PERMANENTLY;
|
2003-11-26 04:44:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2003-12-01 04:03:18 +08:00
|
|
|
redirect = NULL;
|
2003-11-26 04:44:56 +08:00
|
|
|
}
|
|
|
|
|
2004-03-13 00:57:08 +08:00
|
|
|
#endif
|
2003-11-29 01:41:47 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
/* open file */
|
|
|
|
|
2003-10-17 04:19:16 +08:00
|
|
|
#if (WIN9X)
|
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
/* TODO: redirect cache */
|
|
|
|
|
2003-11-14 00:16:33 +08:00
|
|
|
if (ngx_win32_version < NGX_WIN_NT) {
|
2003-10-17 04:19:16 +08:00
|
|
|
|
2003-11-14 00:16:33 +08:00
|
|
|
/*
|
2003-11-15 00:52:04 +08:00
|
|
|
* there is no way to open a file or a directory in Win9X with
|
|
|
|
* one syscall because Win9X has no FILE_FLAG_BACKUP_SEMANTICS flag
|
|
|
|
* so we need to check its type before the opening
|
2003-11-14 00:16:33 +08:00
|
|
|
*/
|
2003-10-17 04:19:16 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
if (ngx_file_info(name.data, &fi) == NGX_FILE_ERROR) {
|
2003-11-14 00:16:33 +08:00
|
|
|
err = ngx_errno;
|
2003-12-01 04:03:18 +08:00
|
|
|
ngx_log_error(NGX_LOG_ERR, log, err,
|
2003-11-29 01:41:47 +08:00
|
|
|
ngx_file_info_n " \"%s\" failed", name.data);
|
2003-10-17 04:19:16 +08:00
|
|
|
|
2003-11-14 00:16:33 +08:00
|
|
|
if (err == NGX_ENOENT || err == NGX_ENOTDIR) {
|
|
|
|
return NGX_HTTP_NOT_FOUND;
|
|
|
|
|
|
|
|
} else if (err == NGX_EACCES) {
|
|
|
|
return NGX_HTTP_FORBIDDEN;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
if (ngx_is_dir(&fi)) {
|
|
|
|
ngx_log_debug(log, "HTTP DIR: '%s'" _ name.data);
|
2003-11-14 00:16:33 +08:00
|
|
|
|
|
|
|
if (!(r->headers_out.location =
|
|
|
|
ngx_http_add_header(&r->headers_out, ngx_http_headers_out)))
|
|
|
|
{
|
|
|
|
return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
*last++ = '/';
|
|
|
|
*last = '\0';
|
|
|
|
r->headers_out.location->value.len = last - location;
|
|
|
|
r->headers_out.location->value.data = location;
|
|
|
|
|
|
|
|
return NGX_HTTP_MOVED_PERMANENTLY;
|
2003-10-17 04:19:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-11-14 00:16:33 +08:00
|
|
|
#endif
|
2003-10-17 04:19:16 +08:00
|
|
|
|
2003-11-29 01:41:47 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
fd = ngx_open_file(name.data, NGX_FILE_RDONLY, NGX_FILE_OPEN);
|
2003-10-17 04:19:16 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
if (fd == NGX_INVALID_FILE) {
|
2003-10-17 04:19:16 +08:00
|
|
|
err = ngx_errno;
|
|
|
|
|
|
|
|
if (err == NGX_ENOENT || err == NGX_ENOTDIR) {
|
2003-11-03 06:56:18 +08:00
|
|
|
level = NGX_LOG_ERR;
|
|
|
|
rc = NGX_HTTP_NOT_FOUND;
|
2003-10-17 04:19:16 +08:00
|
|
|
|
|
|
|
} else if (err == NGX_EACCES) {
|
2003-11-03 06:56:18 +08:00
|
|
|
level = NGX_LOG_ERR;
|
|
|
|
rc = NGX_HTTP_FORBIDDEN;
|
2003-10-17 04:19:16 +08:00
|
|
|
|
|
|
|
} else {
|
2003-11-03 06:56:18 +08:00
|
|
|
level = NGX_LOG_CRIT;
|
|
|
|
rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
|
2003-10-17 04:19:16 +08:00
|
|
|
}
|
2003-11-03 06:56:18 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
ngx_log_error(level, log, err,
|
|
|
|
ngx_open_file_n " \"%s\" failed", name.data);
|
2003-11-03 06:56:18 +08:00
|
|
|
|
|
|
|
return rc;
|
2003-10-17 04:19:16 +08:00
|
|
|
}
|
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0, "http static fd: %d", fd);
|
2003-10-17 04:19:16 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
if (ngx_fd_info(fd, &fi) == NGX_FILE_ERROR) {
|
|
|
|
ngx_log_error(NGX_LOG_CRIT, log, ngx_errno,
|
|
|
|
ngx_fd_info_n " \"%s\" failed", name.data);
|
2003-10-17 04:19:16 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
if (ngx_close_file(fd) == NGX_FILE_ERROR) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
|
|
|
|
ngx_close_file_n " \"%s\" failed", name.data);
|
2003-10-17 04:19:16 +08:00
|
|
|
}
|
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
2003-10-17 04:19:16 +08:00
|
|
|
}
|
2003-10-27 16:53:49 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
if (ngx_is_dir(&fi)) {
|
2003-11-27 15:45:22 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0, "http dir");
|
2003-11-27 15:45:22 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
if (ngx_close_file(fd) == NGX_FILE_ERROR) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
|
|
|
|
ngx_close_file_n " \"%s\" failed", name.data);
|
2003-10-17 04:19:16 +08:00
|
|
|
}
|
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
*last++ = '/';
|
|
|
|
*last = '\0';
|
2003-10-17 04:19:16 +08:00
|
|
|
|
2003-10-30 01:39:05 +08:00
|
|
|
if (!(r->headers_out.location =
|
|
|
|
ngx_http_add_header(&r->headers_out, ngx_http_headers_out)))
|
|
|
|
{
|
|
|
|
return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
|
|
}
|
2003-10-17 04:19:16 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
r->headers_out.location->value = location;
|
2003-10-17 04:19:16 +08:00
|
|
|
|
2004-03-13 00:57:08 +08:00
|
|
|
#if (NGX_HTTP_CACHE)
|
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
if (slcf->redirect_cache) {
|
|
|
|
if (redirect) {
|
|
|
|
if (location.len == redirect->data.value.len
|
|
|
|
&& ngx_memcmp(redirect->data.value.data, location.data,
|
|
|
|
location.len) == 0)
|
|
|
|
{
|
|
|
|
redirect->accessed = ngx_cached_time;
|
|
|
|
redirect->updated = ngx_cached_time;
|
2003-11-10 04:03:38 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
/*
|
|
|
|
* we can unlock the cache entry because
|
|
|
|
* we have the local copy anyway
|
|
|
|
*/
|
2003-11-10 04:03:38 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
ngx_http_cache_unlock(slcf->redirect_cache, redirect, log);
|
|
|
|
redirect_cleanup->valid = 0;
|
2003-11-10 04:03:38 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
return NGX_HTTP_MOVED_PERMANENTLY;
|
|
|
|
}
|
|
|
|
}
|
2003-11-10 04:03:38 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
location.len++;
|
|
|
|
redirect = ngx_http_cache_alloc(slcf->redirect_cache, redirect,
|
|
|
|
redirect_cleanup,
|
|
|
|
&name, redirect_crc,
|
|
|
|
&location, log);
|
|
|
|
location.len--;
|
|
|
|
|
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
|
|
|
|
"http redirect cache alloc: " PTR_FMT, redirect);
|
|
|
|
|
|
|
|
if (redirect) {
|
|
|
|
redirect->fd = NGX_INVALID_FILE;
|
|
|
|
redirect->accessed = ngx_cached_time;
|
|
|
|
redirect->last_modified = 0;
|
|
|
|
redirect->updated = ngx_cached_time;
|
|
|
|
redirect->memory = 1;
|
|
|
|
ngx_http_cache_unlock(slcf->redirect_cache, redirect, log);
|
|
|
|
redirect_cleanup->valid = 0;
|
|
|
|
}
|
2003-11-10 04:03:38 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
}
|
2003-10-17 04:19:16 +08:00
|
|
|
|
2004-03-13 00:57:08 +08:00
|
|
|
#endif
|
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
return NGX_HTTP_MOVED_PERMANENTLY;
|
|
|
|
}
|
2003-10-17 04:19:16 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
#if !(WIN32) /* the not regular files are probably Unix specific */
|
2003-10-17 04:19:16 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
if (!ngx_is_file(&fi)) {
|
|
|
|
ngx_log_error(NGX_LOG_CRIT, log, ngx_errno,
|
|
|
|
"%s is not a regular file", name.data);
|
2002-08-16 01:20:26 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
if (ngx_close_file(fd) == NGX_FILE_ERROR) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
|
|
|
|
ngx_close_file_n " \"%s\" failed", name.data);
|
|
|
|
}
|
2003-05-15 23:42:53 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
return NGX_HTTP_NOT_FOUND;
|
2003-05-15 23:42:53 +08:00
|
|
|
}
|
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
#endif
|
2003-11-29 01:41:47 +08:00
|
|
|
|
2002-12-11 02:05:12 +08:00
|
|
|
|
2004-03-13 00:57:08 +08:00
|
|
|
#if (NGX_HTTP_CACHE)
|
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
if (clcf->open_files) {
|
2002-12-15 14:25:09 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
#if (NGX_USE_HTTP_FILE_CACHE_UNIQ)
|
2003-04-12 00:01:14 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
if (file && file->uniq == ngx_file_uniq(&fi)) {
|
|
|
|
if (ngx_close_file(fd) == NGX_FILE_ERROR) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
|
|
|
|
ngx_close_file_n " \"%s\" failed", name.data);
|
2003-05-15 01:13:13 +08:00
|
|
|
}
|
2003-12-01 04:03:18 +08:00
|
|
|
file->accessed = ngx_cached_time;
|
|
|
|
file->updated = ngx_cached_time;
|
2003-12-02 00:28:14 +08:00
|
|
|
file->expired = 0;
|
2003-12-01 04:03:18 +08:00
|
|
|
r->cache = file;
|
2003-05-15 01:13:13 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
return ngx_http_send_cached(r);
|
2003-11-29 01:41:47 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
} else {
|
|
|
|
if (file) {
|
|
|
|
ngx_http_cache_unlock(clcf->open_files, file, log);
|
|
|
|
file = NULL;
|
|
|
|
}
|
2002-09-11 23:18:33 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
file = ngx_http_cache_alloc(clcf->open_files, file,
|
|
|
|
file_cleanup,
|
|
|
|
&name, file_crc, NULL, log);
|
|
|
|
if (file) {
|
|
|
|
file->uniq = ngx_file_uniq(&fi);
|
|
|
|
}
|
2002-12-15 14:25:09 +08:00
|
|
|
}
|
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
#else
|
|
|
|
file = ngx_http_cache_alloc(clcf->open_files, file,
|
|
|
|
file_cleanup,
|
|
|
|
&name, file_crc, NULL, log);
|
|
|
|
#endif
|
2002-08-16 01:20:26 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
|
|
|
|
"http open file cache alloc: " PTR_FMT, file);
|
|
|
|
|
|
|
|
if (file) {
|
|
|
|
file->fd = fd;
|
|
|
|
file->data.size = ngx_file_size(&fi);
|
|
|
|
file->accessed = ngx_cached_time;
|
|
|
|
file->last_modified = ngx_file_mtime(&fi);
|
|
|
|
file->updated = ngx_cached_time;
|
|
|
|
r->cache = file;
|
|
|
|
}
|
2002-08-16 01:20:26 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
return ngx_http_send_cached(r);
|
2003-10-30 01:39:05 +08:00
|
|
|
}
|
2002-12-15 14:25:09 +08:00
|
|
|
|
2004-03-13 00:57:08 +08:00
|
|
|
#endif
|
2002-12-11 02:05:12 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
ctx = log->data;
|
|
|
|
ctx->action = "sending response to client";
|
2003-05-15 01:13:13 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
file_cleanup->data.file.fd = fd;
|
|
|
|
file_cleanup->data.file.name = name.data;
|
|
|
|
file_cleanup->valid = 1;
|
|
|
|
file_cleanup->cache = 0;
|
2003-05-15 01:13:13 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
r->headers_out.status = NGX_HTTP_OK;
|
|
|
|
r->headers_out.content_length_n = ngx_file_size(&fi);
|
|
|
|
r->headers_out.last_modified_time = ngx_file_mtime(&fi);
|
2003-05-27 20:18:54 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
if (ngx_http_set_content_type(r) != NGX_OK) {
|
|
|
|
return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
2003-05-15 01:13:13 +08:00
|
|
|
}
|
2002-09-16 23:01:44 +08:00
|
|
|
|
2002-08-20 22:48:28 +08:00
|
|
|
|
2003-05-15 01:13:13 +08:00
|
|
|
/* we need to allocate all before the header would be sent */
|
2003-10-09 15:00:45 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
if (!(h = ngx_pcalloc(r->pool, sizeof(ngx_hunk_t)))) {
|
|
|
|
return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
|
|
}
|
2002-09-02 22:48:24 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
if (!(h->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t)))) {
|
|
|
|
return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
|
|
}
|
2002-08-16 01:20:26 +08:00
|
|
|
|
2003-05-27 20:18:54 +08:00
|
|
|
rc = ngx_http_send_header(r);
|
|
|
|
|
2003-10-10 23:10:50 +08:00
|
|
|
if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
|
|
|
|
return rc;
|
2003-05-27 20:18:54 +08:00
|
|
|
}
|
2002-08-16 01:20:26 +08:00
|
|
|
|
2003-10-09 15:00:45 +08:00
|
|
|
h->type = r->main ? NGX_HUNK_FILE : NGX_HUNK_FILE|NGX_HUNK_LAST;
|
|
|
|
|
2003-03-12 04:38:13 +08:00
|
|
|
h->file_pos = 0;
|
2003-12-01 04:03:18 +08:00
|
|
|
h->file_last = ngx_file_size(&fi);
|
2002-08-16 01:20:26 +08:00
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
h->file->fd = fd;
|
|
|
|
h->file->log = log;
|
2002-08-16 01:20:26 +08:00
|
|
|
|
2003-10-22 00:49:56 +08:00
|
|
|
out.hunk = h;
|
|
|
|
out.next = NULL;
|
|
|
|
|
|
|
|
return ngx_http_output_filter(r, &out);
|
2002-08-16 01:20:26 +08:00
|
|
|
}
|
2003-10-17 04:19:16 +08:00
|
|
|
|
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
static void *ngx_http_static_create_loc_conf(ngx_conf_t *cf)
|
|
|
|
{
|
|
|
|
ngx_http_static_loc_conf_t *conf;
|
|
|
|
|
|
|
|
if (!(conf = ngx_palloc(cf->pool, sizeof(ngx_http_static_loc_conf_t)))) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
conf->redirect_cache = NULL;
|
|
|
|
|
|
|
|
return conf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *ngx_http_static_merge_loc_conf(ngx_conf_t *cf,
|
|
|
|
void *parent, void *child)
|
|
|
|
{
|
|
|
|
ngx_http_static_loc_conf_t *prev = parent;
|
|
|
|
ngx_http_static_loc_conf_t *conf = child;
|
|
|
|
|
|
|
|
if (conf->redirect_cache == NULL) {
|
|
|
|
conf->redirect_cache = prev->redirect_cache;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NGX_CONF_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static ngx_int_t ngx_http_static_init(ngx_cycle_t *cycle)
|
2003-10-17 04:19:16 +08:00
|
|
|
{
|
|
|
|
ngx_http_handler_pt *h;
|
|
|
|
ngx_http_conf_ctx_t *ctx;
|
|
|
|
ngx_http_core_main_conf_t *cmcf;
|
|
|
|
|
|
|
|
ctx = (ngx_http_conf_ctx_t *) cycle->conf_ctx[ngx_http_module.index];
|
|
|
|
cmcf = ctx->main_conf[ngx_http_core_module.ctx_index];
|
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
h = ngx_push_array(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
|
|
|
|
if (h == NULL) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
*h = ngx_http_static_handler;
|
2003-10-17 04:19:16 +08:00
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|