50
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9 #include <ngx_http.h>
|
|
10
|
|
11
|
|
12 typedef struct {
|
|
13 ngx_http_cache_hash_t *redirect_cache;
|
|
14 } ngx_http_static_loc_conf_t;
|
|
15
|
|
16
|
|
17 static ngx_int_t ngx_http_static_handler(ngx_http_request_t *r);
|
|
18 static void *ngx_http_static_create_loc_conf(ngx_conf_t *cf);
|
|
19 static char *ngx_http_static_merge_loc_conf(ngx_conf_t *cf,
|
58
|
20 void *parent, void *child);
|
50
|
21 static ngx_int_t ngx_http_static_init(ngx_cycle_t *cycle);
|
|
22
|
|
23
|
|
24 static ngx_command_t ngx_http_static_commands[] = {
|
|
25
|
|
26 #if (NGX_HTTP_CACHE)
|
|
27
|
|
28 { ngx_string("redirect_cache"),
|
|
29 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE3,
|
|
30 ngx_http_set_cache_slot,
|
|
31 NGX_HTTP_LOC_CONF_OFFSET,
|
|
32 offsetof(ngx_http_static_loc_conf_t, redirect_cache),
|
|
33 NULL },
|
|
34
|
|
35 #endif
|
|
36
|
|
37 ngx_null_command
|
|
38 };
|
|
39
|
|
40
|
|
41 ngx_http_module_t ngx_http_static_module_ctx = {
|
58
|
42 NULL, /* preconfiguration */
|
|
43 NULL, /* postconfiguration */
|
50
|
44
|
|
45 NULL, /* create main configuration */
|
|
46 NULL, /* init main configuration */
|
|
47
|
|
48 NULL, /* create server configuration */
|
|
49 NULL, /* merge server configuration */
|
|
50
|
|
51 ngx_http_static_create_loc_conf, /* create location configuration */
|
|
52 ngx_http_static_merge_loc_conf /* merge location configuration */
|
|
53 };
|
|
54
|
|
55
|
|
56 ngx_module_t ngx_http_static_module = {
|
58
|
57 NGX_MODULE_V1,
|
50
|
58 &ngx_http_static_module_ctx, /* module context */
|
|
59 ngx_http_static_commands, /* module directives */
|
|
60 NGX_HTTP_MODULE, /* module type */
|
|
61 ngx_http_static_init, /* init module */
|
|
62 NULL /* init process */
|
|
63 };
|
|
64
|
|
65
|
58
|
66 static ngx_int_t
|
|
67 ngx_http_static_handler(ngx_http_request_t *r)
|
50
|
68 {
|
|
69 u_char *last;
|
|
70 ngx_fd_t fd;
|
|
71 ngx_int_t rc;
|
|
72 ngx_uint_t level;
|
|
73 ngx_str_t name, location;
|
|
74 ngx_err_t err;
|
|
75 ngx_log_t *log;
|
|
76 ngx_buf_t *b;
|
|
77 ngx_chain_t out;
|
|
78 ngx_file_info_t fi;
|
58
|
79 ngx_pool_cleanup_file_t *cln;
|
50
|
80 ngx_http_core_loc_conf_t *clcf;
|
|
81
|
|
82 if (r->uri.data[r->uri.len - 1] == '/') {
|
|
83 return NGX_DECLINED;
|
|
84 }
|
|
85
|
|
86 /* TODO: Win32 */
|
|
87 if (r->zero_in_uri) {
|
|
88 return NGX_DECLINED;
|
|
89 }
|
|
90
|
|
91 if (r->method != NGX_HTTP_GET && r->method != NGX_HTTP_HEAD) {
|
|
92 return NGX_HTTP_NOT_ALLOWED;
|
|
93 }
|
|
94
|
|
95 rc = ngx_http_discard_body(r);
|
|
96
|
|
97 if (rc != NGX_OK && rc != NGX_AGAIN) {
|
|
98 return rc;
|
|
99 }
|
|
100
|
|
101 log = r->connection->log;
|
|
102
|
|
103 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
104
|
|
105 /*
|
|
106 * make a file name, reserve 2 bytes for a trailing '/'
|
|
107 * in a possible redirect and for the last '\0'
|
|
108 */
|
|
109
|
58
|
110 if (!clcf->alias) {
|
|
111 name.data = ngx_palloc(r->pool, clcf->root.len + r->uri.len + 2);
|
|
112 if (name.data == NULL) {
|
|
113 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
114 }
|
|
115
|
|
116 location.data = ngx_cpymem(name.data, clcf->root.data, clcf->root.len);
|
|
117 last = ngx_cpystrn(location.data, r->uri.data, r->uri.len + 1);
|
|
118
|
|
119 name.len = last - name.data;
|
|
120 location.len = last - location.data + 1;
|
|
121
|
|
122 } else {
|
50
|
123 name.data = ngx_palloc(r->pool, clcf->root.len + r->uri.len + 2
|
|
124 - clcf->name.len);
|
|
125 if (name.data == NULL) {
|
|
126 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
127 }
|
|
128
|
|
129 last = ngx_cpymem(name.data, clcf->root.data, clcf->root.len);
|
|
130 last = ngx_cpystrn(last, r->uri.data + clcf->name.len,
|
|
131 r->uri.len + 1 - clcf->name.len);
|
|
132
|
|
133 name.len = last - name.data;
|
|
134
|
|
135 location.data = ngx_palloc(r->pool, r->uri.len + 2);
|
|
136 if (location.data == NULL) {
|
|
137 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
138 }
|
|
139
|
|
140 last = ngx_cpystrn(location.data, r->uri.data, r->uri.len + 1);
|
|
141
|
|
142 location.len = last - location.data + 1;
|
|
143 }
|
|
144
|
|
145 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
|
|
146 "http filename: \"%s\"", name.data);
|
|
147
|
|
148 /* open file */
|
|
149
|
|
150 #if (NGX_WIN9X)
|
|
151
|
|
152 if (ngx_win32_version < NGX_WIN_NT) {
|
|
153
|
|
154 /*
|
|
155 * there is no way to open a file or a directory in Win9X with
|
|
156 * one syscall because Win9X has no FILE_FLAG_BACKUP_SEMANTICS flag
|
|
157 * so we need to check its type before the opening
|
|
158 */
|
|
159
|
|
160 if (ngx_file_info(name.data, &fi) == NGX_FILE_ERROR) {
|
|
161 err = ngx_errno;
|
|
162 ngx_log_error(NGX_LOG_ERR, log, err,
|
|
163 ngx_file_info_n " \"%s\" failed", name.data);
|
|
164
|
|
165 if (err == NGX_ENOENT || err == NGX_ENOTDIR) {
|
|
166 return NGX_HTTP_NOT_FOUND;
|
|
167
|
|
168 } else if (err == NGX_EACCES) {
|
|
169 return NGX_HTTP_FORBIDDEN;
|
|
170
|
|
171 } else {
|
|
172 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
173 }
|
|
174 }
|
|
175
|
|
176 if (ngx_is_dir(&fi)) {
|
|
177 r->headers_out.location = ngx_http_add_header(&r->headers_out,
|
|
178 ngx_http_headers_out);
|
|
179 if (r->headers_out.location == NULL) {
|
|
180 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
181 }
|
|
182
|
|
183 *last++ = '/';
|
|
184 *last = '\0';
|
|
185 r->headers_out.location->value.len = last - location;
|
|
186 r->headers_out.location->value.data = location;
|
|
187
|
|
188 return NGX_HTTP_MOVED_PERMANENTLY;
|
|
189 }
|
|
190 }
|
|
191
|
|
192 #endif
|
|
193
|
|
194
|
|
195 fd = ngx_open_file(name.data, NGX_FILE_RDONLY, NGX_FILE_OPEN);
|
|
196
|
|
197 if (fd == NGX_INVALID_FILE) {
|
|
198 err = ngx_errno;
|
|
199
|
|
200 if (err == NGX_ENOENT || err == NGX_ENOTDIR) {
|
|
201 level = NGX_LOG_ERR;
|
|
202 rc = NGX_HTTP_NOT_FOUND;
|
|
203
|
|
204 } else if (err == NGX_EACCES) {
|
|
205 level = NGX_LOG_ERR;
|
|
206 rc = NGX_HTTP_FORBIDDEN;
|
|
207
|
|
208 } else {
|
|
209 level = NGX_LOG_CRIT;
|
|
210 rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
211 }
|
|
212
|
|
213 ngx_log_error(level, log, err,
|
|
214 ngx_open_file_n " \"%s\" failed", name.data);
|
|
215
|
|
216 return rc;
|
|
217 }
|
|
218
|
|
219 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0, "http static fd: %d", fd);
|
|
220
|
|
221 if (ngx_fd_info(fd, &fi) == NGX_FILE_ERROR) {
|
|
222 ngx_log_error(NGX_LOG_CRIT, log, ngx_errno,
|
|
223 ngx_fd_info_n " \"%s\" failed", name.data);
|
|
224
|
|
225 if (ngx_close_file(fd) == NGX_FILE_ERROR) {
|
|
226 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
|
|
227 ngx_close_file_n " \"%s\" failed", name.data);
|
|
228 }
|
|
229
|
|
230 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
231 }
|
|
232
|
|
233 if (ngx_is_dir(&fi)) {
|
|
234
|
|
235 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0, "http dir");
|
|
236
|
|
237 if (ngx_close_file(fd) == NGX_FILE_ERROR) {
|
|
238 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
|
|
239 ngx_close_file_n " \"%s\" failed", name.data);
|
|
240 }
|
|
241
|
|
242 *last++ = '/';
|
|
243 *last = '\0';
|
|
244
|
58
|
245 r->headers_out.location = ngx_palloc(r->pool, sizeof(ngx_table_elt_t));
|
50
|
246 if (r->headers_out.location == NULL) {
|
|
247 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
248 }
|
|
249
|
58
|
250 /*
|
|
251 * we do not need to set the r->headers_out.location->hash and
|
|
252 * r->headers_out.location->key fields
|
|
253 */
|
50
|
254
|
58
|
255 r->headers_out.location->value = location;
|
50
|
256
|
|
257 return NGX_HTTP_MOVED_PERMANENTLY;
|
|
258 }
|
|
259
|
|
260 #if !(NGX_WIN32) /* the not regular files are probably Unix specific */
|
|
261
|
|
262 if (!ngx_is_file(&fi)) {
|
|
263 ngx_log_error(NGX_LOG_CRIT, log, ngx_errno,
|
|
264 "\"%s\" is not a regular file", name.data);
|
|
265
|
|
266 if (ngx_close_file(fd) == NGX_FILE_ERROR) {
|
|
267 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
|
|
268 ngx_close_file_n " \"%s\" failed", name.data);
|
|
269 }
|
|
270
|
|
271 return NGX_HTTP_NOT_FOUND;
|
|
272 }
|
|
273
|
|
274 #endif
|
|
275
|
58
|
276 log->action = "sending response to client";
|
50
|
277
|
58
|
278 cln = ngx_palloc(r->pool, sizeof(ngx_pool_cleanup_file_t));
|
|
279 if (cln == NULL) {
|
|
280 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
50
|
281 }
|
|
282
|
58
|
283 cln->fd = fd;
|
|
284 cln->name = name.data;
|
|
285 cln->log = r->pool->log;
|
50
|
286
|
58
|
287 if (ngx_pool_cleanup_add(r->pool, ngx_pool_cleanup_file, cln) == NULL) {
|
|
288 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
289 }
|
50
|
290
|
|
291 r->headers_out.status = NGX_HTTP_OK;
|
|
292 r->headers_out.content_length_n = ngx_file_size(&fi);
|
|
293 r->headers_out.last_modified_time = ngx_file_mtime(&fi);
|
|
294
|
|
295 if (r->headers_out.content_length_n == 0) {
|
|
296 r->header_only = 1;
|
|
297 }
|
|
298
|
|
299 if (ngx_http_set_content_type(r) != NGX_OK) {
|
|
300 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
301 }
|
|
302
|
|
303 #if (NGX_SUPPRESS_WARN)
|
|
304 b = NULL;
|
|
305 #endif
|
|
306
|
|
307 if (!r->header_only) {
|
|
308 /* we need to allocate all before the header would be sent */
|
|
309
|
|
310 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
|
|
311 if (b == NULL) {
|
|
312 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
313 }
|
|
314
|
|
315 b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t));
|
|
316 if (b->file == NULL) {
|
|
317 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
318 }
|
|
319
|
|
320 r->filter_allow_ranges = 1;
|
|
321 }
|
|
322
|
|
323 rc = ngx_http_send_header(r);
|
|
324
|
|
325 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
|
|
326 return rc;
|
|
327 }
|
|
328
|
|
329 b->in_file = 1;
|
|
330
|
58
|
331 if (r->main == NULL) {
|
50
|
332 b->last_buf = 1;
|
|
333 }
|
|
334
|
58
|
335 b->last_in_chain = 1;
|
|
336
|
50
|
337 b->file_pos = 0;
|
|
338 b->file_last = ngx_file_size(&fi);
|
|
339
|
|
340 b->file->fd = fd;
|
|
341 b->file->name = name;
|
|
342 b->file->log = log;
|
|
343
|
|
344 out.buf = b;
|
|
345 out.next = NULL;
|
|
346
|
|
347 return ngx_http_output_filter(r, &out);
|
|
348 }
|
|
349
|
|
350
|
58
|
351 static void *
|
|
352 ngx_http_static_create_loc_conf(ngx_conf_t *cf)
|
50
|
353 {
|
|
354 ngx_http_static_loc_conf_t *conf;
|
|
355
|
|
356 conf = ngx_palloc(cf->pool, sizeof(ngx_http_static_loc_conf_t));
|
|
357 if (conf == NULL) {
|
|
358 return NGX_CONF_ERROR;
|
|
359 }
|
|
360
|
|
361 conf->redirect_cache = NULL;
|
|
362
|
|
363 return conf;
|
|
364 }
|
|
365
|
|
366
|
58
|
367 static char *
|
|
368 ngx_http_static_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
|
50
|
369 {
|
|
370 ngx_http_static_loc_conf_t *prev = parent;
|
|
371 ngx_http_static_loc_conf_t *conf = child;
|
|
372
|
|
373 if (conf->redirect_cache == NULL) {
|
|
374 conf->redirect_cache = prev->redirect_cache;
|
|
375 }
|
|
376
|
|
377 return NGX_CONF_OK;
|
|
378 }
|
|
379
|
|
380
|
58
|
381 static ngx_int_t
|
|
382 ngx_http_static_init(ngx_cycle_t *cycle)
|
50
|
383 {
|
|
384 ngx_http_handler_pt *h;
|
|
385 ngx_http_core_main_conf_t *cmcf;
|
|
386
|
|
387 cmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_core_module);
|
|
388
|
|
389 h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
|
|
390 if (h == NULL) {
|
|
391 return NGX_ERROR;
|
|
392 }
|
|
393
|
|
394 *h = ngx_http_static_handler;
|
|
395
|
|
396 return NGX_OK;
|
|
397 }
|