Mercurial > hg > nginx-quic
annotate src/http/modules/ngx_http_autoindex_module.c @ 7524:deebe988cbd7
Perl: reworked perl module to pass ctx instead of request.
This ensures that correct ctx is always available, including after
filter finalization. In particular, this fixes a segmentation fault
with the following configuration:
location / {
image_filter test;
perl 'sub {
my $r = shift;
$r->send_http_header();
$r->print("foo\n");
$r->print("bar\n");
}';
}
This also seems to be the only way to correctly handle filter finalization
in various complex cases, for example, when embedded perl is used both
in the original handler and in an error page called after filter
finalization.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Fri, 12 Jul 2019 11:29:22 +0300 |
parents | a91b93f3f3e7 |
children |
rev | line source |
---|---|
457 | 1 |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4412 | 4 * Copyright (C) Nginx, Inc. |
457 | 5 */ |
6 | |
7 | |
8 #include <ngx_config.h> | |
9 #include <ngx_core.h> | |
10 #include <ngx_http.h> | |
11 | |
12 | |
13 #if 0 | |
14 | |
15 typedef struct { | |
16 ngx_buf_t *buf; | |
17 size_t size; | |
18 ngx_pool_t *pool; | |
19 size_t alloc_size; | |
20 ngx_chain_t **last_out; | |
21 } ngx_http_autoindex_ctx_t; | |
22 | |
23 #endif | |
24 | |
25 | |
26 typedef struct { | |
27 ngx_str_t name; | |
525 | 28 size_t utf_len; |
2849
6a62bed048cd
fix colon in file name for ngx_http_autoindex_module
Igor Sysoev <igor@sysoev.ru>
parents:
2721
diff
changeset
|
29 size_t escape; |
4193
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
30 size_t escape_html; |
2849
6a62bed048cd
fix colon in file name for ngx_http_autoindex_module
Igor Sysoev <igor@sysoev.ru>
parents:
2721
diff
changeset
|
31 |
6a62bed048cd
fix colon in file name for ngx_http_autoindex_module
Igor Sysoev <igor@sysoev.ru>
parents:
2721
diff
changeset
|
32 unsigned dir:1; |
5944
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
33 unsigned file:1; |
2849
6a62bed048cd
fix colon in file name for ngx_http_autoindex_module
Igor Sysoev <igor@sysoev.ru>
parents:
2721
diff
changeset
|
34 |
457 | 35 time_t mtime; |
36 off_t size; | |
37 } ngx_http_autoindex_entry_t; | |
38 | |
39 | |
40 typedef struct { | |
41 ngx_flag_t enable; | |
5944
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
42 ngx_uint_t format; |
519 | 43 ngx_flag_t localtime; |
527 | 44 ngx_flag_t exact_size; |
457 | 45 } ngx_http_autoindex_loc_conf_t; |
46 | |
47 | |
5944
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
48 #define NGX_HTTP_AUTOINDEX_HTML 0 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
49 #define NGX_HTTP_AUTOINDEX_JSON 1 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
50 #define NGX_HTTP_AUTOINDEX_JSONP 2 |
5945
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
51 #define NGX_HTTP_AUTOINDEX_XML 3 |
5944
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
52 |
557 | 53 #define NGX_HTTP_AUTOINDEX_PREALLOCATE 50 |
54 | |
55 #define NGX_HTTP_AUTOINDEX_NAME_LEN 50 | |
457 | 56 |
57 | |
5943
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
58 static ngx_buf_t *ngx_http_autoindex_html(ngx_http_request_t *r, |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
59 ngx_array_t *entries); |
5944
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
60 static ngx_buf_t *ngx_http_autoindex_json(ngx_http_request_t *r, |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
61 ngx_array_t *entries, ngx_str_t *callback); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
62 static ngx_int_t ngx_http_autoindex_jsonp_callback(ngx_http_request_t *r, |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
63 ngx_str_t *callback); |
5945
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
64 static ngx_buf_t *ngx_http_autoindex_xml(ngx_http_request_t *r, |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
65 ngx_array_t *entries); |
5944
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
66 |
503 | 67 static int ngx_libc_cdecl ngx_http_autoindex_cmp_entries(const void *one, |
68 const void *two); | |
499 | 69 static ngx_int_t ngx_http_autoindex_error(ngx_http_request_t *r, |
557 | 70 ngx_dir_t *dir, ngx_str_t *name); |
5944
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
71 |
681 | 72 static ngx_int_t ngx_http_autoindex_init(ngx_conf_t *cf); |
457 | 73 static void *ngx_http_autoindex_create_loc_conf(ngx_conf_t *cf); |
74 static char *ngx_http_autoindex_merge_loc_conf(ngx_conf_t *cf, | |
499 | 75 void *parent, void *child); |
457 | 76 |
77 | |
5944
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
78 static ngx_conf_enum_t ngx_http_autoindex_format[] = { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
79 { ngx_string("html"), NGX_HTTP_AUTOINDEX_HTML }, |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
80 { ngx_string("json"), NGX_HTTP_AUTOINDEX_JSON }, |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
81 { ngx_string("jsonp"), NGX_HTTP_AUTOINDEX_JSONP }, |
5945
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
82 { ngx_string("xml"), NGX_HTTP_AUTOINDEX_XML }, |
5944
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
83 { ngx_null_string, 0 } |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
84 }; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
85 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
86 |
457 | 87 static ngx_command_t ngx_http_autoindex_commands[] = { |
88 | |
89 { ngx_string("autoindex"), | |
90 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
91 ngx_conf_set_flag_slot, | |
92 NGX_HTTP_LOC_CONF_OFFSET, | |
93 offsetof(ngx_http_autoindex_loc_conf_t, enable), | |
94 NULL }, | |
95 | |
5944
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
96 { ngx_string("autoindex_format"), |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
97 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
98 ngx_conf_set_enum_slot, |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
99 NGX_HTTP_LOC_CONF_OFFSET, |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
100 offsetof(ngx_http_autoindex_loc_conf_t, format), |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
101 &ngx_http_autoindex_format }, |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
102 |
519 | 103 { ngx_string("autoindex_localtime"), |
104 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
105 ngx_conf_set_flag_slot, | |
106 NGX_HTTP_LOC_CONF_OFFSET, | |
107 offsetof(ngx_http_autoindex_loc_conf_t, localtime), | |
108 NULL }, | |
109 | |
527 | 110 { ngx_string("autoindex_exact_size"), |
111 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
112 ngx_conf_set_flag_slot, | |
113 NGX_HTTP_LOC_CONF_OFFSET, | |
114 offsetof(ngx_http_autoindex_loc_conf_t, exact_size), | |
115 NULL }, | |
116 | |
457 | 117 ngx_null_command |
118 }; | |
119 | |
120 | |
667 | 121 static ngx_http_module_t ngx_http_autoindex_module_ctx = { |
509 | 122 NULL, /* preconfiguration */ |
681 | 123 ngx_http_autoindex_init, /* postconfiguration */ |
457 | 124 |
125 NULL, /* create main configuration */ | |
126 NULL, /* init main configuration */ | |
127 | |
128 NULL, /* create server configuration */ | |
129 NULL, /* merge server configuration */ | |
130 | |
4499
778ef9c3fd2d
Fixed spelling in single-line comments.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
131 ngx_http_autoindex_create_loc_conf, /* create location configuration */ |
778ef9c3fd2d
Fixed spelling in single-line comments.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
132 ngx_http_autoindex_merge_loc_conf /* merge location configuration */ |
457 | 133 }; |
134 | |
135 | |
136 ngx_module_t ngx_http_autoindex_module = { | |
509 | 137 NGX_MODULE_V1, |
577 | 138 &ngx_http_autoindex_module_ctx, /* module context */ |
457 | 139 ngx_http_autoindex_commands, /* module directives */ |
140 NGX_HTTP_MODULE, /* module type */ | |
541 | 141 NULL, /* init master */ |
681 | 142 NULL, /* init module */ |
541 | 143 NULL, /* init process */ |
144 NULL, /* init thread */ | |
145 NULL, /* exit thread */ | |
146 NULL, /* exit process */ | |
147 NULL, /* exit master */ | |
148 NGX_MODULE_V1_PADDING | |
457 | 149 }; |
150 | |
151 | |
499 | 152 static ngx_int_t |
153 ngx_http_autoindex_handler(ngx_http_request_t *r) | |
457 | 154 { |
5943
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
155 u_char *last, *filename; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
156 size_t len, allocated, root; |
457 | 157 ngx_err_t err; |
158 ngx_buf_t *b; | |
5943
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
159 ngx_int_t rc; |
5944
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
160 ngx_str_t path, callback; |
457 | 161 ngx_dir_t dir; |
5944
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
162 ngx_uint_t level, format; |
457 | 163 ngx_pool_t *pool; |
557 | 164 ngx_chain_t out; |
457 | 165 ngx_array_t entries; |
166 ngx_http_autoindex_entry_t *entry; | |
167 ngx_http_autoindex_loc_conf_t *alcf; | |
168 | |
169 if (r->uri.data[r->uri.len - 1] != '/') { | |
170 return NGX_DECLINED; | |
171 } | |
172 | |
645 | 173 if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) { |
637 | 174 return NGX_DECLINED; |
175 } | |
176 | |
457 | 177 alcf = ngx_http_get_module_loc_conf(r, ngx_http_autoindex_module); |
178 | |
179 if (!alcf->enable) { | |
180 return NGX_DECLINED; | |
181 } | |
182 | |
7164
044cb349fd3c
Autoindex: discard request body (ticket #1439).
Maxim Dounin <mdounin@mdounin.ru>
parents:
5945
diff
changeset
|
183 rc = ngx_http_discard_request_body(r); |
044cb349fd3c
Autoindex: discard request body (ticket #1439).
Maxim Dounin <mdounin@mdounin.ru>
parents:
5945
diff
changeset
|
184 |
044cb349fd3c
Autoindex: discard request body (ticket #1439).
Maxim Dounin <mdounin@mdounin.ru>
parents:
5945
diff
changeset
|
185 if (rc != NGX_OK) { |
044cb349fd3c
Autoindex: discard request body (ticket #1439).
Maxim Dounin <mdounin@mdounin.ru>
parents:
5945
diff
changeset
|
186 return rc; |
044cb349fd3c
Autoindex: discard request body (ticket #1439).
Maxim Dounin <mdounin@mdounin.ru>
parents:
5945
diff
changeset
|
187 } |
044cb349fd3c
Autoindex: discard request body (ticket #1439).
Maxim Dounin <mdounin@mdounin.ru>
parents:
5945
diff
changeset
|
188 |
773 | 189 last = ngx_http_map_uri_to_path(r, &path, &root, |
190 NGX_HTTP_AUTOINDEX_PREALLOCATE); | |
557 | 191 if (last == NULL) { |
192 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
457 | 193 } |
194 | |
557 | 195 allocated = path.len; |
1627 | 196 path.len = last - path.data; |
197 if (path.len > 1) { | |
198 path.len--; | |
199 } | |
557 | 200 path.data[path.len] = '\0'; |
457 | 201 |
202 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
557 | 203 "http autoindex: \"%s\"", path.data); |
457 | 204 |
5944
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
205 format = alcf->format; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
206 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
207 if (format == NGX_HTTP_AUTOINDEX_JSONP) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
208 if (ngx_http_autoindex_jsonp_callback(r, &callback) != NGX_OK) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
209 return NGX_HTTP_BAD_REQUEST; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
210 } |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
211 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
212 if (callback.len == 0) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
213 format = NGX_HTTP_AUTOINDEX_JSON; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
214 } |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
215 } |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
216 |
557 | 217 if (ngx_open_dir(&path, &dir) == NGX_ERROR) { |
457 | 218 err = ngx_errno; |
219 | |
543 | 220 if (err == NGX_ENOENT |
221 || err == NGX_ENOTDIR | |
222 || err == NGX_ENAMETOOLONG) | |
223 { | |
457 | 224 level = NGX_LOG_ERR; |
225 rc = NGX_HTTP_NOT_FOUND; | |
226 | |
227 } else if (err == NGX_EACCES) { | |
228 level = NGX_LOG_ERR; | |
229 rc = NGX_HTTP_FORBIDDEN; | |
230 | |
231 } else { | |
232 level = NGX_LOG_CRIT; | |
233 rc = NGX_HTTP_INTERNAL_SERVER_ERROR; | |
234 } | |
235 | |
236 ngx_log_error(level, r->connection->log, err, | |
557 | 237 ngx_open_dir_n " \"%s\" failed", path.data); |
457 | 238 |
239 return rc; | |
240 } | |
241 | |
242 #if (NGX_SUPPRESS_WARN) | |
557 | 243 |
457 | 244 /* MSVC thinks 'entries' may be used without having been initialized */ |
245 ngx_memzero(&entries, sizeof(ngx_array_t)); | |
557 | 246 |
457 | 247 #endif |
248 | |
557 | 249 /* TODO: pool should be temporary pool */ |
250 pool = r->pool; | |
251 | |
252 if (ngx_array_init(&entries, pool, 40, sizeof(ngx_http_autoindex_entry_t)) | |
253 != NGX_OK) | |
457 | 254 { |
557 | 255 return ngx_http_autoindex_error(r, &dir, &path); |
457 | 256 } |
257 | |
523 | 258 r->headers_out.status = NGX_HTTP_OK; |
5944
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
259 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
260 switch (format) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
261 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
262 case NGX_HTTP_AUTOINDEX_JSON: |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
263 ngx_str_set(&r->headers_out.content_type, "application/json"); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
264 break; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
265 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
266 case NGX_HTTP_AUTOINDEX_JSONP: |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
267 ngx_str_set(&r->headers_out.content_type, "application/javascript"); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
268 break; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
269 |
5945
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
270 case NGX_HTTP_AUTOINDEX_XML: |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
271 ngx_str_set(&r->headers_out.content_type, "text/xml"); |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
272 ngx_str_set(&r->headers_out.charset, "utf-8"); |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
273 break; |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
274 |
5944
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
275 default: /* NGX_HTTP_AUTOINDEX_HTML */ |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
276 ngx_str_set(&r->headers_out.content_type, "text/html"); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
277 break; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
278 } |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
279 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
280 r->headers_out.content_type_len = r->headers_out.content_type.len; |
5497
2cfc095a607a
Fixed setting of content type in some cases.
Ruslan Ermilov <ru@nginx.com>
parents:
5333
diff
changeset
|
281 r->headers_out.content_type_lowcase = NULL; |
523 | 282 |
283 rc = ngx_http_send_header(r); | |
284 | |
285 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) { | |
1499
090e391f53db
fix file leak for HEAD requests
Igor Sysoev <igor@sysoev.ru>
parents:
842
diff
changeset
|
286 if (ngx_close_dir(&dir) == NGX_ERROR) { |
090e391f53db
fix file leak for HEAD requests
Igor Sysoev <igor@sysoev.ru>
parents:
842
diff
changeset
|
287 ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno, |
090e391f53db
fix file leak for HEAD requests
Igor Sysoev <igor@sysoev.ru>
parents:
842
diff
changeset
|
288 ngx_close_dir_n " \"%V\" failed", &path); |
090e391f53db
fix file leak for HEAD requests
Igor Sysoev <igor@sysoev.ru>
parents:
842
diff
changeset
|
289 } |
090e391f53db
fix file leak for HEAD requests
Igor Sysoev <igor@sysoev.ru>
parents:
842
diff
changeset
|
290 |
523 | 291 return rc; |
292 } | |
293 | |
557 | 294 filename = path.data; |
295 filename[path.len] = '/'; | |
457 | 296 |
297 for ( ;; ) { | |
298 ngx_set_errno(0); | |
299 | |
300 if (ngx_read_dir(&dir) == NGX_ERROR) { | |
301 err = ngx_errno; | |
302 | |
501 | 303 if (err != NGX_ENOMOREFILES) { |
457 | 304 ngx_log_error(NGX_LOG_CRIT, r->connection->log, err, |
557 | 305 ngx_read_dir_n " \"%V\" failed", &path); |
306 return ngx_http_autoindex_error(r, &dir, &path); | |
457 | 307 } |
308 | |
577 | 309 break; |
457 | 310 } |
311 | |
312 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
313 "http autoindex file: \"%s\"", ngx_de_name(&dir)); | |
314 | |
315 len = ngx_de_namelen(&dir); | |
316 | |
547 | 317 if (ngx_de_name(&dir)[0] == '.') { |
459 | 318 continue; |
319 } | |
320 | |
457 | 321 if (!dir.valid_info) { |
322 | |
557 | 323 /* 1 byte for '/' and 1 byte for terminating '\0' */ |
457 | 324 |
557 | 325 if (path.len + 1 + len + 1 > allocated) { |
326 allocated = path.len + 1 + len + 1 | |
327 + NGX_HTTP_AUTOINDEX_PREALLOCATE; | |
328 | |
2049 | 329 filename = ngx_pnalloc(pool, allocated); |
557 | 330 if (filename == NULL) { |
331 return ngx_http_autoindex_error(r, &dir, &path); | |
457 | 332 } |
333 | |
557 | 334 last = ngx_cpystrn(filename, path.data, path.len + 1); |
457 | 335 *last++ = '/'; |
336 } | |
337 | |
338 ngx_cpystrn(last, ngx_de_name(&dir), len + 1); | |
339 | |
557 | 340 if (ngx_de_info(filename, &dir) == NGX_FILE_ERROR) { |
497 | 341 err = ngx_errno; |
342 | |
5333
e8bca8397625
Autoindex: improved ngx_de_info() error handling.
Sergey Kandaurov <pluknet@nginx.com>
parents:
5332
diff
changeset
|
343 if (err != NGX_ENOENT && err != NGX_ELOOP) { |
497 | 344 ngx_log_error(NGX_LOG_CRIT, r->connection->log, err, |
557 | 345 ngx_de_info_n " \"%s\" failed", filename); |
2371
b438ffe54e34
skip protected symlinks in autoindex
Igor Sysoev <igor@sysoev.ru>
parents:
2125
diff
changeset
|
346 |
b438ffe54e34
skip protected symlinks in autoindex
Igor Sysoev <igor@sysoev.ru>
parents:
2125
diff
changeset
|
347 if (err == NGX_EACCES) { |
b438ffe54e34
skip protected symlinks in autoindex
Igor Sysoev <igor@sysoev.ru>
parents:
2125
diff
changeset
|
348 continue; |
b438ffe54e34
skip protected symlinks in autoindex
Igor Sysoev <igor@sysoev.ru>
parents:
2125
diff
changeset
|
349 } |
b438ffe54e34
skip protected symlinks in autoindex
Igor Sysoev <igor@sysoev.ru>
parents:
2125
diff
changeset
|
350 |
557 | 351 return ngx_http_autoindex_error(r, &dir, &path); |
497 | 352 } |
353 | |
557 | 354 if (ngx_de_link_info(filename, &dir) == NGX_FILE_ERROR) { |
497 | 355 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno, |
356 ngx_de_link_info_n " \"%s\" failed", | |
557 | 357 filename); |
358 return ngx_http_autoindex_error(r, &dir, &path); | |
497 | 359 } |
457 | 360 } |
361 } | |
362 | |
501 | 363 entry = ngx_array_push(&entries); |
364 if (entry == NULL) { | |
557 | 365 return ngx_http_autoindex_error(r, &dir, &path); |
457 | 366 } |
367 | |
577 | 368 entry->name.len = len; |
525 | 369 |
2049 | 370 entry->name.data = ngx_pnalloc(pool, len + 1); |
501 | 371 if (entry->name.data == NULL) { |
557 | 372 return ngx_http_autoindex_error(r, &dir, &path); |
457 | 373 } |
461 | 374 |
457 | 375 ngx_cpystrn(entry->name.data, ngx_de_name(&dir), len + 1); |
376 | |
377 entry->dir = ngx_de_is_dir(&dir); | |
5944
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
378 entry->file = ngx_de_is_file(&dir); |
457 | 379 entry->mtime = ngx_de_mtime(&dir); |
380 entry->size = ngx_de_size(&dir); | |
381 } | |
382 | |
383 if (ngx_close_dir(&dir) == NGX_ERROR) { | |
384 ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno, | |
5218 | 385 ngx_close_dir_n " \"%V\" failed", &path); |
457 | 386 } |
387 | |
5943
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
388 if (entries.nelts > 1) { |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
389 ngx_qsort(entries.elts, (size_t) entries.nelts, |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
390 sizeof(ngx_http_autoindex_entry_t), |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
391 ngx_http_autoindex_cmp_entries); |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
392 } |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
393 |
5944
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
394 switch (format) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
395 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
396 case NGX_HTTP_AUTOINDEX_JSON: |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
397 b = ngx_http_autoindex_json(r, &entries, NULL); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
398 break; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
399 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
400 case NGX_HTTP_AUTOINDEX_JSONP: |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
401 b = ngx_http_autoindex_json(r, &entries, &callback); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
402 break; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
403 |
5945
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
404 case NGX_HTTP_AUTOINDEX_XML: |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
405 b = ngx_http_autoindex_xml(r, &entries); |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
406 break; |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
407 |
5944
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
408 default: /* NGX_HTTP_AUTOINDEX_HTML */ |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
409 b = ngx_http_autoindex_html(r, &entries); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
410 break; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
411 } |
5943
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
412 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
413 if (b == NULL) { |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
414 return NGX_ERROR; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
415 } |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
416 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
417 /* TODO: free temporary pool */ |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
418 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
419 if (r == r->main) { |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
420 b->last_buf = 1; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
421 } |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
422 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
423 b->last_in_chain = 1; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
424 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
425 out.buf = b; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
426 out.next = NULL; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
427 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
428 return ngx_http_output_filter(r, &out); |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
429 } |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
430 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
431 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
432 static ngx_buf_t * |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
433 ngx_http_autoindex_html(ngx_http_request_t *r, ngx_array_t *entries) |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
434 { |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
435 u_char *last, scale; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
436 off_t length; |
7435
a91b93f3f3e7
Autoindex: fixed possible integer overflow on 32-bit systems.
Vladimir Homutov <vl@nginx.com>
parents:
7433
diff
changeset
|
437 size_t len, entry_len, char_len, escape_html; |
5943
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
438 ngx_tm_t tm; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
439 ngx_buf_t *b; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
440 ngx_int_t size; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
441 ngx_uint_t i, utf8; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
442 ngx_time_t *tp; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
443 ngx_http_autoindex_entry_t *entry; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
444 ngx_http_autoindex_loc_conf_t *alcf; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
445 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
446 static u_char title[] = |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
447 "<html>" CRLF |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
448 "<head><title>Index of " |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
449 ; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
450 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
451 static u_char header[] = |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
452 "</title></head>" CRLF |
7358
170922952477
Removed bgcolor attribute on body in error pages and autoindex.
Nova DasSarma <nova@novalinium.com>
parents:
7164
diff
changeset
|
453 "<body>" CRLF |
5943
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
454 "<h1>Index of " |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
455 ; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
456 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
457 static u_char tail[] = |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
458 "</body>" CRLF |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
459 "</html>" CRLF |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
460 ; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
461 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
462 static char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
463 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
464 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
465 if (r->headers_out.charset.len == 5 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
466 && ngx_strncasecmp(r->headers_out.charset.data, (u_char *) "utf-8", 5) |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
467 == 0) |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
468 { |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
469 utf8 = 1; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
470 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
471 } else { |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
472 utf8 = 0; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
473 } |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
474 |
4193
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
475 escape_html = ngx_escape_html(NULL, r->uri.data, r->uri.len); |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
476 |
457 | 477 len = sizeof(title) - 1 |
4193
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
478 + r->uri.len + escape_html |
457 | 479 + sizeof(header) - 1 |
4193
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
480 + r->uri.len + escape_html |
457 | 481 + sizeof("</h1>") - 1 |
482 + sizeof("<hr><pre><a href=\"../\">../</a>" CRLF) - 1 | |
483 + sizeof("</pre><hr>") - 1 | |
484 + sizeof(tail) - 1; | |
485 | |
5943
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
486 entry = entries->elts; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
487 for (i = 0; i < entries->nelts; i++) { |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
488 entry[i].escape = 2 * ngx_escape_uri(NULL, entry[i].name.data, |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
489 entry[i].name.len, |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
490 NGX_ESCAPE_URI_COMPONENT); |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
491 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
492 entry[i].escape_html = ngx_escape_html(NULL, entry[i].name.data, |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
493 entry[i].name.len); |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
494 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
495 if (utf8) { |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
496 entry[i].utf_len = ngx_utf8_length(entry[i].name.data, |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
497 entry[i].name.len); |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
498 } else { |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
499 entry[i].utf_len = entry[i].name.len; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
500 } |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
501 |
7435
a91b93f3f3e7
Autoindex: fixed possible integer overflow on 32-bit systems.
Vladimir Homutov <vl@nginx.com>
parents:
7433
diff
changeset
|
502 entry_len = sizeof("<a href=\"") - 1 |
a91b93f3f3e7
Autoindex: fixed possible integer overflow on 32-bit systems.
Vladimir Homutov <vl@nginx.com>
parents:
7433
diff
changeset
|
503 + entry[i].name.len + entry[i].escape |
a91b93f3f3e7
Autoindex: fixed possible integer overflow on 32-bit systems.
Vladimir Homutov <vl@nginx.com>
parents:
7433
diff
changeset
|
504 + 1 /* 1 is for "/" */ |
a91b93f3f3e7
Autoindex: fixed possible integer overflow on 32-bit systems.
Vladimir Homutov <vl@nginx.com>
parents:
7433
diff
changeset
|
505 + sizeof("\">") - 1 |
a91b93f3f3e7
Autoindex: fixed possible integer overflow on 32-bit systems.
Vladimir Homutov <vl@nginx.com>
parents:
7433
diff
changeset
|
506 + entry[i].name.len - entry[i].utf_len |
a91b93f3f3e7
Autoindex: fixed possible integer overflow on 32-bit systems.
Vladimir Homutov <vl@nginx.com>
parents:
7433
diff
changeset
|
507 + entry[i].escape_html |
a91b93f3f3e7
Autoindex: fixed possible integer overflow on 32-bit systems.
Vladimir Homutov <vl@nginx.com>
parents:
7433
diff
changeset
|
508 + NGX_HTTP_AUTOINDEX_NAME_LEN + sizeof(">") - 2 |
a91b93f3f3e7
Autoindex: fixed possible integer overflow on 32-bit systems.
Vladimir Homutov <vl@nginx.com>
parents:
7433
diff
changeset
|
509 + sizeof("</a>") - 1 |
a91b93f3f3e7
Autoindex: fixed possible integer overflow on 32-bit systems.
Vladimir Homutov <vl@nginx.com>
parents:
7433
diff
changeset
|
510 + sizeof(" 28-Sep-1970 12:00 ") - 1 |
a91b93f3f3e7
Autoindex: fixed possible integer overflow on 32-bit systems.
Vladimir Homutov <vl@nginx.com>
parents:
7433
diff
changeset
|
511 + 20 /* the file size */ |
a91b93f3f3e7
Autoindex: fixed possible integer overflow on 32-bit systems.
Vladimir Homutov <vl@nginx.com>
parents:
7433
diff
changeset
|
512 + 2; |
a91b93f3f3e7
Autoindex: fixed possible integer overflow on 32-bit systems.
Vladimir Homutov <vl@nginx.com>
parents:
7433
diff
changeset
|
513 |
a91b93f3f3e7
Autoindex: fixed possible integer overflow on 32-bit systems.
Vladimir Homutov <vl@nginx.com>
parents:
7433
diff
changeset
|
514 if (len > NGX_MAX_SIZE_T_VALUE - entry_len) { |
a91b93f3f3e7
Autoindex: fixed possible integer overflow on 32-bit systems.
Vladimir Homutov <vl@nginx.com>
parents:
7433
diff
changeset
|
515 return NULL; |
a91b93f3f3e7
Autoindex: fixed possible integer overflow on 32-bit systems.
Vladimir Homutov <vl@nginx.com>
parents:
7433
diff
changeset
|
516 } |
a91b93f3f3e7
Autoindex: fixed possible integer overflow on 32-bit systems.
Vladimir Homutov <vl@nginx.com>
parents:
7433
diff
changeset
|
517 |
a91b93f3f3e7
Autoindex: fixed possible integer overflow on 32-bit systems.
Vladimir Homutov <vl@nginx.com>
parents:
7433
diff
changeset
|
518 len += entry_len; |
457 | 519 } |
520 | |
501 | 521 b = ngx_create_temp_buf(r->pool, len); |
522 if (b == NULL) { | |
5943
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
523 return NULL; |
457 | 524 } |
525 | |
526 b->last = ngx_cpymem(b->last, title, sizeof(title) - 1); | |
4193
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
527 |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
528 if (escape_html) { |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
529 b->last = (u_char *) ngx_escape_html(b->last, r->uri.data, r->uri.len); |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
530 b->last = ngx_cpymem(b->last, header, sizeof(header) - 1); |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
531 b->last = (u_char *) ngx_escape_html(b->last, r->uri.data, r->uri.len); |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
532 |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
533 } else { |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
534 b->last = ngx_cpymem(b->last, r->uri.data, r->uri.len); |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
535 b->last = ngx_cpymem(b->last, header, sizeof(header) - 1); |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
536 b->last = ngx_cpymem(b->last, r->uri.data, r->uri.len); |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
537 } |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
538 |
457 | 539 b->last = ngx_cpymem(b->last, "</h1>", sizeof("</h1>") - 1); |
540 | |
541 b->last = ngx_cpymem(b->last, "<hr><pre><a href=\"../\">../</a>" CRLF, | |
542 sizeof("<hr><pre><a href=\"../\">../</a>" CRLF) - 1); | |
543 | |
5943
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
544 alcf = ngx_http_get_module_loc_conf(r, ngx_http_autoindex_module); |
563 | 545 tp = ngx_timeofday(); |
546 | |
5943
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
547 for (i = 0; i < entries->nelts; i++) { |
457 | 548 b->last = ngx_cpymem(b->last, "<a href=\"", sizeof("<a href=\"") - 1); |
461 | 549 |
550 if (entry[i].escape) { | |
551 ngx_escape_uri(b->last, entry[i].name.data, entry[i].name.len, | |
4192
61e4af19df9f
Autoindex: escape '?' in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
3527
diff
changeset
|
552 NGX_ESCAPE_URI_COMPONENT); |
461 | 553 |
554 b->last += entry[i].name.len + entry[i].escape; | |
555 | |
556 } else { | |
557 b->last = ngx_cpymem(b->last, entry[i].name.data, | |
558 entry[i].name.len); | |
559 } | |
457 | 560 |
561 if (entry[i].dir) { | |
562 *b->last++ = '/'; | |
563 } | |
564 | |
565 *b->last++ = '"'; | |
566 *b->last++ = '>'; | |
567 | |
525 | 568 len = entry[i].utf_len; |
457 | 569 |
2120 | 570 if (entry[i].name.len != len) { |
527 | 571 if (len > NGX_HTTP_AUTOINDEX_NAME_LEN) { |
4193
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
572 char_len = NGX_HTTP_AUTOINDEX_NAME_LEN - 3 + 1; |
527 | 573 |
574 } else { | |
4193
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
575 char_len = NGX_HTTP_AUTOINDEX_NAME_LEN + 1; |
527 | 576 } |
577 | |
4193
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
578 last = b->last; |
2125
8e4b9d2acde8
rename ngx_utf_...() to ngx_utf8_...()
Igor Sysoev <igor@sysoev.ru>
parents:
2120
diff
changeset
|
579 b->last = ngx_utf8_cpystrn(b->last, entry[i].name.data, |
4193
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
580 char_len, entry[i].name.len + 1); |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
581 |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
582 if (entry[i].escape_html) { |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
583 b->last = (u_char *) ngx_escape_html(last, entry[i].name.data, |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
584 b->last - last); |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
585 } |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
586 |
527 | 587 last = b->last; |
588 | |
589 } else { | |
4193
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
590 if (entry[i].escape_html) { |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
591 if (len > NGX_HTTP_AUTOINDEX_NAME_LEN) { |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
592 char_len = NGX_HTTP_AUTOINDEX_NAME_LEN - 3; |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
593 |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
594 } else { |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
595 char_len = len; |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
596 } |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
597 |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
598 b->last = (u_char *) ngx_escape_html(b->last, |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
599 entry[i].name.data, char_len); |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
600 last = b->last; |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
601 |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
602 } else { |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
603 b->last = ngx_cpystrn(b->last, entry[i].name.data, |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
604 NGX_HTTP_AUTOINDEX_NAME_LEN + 1); |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
605 last = b->last - 3; |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
606 } |
527 | 607 } |
608 | |
457 | 609 if (len > NGX_HTTP_AUTOINDEX_NAME_LEN) { |
527 | 610 b->last = ngx_cpymem(last, "..></a>", sizeof("..></a>") - 1); |
457 | 611 |
612 } else { | |
613 if (entry[i].dir && NGX_HTTP_AUTOINDEX_NAME_LEN - len > 0) { | |
614 *b->last++ = '/'; | |
615 len++; | |
616 } | |
617 | |
618 b->last = ngx_cpymem(b->last, "</a>", sizeof("</a>") - 1); | |
5069
e9d188e295cf
Fixed false memset warning on Linux with -O3 (ticket #275).
Maxim Dounin <mdounin@mdounin.ru>
parents:
4499
diff
changeset
|
619 |
e9d188e295cf
Fixed false memset warning on Linux with -O3 (ticket #275).
Maxim Dounin <mdounin@mdounin.ru>
parents:
4499
diff
changeset
|
620 if (NGX_HTTP_AUTOINDEX_NAME_LEN - len > 0) { |
e9d188e295cf
Fixed false memset warning on Linux with -O3 (ticket #275).
Maxim Dounin <mdounin@mdounin.ru>
parents:
4499
diff
changeset
|
621 ngx_memset(b->last, ' ', NGX_HTTP_AUTOINDEX_NAME_LEN - len); |
e9d188e295cf
Fixed false memset warning on Linux with -O3 (ticket #275).
Maxim Dounin <mdounin@mdounin.ru>
parents:
4499
diff
changeset
|
622 b->last += NGX_HTTP_AUTOINDEX_NAME_LEN - len; |
e9d188e295cf
Fixed false memset warning on Linux with -O3 (ticket #275).
Maxim Dounin <mdounin@mdounin.ru>
parents:
4499
diff
changeset
|
623 } |
457 | 624 } |
625 | |
626 *b->last++ = ' '; | |
627 | |
563 | 628 ngx_gmtime(entry[i].mtime + tp->gmtoff * 60 * alcf->localtime, &tm); |
457 | 629 |
630 b->last = ngx_sprintf(b->last, "%02d-%s-%d %02d:%02d ", | |
631 tm.ngx_tm_mday, | |
632 months[tm.ngx_tm_mon - 1], | |
633 tm.ngx_tm_year, | |
634 tm.ngx_tm_hour, | |
635 tm.ngx_tm_min); | |
636 | |
527 | 637 if (alcf->exact_size) { |
638 if (entry[i].dir) { | |
639 b->last = ngx_cpymem(b->last, " -", | |
640 sizeof(" -") - 1); | |
641 } else { | |
642 b->last = ngx_sprintf(b->last, "%19O", entry[i].size); | |
577 | 643 } |
457 | 644 |
645 } else { | |
527 | 646 if (entry[i].dir) { |
547 | 647 b->last = ngx_cpymem(b->last, " -", |
648 sizeof(" -") - 1); | |
527 | 649 |
650 } else { | |
651 length = entry[i].size; | |
652 | |
653 if (length > 1024 * 1024 * 1024 - 1) { | |
654 size = (ngx_int_t) (length / (1024 * 1024 * 1024)); | |
655 if ((length % (1024 * 1024 * 1024)) | |
656 > (1024 * 1024 * 1024 / 2 - 1)) | |
577 | 657 { |
527 | 658 size++; |
659 } | |
660 scale = 'G'; | |
661 | |
662 } else if (length > 1024 * 1024 - 1) { | |
663 size = (ngx_int_t) (length / (1024 * 1024)); | |
664 if ((length % (1024 * 1024)) > (1024 * 1024 / 2 - 1)) { | |
665 size++; | |
666 } | |
667 scale = 'M'; | |
668 | |
669 } else if (length > 9999) { | |
670 size = (ngx_int_t) (length / 1024); | |
671 if (length % 1024 > 511) { | |
672 size++; | |
673 } | |
674 scale = 'K'; | |
675 | |
676 } else { | |
677 size = (ngx_int_t) length; | |
547 | 678 scale = '\0'; |
527 | 679 } |
680 | |
547 | 681 if (scale) { |
682 b->last = ngx_sprintf(b->last, "%6i%c", size, scale); | |
527 | 683 |
547 | 684 } else { |
685 b->last = ngx_sprintf(b->last, " %6i", size); | |
527 | 686 } |
687 } | |
457 | 688 } |
689 | |
690 *b->last++ = CR; | |
691 *b->last++ = LF; | |
692 } | |
693 | |
694 b->last = ngx_cpymem(b->last, "</pre><hr>", sizeof("</pre><hr>") - 1); | |
695 | |
696 b->last = ngx_cpymem(b->last, tail, sizeof(tail) - 1); | |
697 | |
5943
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
698 return b; |
457 | 699 } |
700 | |
701 | |
5944
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
702 static ngx_buf_t * |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
703 ngx_http_autoindex_json(ngx_http_request_t *r, ngx_array_t *entries, |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
704 ngx_str_t *callback) |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
705 { |
7435
a91b93f3f3e7
Autoindex: fixed possible integer overflow on 32-bit systems.
Vladimir Homutov <vl@nginx.com>
parents:
7433
diff
changeset
|
706 size_t len, entry_len; |
5944
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
707 ngx_buf_t *b; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
708 ngx_uint_t i; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
709 ngx_http_autoindex_entry_t *entry; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
710 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
711 len = sizeof("[" CRLF CRLF "]") - 1; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
712 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
713 if (callback) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
714 len += sizeof("/* callback */" CRLF "();") - 1 + callback->len; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
715 } |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
716 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
717 entry = entries->elts; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
718 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
719 for (i = 0; i < entries->nelts; i++) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
720 entry[i].escape = ngx_escape_json(NULL, entry[i].name.data, |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
721 entry[i].name.len); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
722 |
7435
a91b93f3f3e7
Autoindex: fixed possible integer overflow on 32-bit systems.
Vladimir Homutov <vl@nginx.com>
parents:
7433
diff
changeset
|
723 entry_len = sizeof("{ }," CRLF) - 1 |
a91b93f3f3e7
Autoindex: fixed possible integer overflow on 32-bit systems.
Vladimir Homutov <vl@nginx.com>
parents:
7433
diff
changeset
|
724 + sizeof("\"name\":\"\"") - 1 |
a91b93f3f3e7
Autoindex: fixed possible integer overflow on 32-bit systems.
Vladimir Homutov <vl@nginx.com>
parents:
7433
diff
changeset
|
725 + entry[i].name.len + entry[i].escape |
a91b93f3f3e7
Autoindex: fixed possible integer overflow on 32-bit systems.
Vladimir Homutov <vl@nginx.com>
parents:
7433
diff
changeset
|
726 + sizeof(", \"type\":\"directory\"") - 1 |
a91b93f3f3e7
Autoindex: fixed possible integer overflow on 32-bit systems.
Vladimir Homutov <vl@nginx.com>
parents:
7433
diff
changeset
|
727 + sizeof(", \"mtime\":\"Wed, 31 Dec 1986 10:00:00 GMT\"") - 1; |
5944
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
728 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
729 if (entry[i].file) { |
7435
a91b93f3f3e7
Autoindex: fixed possible integer overflow on 32-bit systems.
Vladimir Homutov <vl@nginx.com>
parents:
7433
diff
changeset
|
730 entry_len += sizeof(", \"size\":") - 1 + NGX_OFF_T_LEN; |
5944
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
731 } |
7435
a91b93f3f3e7
Autoindex: fixed possible integer overflow on 32-bit systems.
Vladimir Homutov <vl@nginx.com>
parents:
7433
diff
changeset
|
732 |
a91b93f3f3e7
Autoindex: fixed possible integer overflow on 32-bit systems.
Vladimir Homutov <vl@nginx.com>
parents:
7433
diff
changeset
|
733 if (len > NGX_MAX_SIZE_T_VALUE - entry_len) { |
a91b93f3f3e7
Autoindex: fixed possible integer overflow on 32-bit systems.
Vladimir Homutov <vl@nginx.com>
parents:
7433
diff
changeset
|
734 return NULL; |
a91b93f3f3e7
Autoindex: fixed possible integer overflow on 32-bit systems.
Vladimir Homutov <vl@nginx.com>
parents:
7433
diff
changeset
|
735 } |
a91b93f3f3e7
Autoindex: fixed possible integer overflow on 32-bit systems.
Vladimir Homutov <vl@nginx.com>
parents:
7433
diff
changeset
|
736 |
a91b93f3f3e7
Autoindex: fixed possible integer overflow on 32-bit systems.
Vladimir Homutov <vl@nginx.com>
parents:
7433
diff
changeset
|
737 len += entry_len; |
5944
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
738 } |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
739 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
740 b = ngx_create_temp_buf(r->pool, len); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
741 if (b == NULL) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
742 return NULL; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
743 } |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
744 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
745 if (callback) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
746 b->last = ngx_cpymem(b->last, "/* callback */" CRLF, |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
747 sizeof("/* callback */" CRLF) - 1); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
748 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
749 b->last = ngx_cpymem(b->last, callback->data, callback->len); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
750 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
751 *b->last++ = '('; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
752 } |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
753 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
754 *b->last++ = '['; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
755 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
756 for (i = 0; i < entries->nelts; i++) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
757 b->last = ngx_cpymem(b->last, CRLF "{ \"name\":\"", |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
758 sizeof(CRLF "{ \"name\":\"") - 1); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
759 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
760 if (entry[i].escape) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
761 b->last = (u_char *) ngx_escape_json(b->last, entry[i].name.data, |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
762 entry[i].name.len); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
763 } else { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
764 b->last = ngx_cpymem(b->last, entry[i].name.data, |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
765 entry[i].name.len); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
766 } |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
767 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
768 b->last = ngx_cpymem(b->last, "\", \"type\":\"", |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
769 sizeof("\", \"type\":\"") - 1); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
770 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
771 if (entry[i].dir) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
772 b->last = ngx_cpymem(b->last, "directory", sizeof("directory") - 1); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
773 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
774 } else if (entry[i].file) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
775 b->last = ngx_cpymem(b->last, "file", sizeof("file") - 1); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
776 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
777 } else { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
778 b->last = ngx_cpymem(b->last, "other", sizeof("other") - 1); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
779 } |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
780 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
781 b->last = ngx_cpymem(b->last, "\", \"mtime\":\"", |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
782 sizeof("\", \"mtime\":\"") - 1); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
783 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
784 b->last = ngx_http_time(b->last, entry[i].mtime); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
785 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
786 if (entry[i].file) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
787 b->last = ngx_cpymem(b->last, "\", \"size\":", |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
788 sizeof("\", \"size\":") - 1); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
789 b->last = ngx_sprintf(b->last, "%O", entry[i].size); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
790 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
791 } else { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
792 *b->last++ = '"'; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
793 } |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
794 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
795 b->last = ngx_cpymem(b->last, " },", sizeof(" },") - 1); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
796 } |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
797 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
798 if (i > 0) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
799 b->last--; /* strip last comma */ |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
800 } |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
801 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
802 b->last = ngx_cpymem(b->last, CRLF "]", sizeof(CRLF "]") - 1); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
803 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
804 if (callback) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
805 *b->last++ = ')'; *b->last++ = ';'; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
806 } |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
807 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
808 return b; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
809 } |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
810 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
811 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
812 static ngx_int_t |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
813 ngx_http_autoindex_jsonp_callback(ngx_http_request_t *r, ngx_str_t *callback) |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
814 { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
815 u_char *p, c, ch; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
816 ngx_uint_t i; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
817 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
818 if (ngx_http_arg(r, (u_char *) "callback", 8, callback) != NGX_OK) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
819 callback->len = 0; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
820 return NGX_OK; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
821 } |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
822 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
823 if (callback->len > 128) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
824 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0, |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
825 "client sent too long callback name: \"%V\"", callback); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
826 return NGX_DECLINED; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
827 } |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
828 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
829 p = callback->data; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
830 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
831 for (i = 0; i < callback->len; i++) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
832 ch = p[i]; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
833 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
834 c = (u_char) (ch | 0x20); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
835 if (c >= 'a' && c <= 'z') { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
836 continue; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
837 } |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
838 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
839 if ((ch >= '0' && ch <= '9') || ch == '_' || ch == '.') { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
840 continue; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
841 } |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
842 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
843 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0, |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
844 "client sent invalid callback name: \"%V\"", callback); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
845 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
846 return NGX_DECLINED; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
847 } |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
848 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
849 return NGX_OK; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
850 } |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
851 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
852 |
5945
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
853 static ngx_buf_t * |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
854 ngx_http_autoindex_xml(ngx_http_request_t *r, ngx_array_t *entries) |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
855 { |
7435
a91b93f3f3e7
Autoindex: fixed possible integer overflow on 32-bit systems.
Vladimir Homutov <vl@nginx.com>
parents:
7433
diff
changeset
|
856 size_t len, entry_len; |
5945
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
857 ngx_tm_t tm; |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
858 ngx_buf_t *b; |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
859 ngx_str_t type; |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
860 ngx_uint_t i; |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
861 ngx_http_autoindex_entry_t *entry; |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
862 |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
863 static u_char head[] = "<?xml version=\"1.0\"?>" CRLF "<list>" CRLF; |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
864 static u_char tail[] = "</list>" CRLF; |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
865 |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
866 len = sizeof(head) - 1 + sizeof(tail) - 1; |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
867 |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
868 entry = entries->elts; |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
869 |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
870 for (i = 0; i < entries->nelts; i++) { |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
871 entry[i].escape = ngx_escape_html(NULL, entry[i].name.data, |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
872 entry[i].name.len); |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
873 |
7435
a91b93f3f3e7
Autoindex: fixed possible integer overflow on 32-bit systems.
Vladimir Homutov <vl@nginx.com>
parents:
7433
diff
changeset
|
874 entry_len = sizeof("<directory></directory>" CRLF) - 1 |
a91b93f3f3e7
Autoindex: fixed possible integer overflow on 32-bit systems.
Vladimir Homutov <vl@nginx.com>
parents:
7433
diff
changeset
|
875 + entry[i].name.len + entry[i].escape |
a91b93f3f3e7
Autoindex: fixed possible integer overflow on 32-bit systems.
Vladimir Homutov <vl@nginx.com>
parents:
7433
diff
changeset
|
876 + sizeof(" mtime=\"1986-12-31T10:00:00Z\"") - 1; |
5945
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
877 |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
878 if (entry[i].file) { |
7435
a91b93f3f3e7
Autoindex: fixed possible integer overflow on 32-bit systems.
Vladimir Homutov <vl@nginx.com>
parents:
7433
diff
changeset
|
879 entry_len += sizeof(" size=\"\"") - 1 + NGX_OFF_T_LEN; |
5945
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
880 } |
7435
a91b93f3f3e7
Autoindex: fixed possible integer overflow on 32-bit systems.
Vladimir Homutov <vl@nginx.com>
parents:
7433
diff
changeset
|
881 |
a91b93f3f3e7
Autoindex: fixed possible integer overflow on 32-bit systems.
Vladimir Homutov <vl@nginx.com>
parents:
7433
diff
changeset
|
882 if (len > NGX_MAX_SIZE_T_VALUE - entry_len) { |
a91b93f3f3e7
Autoindex: fixed possible integer overflow on 32-bit systems.
Vladimir Homutov <vl@nginx.com>
parents:
7433
diff
changeset
|
883 return NULL; |
a91b93f3f3e7
Autoindex: fixed possible integer overflow on 32-bit systems.
Vladimir Homutov <vl@nginx.com>
parents:
7433
diff
changeset
|
884 } |
a91b93f3f3e7
Autoindex: fixed possible integer overflow on 32-bit systems.
Vladimir Homutov <vl@nginx.com>
parents:
7433
diff
changeset
|
885 |
a91b93f3f3e7
Autoindex: fixed possible integer overflow on 32-bit systems.
Vladimir Homutov <vl@nginx.com>
parents:
7433
diff
changeset
|
886 len += entry_len; |
5945
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
887 } |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
888 |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
889 b = ngx_create_temp_buf(r->pool, len); |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
890 if (b == NULL) { |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
891 return NULL; |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
892 } |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
893 |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
894 b->last = ngx_cpymem(b->last, head, sizeof(head) - 1); |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
895 |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
896 for (i = 0; i < entries->nelts; i++) { |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
897 *b->last++ = '<'; |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
898 |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
899 if (entry[i].dir) { |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
900 ngx_str_set(&type, "directory"); |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
901 |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
902 } else if (entry[i].file) { |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
903 ngx_str_set(&type, "file"); |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
904 |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
905 } else { |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
906 ngx_str_set(&type, "other"); |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
907 } |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
908 |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
909 b->last = ngx_cpymem(b->last, type.data, type.len); |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
910 |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
911 b->last = ngx_cpymem(b->last, " mtime=\"", sizeof(" mtime=\"") - 1); |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
912 |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
913 ngx_gmtime(entry[i].mtime, &tm); |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
914 |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
915 b->last = ngx_sprintf(b->last, "%4d-%02d-%02dT%02d:%02d:%02dZ", |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
916 tm.ngx_tm_year, tm.ngx_tm_mon, |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
917 tm.ngx_tm_mday, tm.ngx_tm_hour, |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
918 tm.ngx_tm_min, tm.ngx_tm_sec); |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
919 |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
920 if (entry[i].file) { |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
921 b->last = ngx_cpymem(b->last, "\" size=\"", |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
922 sizeof("\" size=\"") - 1); |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
923 b->last = ngx_sprintf(b->last, "%O", entry[i].size); |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
924 } |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
925 |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
926 *b->last++ = '"'; *b->last++ = '>'; |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
927 |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
928 if (entry[i].escape) { |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
929 b->last = (u_char *) ngx_escape_html(b->last, entry[i].name.data, |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
930 entry[i].name.len); |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
931 } else { |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
932 b->last = ngx_cpymem(b->last, entry[i].name.data, |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
933 entry[i].name.len); |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
934 } |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
935 |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
936 *b->last++ = '<'; *b->last++ = '/'; |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
937 |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
938 b->last = ngx_cpymem(b->last, type.data, type.len); |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
939 |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
940 *b->last++ = '>'; |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
941 |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
942 *b->last++ = CR; *b->last++ = LF; |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
943 } |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
944 |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
945 b->last = ngx_cpymem(b->last, tail, sizeof(tail) - 1); |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
946 |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
947 return b; |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
948 } |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
949 |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
950 |
503 | 951 static int ngx_libc_cdecl |
499 | 952 ngx_http_autoindex_cmp_entries(const void *one, const void *two) |
457 | 953 { |
954 ngx_http_autoindex_entry_t *first = (ngx_http_autoindex_entry_t *) one; | |
955 ngx_http_autoindex_entry_t *second = (ngx_http_autoindex_entry_t *) two; | |
956 | |
957 if (first->dir && !second->dir) { | |
958 /* move the directories to the start */ | |
959 return -1; | |
960 } | |
961 | |
962 if (!first->dir && second->dir) { | |
963 /* move the directories to the start */ | |
964 return 1; | |
965 } | |
966 | |
967 return (int) ngx_strcmp(first->name.data, second->name.data); | |
968 } | |
969 | |
970 | |
971 #if 0 | |
972 | |
499 | 973 static ngx_buf_t * |
974 ngx_http_autoindex_alloc(ngx_http_autoindex_ctx_t *ctx, size_t size) | |
457 | 975 { |
976 ngx_chain_t *cl; | |
977 | |
978 if (ctx->buf) { | |
979 | |
980 if ((size_t) (ctx->buf->end - ctx->buf->last) >= size) { | |
981 return ctx->buf; | |
982 } | |
983 | |
984 ctx->size += ctx->buf->last - ctx->buf->pos; | |
985 } | |
986 | |
501 | 987 ctx->buf = ngx_create_temp_buf(ctx->pool, ctx->alloc_size); |
988 if (ctx->buf == NULL) { | |
457 | 989 return NULL; |
990 } | |
991 | |
501 | 992 cl = ngx_alloc_chain_link(ctx->pool); |
993 if (cl == NULL) { | |
457 | 994 return NULL; |
995 } | |
996 | |
997 cl->buf = ctx->buf; | |
998 cl->next = NULL; | |
999 | |
1000 *ctx->last_out = cl; | |
1001 ctx->last_out = &cl->next; | |
1002 | |
1003 return ctx->buf; | |
1004 } | |
1005 | |
1006 #endif | |
1007 | |
1008 | |
499 | 1009 static ngx_int_t |
557 | 1010 ngx_http_autoindex_error(ngx_http_request_t *r, ngx_dir_t *dir, ngx_str_t *name) |
457 | 1011 { |
1012 if (ngx_close_dir(dir) == NGX_ERROR) { | |
1013 ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno, | |
557 | 1014 ngx_close_dir_n " \"%V\" failed", name); |
457 | 1015 } |
1016 | |
5332
1a9700ef9725
Autoindex: return NGX_ERROR on error if headers were sent.
Sergey Kandaurov <pluknet@nginx.com>
parents:
5218
diff
changeset
|
1017 return r->header_sent ? NGX_ERROR : NGX_HTTP_INTERNAL_SERVER_ERROR; |
457 | 1018 } |
1019 | |
1020 | |
499 | 1021 static void * |
1022 ngx_http_autoindex_create_loc_conf(ngx_conf_t *cf) | |
457 | 1023 { |
1024 ngx_http_autoindex_loc_conf_t *conf; | |
1025 | |
1026 conf = ngx_palloc(cf->pool, sizeof(ngx_http_autoindex_loc_conf_t)); | |
1027 if (conf == NULL) { | |
2912
c7d57b539248
return NULL instead of NGX_CONF_ERROR on a create conf failure
Igor Sysoev <igor@sysoev.ru>
parents:
2889
diff
changeset
|
1028 return NULL; |
457 | 1029 } |
1030 | |
1031 conf->enable = NGX_CONF_UNSET; | |
5944
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
1032 conf->format = NGX_CONF_UNSET_UINT; |
519 | 1033 conf->localtime = NGX_CONF_UNSET; |
527 | 1034 conf->exact_size = NGX_CONF_UNSET; |
457 | 1035 |
1036 return conf; | |
1037 } | |
1038 | |
1039 | |
499 | 1040 static char * |
1041 ngx_http_autoindex_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) | |
457 | 1042 { |
1043 ngx_http_autoindex_loc_conf_t *prev = parent; | |
1044 ngx_http_autoindex_loc_conf_t *conf = child; | |
1045 | |
1046 ngx_conf_merge_value(conf->enable, prev->enable, 0); | |
5944
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
1047 ngx_conf_merge_uint_value(conf->format, prev->format, |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
1048 NGX_HTTP_AUTOINDEX_HTML); |
519 | 1049 ngx_conf_merge_value(conf->localtime, prev->localtime, 0); |
527 | 1050 ngx_conf_merge_value(conf->exact_size, prev->exact_size, 1); |
457 | 1051 |
1052 return NGX_CONF_OK; | |
1053 } | |
681 | 1054 |
1055 | |
1056 static ngx_int_t | |
1057 ngx_http_autoindex_init(ngx_conf_t *cf) | |
1058 { | |
1059 ngx_http_handler_pt *h; | |
1060 ngx_http_core_main_conf_t *cmcf; | |
1061 | |
1062 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module); | |
1063 | |
1064 h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers); | |
1065 if (h == NULL) { | |
1066 return NGX_ERROR; | |
1067 } | |
1068 | |
1069 *h = ngx_http_autoindex_handler; | |
1070 | |
1071 return NGX_OK; | |
1072 } |