Mercurial > hg > nginx
annotate src/http/modules/ngx_http_autoindex_module.c @ 5944:33c08d7e2915
Autoindex: implemented JSON output format.
author | Valentin Bartenev <vbart@nginx.com> |
---|---|
date | Fri, 12 Dec 2014 20:25:35 +0300 |
parents | 631dee7bfd4e |
children | 99751fe3bc3b |
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 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
51 |
557 | 52 #define NGX_HTTP_AUTOINDEX_PREALLOCATE 50 |
53 | |
54 #define NGX_HTTP_AUTOINDEX_NAME_LEN 50 | |
457 | 55 |
56 | |
5943
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
57 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
|
58 ngx_array_t *entries); |
5944
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
59 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
|
60 ngx_array_t *entries, ngx_str_t *callback); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
61 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
|
62 ngx_str_t *callback); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
63 |
503 | 64 static int ngx_libc_cdecl ngx_http_autoindex_cmp_entries(const void *one, |
65 const void *two); | |
499 | 66 static ngx_int_t ngx_http_autoindex_error(ngx_http_request_t *r, |
557 | 67 ngx_dir_t *dir, ngx_str_t *name); |
5944
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
68 |
681 | 69 static ngx_int_t ngx_http_autoindex_init(ngx_conf_t *cf); |
457 | 70 static void *ngx_http_autoindex_create_loc_conf(ngx_conf_t *cf); |
71 static char *ngx_http_autoindex_merge_loc_conf(ngx_conf_t *cf, | |
499 | 72 void *parent, void *child); |
457 | 73 |
74 | |
5944
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
75 static ngx_conf_enum_t ngx_http_autoindex_format[] = { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
76 { ngx_string("html"), NGX_HTTP_AUTOINDEX_HTML }, |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
77 { ngx_string("json"), NGX_HTTP_AUTOINDEX_JSON }, |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
78 { ngx_string("jsonp"), NGX_HTTP_AUTOINDEX_JSONP }, |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
79 { ngx_null_string, 0 } |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
80 }; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
81 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
82 |
457 | 83 static ngx_command_t ngx_http_autoindex_commands[] = { |
84 | |
85 { ngx_string("autoindex"), | |
86 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
87 ngx_conf_set_flag_slot, | |
88 NGX_HTTP_LOC_CONF_OFFSET, | |
89 offsetof(ngx_http_autoindex_loc_conf_t, enable), | |
90 NULL }, | |
91 | |
5944
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
92 { ngx_string("autoindex_format"), |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
93 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
|
94 ngx_conf_set_enum_slot, |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
95 NGX_HTTP_LOC_CONF_OFFSET, |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
96 offsetof(ngx_http_autoindex_loc_conf_t, format), |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
97 &ngx_http_autoindex_format }, |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
98 |
519 | 99 { ngx_string("autoindex_localtime"), |
100 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
101 ngx_conf_set_flag_slot, | |
102 NGX_HTTP_LOC_CONF_OFFSET, | |
103 offsetof(ngx_http_autoindex_loc_conf_t, localtime), | |
104 NULL }, | |
105 | |
527 | 106 { ngx_string("autoindex_exact_size"), |
107 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
108 ngx_conf_set_flag_slot, | |
109 NGX_HTTP_LOC_CONF_OFFSET, | |
110 offsetof(ngx_http_autoindex_loc_conf_t, exact_size), | |
111 NULL }, | |
112 | |
457 | 113 ngx_null_command |
114 }; | |
115 | |
116 | |
667 | 117 static ngx_http_module_t ngx_http_autoindex_module_ctx = { |
509 | 118 NULL, /* preconfiguration */ |
681 | 119 ngx_http_autoindex_init, /* postconfiguration */ |
457 | 120 |
121 NULL, /* create main configuration */ | |
122 NULL, /* init main configuration */ | |
123 | |
124 NULL, /* create server configuration */ | |
125 NULL, /* merge server configuration */ | |
126 | |
4499
778ef9c3fd2d
Fixed spelling in single-line comments.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
127 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
|
128 ngx_http_autoindex_merge_loc_conf /* merge location configuration */ |
457 | 129 }; |
130 | |
131 | |
132 ngx_module_t ngx_http_autoindex_module = { | |
509 | 133 NGX_MODULE_V1, |
577 | 134 &ngx_http_autoindex_module_ctx, /* module context */ |
457 | 135 ngx_http_autoindex_commands, /* module directives */ |
136 NGX_HTTP_MODULE, /* module type */ | |
541 | 137 NULL, /* init master */ |
681 | 138 NULL, /* init module */ |
541 | 139 NULL, /* init process */ |
140 NULL, /* init thread */ | |
141 NULL, /* exit thread */ | |
142 NULL, /* exit process */ | |
143 NULL, /* exit master */ | |
144 NGX_MODULE_V1_PADDING | |
457 | 145 }; |
146 | |
147 | |
499 | 148 static ngx_int_t |
149 ngx_http_autoindex_handler(ngx_http_request_t *r) | |
457 | 150 { |
5943
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
151 u_char *last, *filename; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
152 size_t len, allocated, root; |
457 | 153 ngx_err_t err; |
154 ngx_buf_t *b; | |
5943
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
155 ngx_int_t rc; |
5944
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
156 ngx_str_t path, callback; |
457 | 157 ngx_dir_t dir; |
5944
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
158 ngx_uint_t level, format; |
457 | 159 ngx_pool_t *pool; |
557 | 160 ngx_chain_t out; |
457 | 161 ngx_array_t entries; |
162 ngx_http_autoindex_entry_t *entry; | |
163 ngx_http_autoindex_loc_conf_t *alcf; | |
164 | |
165 if (r->uri.data[r->uri.len - 1] != '/') { | |
166 return NGX_DECLINED; | |
167 } | |
168 | |
645 | 169 if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) { |
637 | 170 return NGX_DECLINED; |
171 } | |
172 | |
457 | 173 alcf = ngx_http_get_module_loc_conf(r, ngx_http_autoindex_module); |
174 | |
175 if (!alcf->enable) { | |
176 return NGX_DECLINED; | |
177 } | |
178 | |
557 | 179 /* NGX_DIR_MASK_LEN is lesser than NGX_HTTP_AUTOINDEX_PREALLOCATE */ |
457 | 180 |
773 | 181 last = ngx_http_map_uri_to_path(r, &path, &root, |
182 NGX_HTTP_AUTOINDEX_PREALLOCATE); | |
557 | 183 if (last == NULL) { |
184 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
457 | 185 } |
186 | |
557 | 187 allocated = path.len; |
1627 | 188 path.len = last - path.data; |
189 if (path.len > 1) { | |
190 path.len--; | |
191 } | |
557 | 192 path.data[path.len] = '\0'; |
457 | 193 |
194 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
557 | 195 "http autoindex: \"%s\"", path.data); |
457 | 196 |
5944
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
197 format = alcf->format; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
198 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
199 if (format == NGX_HTTP_AUTOINDEX_JSONP) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
200 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
|
201 return NGX_HTTP_BAD_REQUEST; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
202 } |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
203 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
204 if (callback.len == 0) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
205 format = NGX_HTTP_AUTOINDEX_JSON; |
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 } |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
208 |
557 | 209 if (ngx_open_dir(&path, &dir) == NGX_ERROR) { |
457 | 210 err = ngx_errno; |
211 | |
543 | 212 if (err == NGX_ENOENT |
213 || err == NGX_ENOTDIR | |
214 || err == NGX_ENAMETOOLONG) | |
215 { | |
457 | 216 level = NGX_LOG_ERR; |
217 rc = NGX_HTTP_NOT_FOUND; | |
218 | |
219 } else if (err == NGX_EACCES) { | |
220 level = NGX_LOG_ERR; | |
221 rc = NGX_HTTP_FORBIDDEN; | |
222 | |
223 } else { | |
224 level = NGX_LOG_CRIT; | |
225 rc = NGX_HTTP_INTERNAL_SERVER_ERROR; | |
226 } | |
227 | |
228 ngx_log_error(level, r->connection->log, err, | |
557 | 229 ngx_open_dir_n " \"%s\" failed", path.data); |
457 | 230 |
231 return rc; | |
232 } | |
233 | |
234 #if (NGX_SUPPRESS_WARN) | |
557 | 235 |
457 | 236 /* MSVC thinks 'entries' may be used without having been initialized */ |
237 ngx_memzero(&entries, sizeof(ngx_array_t)); | |
557 | 238 |
457 | 239 #endif |
240 | |
557 | 241 /* TODO: pool should be temporary pool */ |
242 pool = r->pool; | |
243 | |
244 if (ngx_array_init(&entries, pool, 40, sizeof(ngx_http_autoindex_entry_t)) | |
245 != NGX_OK) | |
457 | 246 { |
557 | 247 return ngx_http_autoindex_error(r, &dir, &path); |
457 | 248 } |
249 | |
523 | 250 r->headers_out.status = NGX_HTTP_OK; |
5944
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
251 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
252 switch (format) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
253 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
254 case NGX_HTTP_AUTOINDEX_JSON: |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
255 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
|
256 break; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
257 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
258 case NGX_HTTP_AUTOINDEX_JSONP: |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
259 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
|
260 break; |
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 default: /* NGX_HTTP_AUTOINDEX_HTML */ |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
263 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
|
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 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
267 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
|
268 r->headers_out.content_type_lowcase = NULL; |
523 | 269 |
270 rc = ngx_http_send_header(r); | |
271 | |
272 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
|
273 if (ngx_close_dir(&dir) == NGX_ERROR) { |
090e391f53db
fix file leak for HEAD requests
Igor Sysoev <igor@sysoev.ru>
parents:
842
diff
changeset
|
274 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
|
275 ngx_close_dir_n " \"%V\" failed", &path); |
090e391f53db
fix file leak for HEAD requests
Igor Sysoev <igor@sysoev.ru>
parents:
842
diff
changeset
|
276 } |
090e391f53db
fix file leak for HEAD requests
Igor Sysoev <igor@sysoev.ru>
parents:
842
diff
changeset
|
277 |
523 | 278 return rc; |
279 } | |
280 | |
557 | 281 filename = path.data; |
282 filename[path.len] = '/'; | |
457 | 283 |
284 for ( ;; ) { | |
285 ngx_set_errno(0); | |
286 | |
287 if (ngx_read_dir(&dir) == NGX_ERROR) { | |
288 err = ngx_errno; | |
289 | |
501 | 290 if (err != NGX_ENOMOREFILES) { |
457 | 291 ngx_log_error(NGX_LOG_CRIT, r->connection->log, err, |
557 | 292 ngx_read_dir_n " \"%V\" failed", &path); |
293 return ngx_http_autoindex_error(r, &dir, &path); | |
457 | 294 } |
295 | |
577 | 296 break; |
457 | 297 } |
298 | |
299 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
300 "http autoindex file: \"%s\"", ngx_de_name(&dir)); | |
301 | |
302 len = ngx_de_namelen(&dir); | |
303 | |
547 | 304 if (ngx_de_name(&dir)[0] == '.') { |
459 | 305 continue; |
306 } | |
307 | |
457 | 308 if (!dir.valid_info) { |
309 | |
557 | 310 /* 1 byte for '/' and 1 byte for terminating '\0' */ |
457 | 311 |
557 | 312 if (path.len + 1 + len + 1 > allocated) { |
313 allocated = path.len + 1 + len + 1 | |
314 + NGX_HTTP_AUTOINDEX_PREALLOCATE; | |
315 | |
2049 | 316 filename = ngx_pnalloc(pool, allocated); |
557 | 317 if (filename == NULL) { |
318 return ngx_http_autoindex_error(r, &dir, &path); | |
457 | 319 } |
320 | |
557 | 321 last = ngx_cpystrn(filename, path.data, path.len + 1); |
457 | 322 *last++ = '/'; |
323 } | |
324 | |
325 ngx_cpystrn(last, ngx_de_name(&dir), len + 1); | |
326 | |
557 | 327 if (ngx_de_info(filename, &dir) == NGX_FILE_ERROR) { |
497 | 328 err = ngx_errno; |
329 | |
5333
e8bca8397625
Autoindex: improved ngx_de_info() error handling.
Sergey Kandaurov <pluknet@nginx.com>
parents:
5332
diff
changeset
|
330 if (err != NGX_ENOENT && err != NGX_ELOOP) { |
497 | 331 ngx_log_error(NGX_LOG_CRIT, r->connection->log, err, |
557 | 332 ngx_de_info_n " \"%s\" failed", filename); |
2371
b438ffe54e34
skip protected symlinks in autoindex
Igor Sysoev <igor@sysoev.ru>
parents:
2125
diff
changeset
|
333 |
b438ffe54e34
skip protected symlinks in autoindex
Igor Sysoev <igor@sysoev.ru>
parents:
2125
diff
changeset
|
334 if (err == NGX_EACCES) { |
b438ffe54e34
skip protected symlinks in autoindex
Igor Sysoev <igor@sysoev.ru>
parents:
2125
diff
changeset
|
335 continue; |
b438ffe54e34
skip protected symlinks in autoindex
Igor Sysoev <igor@sysoev.ru>
parents:
2125
diff
changeset
|
336 } |
b438ffe54e34
skip protected symlinks in autoindex
Igor Sysoev <igor@sysoev.ru>
parents:
2125
diff
changeset
|
337 |
557 | 338 return ngx_http_autoindex_error(r, &dir, &path); |
497 | 339 } |
340 | |
557 | 341 if (ngx_de_link_info(filename, &dir) == NGX_FILE_ERROR) { |
497 | 342 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno, |
343 ngx_de_link_info_n " \"%s\" failed", | |
557 | 344 filename); |
345 return ngx_http_autoindex_error(r, &dir, &path); | |
497 | 346 } |
457 | 347 } |
348 } | |
349 | |
501 | 350 entry = ngx_array_push(&entries); |
351 if (entry == NULL) { | |
557 | 352 return ngx_http_autoindex_error(r, &dir, &path); |
457 | 353 } |
354 | |
577 | 355 entry->name.len = len; |
525 | 356 |
2049 | 357 entry->name.data = ngx_pnalloc(pool, len + 1); |
501 | 358 if (entry->name.data == NULL) { |
557 | 359 return ngx_http_autoindex_error(r, &dir, &path); |
457 | 360 } |
461 | 361 |
457 | 362 ngx_cpystrn(entry->name.data, ngx_de_name(&dir), len + 1); |
363 | |
364 entry->dir = ngx_de_is_dir(&dir); | |
5944
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
365 entry->file = ngx_de_is_file(&dir); |
457 | 366 entry->mtime = ngx_de_mtime(&dir); |
367 entry->size = ngx_de_size(&dir); | |
368 } | |
369 | |
370 if (ngx_close_dir(&dir) == NGX_ERROR) { | |
371 ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno, | |
5218 | 372 ngx_close_dir_n " \"%V\" failed", &path); |
457 | 373 } |
374 | |
5943
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
375 if (entries.nelts > 1) { |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
376 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
|
377 sizeof(ngx_http_autoindex_entry_t), |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
378 ngx_http_autoindex_cmp_entries); |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
379 } |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
380 |
5944
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
381 switch (format) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
382 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
383 case NGX_HTTP_AUTOINDEX_JSON: |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
384 b = ngx_http_autoindex_json(r, &entries, NULL); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
385 break; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
386 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
387 case NGX_HTTP_AUTOINDEX_JSONP: |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
388 b = ngx_http_autoindex_json(r, &entries, &callback); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
389 break; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
390 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
391 default: /* NGX_HTTP_AUTOINDEX_HTML */ |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
392 b = ngx_http_autoindex_html(r, &entries); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
393 break; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
394 } |
5943
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
395 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
396 if (b == NULL) { |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
397 return NGX_ERROR; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
398 } |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
399 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
400 /* TODO: free temporary pool */ |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
401 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
402 if (r == r->main) { |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
403 b->last_buf = 1; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
404 } |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
405 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
406 b->last_in_chain = 1; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
407 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
408 out.buf = b; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
409 out.next = NULL; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
410 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
411 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
|
412 } |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
413 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
414 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
415 static ngx_buf_t * |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
416 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
|
417 { |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
418 u_char *last, scale; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
419 off_t length; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
420 size_t len, char_len, escape_html; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
421 ngx_tm_t tm; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
422 ngx_buf_t *b; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
423 ngx_int_t size; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
424 ngx_uint_t i, utf8; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
425 ngx_time_t *tp; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
426 ngx_http_autoindex_entry_t *entry; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
427 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
|
428 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
429 static u_char title[] = |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
430 "<html>" CRLF |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
431 "<head><title>Index of " |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
432 ; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
433 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
434 static u_char header[] = |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
435 "</title></head>" CRLF |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
436 "<body bgcolor=\"white\">" CRLF |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
437 "<h1>Index of " |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
438 ; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
439 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
440 static u_char tail[] = |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
441 "</body>" CRLF |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
442 "</html>" CRLF |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
443 ; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
444 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
445 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
|
446 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
447 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
448 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
|
449 && 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
|
450 == 0) |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
451 { |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
452 utf8 = 1; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
453 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
454 } else { |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
455 utf8 = 0; |
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 |
4193
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
458 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
|
459 |
457 | 460 len = sizeof(title) - 1 |
4193
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
461 + r->uri.len + escape_html |
457 | 462 + sizeof(header) - 1 |
4193
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
463 + r->uri.len + escape_html |
457 | 464 + sizeof("</h1>") - 1 |
465 + sizeof("<hr><pre><a href=\"../\">../</a>" CRLF) - 1 | |
466 + sizeof("</pre><hr>") - 1 | |
467 + sizeof(tail) - 1; | |
468 | |
5943
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
469 entry = entries->elts; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
470 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
|
471 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
|
472 entry[i].name.len, |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
473 NGX_ESCAPE_URI_COMPONENT); |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
474 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
475 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
|
476 entry[i].name.len); |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
477 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
478 if (utf8) { |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
479 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
|
480 entry[i].name.len); |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
481 } else { |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
482 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
|
483 } |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
484 |
457 | 485 len += sizeof("<a href=\"") - 1 |
529 | 486 + entry[i].name.len + entry[i].escape |
525 | 487 + 1 /* 1 is for "/" */ |
488 + sizeof("\">") - 1 | |
4192
61e4af19df9f
Autoindex: escape '?' in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
3527
diff
changeset
|
489 + entry[i].name.len - entry[i].utf_len |
4193
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
490 + entry[i].escape_html |
525 | 491 + NGX_HTTP_AUTOINDEX_NAME_LEN + sizeof(">") - 2 |
492 + sizeof("</a>") - 1 | |
493 + sizeof(" 28-Sep-1970 12:00 ") - 1 | |
529 | 494 + 20 /* the file size */ |
525 | 495 + 2; |
457 | 496 } |
497 | |
501 | 498 b = ngx_create_temp_buf(r->pool, len); |
499 if (b == NULL) { | |
5943
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
500 return NULL; |
457 | 501 } |
502 | |
503 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
|
504 |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
505 if (escape_html) { |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
506 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
|
507 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
|
508 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
|
509 |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
510 } else { |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
511 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
|
512 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
|
513 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
|
514 } |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
515 |
457 | 516 b->last = ngx_cpymem(b->last, "</h1>", sizeof("</h1>") - 1); |
517 | |
518 b->last = ngx_cpymem(b->last, "<hr><pre><a href=\"../\">../</a>" CRLF, | |
519 sizeof("<hr><pre><a href=\"../\">../</a>" CRLF) - 1); | |
520 | |
5943
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
521 alcf = ngx_http_get_module_loc_conf(r, ngx_http_autoindex_module); |
563 | 522 tp = ngx_timeofday(); |
523 | |
5943
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
524 for (i = 0; i < entries->nelts; i++) { |
457 | 525 b->last = ngx_cpymem(b->last, "<a href=\"", sizeof("<a href=\"") - 1); |
461 | 526 |
527 if (entry[i].escape) { | |
528 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
|
529 NGX_ESCAPE_URI_COMPONENT); |
461 | 530 |
531 b->last += entry[i].name.len + entry[i].escape; | |
532 | |
533 } else { | |
534 b->last = ngx_cpymem(b->last, entry[i].name.data, | |
535 entry[i].name.len); | |
536 } | |
457 | 537 |
538 if (entry[i].dir) { | |
539 *b->last++ = '/'; | |
540 } | |
541 | |
542 *b->last++ = '"'; | |
543 *b->last++ = '>'; | |
544 | |
525 | 545 len = entry[i].utf_len; |
457 | 546 |
2120 | 547 if (entry[i].name.len != len) { |
527 | 548 if (len > NGX_HTTP_AUTOINDEX_NAME_LEN) { |
4193
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
549 char_len = NGX_HTTP_AUTOINDEX_NAME_LEN - 3 + 1; |
527 | 550 |
551 } else { | |
4193
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
552 char_len = NGX_HTTP_AUTOINDEX_NAME_LEN + 1; |
527 | 553 } |
554 | |
4193
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
555 last = b->last; |
2125
8e4b9d2acde8
rename ngx_utf_...() to ngx_utf8_...()
Igor Sysoev <igor@sysoev.ru>
parents:
2120
diff
changeset
|
556 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
|
557 char_len, entry[i].name.len + 1); |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
558 |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
559 if (entry[i].escape_html) { |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
560 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
|
561 b->last - last); |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
562 } |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
563 |
527 | 564 last = b->last; |
565 | |
566 } else { | |
4193
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
567 if (entry[i].escape_html) { |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
568 if (len > NGX_HTTP_AUTOINDEX_NAME_LEN) { |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
569 char_len = NGX_HTTP_AUTOINDEX_NAME_LEN - 3; |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
570 |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
571 } else { |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
572 char_len = len; |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
573 } |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
574 |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
575 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
|
576 entry[i].name.data, char_len); |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
577 last = b->last; |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
578 |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
579 } else { |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
580 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
|
581 NGX_HTTP_AUTOINDEX_NAME_LEN + 1); |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
582 last = b->last - 3; |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
583 } |
527 | 584 } |
585 | |
457 | 586 if (len > NGX_HTTP_AUTOINDEX_NAME_LEN) { |
527 | 587 b->last = ngx_cpymem(last, "..></a>", sizeof("..></a>") - 1); |
457 | 588 |
589 } else { | |
590 if (entry[i].dir && NGX_HTTP_AUTOINDEX_NAME_LEN - len > 0) { | |
591 *b->last++ = '/'; | |
592 len++; | |
593 } | |
594 | |
595 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
|
596 |
e9d188e295cf
Fixed false memset warning on Linux with -O3 (ticket #275).
Maxim Dounin <mdounin@mdounin.ru>
parents:
4499
diff
changeset
|
597 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
|
598 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
|
599 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
|
600 } |
457 | 601 } |
602 | |
603 *b->last++ = ' '; | |
604 | |
563 | 605 ngx_gmtime(entry[i].mtime + tp->gmtoff * 60 * alcf->localtime, &tm); |
457 | 606 |
607 b->last = ngx_sprintf(b->last, "%02d-%s-%d %02d:%02d ", | |
608 tm.ngx_tm_mday, | |
609 months[tm.ngx_tm_mon - 1], | |
610 tm.ngx_tm_year, | |
611 tm.ngx_tm_hour, | |
612 tm.ngx_tm_min); | |
613 | |
527 | 614 if (alcf->exact_size) { |
615 if (entry[i].dir) { | |
616 b->last = ngx_cpymem(b->last, " -", | |
617 sizeof(" -") - 1); | |
618 } else { | |
619 b->last = ngx_sprintf(b->last, "%19O", entry[i].size); | |
577 | 620 } |
457 | 621 |
622 } else { | |
527 | 623 if (entry[i].dir) { |
547 | 624 b->last = ngx_cpymem(b->last, " -", |
625 sizeof(" -") - 1); | |
527 | 626 |
627 } else { | |
628 length = entry[i].size; | |
629 | |
630 if (length > 1024 * 1024 * 1024 - 1) { | |
631 size = (ngx_int_t) (length / (1024 * 1024 * 1024)); | |
632 if ((length % (1024 * 1024 * 1024)) | |
633 > (1024 * 1024 * 1024 / 2 - 1)) | |
577 | 634 { |
527 | 635 size++; |
636 } | |
637 scale = 'G'; | |
638 | |
639 } else if (length > 1024 * 1024 - 1) { | |
640 size = (ngx_int_t) (length / (1024 * 1024)); | |
641 if ((length % (1024 * 1024)) > (1024 * 1024 / 2 - 1)) { | |
642 size++; | |
643 } | |
644 scale = 'M'; | |
645 | |
646 } else if (length > 9999) { | |
647 size = (ngx_int_t) (length / 1024); | |
648 if (length % 1024 > 511) { | |
649 size++; | |
650 } | |
651 scale = 'K'; | |
652 | |
653 } else { | |
654 size = (ngx_int_t) length; | |
547 | 655 scale = '\0'; |
527 | 656 } |
657 | |
547 | 658 if (scale) { |
659 b->last = ngx_sprintf(b->last, "%6i%c", size, scale); | |
527 | 660 |
547 | 661 } else { |
662 b->last = ngx_sprintf(b->last, " %6i", size); | |
527 | 663 } |
664 } | |
457 | 665 } |
666 | |
667 *b->last++ = CR; | |
668 *b->last++ = LF; | |
669 } | |
670 | |
671 b->last = ngx_cpymem(b->last, "</pre><hr>", sizeof("</pre><hr>") - 1); | |
672 | |
673 b->last = ngx_cpymem(b->last, tail, sizeof(tail) - 1); | |
674 | |
5943
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
675 return b; |
457 | 676 } |
677 | |
678 | |
5944
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
679 static ngx_buf_t * |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
680 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
|
681 ngx_str_t *callback) |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
682 { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
683 size_t len; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
684 ngx_buf_t *b; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
685 ngx_uint_t i; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
686 ngx_http_autoindex_entry_t *entry; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
687 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
688 len = sizeof("[" CRLF CRLF "]") - 1; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
689 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
690 if (callback) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
691 len += sizeof("/* callback */" CRLF "();") - 1 + callback->len; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
692 } |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
693 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
694 entry = entries->elts; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
695 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
696 for (i = 0; i < entries->nelts; i++) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
697 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
|
698 entry[i].name.len); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
699 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
700 len += sizeof("{ }," CRLF) - 1 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
701 + sizeof("\"name\":\"\"") - 1 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
702 + entry[i].name.len + entry[i].escape |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
703 + sizeof(", \"type\":\"directory\"") - 1 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
704 + sizeof(", \"mtime\":\"Wed, 31 Dec 1986 10:00:00 GMT\"") - 1; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
705 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
706 if (entry[i].file) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
707 len += sizeof(", \"size\":") - 1 + NGX_OFF_T_LEN; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
708 } |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
709 } |
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 b = ngx_create_temp_buf(r->pool, len); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
712 if (b == NULL) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
713 return NULL; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
714 } |
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 if (callback) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
717 b->last = ngx_cpymem(b->last, "/* callback */" CRLF, |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
718 sizeof("/* callback */" CRLF) - 1); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
719 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
720 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
|
721 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
722 *b->last++ = '('; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
723 } |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
724 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
725 *b->last++ = '['; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
726 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
727 for (i = 0; i < entries->nelts; i++) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
728 b->last = ngx_cpymem(b->last, CRLF "{ \"name\":\"", |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
729 sizeof(CRLF "{ \"name\":\"") - 1); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
730 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
731 if (entry[i].escape) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
732 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
|
733 entry[i].name.len); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
734 } else { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
735 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
|
736 entry[i].name.len); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
737 } |
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 b->last = ngx_cpymem(b->last, "\", \"type\":\"", |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
740 sizeof("\", \"type\":\"") - 1); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
741 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
742 if (entry[i].dir) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
743 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
|
744 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
745 } else if (entry[i].file) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
746 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
|
747 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
748 } else { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
749 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
|
750 } |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
751 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
752 b->last = ngx_cpymem(b->last, "\", \"mtime\":\"", |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
753 sizeof("\", \"mtime\":\"") - 1); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
754 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
755 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
|
756 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
757 if (entry[i].file) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
758 b->last = ngx_cpymem(b->last, "\", \"size\":", |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
759 sizeof("\", \"size\":") - 1); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
760 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
|
761 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
762 } else { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
763 *b->last++ = '"'; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
764 } |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
765 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
766 b->last = ngx_cpymem(b->last, " },", sizeof(" },") - 1); |
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 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
769 if (i > 0) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
770 b->last--; /* strip last comma */ |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
771 } |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
772 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
773 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
|
774 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
775 if (callback) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
776 *b->last++ = ')'; *b->last++ = ';'; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
777 } |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
778 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
779 return b; |
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 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
782 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
783 static ngx_int_t |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
784 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
|
785 { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
786 u_char *p, c, ch; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
787 ngx_uint_t i; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
788 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
789 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
|
790 callback->len = 0; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
791 return NGX_OK; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
792 } |
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 if (callback->len > 128) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
795 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
|
796 "client sent too long callback name: \"%V\"", callback); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
797 return NGX_DECLINED; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
798 } |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
799 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
800 p = callback->data; |
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 for (i = 0; i < callback->len; i++) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
803 ch = p[i]; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
804 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
805 c = (u_char) (ch | 0x20); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
806 if (c >= 'a' && c <= 'z') { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
807 continue; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
808 } |
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 if ((ch >= '0' && ch <= '9') || ch == '_' || ch == '.') { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
811 continue; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
812 } |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
813 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
814 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
|
815 "client sent invalid callback name: \"%V\"", callback); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
816 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
817 return NGX_DECLINED; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
818 } |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
819 |
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 |
503 | 824 static int ngx_libc_cdecl |
499 | 825 ngx_http_autoindex_cmp_entries(const void *one, const void *two) |
457 | 826 { |
827 ngx_http_autoindex_entry_t *first = (ngx_http_autoindex_entry_t *) one; | |
828 ngx_http_autoindex_entry_t *second = (ngx_http_autoindex_entry_t *) two; | |
829 | |
830 if (first->dir && !second->dir) { | |
831 /* move the directories to the start */ | |
832 return -1; | |
833 } | |
834 | |
835 if (!first->dir && second->dir) { | |
836 /* move the directories to the start */ | |
837 return 1; | |
838 } | |
839 | |
840 return (int) ngx_strcmp(first->name.data, second->name.data); | |
841 } | |
842 | |
843 | |
844 #if 0 | |
845 | |
499 | 846 static ngx_buf_t * |
847 ngx_http_autoindex_alloc(ngx_http_autoindex_ctx_t *ctx, size_t size) | |
457 | 848 { |
849 ngx_chain_t *cl; | |
850 | |
851 if (ctx->buf) { | |
852 | |
853 if ((size_t) (ctx->buf->end - ctx->buf->last) >= size) { | |
854 return ctx->buf; | |
855 } | |
856 | |
857 ctx->size += ctx->buf->last - ctx->buf->pos; | |
858 } | |
859 | |
501 | 860 ctx->buf = ngx_create_temp_buf(ctx->pool, ctx->alloc_size); |
861 if (ctx->buf == NULL) { | |
457 | 862 return NULL; |
863 } | |
864 | |
501 | 865 cl = ngx_alloc_chain_link(ctx->pool); |
866 if (cl == NULL) { | |
457 | 867 return NULL; |
868 } | |
869 | |
870 cl->buf = ctx->buf; | |
871 cl->next = NULL; | |
872 | |
873 *ctx->last_out = cl; | |
874 ctx->last_out = &cl->next; | |
875 | |
876 return ctx->buf; | |
877 } | |
878 | |
879 #endif | |
880 | |
881 | |
499 | 882 static ngx_int_t |
557 | 883 ngx_http_autoindex_error(ngx_http_request_t *r, ngx_dir_t *dir, ngx_str_t *name) |
457 | 884 { |
885 if (ngx_close_dir(dir) == NGX_ERROR) { | |
886 ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno, | |
557 | 887 ngx_close_dir_n " \"%V\" failed", name); |
457 | 888 } |
889 | |
5332
1a9700ef9725
Autoindex: return NGX_ERROR on error if headers were sent.
Sergey Kandaurov <pluknet@nginx.com>
parents:
5218
diff
changeset
|
890 return r->header_sent ? NGX_ERROR : NGX_HTTP_INTERNAL_SERVER_ERROR; |
457 | 891 } |
892 | |
893 | |
499 | 894 static void * |
895 ngx_http_autoindex_create_loc_conf(ngx_conf_t *cf) | |
457 | 896 { |
897 ngx_http_autoindex_loc_conf_t *conf; | |
898 | |
899 conf = ngx_palloc(cf->pool, sizeof(ngx_http_autoindex_loc_conf_t)); | |
900 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
|
901 return NULL; |
457 | 902 } |
903 | |
904 conf->enable = NGX_CONF_UNSET; | |
5944
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
905 conf->format = NGX_CONF_UNSET_UINT; |
519 | 906 conf->localtime = NGX_CONF_UNSET; |
527 | 907 conf->exact_size = NGX_CONF_UNSET; |
457 | 908 |
909 return conf; | |
910 } | |
911 | |
912 | |
499 | 913 static char * |
914 ngx_http_autoindex_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) | |
457 | 915 { |
916 ngx_http_autoindex_loc_conf_t *prev = parent; | |
917 ngx_http_autoindex_loc_conf_t *conf = child; | |
918 | |
919 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
|
920 ngx_conf_merge_uint_value(conf->format, prev->format, |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
921 NGX_HTTP_AUTOINDEX_HTML); |
519 | 922 ngx_conf_merge_value(conf->localtime, prev->localtime, 0); |
527 | 923 ngx_conf_merge_value(conf->exact_size, prev->exact_size, 1); |
457 | 924 |
925 return NGX_CONF_OK; | |
926 } | |
681 | 927 |
928 | |
929 static ngx_int_t | |
930 ngx_http_autoindex_init(ngx_conf_t *cf) | |
931 { | |
932 ngx_http_handler_pt *h; | |
933 ngx_http_core_main_conf_t *cmcf; | |
934 | |
935 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module); | |
936 | |
937 h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers); | |
938 if (h == NULL) { | |
939 return NGX_ERROR; | |
940 } | |
941 | |
942 *h = ngx_http_autoindex_handler; | |
943 | |
944 return NGX_OK; | |
945 } |