|
0 |
/*
|
|
1 |
* ngx_http_fancyindex_module.c
|
|
2 |
* Copyright © 2007-2011 Adrian Perez <aperez@igalia.com>
|
|
3 |
*
|
|
4 |
* Module used for fancy indexing of directories. Features and differences
|
|
5 |
* with the stock nginx autoindex module:
|
|
6 |
*
|
|
7 |
* - Output is a table instead of a <pre> element with embedded <a> links.
|
|
8 |
* - Header and footer may be added to every generated directory listing.
|
|
9 |
* - Default header and/or footer are generated if custom ones are not
|
|
10 |
* configured. Files used for header and footer can only be local path
|
|
11 |
* names (i.e. you cannot insert the result of a subrequest.)
|
|
12 |
* - Proper HTML is generated: it should validate both as XHTML 1.0 Strict
|
|
13 |
* and HTML 4.01.
|
|
14 |
*
|
|
15 |
* Base functionality heavy based upon the stock nginx autoindex module,
|
|
16 |
* which in turn was made by Igor Sysoev, like the majority of nginx.
|
|
17 |
*
|
|
18 |
* Distributed under terms of the BSD license.
|
|
19 |
*/
|
|
20 |
|
|
21 |
#include <ngx_config.h>
|
|
22 |
#include <ngx_core.h>
|
|
23 |
#include <ngx_http.h>
|
|
24 |
#include <ngx_log.h>
|
|
25 |
|
|
26 |
#include "template.h"
|
|
27 |
|
|
28 |
#if defined(__GNUC__) && (__GNUC__ >= 3)
|
|
29 |
# define ngx_force_inline __attribute__((__always_inline__))
|
|
30 |
#else /* !__GNUC__ */
|
|
31 |
# define ngx_force_inline
|
|
32 |
#endif /* __GNUC__ */
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
/**
|
|
37 |
* Configuration structure for the fancyindex module. The configuration
|
|
38 |
* commands defined in the module do fill in the members of this structure.
|
|
39 |
*/
|
|
40 |
typedef struct {
|
|
41 |
ngx_flag_t enable; /**< Module is enabled. */
|
|
42 |
ngx_flag_t localtime; /**< File mtime dates are sent in local time. */
|
|
43 |
ngx_flag_t exact_size; /**< Sizes are sent always in bytes. */
|
|
44 |
|
|
45 |
ngx_str_t header; /**< File name for header, or empty if none. */
|
|
46 |
ngx_str_t footer; /**< File name for footer, or empty if none. */
|
|
47 |
ngx_str_t css_href; /**< Link to a CSS stylesheet, or empty if none. */
|
|
48 |
|
|
49 |
ngx_array_t *ignore; /**< List of files to ignore in listings. */
|
|
50 |
} ngx_http_fancyindex_loc_conf_t;
|
|
51 |
|
|
52 |
|
|
53 |
#define NGX_HTTP_FANCYINDEX_PREALLOCATE 50
|
|
54 |
#define NGX_HTTP_FANCYINDEX_NAME_LEN 50
|
|
55 |
|
|
56 |
|
|
57 |
/**
|
|
58 |
* Calculates the length of a NULL-terminated string. It is ugly having to
|
|
59 |
* remember to substract 1 from the sizeof result.
|
|
60 |
*/
|
|
61 |
#define ngx_sizeof_ssz(_s) (sizeof(_s) - 1)
|
|
62 |
|
|
63 |
|
|
64 |
/**
|
|
65 |
* Copy a static zero-terminated string. Useful to output template
|
|
66 |
* string pieces into a temporary buffer.
|
|
67 |
*/
|
|
68 |
#define ngx_cpymem_ssz(_p, _t) \
|
|
69 |
(ngx_cpymem((_p), (_t), sizeof(_t) - 1))
|
|
70 |
|
|
71 |
/**
|
|
72 |
* Copy a ngx_str_t.
|
|
73 |
*/
|
|
74 |
#define ngx_cpymem_str(_p, _s) \
|
|
75 |
(ngx_cpymem((_p), (_s).data, (_s).len))
|
|
76 |
|
|
77 |
/**
|
|
78 |
* Check whether a particular bit is set in a particular value.
|
|
79 |
*/
|
|
80 |
#define ngx_has_flag(_where, _what) \
|
|
81 |
(((_where) & (_what)) == (_what))
|
|
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
|
86 |
typedef struct {
|
|
87 |
ngx_str_t name;
|
|
88 |
size_t utf_len;
|
|
89 |
ngx_uint_t escape;
|
|
90 |
ngx_uint_t dir;
|
|
91 |
time_t mtime;
|
|
92 |
off_t size;
|
|
93 |
} ngx_http_fancyindex_entry_t;
|
|
94 |
|
|
95 |
|
|
96 |
|
|
97 |
static int ngx_libc_cdecl
|
|
98 |
ngx_http_fancyindex_cmp_entries(const void *one, const void *two);
|
|
99 |
|
|
100 |
static ngx_int_t ngx_http_fancyindex_error(ngx_http_request_t *r,
|
|
101 |
ngx_dir_t *dir, ngx_str_t *name);
|
|
102 |
|
|
103 |
static ngx_int_t ngx_http_fancyindex_init(ngx_conf_t *cf);
|
|
104 |
|
|
105 |
static void *ngx_http_fancyindex_create_loc_conf(ngx_conf_t *cf);
|
|
106 |
|
|
107 |
static char *ngx_http_fancyindex_merge_loc_conf(ngx_conf_t *cf,
|
|
108 |
void *parent, void *child);
|
|
109 |
|
|
110 |
static char *ngx_http_fancyindex_ignore(ngx_conf_t *cf,
|
|
111 |
ngx_command_t *cmd,
|
|
112 |
void *conf);
|
|
113 |
|
|
114 |
static uintptr_t
|
|
115 |
ngx_fancyindex_escape_uri(u_char *dst, u_char*src, size_t size);
|
|
116 |
|
|
117 |
/*
|
|
118 |
* These are used only once per handler invocation. We can tell GCC to
|
|
119 |
* inline them always, if possible (see how ngx_force_inline is defined
|
|
120 |
* above).
|
|
121 |
*/
|
|
122 |
static ngx_inline ngx_buf_t*
|
|
123 |
make_header_buf(ngx_http_request_t *r, const ngx_str_t css_href)
|
|
124 |
ngx_force_inline;
|
|
125 |
|
|
126 |
static ngx_inline ngx_buf_t*
|
|
127 |
make_footer_buf(ngx_http_request_t *r)
|
|
128 |
ngx_force_inline;
|
|
129 |
|
|
130 |
|
|
131 |
|
|
132 |
static ngx_command_t ngx_http_fancyindex_commands[] = {
|
|
133 |
|
|
134 |
{ ngx_string("fancyindex"),
|
|
135 |
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
136 |
ngx_conf_set_flag_slot,
|
|
137 |
NGX_HTTP_LOC_CONF_OFFSET,
|
|
138 |
offsetof(ngx_http_fancyindex_loc_conf_t, enable),
|
|
139 |
NULL },
|
|
140 |
|
|
141 |
{ ngx_string("fancyindex_localtime"),
|
|
142 |
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
143 |
ngx_conf_set_flag_slot,
|
|
144 |
NGX_HTTP_LOC_CONF_OFFSET,
|
|
145 |
offsetof(ngx_http_fancyindex_loc_conf_t, localtime),
|
|
146 |
NULL },
|
|
147 |
|
|
148 |
{ ngx_string("fancyindex_exact_size"),
|
|
149 |
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
150 |
ngx_conf_set_flag_slot,
|
|
151 |
NGX_HTTP_LOC_CONF_OFFSET,
|
|
152 |
offsetof(ngx_http_fancyindex_loc_conf_t, exact_size),
|
|
153 |
NULL },
|
|
154 |
|
|
155 |
{ ngx_string("fancyindex_header"),
|
|
156 |
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
157 |
ngx_conf_set_str_slot,
|
|
158 |
NGX_HTTP_LOC_CONF_OFFSET,
|
|
159 |
offsetof(ngx_http_fancyindex_loc_conf_t, header),
|
|
160 |
NULL },
|
|
161 |
|
|
162 |
{ ngx_string("fancyindex_footer"),
|
|
163 |
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
164 |
ngx_conf_set_str_slot,
|
|
165 |
NGX_HTTP_LOC_CONF_OFFSET,
|
|
166 |
offsetof(ngx_http_fancyindex_loc_conf_t, footer),
|
|
167 |
NULL },
|
|
168 |
|
|
169 |
{ ngx_string("fancyindex_css_href"),
|
|
170 |
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
171 |
ngx_conf_set_str_slot,
|
|
172 |
NGX_HTTP_LOC_CONF_OFFSET,
|
|
173 |
offsetof(ngx_http_fancyindex_loc_conf_t, css_href),
|
|
174 |
NULL },
|
|
175 |
|
|
176 |
{ ngx_string("fancyindex_ignore"),
|
|
177 |
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
|
|
178 |
ngx_http_fancyindex_ignore,
|
|
179 |
NGX_HTTP_LOC_CONF_OFFSET,
|
|
180 |
0,
|
|
181 |
NULL },
|
|
182 |
|
|
183 |
ngx_null_command
|
|
184 |
};
|
|
185 |
|
|
186 |
|
|
187 |
static ngx_http_module_t ngx_http_fancyindex_module_ctx = {
|
|
188 |
NULL, /* preconfiguration */
|
|
189 |
ngx_http_fancyindex_init, /* postconfiguration */
|
|
190 |
|
|
191 |
NULL, /* create main configuration */
|
|
192 |
NULL, /* init main configuration */
|
|
193 |
|
|
194 |
NULL, /* create server configuration */
|
|
195 |
NULL, /* merge server configuration */
|
|
196 |
|
|
197 |
ngx_http_fancyindex_create_loc_conf, /* create location configration */
|
|
198 |
ngx_http_fancyindex_merge_loc_conf /* merge location configration */
|
|
199 |
};
|
|
200 |
|
|
201 |
|
|
202 |
ngx_module_t ngx_http_fancyindex_module = {
|
|
203 |
NGX_MODULE_V1,
|
|
204 |
&ngx_http_fancyindex_module_ctx, /* module context */
|
|
205 |
ngx_http_fancyindex_commands, /* module directives */
|
|
206 |
NGX_HTTP_MODULE, /* module type */
|
|
207 |
NULL, /* init master */
|
|
208 |
NULL, /* init module */
|
|
209 |
NULL, /* init process */
|
|
210 |
NULL, /* init thread */
|
|
211 |
NULL, /* exit thread */
|
|
212 |
NULL, /* exit process */
|
|
213 |
NULL, /* exit master */
|
|
214 |
NGX_MODULE_V1_PADDING
|
|
215 |
};
|
|
216 |
|
|
217 |
|
|
218 |
|
|
219 |
static const ngx_str_t css_href_pre =
|
|
220 |
ngx_string("<link rel=\"stylesheet\" href=\"");
|
|
221 |
static const ngx_str_t css_href_post =
|
|
222 |
ngx_string("\" type=\"text/css\"/>\n");
|
|
223 |
|
|
224 |
|
|
225 |
static uintptr_t
|
|
226 |
ngx_fancyindex_escape_uri(u_char *dst, u_char *src, size_t size)
|
|
227 |
{
|
|
228 |
/*
|
|
229 |
* The ngx_escape_uri() function will not escape colons or the
|
|
230 |
* ? character, which signals the beginning of the query string.
|
|
231 |
* So we handle those characters ourselves.
|
|
232 |
*
|
|
233 |
* TODO: Get rid of this once ngx_escape_uri() works as expected!
|
|
234 |
*/
|
|
235 |
|
|
236 |
u_int escapes = 0;
|
|
237 |
u_char *psrc = src;
|
|
238 |
size_t psize = size;
|
|
239 |
|
|
240 |
while (psize--) {
|
|
241 |
switch (*psrc++) {
|
|
242 |
case ':':
|
|
243 |
case '?':
|
|
244 |
escapes++;
|
|
245 |
break;
|
|
246 |
}
|
|
247 |
}
|
|
248 |
|
|
249 |
if (dst == NULL) {
|
|
250 |
return escapes + ngx_escape_uri(NULL, src, size, NGX_ESCAPE_HTML);
|
|
251 |
}
|
|
252 |
else if (escapes == 0) {
|
|
253 |
/* No need to do extra escaping, avoid the temporary buffer */
|
|
254 |
return ngx_escape_uri(dst, src, size, NGX_ESCAPE_HTML);
|
|
255 |
}
|
|
256 |
else {
|
|
257 |
uintptr_t uescapes = ngx_escape_uri(NULL, src, size, NGX_ESCAPE_HTML);
|
|
258 |
size_t bufsz = size + 2 * uescapes;
|
|
259 |
|
|
260 |
/*
|
|
261 |
* GCC and CLANG both support stack-allocated variable length
|
|
262 |
* arrays. Take advantage of that to avoid a malloc-free cycle.
|
|
263 |
*/
|
|
264 |
#if defined(__GNUC__) || defined(__clang__)
|
|
265 |
u_char cbuf[bufsz];
|
|
266 |
u_char *buf = cbuf;
|
|
267 |
#else /* __GNUC__ || __clang__ */
|
|
268 |
u_char *buf = (u_char*) malloc(sizeof(u_char) * bufsz);
|
|
269 |
#endif /* __GNUC__ || __clang__ */
|
|
270 |
|
|
271 |
ngx_escape_uri(buf, src, size, NGX_ESCAPE_HTML);
|
|
272 |
|
|
273 |
while (bufsz--) {
|
|
274 |
switch (*buf) {
|
|
275 |
case ':':
|
|
276 |
*dst++ = '%';
|
|
277 |
*dst++ = '3';
|
|
278 |
*dst++ = 'A';
|
|
279 |
break;
|
|
280 |
case '?':
|
|
281 |
*dst++ = '%';
|
|
282 |
*dst++ = '3';
|
|
283 |
*dst++ = 'F';
|
|
284 |
break;
|
|
285 |
default:
|
|
286 |
*dst++ = *buf;
|
|
287 |
}
|
|
288 |
buf++;
|
|
289 |
}
|
|
290 |
|
|
291 |
#if !defined(__GNUC__) && !defined(__clang__)
|
|
292 |
free(buf);
|
|
293 |
#endif /* !__GNUC__ && !__clang__ */
|
|
294 |
|
|
295 |
return escapes + uescapes;
|
|
296 |
}
|
|
297 |
}
|
|
298 |
|
|
299 |
|
|
300 |
static ngx_inline ngx_buf_t*
|
|
301 |
make_header_buf(ngx_http_request_t *r, const ngx_str_t css_href)
|
|
302 |
{
|
|
303 |
size_t blen = r->uri.len
|
|
304 |
+ ngx_sizeof_ssz(t01_head1)
|
|
305 |
+ ngx_sizeof_ssz(t02_head2)
|
|
306 |
+ ngx_sizeof_ssz(t03_head3)
|
|
307 |
+ ngx_sizeof_ssz(t04_body1)
|
|
308 |
;
|
|
309 |
|
|
310 |
if (css_href.len) {
|
|
311 |
blen += css_href_pre.len \
|
|
312 |
+ css_href.len \
|
|
313 |
+ css_href_post.len
|
|
314 |
;
|
|
315 |
}
|
|
316 |
|
|
317 |
ngx_buf_t *b = ngx_create_temp_buf(r->pool, blen);
|
|
318 |
|
|
319 |
if (b == NULL) goto bailout;
|
|
320 |
|
|
321 |
b->last = ngx_cpymem_ssz(b->last, t01_head1);
|
|
322 |
|
|
323 |
if (css_href.len) {
|
|
324 |
b->last = ngx_cpymem_str(b->last, css_href_pre);
|
|
325 |
b->last = ngx_cpymem_str(b->last, css_href);
|
|
326 |
b->last = ngx_cpymem_str(b->last, css_href_post);
|
|
327 |
}
|
|
328 |
|
|
329 |
b->last = ngx_cpymem_ssz(b->last, t02_head2);
|
|
330 |
b->last = ngx_cpymem_str(b->last, r->uri);
|
|
331 |
b->last = ngx_cpymem_ssz(b->last, t03_head3);
|
|
332 |
b->last = ngx_cpymem_ssz(b->last, t04_body1);
|
|
333 |
|
|
334 |
bailout:
|
|
335 |
return b;
|
|
336 |
}
|
|
337 |
|
|
338 |
|
|
339 |
|
|
340 |
static ngx_inline ngx_buf_t*
|
|
341 |
make_footer_buf(ngx_http_request_t *r)
|
|
342 |
{
|
|
343 |
/*
|
|
344 |
* TODO: Make this buffer static (i.e. readonly and reusable from
|
|
345 |
* one request to another.
|
|
346 |
*/
|
|
347 |
ngx_buf_t *b = ngx_create_temp_buf(r->pool, ngx_sizeof_ssz(t08_foot1));
|
|
348 |
|
|
349 |
if (b == NULL) goto bailout;
|
|
350 |
|
|
351 |
b->last = ngx_cpymem_ssz(b->last, t08_foot1);
|
|
352 |
|
|
353 |
bailout:
|
|
354 |
return b;
|
|
355 |
}
|
|
356 |
|
|
357 |
|
|
358 |
|
|
359 |
static ngx_inline ngx_int_t
|
|
360 |
make_content_buf(
|
|
361 |
ngx_http_request_t *r, ngx_buf_t **pb,
|
|
362 |
ngx_http_fancyindex_loc_conf_t *alcf)
|
|
363 |
{
|
|
364 |
ngx_http_fancyindex_entry_t *entry;
|
|
365 |
|
|
366 |
off_t length;
|
|
367 |
size_t len, root, copy, allocated;
|
|
368 |
u_char *filename, *last, scale;
|
|
369 |
ngx_tm_t tm;
|
|
370 |
ngx_array_t entries;
|
|
371 |
ngx_time_t *tp;
|
|
372 |
ngx_uint_t i;
|
|
373 |
ngx_int_t size;
|
|
374 |
ngx_str_t path;
|
|
375 |
ngx_dir_t dir;
|
|
376 |
ngx_buf_t *b;
|
|
377 |
|
|
378 |
static char *months[] = {
|
|
379 |
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
|
380 |
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
|
|
381 |
};
|
|
382 |
|
|
383 |
/*
|
|
384 |
* NGX_DIR_MASK_LEN is lesser than NGX_HTTP_FANCYINDEX_PREALLOCATE
|
|
385 |
*/
|
|
386 |
if ((last = ngx_http_map_uri_to_path(r, &path, &root,
|
|
387 |
NGX_HTTP_FANCYINDEX_PREALLOCATE)) == NULL)
|
|
388 |
return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
389 |
|
|
390 |
allocated = path.len;
|
|
391 |
path.len = last - path.data - 1;
|
|
392 |
path.data[path.len] = '\0';
|
|
393 |
|
|
394 |
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
395 |
"http fancyindex: \"%s\"", path.data);
|
|
396 |
|
|
397 |
if (ngx_open_dir(&path, &dir) == NGX_ERROR) {
|
|
398 |
ngx_int_t rc, err = ngx_errno;
|
|
399 |
ngx_uint_t level;
|
|
400 |
|
|
401 |
if (err == NGX_ENOENT || err == NGX_ENOTDIR || err == NGX_ENAMETOOLONG) {
|
|
402 |
level = NGX_LOG_ERR;
|
|
403 |
rc = NGX_HTTP_NOT_FOUND;
|
|
404 |
} else if (err == NGX_EACCES) {
|
|
405 |
level = NGX_LOG_ERR;
|
|
406 |
rc = NGX_HTTP_FORBIDDEN;
|
|
407 |
} else {
|
|
408 |
level = NGX_LOG_CRIT;
|
|
409 |
rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
410 |
}
|
|
411 |
|
|
412 |
ngx_log_error(level, r->connection->log, err,
|
|
413 |
ngx_open_dir_n " \"%s\" failed", path.data);
|
|
414 |
|
|
415 |
return rc;
|
|
416 |
}
|
|
417 |
|
|
418 |
#if (NGX_SUPPRESS_WARN)
|
|
419 |
/* MSVC thinks 'entries' may be used without having been initialized */
|
|
420 |
ngx_memzero(&entries, sizeof(ngx_array_t));
|
|
421 |
#endif /* NGX_SUPPRESS_WARN */
|
|
422 |
|
|
423 |
|
|
424 |
if (ngx_array_init(&entries, r->pool, 40,
|
|
425 |
sizeof(ngx_http_fancyindex_entry_t)) != NGX_OK)
|
|
426 |
return ngx_http_fancyindex_error(r, &dir, &path);
|
|
427 |
|
|
428 |
filename = path.data;
|
|
429 |
filename[path.len] = '/';
|
|
430 |
|
|
431 |
/* Read directory entries and their associated information. */
|
|
432 |
for (;;) {
|
|
433 |
ngx_set_errno(0);
|
|
434 |
|
|
435 |
if (ngx_read_dir(&dir) == NGX_ERROR) {
|
|
436 |
ngx_int_t err = ngx_errno;
|
|
437 |
|
|
438 |
if (err != NGX_ENOMOREFILES) {
|
|
439 |
ngx_log_error(NGX_LOG_CRIT, r->connection->log, err,
|
|
440 |
ngx_read_dir_n " \"%V\" failed", &path);
|
|
441 |
return ngx_http_fancyindex_error(r, &dir, &path);
|
|
442 |
}
|
|
443 |
break;
|
|
444 |
}
|
|
445 |
|
|
446 |
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
447 |
"http fancyindex file: \"%s\"", ngx_de_name(&dir));
|
|
448 |
|
|
449 |
len = ngx_de_namelen(&dir);
|
|
450 |
|
|
451 |
if (ngx_de_name(&dir)[0] == '.')
|
|
452 |
continue;
|
|
453 |
|
|
454 |
#if NGX_PCRE
|
|
455 |
{
|
|
456 |
ngx_str_t str = { len, ngx_de_name(&dir) };
|
|
457 |
|
|
458 |
if (alcf->ignore && ngx_regex_exec_array(alcf->ignore, &str,
|
|
459 |
r->connection->log)
|
|
460 |
!= NGX_DECLINED)
|
|
461 |
{
|
|
462 |
continue;
|
|
463 |
}
|
|
464 |
}
|
|
465 |
#else /* !NGX_PCRE */
|
|
466 |
if (alcf->ignore) {
|
|
467 |
u_int match_found = 0;
|
|
468 |
ngx_str_t *s = alcf->ignore->elts;
|
|
469 |
|
|
470 |
for (i = 0; i < alcf->ignore->nelts; i++, s++) {
|
|
471 |
if (ngx_strcmp(ngx_de_name(&dir), s->data) == 0) {
|
|
472 |
match_found = 1;
|
|
473 |
break;
|
|
474 |
}
|
|
475 |
}
|
|
476 |
|
|
477 |
if (match_found) {
|
|
478 |
continue;
|
|
479 |
}
|
|
480 |
}
|
|
481 |
#endif /* NGX_PCRE */
|
|
482 |
|
|
483 |
if (!dir.valid_info) {
|
|
484 |
/* 1 byte for '/' and 1 byte for terminating '\0' */
|
|
485 |
if (path.len + 1 + len + 1 > allocated) {
|
|
486 |
allocated = path.len + 1 + len + 1
|
|
487 |
+ NGX_HTTP_FANCYINDEX_PREALLOCATE;
|
|
488 |
|
|
489 |
if ((filename = ngx_palloc(r->pool, allocated)) == NULL)
|
|
490 |
return ngx_http_fancyindex_error(r, &dir, &path);
|
|
491 |
|
|
492 |
last = ngx_cpystrn(filename, path.data, path.len + 1);
|
|
493 |
*last++ = '/';
|
|
494 |
}
|
|
495 |
|
|
496 |
ngx_cpystrn(last, ngx_de_name(&dir), len + 1);
|
|
497 |
|
|
498 |
if (ngx_de_info(filename, &dir) == NGX_FILE_ERROR) {
|
|
499 |
ngx_int_t err = ngx_errno;
|
|
500 |
|
|
501 |
if (err != NGX_ENOENT) {
|
|
502 |
ngx_log_error(NGX_LOG_ERR, r->connection->log, err,
|
|
503 |
ngx_de_info_n " \"%s\" failed", filename);
|
|
504 |
continue;
|
|
505 |
}
|
|
506 |
|
|
507 |
if (ngx_de_link_info(filename, &dir) == NGX_FILE_ERROR) {
|
|
508 |
ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
|
|
509 |
ngx_de_link_info_n " \"%s\" failed", filename);
|
|
510 |
return ngx_http_fancyindex_error(r, &dir, &path);
|
|
511 |
}
|
|
512 |
}
|
|
513 |
}
|
|
514 |
|
|
515 |
if ((entry = ngx_array_push(&entries)) == NULL)
|
|
516 |
return ngx_http_fancyindex_error(r, &dir, &path);
|
|
517 |
|
|
518 |
entry->name.len = len;
|
|
519 |
entry->name.data = ngx_palloc(r->pool, len + 1);
|
|
520 |
if (entry->name.data == NULL)
|
|
521 |
return ngx_http_fancyindex_error(r, &dir, &path);
|
|
522 |
|
|
523 |
ngx_cpystrn(entry->name.data, ngx_de_name(&dir), len + 1);
|
|
524 |
entry->escape = 2 * ngx_fancyindex_escape_uri(NULL,
|
|
525 |
ngx_de_name(&dir),
|
|
526 |
len);
|
|
527 |
|
|
528 |
entry->dir = ngx_de_is_dir(&dir);
|
|
529 |
entry->mtime = ngx_de_mtime(&dir);
|
|
530 |
entry->size = ngx_de_size(&dir);
|
|
531 |
entry->utf_len = (r->headers_out.charset.len == 5 &&
|
|
532 |
ngx_strncasecmp(r->headers_out.charset.data, (u_char*) "utf-8", 5) == 0)
|
|
533 |
? ngx_utf8_length(entry->name.data, entry->name.len)
|
|
534 |
: len;
|
|
535 |
}
|
|
536 |
|
|
537 |
if (ngx_close_dir(&dir) == NGX_ERROR) {
|
|
538 |
ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno,
|
|
539 |
ngx_close_dir_n " \"%s\" failed", &path);
|
|
540 |
}
|
|
541 |
|
|
542 |
/*
|
|
543 |
* Calculate needed buffer length.
|
|
544 |
*/
|
|
545 |
len = r->uri.len
|
|
546 |
+ ngx_sizeof_ssz(t05_body2)
|
|
547 |
+ ngx_sizeof_ssz(t06_list1)
|
|
548 |
+ ngx_sizeof_ssz(t07_list2)
|
|
549 |
;
|
|
550 |
|
|
551 |
entry = entries.elts;
|
|
552 |
for (i = 0; i < entries.nelts; i++) {
|
|
553 |
/*
|
|
554 |
* Genearated table rows are as follows, unneeded whitespace
|
|
555 |
* is stripped out:
|
|
556 |
*
|
|
557 |
* <tr class="X">
|
|
558 |
* <td><a href="U">fname</a></td>
|
|
559 |
* <td>size</td><td>date</td>
|
|
560 |
* </tr>
|
|
561 |
*/
|
|
562 |
len += ngx_sizeof_ssz("<tr class=\"X\"><td><a href=\"")
|
|
563 |
+ entry[i].name.len + entry[i].escape /* Escaped URL */
|
|
564 |
+ ngx_sizeof_ssz("\">")
|
|
565 |
+ entry[i].name.len + entry[i].utf_len
|
|
566 |
+ NGX_HTTP_FANCYINDEX_NAME_LEN + ngx_sizeof_ssz(">")
|
|
567 |
+ ngx_sizeof_ssz("</a></td><td>")
|
|
568 |
+ 20 /* File size */
|
|
569 |
+ ngx_sizeof_ssz("</td><td>")
|
|
570 |
+ ngx_sizeof_ssz(" 28-Sep-1970 12:00 ")
|
|
571 |
+ ngx_sizeof_ssz("</td></tr>\n")
|
|
572 |
+ 2 /* CR LF */
|
|
573 |
;
|
|
574 |
}
|
|
575 |
|
|
576 |
if ((b = ngx_create_temp_buf(r->pool, len)) == NULL)
|
|
577 |
return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
578 |
|
|
579 |
/* Sort entries, if needed */
|
|
580 |
if (entries.nelts > 1) {
|
|
581 |
ngx_qsort(entry, (size_t) entries.nelts,
|
|
582 |
sizeof(ngx_http_fancyindex_entry_t),
|
|
583 |
ngx_http_fancyindex_cmp_entries);
|
|
584 |
}
|
|
585 |
|
|
586 |
b->last = ngx_cpymem_str(b->last, r->uri);
|
|
587 |
b->last = ngx_cpymem_ssz(b->last, t05_body2);
|
|
588 |
b->last = ngx_cpymem_ssz(b->last, t06_list1);
|
|
589 |
|
|
590 |
tp = ngx_timeofday();
|
|
591 |
|
|
592 |
for (i = 0; i < entries.nelts; i++) {
|
|
593 |
static const char _evenodd[] = { 'e', 'o' };
|
|
594 |
b->last = ngx_cpymem_ssz(b->last, "<tr class=\"");
|
|
595 |
*b->last++ = _evenodd[i & 0x01];
|
|
596 |
/*
|
|
597 |
* Alternative implementation:
|
|
598 |
* *b->last++ = (i & 0x01) ? 'e' : 'o';
|
|
599 |
*/
|
|
600 |
b->last = ngx_cpymem_ssz(b->last, "\"><td><a href=\"");
|
|
601 |
|
|
602 |
if (entry[i].escape) {
|
|
603 |
ngx_fancyindex_escape_uri(b->last,
|
|
604 |
entry[i].name.data,
|
|
605 |
entry[i].name.len);
|
|
606 |
|
|
607 |
b->last += entry[i].name.len + entry[i].escape;
|
|
608 |
|
|
609 |
} else {
|
|
610 |
b->last = ngx_cpymem_str(b->last, entry[i].name);
|
|
611 |
}
|
|
612 |
|
|
613 |
if (entry[i].dir) {
|
|
614 |
*b->last++ = '/';
|
|
615 |
}
|
|
616 |
|
|
617 |
*b->last++ = '"';
|
|
618 |
*b->last++ = '>';
|
|
619 |
|
|
620 |
len = entry[i].utf_len;
|
|
621 |
|
|
622 |
if (entry[i].name.len - len) {
|
|
623 |
if (len > NGX_HTTP_FANCYINDEX_NAME_LEN) {
|
|
624 |
copy = NGX_HTTP_FANCYINDEX_NAME_LEN - 3 + 1;
|
|
625 |
} else {
|
|
626 |
copy = NGX_HTTP_FANCYINDEX_NAME_LEN + 1;
|
|
627 |
}
|
|
628 |
|
|
629 |
b->last = ngx_utf8_cpystrn(b->last, entry[i].name.data,
|
|
630 |
copy, entry[i].name.len);
|
|
631 |
last = b->last;
|
|
632 |
|
|
633 |
} else {
|
|
634 |
b->last = ngx_cpystrn(b->last, entry[i].name.data,
|
|
635 |
NGX_HTTP_FANCYINDEX_NAME_LEN + 1);
|
|
636 |
last = b->last - 3;
|
|
637 |
}
|
|
638 |
|
|
639 |
if (len > NGX_HTTP_FANCYINDEX_NAME_LEN) {
|
|
640 |
b->last = ngx_cpymem_ssz(last, "..></a></td><td>");
|
|
641 |
|
|
642 |
} else {
|
|
643 |
if (entry[i].dir && NGX_HTTP_FANCYINDEX_NAME_LEN - len > 0) {
|
|
644 |
*b->last++ = '/';
|
|
645 |
len++;
|
|
646 |
}
|
|
647 |
|
|
648 |
b->last = ngx_cpymem_ssz(b->last, "</a></td><td>");
|
|
649 |
}
|
|
650 |
|
|
651 |
if (alcf->exact_size) {
|
|
652 |
if (entry[i].dir) {
|
|
653 |
*b->last++ = '-';
|
|
654 |
} else {
|
|
655 |
b->last = ngx_sprintf(b->last, "%19O", entry[i].size);
|
|
656 |
}
|
|
657 |
|
|
658 |
} else {
|
|
659 |
if (entry[i].dir) {
|
|
660 |
*b->last++ = '-';
|
|
661 |
} else {
|
|
662 |
length = entry[i].size;
|
|
663 |
|
|
664 |
if (length > 1024 * 1024 * 1024 - 1) {
|
|
665 |
size = (ngx_int_t) (length / (1024 * 1024 * 1024));
|
|
666 |
if ((length % (1024 * 1024 * 1024))
|
|
667 |
> (1024 * 1024 * 1024 / 2 - 1))
|
|
668 |
{
|
|
669 |
size++;
|
|
670 |
}
|
|
671 |
scale = 'G';
|
|
672 |
|
|
673 |
} else if (length > 1024 * 1024 - 1) {
|
|
674 |
size = (ngx_int_t) (length / (1024 * 1024));
|
|
675 |
if ((length % (1024 * 1024)) > (1024 * 1024 / 2 - 1)) {
|
|
676 |
size++;
|
|
677 |
}
|
|
678 |
scale = 'M';
|
|
679 |
|
|
680 |
} else if (length > 9999) {
|
|
681 |
size = (ngx_int_t) (length / 1024);
|
|
682 |
if (length % 1024 > 511) {
|
|
683 |
size++;
|
|
684 |
}
|
|
685 |
scale = 'K';
|
|
686 |
|
|
687 |
} else {
|
|
688 |
size = (ngx_int_t) length;
|
|
689 |
scale = '\0';
|
|
690 |
}
|
|
691 |
|
|
692 |
if (scale) {
|
|
693 |
b->last = ngx_sprintf(b->last, "%6i%c", size, scale);
|
|
694 |
|
|
695 |
} else {
|
|
696 |
b->last = ngx_sprintf(b->last, " %6i", size);
|
|
697 |
}
|
|
698 |
}
|
|
699 |
}
|
|
700 |
|
|
701 |
ngx_gmtime(entry[i].mtime + tp->gmtoff * 60 * alcf->localtime, &tm);
|
|
702 |
|
|
703 |
b->last = ngx_sprintf(b->last, "</td><td>%02d-%s-%d %02d:%02d</td></tr>",
|
|
704 |
tm.ngx_tm_mday,
|
|
705 |
months[tm.ngx_tm_mon - 1],
|
|
706 |
tm.ngx_tm_year,
|
|
707 |
tm.ngx_tm_hour,
|
|
708 |
tm.ngx_tm_min);
|
|
709 |
|
|
710 |
|
|
711 |
*b->last++ = CR;
|
|
712 |
*b->last++ = LF;
|
|
713 |
}
|
|
714 |
|
|
715 |
/* Output table bottom */
|
|
716 |
b->last = ngx_cpymem_ssz(b->last, t07_list2);
|
|
717 |
|
|
718 |
*pb = b;
|
|
719 |
return NGX_OK;
|
|
720 |
}
|
|
721 |
|
|
722 |
|
|
723 |
|
|
724 |
static ngx_int_t
|
|
725 |
ngx_http_fancyindex_handler(ngx_http_request_t *r)
|
|
726 |
{
|
|
727 |
ngx_http_request_t *sr;
|
|
728 |
ngx_str_t *sr_uri;
|
|
729 |
ngx_str_t rel_uri;
|
|
730 |
ngx_int_t rc;
|
|
731 |
ngx_http_fancyindex_loc_conf_t *alcf;
|
|
732 |
ngx_chain_t out[3] = {
|
|
733 |
{ NULL, NULL }, { NULL, NULL}, { NULL, NULL }};
|
|
734 |
|
|
735 |
|
|
736 |
if (r->uri.data[r->uri.len - 1] != '/') {
|
|
737 |
return NGX_DECLINED;
|
|
738 |
}
|
|
739 |
|
|
740 |
/* TODO: Win32 */
|
|
741 |
#if defined(nginx_version) \
|
|
742 |
&& ((nginx_version < 7066) \
|
|
743 |
|| ((nginx_version > 8000) && (nginx_version < 8038)))
|
|
744 |
if (r->zero_in_uri) {
|
|
745 |
return NGX_DECLINED;
|
|
746 |
}
|
|
747 |
#endif
|
|
748 |
|
|
749 |
if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) {
|
|
750 |
return NGX_DECLINED;
|
|
751 |
}
|
|
752 |
|
|
753 |
alcf = ngx_http_get_module_loc_conf(r, ngx_http_fancyindex_module);
|
|
754 |
|
|
755 |
if (!alcf->enable) {
|
|
756 |
return NGX_DECLINED;
|
|
757 |
}
|
|
758 |
|
|
759 |
if ((rc = make_content_buf(r, &out[0].buf, alcf) != NGX_OK))
|
|
760 |
return rc;
|
|
761 |
|
|
762 |
out[0].buf->last_in_chain = 1;
|
|
763 |
|
|
764 |
r->headers_out.status = NGX_HTTP_OK;
|
|
765 |
r->headers_out.content_type_len = ngx_sizeof_ssz("text/html");
|
|
766 |
r->headers_out.content_type.len = ngx_sizeof_ssz("text/html");
|
|
767 |
r->headers_out.content_type.data = (u_char *) "text/html";
|
|
768 |
|
|
769 |
rc = ngx_http_send_header(r);
|
|
770 |
|
|
771 |
if (rc != NGX_OK || r->header_only)
|
|
772 |
return rc;
|
|
773 |
|
|
774 |
if (alcf->header.len > 0) {
|
|
775 |
/* URI is configured, make Nginx take care of with a subrequest. */
|
|
776 |
sr_uri = &alcf->header;
|
|
777 |
|
|
778 |
if (*sr_uri->data != '/') {
|
|
779 |
/* Relative path */
|
|
780 |
rel_uri.len = r->uri.len + alcf->header.len;
|
|
781 |
rel_uri.data = ngx_palloc(r->pool, rel_uri.len);
|
|
782 |
if (rel_uri.data == NULL) {
|
|
783 |
return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
784 |
}
|
|
785 |
ngx_memcpy(ngx_cpymem(rel_uri.data, r->uri.data, r->uri.len),
|
|
786 |
alcf->header.data, alcf->header.len);
|
|
787 |
sr_uri = &rel_uri;
|
|
788 |
}
|
|
789 |
|
|
790 |
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
791 |
"http fancyindex: header subrequest \"%V\"", sr_uri);
|
|
792 |
|
|
793 |
rc = ngx_http_subrequest(r, sr_uri, NULL, &sr, NULL, 0);
|
|
794 |
if (rc == NGX_ERROR || rc == NGX_DONE) {
|
|
795 |
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
796 |
"http fancyindex: header subrequest for \"%V\" failed", sr_uri);
|
|
797 |
return rc;
|
|
798 |
}
|
|
799 |
|
|
800 |
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
801 |
"http fancyindex: header subrequest status = %i",
|
|
802 |
sr->headers_out.status);
|
|
803 |
/* ngx_http_subrequest returns NGX_OK(0), not NGX_HTTP_OK(200) */
|
|
804 |
if (sr->headers_out.status != NGX_OK) {
|
|
805 |
/*
|
|
806 |
* XXX: Should we write a message to the error log just in case
|
|
807 |
* we get something different from a 404?
|
|
808 |
*/
|
|
809 |
goto add_builtin_header;
|
|
810 |
}
|
|
811 |
}
|
|
812 |
else {
|
|
813 |
add_builtin_header:
|
|
814 |
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
815 |
"http fancyindex: adding built-in header");
|
|
816 |
/* Make space before */
|
|
817 |
out[1].next = out[0].next;
|
|
818 |
out[1].buf = out[0].buf;
|
|
819 |
/* Chain header buffer */
|
|
820 |
out[0].next = &out[1];
|
|
821 |
out[0].buf = make_header_buf(r, alcf->css_href);
|
|
822 |
}
|
|
823 |
|
|
824 |
/* If footer is disabled, chain up footer buffer. */
|
|
825 |
if (alcf->footer.len == 0) {
|
|
826 |
ngx_uint_t last = (alcf->header.len == 0) ? 2 : 1;
|
|
827 |
|
|
828 |
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
829 |
"http fancyindex: adding built-in footer at %i", last);
|
|
830 |
|
|
831 |
out[last-1].next = &out[last];
|
|
832 |
out[last].buf = make_footer_buf(r);
|
|
833 |
|
|
834 |
out[last-1].buf->last_in_chain = 0;
|
|
835 |
out[last].buf->last_in_chain = 1;
|
|
836 |
out[last].buf->last_buf = 1;
|
|
837 |
/* Send everything with a single call :D */
|
|
838 |
return ngx_http_output_filter(r, &out[0]);
|
|
839 |
}
|
|
840 |
|
|
841 |
/*
|
|
842 |
* If we reach here, we were asked to send a custom footer. We need to:
|
|
843 |
* partially send whatever is referenced from out[0] and then send the
|
|
844 |
* footer as a subrequest. If the subrequest fails, we should send the
|
|
845 |
* standard footer as well.
|
|
846 |
*/
|
|
847 |
rc = ngx_http_output_filter(r, &out[0]);
|
|
848 |
|
|
849 |
if (rc != NGX_OK && rc != NGX_AGAIN)
|
|
850 |
return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
851 |
|
|
852 |
/* URI is configured, make Nginx take care of with a subrequest. */
|
|
853 |
sr_uri = &alcf->footer;
|
|
854 |
|
|
855 |
if (*sr_uri->data != '/') {
|
|
856 |
/* Relative path */
|
|
857 |
rel_uri.len = r->uri.len + alcf->footer.len;
|
|
858 |
rel_uri.data = ngx_palloc(r->pool, rel_uri.len);
|
|
859 |
if (rel_uri.data == NULL) {
|
|
860 |
return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
861 |
}
|
|
862 |
ngx_memcpy(ngx_cpymem(rel_uri.data, r->uri.data, r->uri.len),
|
|
863 |
alcf->footer.data, alcf->footer.len);
|
|
864 |
sr_uri = &rel_uri;
|
|
865 |
}
|
|
866 |
|
|
867 |
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
868 |
"http fancyindex: footer subrequest \"%V\"", sr_uri);
|
|
869 |
|
|
870 |
rc = ngx_http_subrequest(r, sr_uri, NULL, &sr, NULL, 0);
|
|
871 |
if (rc == NGX_ERROR || rc == NGX_DONE) {
|
|
872 |
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
873 |
"http fancyindex: footer subrequest for \"%V\" failed", sr_uri);
|
|
874 |
return rc;
|
|
875 |
}
|
|
876 |
|
|
877 |
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
878 |
"http fancyindex: header subrequest status = %i",
|
|
879 |
sr->headers_out.status);
|
|
880 |
|
|
881 |
/* see above: ngx_http_subrequest resturns NGX_OK (0) not NGX_HTTP_OK (200) */
|
|
882 |
if (sr->headers_out.status != NGX_OK) {
|
|
883 |
/*
|
|
884 |
* XXX: Should we write a message to the error log just in case
|
|
885 |
* we get something different from a 404?
|
|
886 |
*/
|
|
887 |
out[0].next = NULL;
|
|
888 |
out[0].buf = make_footer_buf(r);
|
|
889 |
out[0].buf->last_in_chain = 1;
|
|
890 |
out[0].buf->last_buf = 1;
|
|
891 |
/* Directly send out the builtin footer */
|
|
892 |
return ngx_http_output_filter(r, &out[0]);
|
|
893 |
}
|
|
894 |
|
|
895 |
return (r != r->main) ? rc : ngx_http_send_special(r, NGX_HTTP_LAST);
|
|
896 |
}
|
|
897 |
|
|
898 |
|
|
899 |
static int ngx_libc_cdecl
|
|
900 |
ngx_http_fancyindex_cmp_entries(const void *one, const void *two)
|
|
901 |
{
|
|
902 |
ngx_http_fancyindex_entry_t *first = (ngx_http_fancyindex_entry_t *) one;
|
|
903 |
ngx_http_fancyindex_entry_t *second = (ngx_http_fancyindex_entry_t *) two;
|
|
904 |
|
|
905 |
if (first->dir && !second->dir) {
|
|
906 |
/* move the directories to the start */
|
|
907 |
return -1;
|
|
908 |
}
|
|
909 |
|
|
910 |
if (!first->dir && second->dir) {
|
|
911 |
/* move the directories to the start */
|
|
912 |
return 1;
|
|
913 |
}
|
|
914 |
|
|
915 |
return (int) ngx_strcmp(first->name.data, second->name.data);
|
|
916 |
}
|
|
917 |
|
|
918 |
|
|
919 |
|
|
920 |
|
|
921 |
static ngx_int_t
|
|
922 |
ngx_http_fancyindex_error(ngx_http_request_t *r, ngx_dir_t *dir, ngx_str_t *name)
|
|
923 |
{
|
|
924 |
if (ngx_close_dir(dir) == NGX_ERROR) {
|
|
925 |
ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno,
|
|
926 |
ngx_close_dir_n " \"%V\" failed", name);
|
|
927 |
}
|
|
928 |
|
|
929 |
return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
930 |
}
|
|
931 |
|
|
932 |
|
|
933 |
static void *
|
|
934 |
ngx_http_fancyindex_create_loc_conf(ngx_conf_t *cf)
|
|
935 |
{
|
|
936 |
ngx_http_fancyindex_loc_conf_t *conf;
|
|
937 |
|
|
938 |
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_fancyindex_loc_conf_t));
|
|
939 |
if (conf == NULL) {
|
|
940 |
return NGX_CONF_ERROR;
|
|
941 |
}
|
|
942 |
|
|
943 |
/*
|
|
944 |
* Set by ngx_pcalloc:
|
|
945 |
* conf->header.len = 0
|
|
946 |
* conf->header.data = NULL
|
|
947 |
* conf->footer.len = 0
|
|
948 |
* conf->footer.data = NULL
|
|
949 |
*/
|
|
950 |
conf->enable = NGX_CONF_UNSET;
|
|
951 |
conf->localtime = NGX_CONF_UNSET;
|
|
952 |
conf->exact_size = NGX_CONF_UNSET;
|
|
953 |
conf->ignore = NGX_CONF_UNSET_PTR;
|
|
954 |
|
|
955 |
return conf;
|
|
956 |
}
|
|
957 |
|
|
958 |
|
|
959 |
static char *
|
|
960 |
ngx_http_fancyindex_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
961 |
{
|
|
962 |
ngx_http_fancyindex_loc_conf_t *prev = parent;
|
|
963 |
ngx_http_fancyindex_loc_conf_t *conf = child;
|
|
964 |
|
|
965 |
ngx_conf_merge_value(conf->enable, prev->enable, 0);
|
|
966 |
ngx_conf_merge_value(conf->localtime, prev->localtime, 0);
|
|
967 |
ngx_conf_merge_value(conf->exact_size, prev->exact_size, 1);
|
|
968 |
|
|
969 |
ngx_conf_merge_str_value(conf->header, prev->header, "");
|
|
970 |
ngx_conf_merge_str_value(conf->footer, prev->footer, "");
|
|
971 |
|
|
972 |
ngx_conf_merge_ptr_value(conf->ignore, prev->ignore, NULL);
|
|
973 |
|
|
974 |
return NGX_CONF_OK;
|
|
975 |
}
|
|
976 |
|
|
977 |
|
|
978 |
static char*
|
|
979 |
ngx_http_fancyindex_ignore(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
980 |
{
|
|
981 |
ngx_http_fancyindex_loc_conf_t *alcf = conf;
|
|
982 |
ngx_str_t *value;
|
|
983 |
|
|
984 |
#if (NGX_PCRE)
|
|
985 |
ngx_uint_t i;
|
|
986 |
ngx_regex_elt_t *re;
|
|
987 |
ngx_regex_compile_t rc;
|
|
988 |
u_char errstr[NGX_MAX_CONF_ERRSTR];
|
|
989 |
|
|
990 |
if (alcf->ignore == NGX_CONF_UNSET_PTR) {
|
|
991 |
alcf->ignore = ngx_array_create(cf->pool, 2, sizeof(ngx_regex_elt_t));
|
|
992 |
if (alcf->ignore == NULL) {
|
|
993 |
return NGX_CONF_ERROR;
|
|
994 |
}
|
|
995 |
}
|
|
996 |
|
|
997 |
value = cf->args->elts;
|
|
998 |
|
|
999 |
ngx_memzero(&rc, sizeof(ngx_regex_compile_t));
|
|
1000 |
|
|
1001 |
rc.err.data = errstr;
|
|
1002 |
rc.err.len = NGX_MAX_CONF_ERRSTR;
|
|
1003 |
rc.pool = cf->pool;
|
|
1004 |
|
|
1005 |
for (i = 1; i < cf->args->nelts; i++) {
|
|
1006 |
re = ngx_array_push(alcf->ignore);
|
|
1007 |
if (re == NULL) {
|
|
1008 |
return NGX_CONF_ERROR;
|
|
1009 |
}
|
|
1010 |
|
|
1011 |
rc.pattern = value[i];
|
|
1012 |
rc.options = NGX_REGEX_CASELESS;
|
|
1013 |
|
|
1014 |
if (ngx_regex_compile(&rc) != NGX_OK) {
|
|
1015 |
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "%V", &rc.err);
|
|
1016 |
return NGX_CONF_ERROR;
|
|
1017 |
}
|
|
1018 |
|
|
1019 |
re->name = value[i].data;
|
|
1020 |
re->regex = rc.regex;
|
|
1021 |
}
|
|
1022 |
|
|
1023 |
return NGX_CONF_OK;
|
|
1024 |
#else /* !NGX_PCRE */
|
|
1025 |
ngx_uint_t i;
|
|
1026 |
ngx_str_t *str;
|
|
1027 |
|
|
1028 |
if (alcf->ignore == NGX_CONF_UNSET_PTR) {
|
|
1029 |
alcf->ignore = ngx_array_create(cf->pool, 2, sizeof(ngx_str_t));
|
|
1030 |
if (alcf->ignore == NULL) {
|
|
1031 |
return NGX_CONF_ERROR;
|
|
1032 |
}
|
|
1033 |
}
|
|
1034 |
|
|
1035 |
value = cf->args->elts;
|
|
1036 |
|
|
1037 |
for (i = 1; i < cf->args->nelts; i++) {
|
|
1038 |
str = ngx_array_push(alcf->ignore);
|
|
1039 |
if (str == NULL) {
|
|
1040 |
return NGX_CONF_ERROR;
|
|
1041 |
}
|
|
1042 |
|
|
1043 |
str->data = value[i].data;
|
|
1044 |
str->len = value[i].len;
|
|
1045 |
}
|
|
1046 |
|
|
1047 |
return NGX_CONF_OK;
|
|
1048 |
#endif /* NGX_PCRE */
|
|
1049 |
|
|
1050 |
}
|
|
1051 |
|
|
1052 |
|
|
1053 |
static ngx_int_t
|
|
1054 |
ngx_http_fancyindex_init(ngx_conf_t *cf)
|
|
1055 |
{
|
|
1056 |
ngx_http_handler_pt *h;
|
|
1057 |
ngx_http_core_main_conf_t *cmcf;
|
|
1058 |
|
|
1059 |
cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
|
|
1060 |
|
|
1061 |
h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
|
|
1062 |
if (h == NULL) {
|
|
1063 |
return NGX_ERROR;
|
|
1064 |
}
|
|
1065 |
|
|
1066 |
*h = ngx_http_fancyindex_handler;
|
|
1067 |
|
|
1068 |
return NGX_OK;
|
|
1069 |
}
|
|
1070 |
|
|
1071 |
/* vim:et:sw=4:ts=4:
|
|
1072 |
*/
|