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_string.h>
|
|
|
|
#include <ngx_file.h>
|
|
|
|
#include <ngx_hunk.h>
|
|
|
|
#include <ngx_http.h>
|
2003-03-21 00:09:44 +08:00
|
|
|
#include <ngx_http_config.h>
|
2003-05-15 01:13:13 +08:00
|
|
|
#include <ngx_http_core_module.h>
|
2003-02-07 01:21:13 +08:00
|
|
|
#include <ngx_http_output_filter.h>
|
2002-08-16 01:20:26 +08:00
|
|
|
|
2002-08-20 22:48:28 +08:00
|
|
|
|
2003-03-21 00:09:44 +08:00
|
|
|
ngx_http_module_t ngx_http_static_module;
|
2002-08-16 01:20:26 +08:00
|
|
|
|
|
|
|
|
|
|
|
int ngx_http_static_handler(ngx_http_request_t *r)
|
|
|
|
{
|
2003-05-15 01:13:13 +08:00
|
|
|
int rc, key, i;
|
|
|
|
ngx_log_e level;
|
2003-05-14 00:02:32 +08:00
|
|
|
ngx_err_t err;
|
|
|
|
ngx_hunk_t *h;
|
2003-05-15 01:13:13 +08:00
|
|
|
ngx_http_type_t *type;
|
2002-09-02 22:48:24 +08:00
|
|
|
ngx_http_log_ctx_t *ctx;
|
2003-05-15 01:13:13 +08:00
|
|
|
ngx_http_core_loc_conf_t *core_lcf;
|
|
|
|
|
|
|
|
core_lcf = (ngx_http_core_loc_conf_t *)
|
|
|
|
ngx_http_get_module_loc_conf(r, ngx_http_core_module_ctx);
|
2002-08-16 01:20:26 +08:00
|
|
|
|
2002-12-15 14:25:09 +08:00
|
|
|
#if 0
|
2003-05-15 01:13:13 +08:00
|
|
|
ngx_http_event_static_handler_loc_conf_t *lcf;
|
2002-08-16 01:20:26 +08:00
|
|
|
|
2003-05-15 01:13:13 +08:00
|
|
|
lcf = (ngx_http_event_static_handler_loc_conf_t *)
|
|
|
|
ngx_get_module_loc_conf(r, &ngx_http_event_static_handler_module_ctx);
|
2002-08-16 01:20:26 +08:00
|
|
|
|
2002-12-15 14:25:09 +08:00
|
|
|
#endif
|
2002-08-16 01:20:26 +08:00
|
|
|
|
2002-09-02 22:48:24 +08:00
|
|
|
ngx_http_discard_body(r);
|
|
|
|
ctx = r->connection->log->data;
|
|
|
|
ctx->action = "sending response";
|
|
|
|
|
2003-05-15 01:13:13 +08:00
|
|
|
if (r->file.fd == NGX_INVALID_FILE) {
|
2002-12-15 14:25:09 +08:00
|
|
|
r->file.fd = ngx_open_file(r->file.name.data, NGX_FILE_RDONLY);
|
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
|
|
|
|
2003-05-15 01:13:13 +08:00
|
|
|
#if !(WIN32) /* not regular files is probably Unix specific */
|
2002-12-15 14:25:09 +08:00
|
|
|
|
|
|
|
if (!ngx_is_file(r->file.info)) {
|
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
|
|
|
"%s is not regular file", 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_NOT_FOUND;
|
2002-08-20 22:48:28 +08:00
|
|
|
}
|
|
|
|
|
2002-12-15 14:25:09 +08:00
|
|
|
#endif
|
|
|
|
|
2002-12-03 00:09:40 +08:00
|
|
|
r->headers_out.status = NGX_HTTP_OK;
|
2002-12-15 14:25:09 +08:00
|
|
|
r->headers_out.content_length = ngx_file_size(r->file.info);
|
|
|
|
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
|
|
|
|
2002-12-11 02:05:12 +08:00
|
|
|
r->headers_out.content_type->key.len = 12;
|
|
|
|
r->headers_out.content_type->key.data = "Content-Type";
|
|
|
|
|
|
|
|
if (r->exten.len) {
|
2003-05-15 01:13:13 +08:00
|
|
|
ngx_http_types_hash_key(key, r->exten);
|
|
|
|
|
|
|
|
type = (ngx_http_type_t *) core_lcf->types[key].elts;
|
|
|
|
for (i = 0; i < core_lcf->types[key].nelts; i++) {
|
|
|
|
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;
|
|
|
|
}
|
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) {
|
|
|
|
/* STUB: default type */
|
2002-12-11 02:05:12 +08:00
|
|
|
r->headers_out.content_type->value.len = 25;
|
|
|
|
r->headers_out.content_type->value.data = "text/html; charset=koi8-r";
|
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 */
|
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-14 00:02:32 +08:00
|
|
|
ngx_http_send_header(r);
|
2002-12-15 14:25:09 +08:00
|
|
|
if (r->header_only)
|
2003-05-14 00:02:32 +08:00
|
|
|
return NGX_OK;
|
2002-08-16 01:20:26 +08:00
|
|
|
|
2003-02-12 00:42:23 +08:00
|
|
|
|
2002-12-15 14:25:09 +08:00
|
|
|
h->type = 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
|
|
|
|
2002-12-15 14:25:09 +08:00
|
|
|
rc = ngx_http_output_filter(r, h);
|
2002-08-16 01:20:26 +08:00
|
|
|
|
2003-05-14 00:02:32 +08:00
|
|
|
if (r->main == NULL) {
|
|
|
|
if (rc == NGX_AGAIN) {
|
|
|
|
ngx_http_set_write_handler(r);
|
2003-02-12 00:42:23 +08:00
|
|
|
|
2003-05-14 00:02:32 +08:00
|
|
|
} else {
|
|
|
|
ngx_http_finalize_request(r, 0);
|
2003-02-12 00:42:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-05-14 00:02:32 +08:00
|
|
|
return NGX_OK;
|
2002-08-16 01:20:26 +08:00
|
|
|
}
|