1 
2 /*
3  * Copyright (C) Igor Sysoev
4  * Copyright (C) Nginx, Inc.
5  */
6 
7 
8 #include <ngx_config.h>
9 #include <ngx_core.h>
10 #include <ngx_http.h>
11 
12 
13 static ngx_int_t ngx_http_static_handler(ngx_http_request_t *r);
14 static ngx_int_t ngx_http_static_init(ngx_conf_t *cf);
15 
16 
17 static ngx_http_module_t  ngx_http_static_module_ctx = {
18     NULL,                                  /* preconfiguration */
19     ngx_http_static_init,                  /* postconfiguration */
20 
21     NULL,                                  /* create main configuration */
22     NULL,                                  /* init main configuration */
23 
24     NULL,                                  /* create server configuration */
25     NULL,                                  /* merge server configuration */
26 
27     NULL,                                  /* create location configuration */
28     NULL                                   /* merge location configuration */
29 };
30 
31 
32 ngx_module_t  ngx_http_static_module = {
33     NGX_MODULE_V1,
34     &ngx_http_static_module_ctx,           /* module context */
35     NULL,                                  /* module directives */
36     NGX_HTTP_MODULE,                       /* module type */
37     NULL,                                  /* init master */
38     NULL,                                  /* init module */
39     NULL,                                  /* init process */
40     NULL,                                  /* init thread */
41     NULL,                                  /* exit thread */
42     NULL,                                  /* exit process */
43     NULL,                                  /* exit master */
44     NGX_MODULE_V1_PADDING
45 };
46 
47 
48 static ngx_int_t
ngx_http_static_handler(ngx_http_request_t * r)49 ngx_http_static_handler(ngx_http_request_t *r)
50 {
51     u_char                    *last, *location;
52     size_t                     root, len;
53     ngx_str_t                  path;
54     ngx_int_t                  rc;
55     ngx_uint_t                 level;
56     ngx_log_t                 *log;
57     ngx_buf_t                 *b;
58     ngx_chain_t                out;
59     ngx_open_file_info_t       of;
60     ngx_http_core_loc_conf_t  *clcf;
61 
62     if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD|NGX_HTTP_POST))) {
63         return NGX_HTTP_NOT_ALLOWED;
64     }
65 
66     if (r->uri.data[r->uri.len - 1] == '/') {
67         return NGX_DECLINED;
68     }
69 
70     log = r->connection->log;
71 
72     /*
73      * ngx_http_map_uri_to_path() allocates memory for terminating '\0'
74      * so we do not need to reserve memory for '/' for possible redirect
75      */
76 
77     last = ngx_http_map_uri_to_path(r, &path, &root, 0);
78     if (last == NULL) {
79         return NGX_HTTP_INTERNAL_SERVER_ERROR;
80     }
81 
82     path.len = last - path.data;
83 
84     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
85                    "http filename: \"%s\"", path.data);
86 
87     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
88 
89     ngx_memzero(&of, sizeof(ngx_open_file_info_t));
90 
91     of.read_ahead = clcf->read_ahead;
92     of.directio = clcf->directio;
93     of.valid = clcf->open_file_cache_valid;
94     of.min_uses = clcf->open_file_cache_min_uses;
95     of.errors = clcf->open_file_cache_errors;
96     of.events = clcf->open_file_cache_events;
97 
98     if (ngx_http_set_disable_symlinks(r, clcf, &path, &of) != NGX_OK) {
99         return NGX_HTTP_INTERNAL_SERVER_ERROR;
100     }
101 
102     if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool)
103         != NGX_OK)
104     {
105         switch (of.err) {
106 
107         case 0:
108             return NGX_HTTP_INTERNAL_SERVER_ERROR;
109 
110         case NGX_ENOENT:
111         case NGX_ENOTDIR:
112         case NGX_ENAMETOOLONG:
113 
114             level = NGX_LOG_ERR;
115             rc = NGX_HTTP_NOT_FOUND;
116             break;
117 
118         case NGX_EACCES:
119 #if (NGX_HAVE_OPENAT)
120         case NGX_EMLINK:
121         case NGX_ELOOP:
122 #endif
123 
124             level = NGX_LOG_ERR;
125             rc = NGX_HTTP_FORBIDDEN;
126             break;
127 
128         default:
129 
130             level = NGX_LOG_CRIT;
131             rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
132             break;
133         }
134 
135         if (rc != NGX_HTTP_NOT_FOUND || clcf->log_not_found) {
136             ngx_log_error(level, log, of.err,
137                           "%s \"%s\" failed", of.failed, path.data);
138         }
139 
140         return rc;
141     }
142 
143     r->root_tested = !r->error_page;
144 
145     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0, "http static fd: %d", of.fd);
146 
147     if (of.is_dir) {
148 
149         ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0, "http dir");
150 
151         ngx_http_clear_location(r);
152 
153         r->headers_out.location = ngx_list_push(&r->headers_out.headers);
154         if (r->headers_out.location == NULL) {
155             return NGX_HTTP_INTERNAL_SERVER_ERROR;
156         }
157 
158         len = r->uri.len + 1;
159 
160         if (!clcf->alias && r->args.len == 0) {
161             location = path.data + root;
162 
163             *last = '/';
164 
165         } else {
166             if (r->args.len) {
167                 len += r->args.len + 1;
168             }
169 
170             location = ngx_pnalloc(r->pool, len);
171             if (location == NULL) {
172                 ngx_http_clear_location(r);
173                 return NGX_HTTP_INTERNAL_SERVER_ERROR;
174             }
175 
176             last = ngx_copy(location, r->uri.data, r->uri.len);
177 
178             *last = '/';
179 
180             if (r->args.len) {
181                 *++last = '?';
182                 ngx_memcpy(++last, r->args.data, r->args.len);
183             }
184         }
185 
186         r->headers_out.location->hash = 1;
187         ngx_str_set(&r->headers_out.location->key, "Location");
188         r->headers_out.location->value.len = len;
189         r->headers_out.location->value.data = location;
190 
191         return NGX_HTTP_MOVED_PERMANENTLY;
192     }
193 
194 #if !(NGX_WIN32) /* the not regular files are probably Unix specific */
195 
196     if (!of.is_file) {
197         ngx_log_error(NGX_LOG_CRIT, log, 0,
198                       "\"%s\" is not a regular file", path.data);
199 
200         return NGX_HTTP_NOT_FOUND;
201     }
202 
203 #endif
204 
205     if (r->method == NGX_HTTP_POST) {
206         return NGX_HTTP_NOT_ALLOWED;
207     }
208 
209     rc = ngx_http_discard_request_body(r);
210 
211     if (rc != NGX_OK) {
212         return rc;
213     }
214 
215     log->action = "sending response to client";
216 
217     r->headers_out.status = NGX_HTTP_OK;
218     r->headers_out.content_length_n = of.size;
219     r->headers_out.last_modified_time = of.mtime;
220 
221     if (ngx_http_set_etag(r) != NGX_OK) {
222         return NGX_HTTP_INTERNAL_SERVER_ERROR;
223     }
224 
225     if (ngx_http_set_content_type(r) != NGX_OK) {
226         return NGX_HTTP_INTERNAL_SERVER_ERROR;
227     }
228 
229     if (r != r->main && of.size == 0) {
230         return ngx_http_send_header(r);
231     }
232 
233     r->allow_ranges = 1;
234 
235     /* we need to allocate all before the header would be sent */
236 
237     b = ngx_calloc_buf(r->pool);
238     if (b == NULL) {
239         return NGX_HTTP_INTERNAL_SERVER_ERROR;
240     }
241 
242     b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t));
243     if (b->file == NULL) {
244         return NGX_HTTP_INTERNAL_SERVER_ERROR;
245     }
246 
247     rc = ngx_http_send_header(r);
248 
249     if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
250         return rc;
251     }
252 
253     b->file_pos = 0;
254     b->file_last = of.size;
255 
256     b->in_file = b->file_last ? 1: 0;
257     b->last_buf = (r == r->main) ? 1: 0;
258     b->last_in_chain = 1;
259 
260     b->file->fd = of.fd;
261     b->file->name = path;
262     b->file->log = log;
263     b->file->directio = of.is_directio;
264 
265     out.buf = b;
266     out.next = NULL;
267 
268     return ngx_http_output_filter(r, &out);
269 }
270 
271 
272 static ngx_int_t
ngx_http_static_init(ngx_conf_t * cf)273 ngx_http_static_init(ngx_conf_t *cf)
274 {
275     ngx_http_handler_pt        *h;
276     ngx_http_core_main_conf_t  *cmcf;
277 
278     cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
279 
280     h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
281     if (h == NULL) {
282         return NGX_ERROR;
283     }
284 
285     *h = ngx_http_static_handler;
286 
287     return NGX_OK;
288 }
289