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-10-17 04:19:16 +08:00
|
|
|
static int ngx_http_static_handler(ngx_http_request_t *r);
|
|
|
|
static int ngx_http_static_init(ngx_cycle_t *cycle);
|
|
|
|
|
|
|
|
|
|
|
|
static ngx_command_t ngx_http_static_commands[] = {
|
|
|
|
|
|
|
|
ngx_null_command
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ngx_http_module_t ngx_http_static_module_ctx = {
|
|
|
|
NULL, /* create main configuration */
|
|
|
|
NULL, /* init main configuration */
|
|
|
|
|
|
|
|
NULL, /* create server configuration */
|
|
|
|
NULL, /* merge server configuration */
|
|
|
|
|
|
|
|
NULL, /* create location configuration */
|
|
|
|
NULL /* merge location configuration */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
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 */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
int ngx_http_static_translate_handler(ngx_http_request_t *r)
|
|
|
|
{
|
|
|
|
char *location, *last;
|
|
|
|
ngx_err_t err;
|
|
|
|
ngx_table_elt_t *h;
|
|
|
|
ngx_http_core_loc_conf_t *clcf;
|
|
|
|
|
|
|
|
if (r->method != NGX_HTTP_GET && r->method != NGX_HTTP_HEAD) {
|
|
|
|
return NGX_HTTP_NOT_ALLOWED;
|
|
|
|
}
|
|
|
|
|
|
|
|
clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
|
|
|
|
|
|
if (r->uri.data[r->uri.len - 1] == '/') {
|
|
|
|
if (r->path.data == NULL) {
|
|
|
|
ngx_test_null(r->path.data,
|
|
|
|
ngx_palloc(r->pool,
|
|
|
|
clcf->doc_root.len + r->uri.len),
|
|
|
|
NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
|
|
|
|
|
|
ngx_cpystrn(ngx_cpymem(r->path.data, clcf->doc_root.data,
|
|
|
|
clcf->doc_root.len),
|
|
|
|
r->uri.data, r->uri.len + 1);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
r->path.data[r->path.len] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
|
|
"directory index of \"%s\" is forbidden", r->path.data);
|
|
|
|
|
|
|
|
return NGX_HTTP_FORBIDDEN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* "+ 2" is for trailing '/' in redirect and '\0' */
|
|
|
|
ngx_test_null(r->file.name.data,
|
|
|
|
ngx_palloc(r->pool, clcf->doc_root.len + r->uri.len + 2),
|
|
|
|
NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
|
|
|
|
|
|
location = ngx_cpymem(r->file.name.data, clcf->doc_root.data,
|
|
|
|
clcf->doc_root.len),
|
|
|
|
|
|
|
|
last = ngx_cpystrn(location, r->uri.data, r->uri.len + 1);
|
|
|
|
|
|
|
|
ngx_log_debug(r->connection->log, "HTTP filename: '%s'" _ r->file.name.data);
|
|
|
|
|
|
|
|
#if (WIN9X)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* There is no way to open a file or a directory in Win9X with
|
|
|
|
* one syscall: Win9X has no FILE_FLAG_BACKUP_SEMANTICS flag.
|
2003-10-27 16:53:49 +08:00
|
|
|
* So we need to check its type before the opening
|
2003-10-17 04:19:16 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
r->file.info.dwFileAttributes = GetFileAttributes(r->file.name.data);
|
|
|
|
if (r->file.info.dwFileAttributes == INVALID_FILE_ATTRIBUTES) {
|
|
|
|
err = ngx_errno;
|
|
|
|
ngx_log_error(NGX_LOG_ERR, r->connection->log, err,
|
|
|
|
ngx_file_type_n " \"%s\" failed", r->file.name.data);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
if (r->file.fd == NGX_INVALID_FILE) {
|
|
|
|
r->file.fd = ngx_open_file(r->file.name.data,
|
|
|
|
NGX_FILE_RDONLY, NGX_FILE_OPEN);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r->file.fd == NGX_INVALID_FILE) {
|
|
|
|
err = ngx_errno;
|
|
|
|
ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_errno,
|
|
|
|
ngx_open_file_n " \"%s\" failed", r->file.name.data);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_log_debug(r->connection->log, "FILE: %d" _ r->file.fd);
|
|
|
|
|
|
|
|
if (!r->file.info_valid) {
|
|
|
|
if (ngx_stat_fd(r->file.fd, &r->file.info) == NGX_FILE_ERROR) {
|
|
|
|
ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
|
|
|
|
ngx_stat_fd_n " \"%s\" failed", r->file.name.data);
|
|
|
|
|
|
|
|
if (ngx_close_file(r->file.fd) == NGX_FILE_ERROR) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno,
|
|
|
|
ngx_close_file_n " \"%s\" failed",
|
|
|
|
r->file.name.data);
|
|
|
|
}
|
|
|
|
|
|
|
|
r->file.fd = NGX_INVALID_FILE;
|
|
|
|
|
|
|
|
return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
r->file.info_valid = 1;
|
|
|
|
}
|
2003-10-27 16:53:49 +08:00
|
|
|
|
|
|
|
#if !(WIN32) /* the not regular files are probably Unix specific */
|
|
|
|
|
|
|
|
if (!ngx_is_file(r->file.info)) {
|
|
|
|
ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
|
|
|
|
"%s is not a regular file", r->file.name.data);
|
|
|
|
|
|
|
|
if (ngx_close_file(r->file.fd) == NGX_FILE_ERROR)
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno,
|
|
|
|
ngx_close_file_n " %s failed", r->file.name.data);
|
|
|
|
|
|
|
|
return NGX_HTTP_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2003-10-17 04:19:16 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (ngx_is_dir(r->file.info)) {
|
|
|
|
ngx_log_debug(r->connection->log, "HTTP DIR: '%s'" _ r->file.name.data);
|
|
|
|
|
|
|
|
#if !(WIN9X)
|
|
|
|
if (ngx_close_file(r->file.fd) == NGX_FILE_ERROR) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno,
|
|
|
|
ngx_close_file_n " \"%s\" failed", r->file.name.data);
|
|
|
|
}
|
|
|
|
|
|
|
|
r->file.fd = NGX_INVALID_FILE;
|
2003-10-27 16:53:49 +08:00
|
|
|
r->file.info_valid = 0;
|
2003-10-17 04:19:16 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
ngx_test_null(h, ngx_push_table(r->headers_out.headers),
|
|
|
|
NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
|
|
|
|
|
|
*last++ = '/';
|
|
|
|
*last = '\0';
|
|
|
|
h->key.len = 8;
|
|
|
|
h->key.data = "Location" ;
|
|
|
|
h->value.len = last - location;
|
|
|
|
h->value.data = location;
|
|
|
|
r->headers_out.location = h;
|
|
|
|
|
|
|
|
return NGX_HTTP_MOVED_PERMANENTLY;
|
|
|
|
}
|
|
|
|
|
|
|
|
r->content_handler = ngx_http_static_handler;
|
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int ngx_http_static_handler(ngx_http_request_t *r)
|
2002-08-16 01:20:26 +08:00
|
|
|
{
|
2003-05-27 20:18:54 +08:00
|
|
|
int rc, key, i;
|
|
|
|
ngx_log_e level;
|
|
|
|
ngx_err_t err;
|
|
|
|
ngx_hunk_t *h;
|
2003-10-22 00:49:56 +08:00
|
|
|
ngx_chain_t out;
|
2003-05-27 20:18:54 +08:00
|
|
|
ngx_http_type_t *type;
|
|
|
|
ngx_http_log_ctx_t *ctx;
|
|
|
|
ngx_http_core_loc_conf_t *clcf;
|
2002-08-16 01:20:26 +08:00
|
|
|
|
2003-05-15 23:42:53 +08:00
|
|
|
rc = ngx_http_discard_body(r);
|
|
|
|
|
|
|
|
if (rc != NGX_OK) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2002-09-02 22:48:24 +08:00
|
|
|
ctx = r->connection->log->data;
|
2003-10-27 16:53:49 +08:00
|
|
|
ctx->action = "sending response to client";
|
2002-09-02 22:48:24 +08:00
|
|
|
|
2003-05-15 01:13:13 +08:00
|
|
|
if (r->file.fd == NGX_INVALID_FILE) {
|
2003-06-02 23:24:30 +08:00
|
|
|
r->file.fd = ngx_open_file(r->file.name.data,
|
|
|
|
NGX_FILE_RDONLY, NGX_FILE_OPEN);
|
2002-12-11 02:05:12 +08:00
|
|
|
|
2003-05-15 01:13:13 +08:00
|
|
|
if (r->file.fd == NGX_INVALID_FILE) {
|
|
|
|
err = ngx_errno;
|
2002-12-15 14:25:09 +08:00
|
|
|
|
2003-05-15 01:13:13 +08:00
|
|
|
if (err == NGX_ENOENT || err == NGX_ENOTDIR) {
|
|
|
|
level = NGX_LOG_ERR;
|
|
|
|
rc = NGX_HTTP_NOT_FOUND;
|
2003-04-12 00:01:14 +08:00
|
|
|
|
2003-05-15 01:13:13 +08:00
|
|
|
} else {
|
|
|
|
level = NGX_LOG_CRIT;
|
|
|
|
rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_log_error(level, r->connection->log, ngx_errno,
|
|
|
|
ngx_open_file_n " %s failed", r->file.name.data);
|
|
|
|
return rc;
|
2003-04-12 00:01:14 +08:00
|
|
|
}
|
2002-12-15 14:25:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!r->file.info_valid) {
|
|
|
|
if (ngx_stat_fd(r->file.fd, &r->file.info) == NGX_FILE_ERROR) {
|
2003-05-15 01:13:13 +08:00
|
|
|
ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
|
2002-12-15 14:25:09 +08:00
|
|
|
ngx_stat_fd_n " %s failed", r->file.name.data);
|
|
|
|
|
|
|
|
if (ngx_close_file(r->file.fd) == NGX_FILE_ERROR)
|
2003-05-15 01:13:13 +08:00
|
|
|
ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno,
|
2002-12-15 14:25:09 +08:00
|
|
|
ngx_close_file_n " %s failed", r->file.name.data);
|
2002-09-11 23:18:33 +08:00
|
|
|
|
2002-12-15 14:25:09 +08:00
|
|
|
return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
r->file.info_valid = 1;
|
2002-08-20 22:48:28 +08:00
|
|
|
}
|
2002-08-16 01:20:26 +08:00
|
|
|
|
2002-12-03 00:09:40 +08:00
|
|
|
r->headers_out.status = NGX_HTTP_OK;
|
2003-10-22 00:49:56 +08:00
|
|
|
r->headers_out.content_length_n = ngx_file_size(r->file.info);
|
2002-12-15 14:25:09 +08:00
|
|
|
r->headers_out.last_modified_time = ngx_file_mtime(r->file.info);
|
2002-08-16 01:20:26 +08:00
|
|
|
|
2002-12-11 02:05:12 +08:00
|
|
|
ngx_test_null(r->headers_out.content_type,
|
|
|
|
ngx_push_table(r->headers_out.headers),
|
|
|
|
NGX_HTTP_INTERNAL_SERVER_ERROR);
|
2002-12-15 14:25:09 +08:00
|
|
|
|
2003-05-27 20:18:54 +08:00
|
|
|
r->headers_out.content_type->key.len = 0;
|
|
|
|
r->headers_out.content_type->key.data = NULL;
|
|
|
|
r->headers_out.content_type->value.len = 0;
|
|
|
|
r->headers_out.content_type->value.data = NULL;
|
|
|
|
|
|
|
|
clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
2002-12-11 02:05:12 +08:00
|
|
|
|
|
|
|
if (r->exten.len) {
|
2003-05-15 01:13:13 +08:00
|
|
|
ngx_http_types_hash_key(key, r->exten);
|
|
|
|
|
2003-05-27 20:18:54 +08:00
|
|
|
type = (ngx_http_type_t *) clcf->types[key].elts;
|
|
|
|
for (i = 0; i < clcf->types[key].nelts; i++) {
|
2003-05-15 01:13:13 +08:00
|
|
|
if (r->exten.len != type[i].exten.len) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ngx_strcasecmp(r->exten.data, type[i].exten.data) == 0) {
|
|
|
|
r->headers_out.content_type->value.len = type[i].type.len;
|
|
|
|
r->headers_out.content_type->value.data = type[i].type.data;
|
2003-05-27 20:18:54 +08:00
|
|
|
|
|
|
|
break;
|
2003-05-15 01:13:13 +08:00
|
|
|
}
|
2002-12-11 02:05:12 +08:00
|
|
|
}
|
2003-05-15 01:13:13 +08:00
|
|
|
}
|
2002-09-16 23:01:44 +08:00
|
|
|
|
2003-05-15 01:13:13 +08:00
|
|
|
if (r->headers_out.content_type->value.len == 0) {
|
2003-05-27 20:18:54 +08:00
|
|
|
r->headers_out.content_type->value.len = clcf->default_type.len;
|
|
|
|
r->headers_out.content_type->value.data = clcf->default_type.data;
|
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
|
|
|
|
2002-09-02 22:48:24 +08:00
|
|
|
ngx_test_null(h, ngx_pcalloc(r->pool, sizeof(ngx_hunk_t)),
|
|
|
|
NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
|
|
|
|
|
|
ngx_test_null(h->file, ngx_pcalloc(r->pool, sizeof(ngx_file_t)),
|
2002-08-16 01:20:26 +08:00
|
|
|
NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
|
|
|
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;
|
|
|
|
h->file_last = ngx_file_size(r->file.info);
|
2002-08-16 01:20:26 +08:00
|
|
|
|
2002-12-15 14:25:09 +08:00
|
|
|
h->file->fd = r->file.fd;
|
|
|
|
h->file->log = r->connection->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
|
|
|
|
|
|
|
|
|
|
|
static int ngx_http_static_init(ngx_cycle_t *cycle)
|
|
|
|
{
|
|
|
|
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];
|
|
|
|
|
|
|
|
ngx_test_null(h, ngx_push_array(
|
|
|
|
&cmcf->phases[NGX_HTTP_TRANSLATE_PHASE].handlers),
|
|
|
|
NGX_ERROR);
|
|
|
|
*h = ngx_http_static_translate_handler;
|
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|