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