3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
12 static ngx_int_t ngx_http_static_handler(ngx_http_request_t *r);
13 static ngx_int_t ngx_http_static_init(ngx_conf_t *cf);
16 ngx_http_module_t ngx_http_static_module_ctx = {
17 NULL, /* preconfiguration */
18 ngx_http_static_init, /* postconfiguration */
20 NULL, /* create main configuration */
21 NULL, /* init main configuration */
23 NULL, /* create server configuration */
24 NULL, /* merge server configuration */
26 NULL, /* create location configuration */
27 NULL /* merge location configuration */
31 ngx_module_t ngx_http_static_module = {
33 &ngx_http_static_module_ctx, /* module context */
34 NULL, /* module directives */
35 NGX_HTTP_MODULE, /* module type */
36 NULL, /* init master */
37 NULL, /* init module */
38 NULL, /* init process */
39 NULL, /* init thread */
40 NULL, /* exit thread */
41 NULL, /* exit process */
42 NULL, /* exit master */
48 ngx_http_static_handler(ngx_http_request_t *r)
50 u_char *last, *location;
58 ngx_open_file_info_t of;
59 ngx_http_core_loc_conf_t *clcf;
61 if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD|NGX_HTTP_POST))) {
62 return NGX_HTTP_NOT_ALLOWED;
65 if (r->uri.data[r->uri.len - 1] == '/') {
74 log = r->connection->log;
77 * ngx_http_map_uri_to_path() allocates memory for terminating '\0'
78 * so we do not need to reserve memory for '/' for possible redirect
81 last = ngx_http_map_uri_to_path(r, &path, &root, 0);
83 return NGX_HTTP_INTERNAL_SERVER_ERROR;
86 path.len = last - path.data;
88 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
89 "http filename: \"%s\"", path.data);
91 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
93 ngx_memzero(&of, sizeof(ngx_open_file_info_t));
95 of.directio = clcf->directio;
96 of.valid = clcf->open_file_cache_valid;
97 of.min_uses = clcf->open_file_cache_min_uses;
98 of.errors = clcf->open_file_cache_errors;
99 of.events = clcf->open_file_cache_events;
101 if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool)
107 return NGX_HTTP_INTERNAL_SERVER_ERROR;
111 case NGX_ENAMETOOLONG:
114 rc = NGX_HTTP_NOT_FOUND;
120 rc = NGX_HTTP_FORBIDDEN;
125 level = NGX_LOG_CRIT;
126 rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
130 if (rc != NGX_HTTP_NOT_FOUND || clcf->log_not_found) {
131 ngx_log_error(level, log, of.err,
132 ngx_open_file_n " \"%s\" failed", path.data);
138 r->root_tested = !r->error_page;
140 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0, "http static fd: %d", of.fd);
144 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0, "http dir");
146 r->headers_out.location = ngx_palloc(r->pool, sizeof(ngx_table_elt_t));
147 if (r->headers_out.location == NULL) {
148 return NGX_HTTP_INTERNAL_SERVER_ERROR;
151 len = r->uri.len + 1;
153 if (!clcf->alias && clcf->root_lengths == NULL && r->args.len == 0) {
154 location = path.data + clcf->root.len;
160 len += r->args.len + 1;
163 location = ngx_pnalloc(r->pool, len);
164 if (location == NULL) {
165 return NGX_HTTP_INTERNAL_SERVER_ERROR;
168 last = ngx_copy(location, r->uri.data, r->uri.len);
174 ngx_memcpy(++last, r->args.data, r->args.len);
179 * we do not need to set the r->headers_out.location->hash and
180 * r->headers_out.location->key fields
183 r->headers_out.location->value.len = len;
184 r->headers_out.location->value.data = location;
186 return NGX_HTTP_MOVED_PERMANENTLY;
189 #if !(NGX_WIN32) /* the not regular files are probably Unix specific */
192 ngx_log_error(NGX_LOG_CRIT, log, ngx_errno,
193 "\"%s\" is not a regular file", path.data);
195 return NGX_HTTP_NOT_FOUND;
200 if (r->method & NGX_HTTP_POST) {
201 return NGX_HTTP_NOT_ALLOWED;
204 rc = ngx_http_discard_request_body(r);
210 log->action = "sending response to client";
212 r->headers_out.status = NGX_HTTP_OK;
213 r->headers_out.content_length_n = of.size;
214 r->headers_out.last_modified_time = of.mtime;
216 if (ngx_http_set_content_type(r) != NGX_OK) {
217 return NGX_HTTP_INTERNAL_SERVER_ERROR;
220 if (r != r->main && of.size == 0) {
221 return ngx_http_send_header(r);
226 /* we need to allocate all before the header would be sent */
228 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
230 return NGX_HTTP_INTERNAL_SERVER_ERROR;
233 b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t));
234 if (b->file == NULL) {
235 return NGX_HTTP_INTERNAL_SERVER_ERROR;
238 rc = ngx_http_send_header(r);
240 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
245 b->file_last = of.size;
247 b->in_file = b->file_last ? 1: 0;
248 b->last_buf = (r == r->main) ? 1: 0;
249 b->last_in_chain = 1;
252 b->file->name = path;
254 b->file->directio = of.is_directio;
259 return ngx_http_output_filter(r, &out);
264 ngx_http_static_init(ngx_conf_t *cf)
266 ngx_http_handler_pt *h;
267 ngx_http_core_main_conf_t *cmcf;
269 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
271 h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
276 *h = ngx_http_static_handler;