Mercurial > hg > nginx
annotate src/http/modules/ngx_http_autoindex_module.c @ 6470:62d950bc0f15
Updated OpenSSL used for win32 builds.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Tue, 29 Mar 2016 09:54:11 +0300 |
parents | 99751fe3bc3b |
children | 044cb349fd3c |
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 | |
557 | 183 /* NGX_DIR_MASK_LEN is lesser than NGX_HTTP_AUTOINDEX_PREALLOCATE */ |
457 | 184 |
773 | 185 last = ngx_http_map_uri_to_path(r, &path, &root, |
186 NGX_HTTP_AUTOINDEX_PREALLOCATE); | |
557 | 187 if (last == NULL) { |
188 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
457 | 189 } |
190 | |
557 | 191 allocated = path.len; |
1627 | 192 path.len = last - path.data; |
193 if (path.len > 1) { | |
194 path.len--; | |
195 } | |
557 | 196 path.data[path.len] = '\0'; |
457 | 197 |
198 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
557 | 199 "http autoindex: \"%s\"", path.data); |
457 | 200 |
5944
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
201 format = alcf->format; |
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 if (format == NGX_HTTP_AUTOINDEX_JSONP) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
204 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
|
205 return NGX_HTTP_BAD_REQUEST; |
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 if (callback.len == 0) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
209 format = NGX_HTTP_AUTOINDEX_JSON; |
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 |
557 | 213 if (ngx_open_dir(&path, &dir) == NGX_ERROR) { |
457 | 214 err = ngx_errno; |
215 | |
543 | 216 if (err == NGX_ENOENT |
217 || err == NGX_ENOTDIR | |
218 || err == NGX_ENAMETOOLONG) | |
219 { | |
457 | 220 level = NGX_LOG_ERR; |
221 rc = NGX_HTTP_NOT_FOUND; | |
222 | |
223 } else if (err == NGX_EACCES) { | |
224 level = NGX_LOG_ERR; | |
225 rc = NGX_HTTP_FORBIDDEN; | |
226 | |
227 } else { | |
228 level = NGX_LOG_CRIT; | |
229 rc = NGX_HTTP_INTERNAL_SERVER_ERROR; | |
230 } | |
231 | |
232 ngx_log_error(level, r->connection->log, err, | |
557 | 233 ngx_open_dir_n " \"%s\" failed", path.data); |
457 | 234 |
235 return rc; | |
236 } | |
237 | |
238 #if (NGX_SUPPRESS_WARN) | |
557 | 239 |
457 | 240 /* MSVC thinks 'entries' may be used without having been initialized */ |
241 ngx_memzero(&entries, sizeof(ngx_array_t)); | |
557 | 242 |
457 | 243 #endif |
244 | |
557 | 245 /* TODO: pool should be temporary pool */ |
246 pool = r->pool; | |
247 | |
248 if (ngx_array_init(&entries, pool, 40, sizeof(ngx_http_autoindex_entry_t)) | |
249 != NGX_OK) | |
457 | 250 { |
557 | 251 return ngx_http_autoindex_error(r, &dir, &path); |
457 | 252 } |
253 | |
523 | 254 r->headers_out.status = NGX_HTTP_OK; |
5944
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
255 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
256 switch (format) { |
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_JSON: |
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/json"); |
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 case NGX_HTTP_AUTOINDEX_JSONP: |
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/javascript"); |
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 |
5945
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
266 case NGX_HTTP_AUTOINDEX_XML: |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
267 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
|
268 ngx_str_set(&r->headers_out.charset, "utf-8"); |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
269 break; |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
270 |
5944
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
271 default: /* NGX_HTTP_AUTOINDEX_HTML */ |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
272 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
|
273 break; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
274 } |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
275 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
276 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
|
277 r->headers_out.content_type_lowcase = NULL; |
523 | 278 |
279 rc = ngx_http_send_header(r); | |
280 | |
281 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
|
282 if (ngx_close_dir(&dir) == NGX_ERROR) { |
090e391f53db
fix file leak for HEAD requests
Igor Sysoev <igor@sysoev.ru>
parents:
842
diff
changeset
|
283 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
|
284 ngx_close_dir_n " \"%V\" failed", &path); |
090e391f53db
fix file leak for HEAD requests
Igor Sysoev <igor@sysoev.ru>
parents:
842
diff
changeset
|
285 } |
090e391f53db
fix file leak for HEAD requests
Igor Sysoev <igor@sysoev.ru>
parents:
842
diff
changeset
|
286 |
523 | 287 return rc; |
288 } | |
289 | |
557 | 290 filename = path.data; |
291 filename[path.len] = '/'; | |
457 | 292 |
293 for ( ;; ) { | |
294 ngx_set_errno(0); | |
295 | |
296 if (ngx_read_dir(&dir) == NGX_ERROR) { | |
297 err = ngx_errno; | |
298 | |
501 | 299 if (err != NGX_ENOMOREFILES) { |
457 | 300 ngx_log_error(NGX_LOG_CRIT, r->connection->log, err, |
557 | 301 ngx_read_dir_n " \"%V\" failed", &path); |
302 return ngx_http_autoindex_error(r, &dir, &path); | |
457 | 303 } |
304 | |
577 | 305 break; |
457 | 306 } |
307 | |
308 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
309 "http autoindex file: \"%s\"", ngx_de_name(&dir)); | |
310 | |
311 len = ngx_de_namelen(&dir); | |
312 | |
547 | 313 if (ngx_de_name(&dir)[0] == '.') { |
459 | 314 continue; |
315 } | |
316 | |
457 | 317 if (!dir.valid_info) { |
318 | |
557 | 319 /* 1 byte for '/' and 1 byte for terminating '\0' */ |
457 | 320 |
557 | 321 if (path.len + 1 + len + 1 > allocated) { |
322 allocated = path.len + 1 + len + 1 | |
323 + NGX_HTTP_AUTOINDEX_PREALLOCATE; | |
324 | |
2049 | 325 filename = ngx_pnalloc(pool, allocated); |
557 | 326 if (filename == NULL) { |
327 return ngx_http_autoindex_error(r, &dir, &path); | |
457 | 328 } |
329 | |
557 | 330 last = ngx_cpystrn(filename, path.data, path.len + 1); |
457 | 331 *last++ = '/'; |
332 } | |
333 | |
334 ngx_cpystrn(last, ngx_de_name(&dir), len + 1); | |
335 | |
557 | 336 if (ngx_de_info(filename, &dir) == NGX_FILE_ERROR) { |
497 | 337 err = ngx_errno; |
338 | |
5333
e8bca8397625
Autoindex: improved ngx_de_info() error handling.
Sergey Kandaurov <pluknet@nginx.com>
parents:
5332
diff
changeset
|
339 if (err != NGX_ENOENT && err != NGX_ELOOP) { |
497 | 340 ngx_log_error(NGX_LOG_CRIT, r->connection->log, err, |
557 | 341 ngx_de_info_n " \"%s\" failed", filename); |
2371
b438ffe54e34
skip protected symlinks in autoindex
Igor Sysoev <igor@sysoev.ru>
parents:
2125
diff
changeset
|
342 |
b438ffe54e34
skip protected symlinks in autoindex
Igor Sysoev <igor@sysoev.ru>
parents:
2125
diff
changeset
|
343 if (err == NGX_EACCES) { |
b438ffe54e34
skip protected symlinks in autoindex
Igor Sysoev <igor@sysoev.ru>
parents:
2125
diff
changeset
|
344 continue; |
b438ffe54e34
skip protected symlinks in autoindex
Igor Sysoev <igor@sysoev.ru>
parents:
2125
diff
changeset
|
345 } |
b438ffe54e34
skip protected symlinks in autoindex
Igor Sysoev <igor@sysoev.ru>
parents:
2125
diff
changeset
|
346 |
557 | 347 return ngx_http_autoindex_error(r, &dir, &path); |
497 | 348 } |
349 | |
557 | 350 if (ngx_de_link_info(filename, &dir) == NGX_FILE_ERROR) { |
497 | 351 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno, |
352 ngx_de_link_info_n " \"%s\" failed", | |
557 | 353 filename); |
354 return ngx_http_autoindex_error(r, &dir, &path); | |
497 | 355 } |
457 | 356 } |
357 } | |
358 | |
501 | 359 entry = ngx_array_push(&entries); |
360 if (entry == NULL) { | |
557 | 361 return ngx_http_autoindex_error(r, &dir, &path); |
457 | 362 } |
363 | |
577 | 364 entry->name.len = len; |
525 | 365 |
2049 | 366 entry->name.data = ngx_pnalloc(pool, len + 1); |
501 | 367 if (entry->name.data == NULL) { |
557 | 368 return ngx_http_autoindex_error(r, &dir, &path); |
457 | 369 } |
461 | 370 |
457 | 371 ngx_cpystrn(entry->name.data, ngx_de_name(&dir), len + 1); |
372 | |
373 entry->dir = ngx_de_is_dir(&dir); | |
5944
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
374 entry->file = ngx_de_is_file(&dir); |
457 | 375 entry->mtime = ngx_de_mtime(&dir); |
376 entry->size = ngx_de_size(&dir); | |
377 } | |
378 | |
379 if (ngx_close_dir(&dir) == NGX_ERROR) { | |
380 ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno, | |
5218 | 381 ngx_close_dir_n " \"%V\" failed", &path); |
457 | 382 } |
383 | |
5943
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
384 if (entries.nelts > 1) { |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
385 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
|
386 sizeof(ngx_http_autoindex_entry_t), |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
387 ngx_http_autoindex_cmp_entries); |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
388 } |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
389 |
5944
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
390 switch (format) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
391 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
392 case NGX_HTTP_AUTOINDEX_JSON: |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
393 b = ngx_http_autoindex_json(r, &entries, NULL); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
394 break; |
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_JSONP: |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
397 b = ngx_http_autoindex_json(r, &entries, &callback); |
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 |
5945
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
400 case NGX_HTTP_AUTOINDEX_XML: |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
401 b = ngx_http_autoindex_xml(r, &entries); |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
402 break; |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
403 |
5944
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
404 default: /* NGX_HTTP_AUTOINDEX_HTML */ |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
405 b = ngx_http_autoindex_html(r, &entries); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
406 break; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
407 } |
5943
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
408 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
409 if (b == NULL) { |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
410 return NGX_ERROR; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
411 } |
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 /* TODO: free temporary pool */ |
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 if (r == r->main) { |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
416 b->last_buf = 1; |
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 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
419 b->last_in_chain = 1; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
420 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
421 out.buf = b; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
422 out.next = NULL; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
423 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
424 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
|
425 } |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
426 |
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 static ngx_buf_t * |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
429 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
|
430 { |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
431 u_char *last, scale; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
432 off_t length; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
433 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
|
434 ngx_tm_t tm; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
435 ngx_buf_t *b; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
436 ngx_int_t size; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
437 ngx_uint_t i, utf8; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
438 ngx_time_t *tp; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
439 ngx_http_autoindex_entry_t *entry; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
440 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
|
441 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
442 static u_char title[] = |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
443 "<html>" CRLF |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
444 "<head><title>Index of " |
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 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
447 static u_char header[] = |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
448 "</title></head>" CRLF |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
449 "<body bgcolor=\"white\">" CRLF |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
450 "<h1>Index of " |
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 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
453 static u_char tail[] = |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
454 "</body>" CRLF |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
455 "</html>" CRLF |
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 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
458 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
|
459 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
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 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
|
462 && 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
|
463 == 0) |
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 utf8 = 1; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
466 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
467 } else { |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
468 utf8 = 0; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
469 } |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
470 |
4193
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
471 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
|
472 |
457 | 473 len = sizeof(title) - 1 |
4193
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
474 + r->uri.len + escape_html |
457 | 475 + sizeof(header) - 1 |
4193
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
476 + r->uri.len + escape_html |
457 | 477 + sizeof("</h1>") - 1 |
478 + sizeof("<hr><pre><a href=\"../\">../</a>" CRLF) - 1 | |
479 + sizeof("</pre><hr>") - 1 | |
480 + sizeof(tail) - 1; | |
481 | |
5943
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
482 entry = entries->elts; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
483 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
|
484 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
|
485 entry[i].name.len, |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
486 NGX_ESCAPE_URI_COMPONENT); |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
487 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
488 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
|
489 entry[i].name.len); |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
490 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
491 if (utf8) { |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
492 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
|
493 entry[i].name.len); |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
494 } else { |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
495 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
|
496 } |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
497 |
457 | 498 len += sizeof("<a href=\"") - 1 |
529 | 499 + entry[i].name.len + entry[i].escape |
525 | 500 + 1 /* 1 is for "/" */ |
501 + sizeof("\">") - 1 | |
4192
61e4af19df9f
Autoindex: escape '?' in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
3527
diff
changeset
|
502 + 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
|
503 + entry[i].escape_html |
525 | 504 + NGX_HTTP_AUTOINDEX_NAME_LEN + sizeof(">") - 2 |
505 + sizeof("</a>") - 1 | |
506 + sizeof(" 28-Sep-1970 12:00 ") - 1 | |
529 | 507 + 20 /* the file size */ |
525 | 508 + 2; |
457 | 509 } |
510 | |
501 | 511 b = ngx_create_temp_buf(r->pool, len); |
512 if (b == NULL) { | |
5943
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
513 return NULL; |
457 | 514 } |
515 | |
516 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
|
517 |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
518 if (escape_html) { |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
519 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
|
520 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
|
521 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
|
522 |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
523 } else { |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
524 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
|
525 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
|
526 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
|
527 } |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
528 |
457 | 529 b->last = ngx_cpymem(b->last, "</h1>", sizeof("</h1>") - 1); |
530 | |
531 b->last = ngx_cpymem(b->last, "<hr><pre><a href=\"../\">../</a>" CRLF, | |
532 sizeof("<hr><pre><a href=\"../\">../</a>" CRLF) - 1); | |
533 | |
5943
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
534 alcf = ngx_http_get_module_loc_conf(r, ngx_http_autoindex_module); |
563 | 535 tp = ngx_timeofday(); |
536 | |
5943
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
537 for (i = 0; i < entries->nelts; i++) { |
457 | 538 b->last = ngx_cpymem(b->last, "<a href=\"", sizeof("<a href=\"") - 1); |
461 | 539 |
540 if (entry[i].escape) { | |
541 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
|
542 NGX_ESCAPE_URI_COMPONENT); |
461 | 543 |
544 b->last += entry[i].name.len + entry[i].escape; | |
545 | |
546 } else { | |
547 b->last = ngx_cpymem(b->last, entry[i].name.data, | |
548 entry[i].name.len); | |
549 } | |
457 | 550 |
551 if (entry[i].dir) { | |
552 *b->last++ = '/'; | |
553 } | |
554 | |
555 *b->last++ = '"'; | |
556 *b->last++ = '>'; | |
557 | |
525 | 558 len = entry[i].utf_len; |
457 | 559 |
2120 | 560 if (entry[i].name.len != len) { |
527 | 561 if (len > NGX_HTTP_AUTOINDEX_NAME_LEN) { |
4193
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
562 char_len = NGX_HTTP_AUTOINDEX_NAME_LEN - 3 + 1; |
527 | 563 |
564 } else { | |
4193
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
565 char_len = NGX_HTTP_AUTOINDEX_NAME_LEN + 1; |
527 | 566 } |
567 | |
4193
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
568 last = b->last; |
2125
8e4b9d2acde8
rename ngx_utf_...() to ngx_utf8_...()
Igor Sysoev <igor@sysoev.ru>
parents:
2120
diff
changeset
|
569 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
|
570 char_len, entry[i].name.len + 1); |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
571 |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
572 if (entry[i].escape_html) { |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
573 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
|
574 b->last - last); |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
575 } |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
576 |
527 | 577 last = b->last; |
578 | |
579 } else { | |
4193
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
580 if (entry[i].escape_html) { |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
581 if (len > NGX_HTTP_AUTOINDEX_NAME_LEN) { |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
582 char_len = NGX_HTTP_AUTOINDEX_NAME_LEN - 3; |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
583 |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
584 } else { |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
585 char_len = len; |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
586 } |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
587 |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
588 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
|
589 entry[i].name.data, char_len); |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
590 last = b->last; |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
591 |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
592 } else { |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
593 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
|
594 NGX_HTTP_AUTOINDEX_NAME_LEN + 1); |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
595 last = b->last - 3; |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
596 } |
527 | 597 } |
598 | |
457 | 599 if (len > NGX_HTTP_AUTOINDEX_NAME_LEN) { |
527 | 600 b->last = ngx_cpymem(last, "..></a>", sizeof("..></a>") - 1); |
457 | 601 |
602 } else { | |
603 if (entry[i].dir && NGX_HTTP_AUTOINDEX_NAME_LEN - len > 0) { | |
604 *b->last++ = '/'; | |
605 len++; | |
606 } | |
607 | |
608 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
|
609 |
e9d188e295cf
Fixed false memset warning on Linux with -O3 (ticket #275).
Maxim Dounin <mdounin@mdounin.ru>
parents:
4499
diff
changeset
|
610 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
|
611 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
|
612 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
|
613 } |
457 | 614 } |
615 | |
616 *b->last++ = ' '; | |
617 | |
563 | 618 ngx_gmtime(entry[i].mtime + tp->gmtoff * 60 * alcf->localtime, &tm); |
457 | 619 |
620 b->last = ngx_sprintf(b->last, "%02d-%s-%d %02d:%02d ", | |
621 tm.ngx_tm_mday, | |
622 months[tm.ngx_tm_mon - 1], | |
623 tm.ngx_tm_year, | |
624 tm.ngx_tm_hour, | |
625 tm.ngx_tm_min); | |
626 | |
527 | 627 if (alcf->exact_size) { |
628 if (entry[i].dir) { | |
629 b->last = ngx_cpymem(b->last, " -", | |
630 sizeof(" -") - 1); | |
631 } else { | |
632 b->last = ngx_sprintf(b->last, "%19O", entry[i].size); | |
577 | 633 } |
457 | 634 |
635 } else { | |
527 | 636 if (entry[i].dir) { |
547 | 637 b->last = ngx_cpymem(b->last, " -", |
638 sizeof(" -") - 1); | |
527 | 639 |
640 } else { | |
641 length = entry[i].size; | |
642 | |
643 if (length > 1024 * 1024 * 1024 - 1) { | |
644 size = (ngx_int_t) (length / (1024 * 1024 * 1024)); | |
645 if ((length % (1024 * 1024 * 1024)) | |
646 > (1024 * 1024 * 1024 / 2 - 1)) | |
577 | 647 { |
527 | 648 size++; |
649 } | |
650 scale = 'G'; | |
651 | |
652 } else if (length > 1024 * 1024 - 1) { | |
653 size = (ngx_int_t) (length / (1024 * 1024)); | |
654 if ((length % (1024 * 1024)) > (1024 * 1024 / 2 - 1)) { | |
655 size++; | |
656 } | |
657 scale = 'M'; | |
658 | |
659 } else if (length > 9999) { | |
660 size = (ngx_int_t) (length / 1024); | |
661 if (length % 1024 > 511) { | |
662 size++; | |
663 } | |
664 scale = 'K'; | |
665 | |
666 } else { | |
667 size = (ngx_int_t) length; | |
547 | 668 scale = '\0'; |
527 | 669 } |
670 | |
547 | 671 if (scale) { |
672 b->last = ngx_sprintf(b->last, "%6i%c", size, scale); | |
527 | 673 |
547 | 674 } else { |
675 b->last = ngx_sprintf(b->last, " %6i", size); | |
527 | 676 } |
677 } | |
457 | 678 } |
679 | |
680 *b->last++ = CR; | |
681 *b->last++ = LF; | |
682 } | |
683 | |
684 b->last = ngx_cpymem(b->last, "</pre><hr>", sizeof("</pre><hr>") - 1); | |
685 | |
686 b->last = ngx_cpymem(b->last, tail, sizeof(tail) - 1); | |
687 | |
5943
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
688 return b; |
457 | 689 } |
690 | |
691 | |
5944
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
692 static ngx_buf_t * |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
693 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
|
694 ngx_str_t *callback) |
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 size_t len; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
697 ngx_buf_t *b; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
698 ngx_uint_t i; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
699 ngx_http_autoindex_entry_t *entry; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
700 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
701 len = sizeof("[" CRLF CRLF "]") - 1; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
702 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
703 if (callback) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
704 len += sizeof("/* callback */" CRLF "();") - 1 + callback->len; |
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 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
707 entry = entries->elts; |
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 for (i = 0; i < entries->nelts; i++) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
710 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
|
711 entry[i].name.len); |
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 len += sizeof("{ }," CRLF) - 1 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
714 + sizeof("\"name\":\"\"") - 1 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
715 + entry[i].name.len + entry[i].escape |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
716 + sizeof(", \"type\":\"directory\"") - 1 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
717 + 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
|
718 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
719 if (entry[i].file) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
720 len += sizeof(", \"size\":") - 1 + NGX_OFF_T_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 } |
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 b = ngx_create_temp_buf(r->pool, len); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
725 if (b == NULL) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
726 return NULL; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
727 } |
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 (callback) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
730 b->last = ngx_cpymem(b->last, "/* callback */" CRLF, |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
731 sizeof("/* callback */" CRLF) - 1); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
732 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
733 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
|
734 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
735 *b->last++ = '('; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
736 } |
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 *b->last++ = '['; |
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 for (i = 0; i < entries->nelts; i++) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
741 b->last = ngx_cpymem(b->last, CRLF "{ \"name\":\"", |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
742 sizeof(CRLF "{ \"name\":\"") - 1); |
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 if (entry[i].escape) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
745 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
|
746 entry[i].name.len); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
747 } else { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
748 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
|
749 entry[i].name.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 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
752 b->last = ngx_cpymem(b->last, "\", \"type\":\"", |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
753 sizeof("\", \"type\":\"") - 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 if (entry[i].dir) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
756 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
|
757 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
758 } else if (entry[i].file) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
759 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
|
760 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
761 } else { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
762 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
|
763 } |
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 b->last = ngx_cpymem(b->last, "\", \"mtime\":\"", |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
766 sizeof("\", \"mtime\":\"") - 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 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
|
769 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
770 if (entry[i].file) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
771 b->last = ngx_cpymem(b->last, "\", \"size\":", |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
772 sizeof("\", \"size\":") - 1); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
773 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
|
774 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
775 } else { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
776 *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 b->last = ngx_cpymem(b->last, " },", sizeof(" },") - 1); |
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 if (i > 0) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
783 b->last--; /* strip last comma */ |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
784 } |
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 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
|
787 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
788 if (callback) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
789 *b->last++ = ')'; *b->last++ = ';'; |
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 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
792 return b; |
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 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
796 static ngx_int_t |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
797 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
|
798 { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
799 u_char *p, c, ch; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
800 ngx_uint_t i; |
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 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
|
803 callback->len = 0; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
804 return NGX_OK; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
805 } |
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 if (callback->len > 128) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
808 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
|
809 "client sent too long callback name: \"%V\"", callback); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
810 return NGX_DECLINED; |
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 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
813 p = callback->data; |
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 for (i = 0; i < callback->len; i++) { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
816 ch = p[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 c = (u_char) (ch | 0x20); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
819 if (c >= 'a' && c <= 'z') { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
820 continue; |
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 ((ch >= '0' && ch <= '9') || ch == '_' || ch == '.') { |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
824 continue; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
825 } |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
826 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
827 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
|
828 "client sent invalid callback name: \"%V\"", callback); |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
829 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
830 return NGX_DECLINED; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
831 } |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
832 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
833 return NGX_OK; |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
834 } |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
835 |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
836 |
5945
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
837 static ngx_buf_t * |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
838 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
|
839 { |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
840 size_t len; |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
841 ngx_tm_t tm; |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
842 ngx_buf_t *b; |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
843 ngx_str_t type; |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
844 ngx_uint_t i; |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
845 ngx_http_autoindex_entry_t *entry; |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
846 |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
847 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
|
848 static u_char tail[] = "</list>" CRLF; |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
849 |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
850 len = sizeof(head) - 1 + sizeof(tail) - 1; |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
851 |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
852 entry = entries->elts; |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
853 |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
854 for (i = 0; i < entries->nelts; i++) { |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
855 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
|
856 entry[i].name.len); |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
857 |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
858 len += sizeof("<directory></directory>" CRLF) - 1 |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
859 + entry[i].name.len + entry[i].escape |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
860 + sizeof(" mtime=\"1986-12-31T10:00:00Z\"") - 1; |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
861 |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
862 if (entry[i].file) { |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
863 len += sizeof(" size=\"\"") - 1 + NGX_OFF_T_LEN; |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
864 } |
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 |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
867 b = ngx_create_temp_buf(r->pool, len); |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
868 if (b == NULL) { |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
869 return NULL; |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
870 } |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
871 |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
872 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
|
873 |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
874 for (i = 0; i < entries->nelts; i++) { |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
875 *b->last++ = '<'; |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
876 |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
877 if (entry[i].dir) { |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
878 ngx_str_set(&type, "directory"); |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
879 |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
880 } else if (entry[i].file) { |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
881 ngx_str_set(&type, "file"); |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
882 |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
883 } else { |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
884 ngx_str_set(&type, "other"); |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
885 } |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
886 |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
887 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
|
888 |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
889 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
|
890 |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
891 ngx_gmtime(entry[i].mtime, &tm); |
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 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
|
894 tm.ngx_tm_year, tm.ngx_tm_mon, |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
895 tm.ngx_tm_mday, tm.ngx_tm_hour, |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
896 tm.ngx_tm_min, tm.ngx_tm_sec); |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
897 |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
898 if (entry[i].file) { |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
899 b->last = ngx_cpymem(b->last, "\" size=\"", |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
900 sizeof("\" size=\"") - 1); |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
901 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
|
902 } |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
903 |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
904 *b->last++ = '"'; *b->last++ = '>'; |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
905 |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
906 if (entry[i].escape) { |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
907 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
|
908 entry[i].name.len); |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
909 } else { |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
910 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
|
911 entry[i].name.len); |
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 |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
914 *b->last++ = '<'; *b->last++ = '/'; |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
915 |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
916 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
|
917 |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
918 *b->last++ = '>'; |
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 *b->last++ = CR; *b->last++ = LF; |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
921 } |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
922 |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
923 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
|
924 |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
925 return b; |
99751fe3bc3b
Autoindex: implemented XML output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5944
diff
changeset
|
926 } |
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 |
503 | 929 static int ngx_libc_cdecl |
499 | 930 ngx_http_autoindex_cmp_entries(const void *one, const void *two) |
457 | 931 { |
932 ngx_http_autoindex_entry_t *first = (ngx_http_autoindex_entry_t *) one; | |
933 ngx_http_autoindex_entry_t *second = (ngx_http_autoindex_entry_t *) two; | |
934 | |
935 if (first->dir && !second->dir) { | |
936 /* move the directories to the start */ | |
937 return -1; | |
938 } | |
939 | |
940 if (!first->dir && second->dir) { | |
941 /* move the directories to the start */ | |
942 return 1; | |
943 } | |
944 | |
945 return (int) ngx_strcmp(first->name.data, second->name.data); | |
946 } | |
947 | |
948 | |
949 #if 0 | |
950 | |
499 | 951 static ngx_buf_t * |
952 ngx_http_autoindex_alloc(ngx_http_autoindex_ctx_t *ctx, size_t size) | |
457 | 953 { |
954 ngx_chain_t *cl; | |
955 | |
956 if (ctx->buf) { | |
957 | |
958 if ((size_t) (ctx->buf->end - ctx->buf->last) >= size) { | |
959 return ctx->buf; | |
960 } | |
961 | |
962 ctx->size += ctx->buf->last - ctx->buf->pos; | |
963 } | |
964 | |
501 | 965 ctx->buf = ngx_create_temp_buf(ctx->pool, ctx->alloc_size); |
966 if (ctx->buf == NULL) { | |
457 | 967 return NULL; |
968 } | |
969 | |
501 | 970 cl = ngx_alloc_chain_link(ctx->pool); |
971 if (cl == NULL) { | |
457 | 972 return NULL; |
973 } | |
974 | |
975 cl->buf = ctx->buf; | |
976 cl->next = NULL; | |
977 | |
978 *ctx->last_out = cl; | |
979 ctx->last_out = &cl->next; | |
980 | |
981 return ctx->buf; | |
982 } | |
983 | |
984 #endif | |
985 | |
986 | |
499 | 987 static ngx_int_t |
557 | 988 ngx_http_autoindex_error(ngx_http_request_t *r, ngx_dir_t *dir, ngx_str_t *name) |
457 | 989 { |
990 if (ngx_close_dir(dir) == NGX_ERROR) { | |
991 ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno, | |
557 | 992 ngx_close_dir_n " \"%V\" failed", name); |
457 | 993 } |
994 | |
5332
1a9700ef9725
Autoindex: return NGX_ERROR on error if headers were sent.
Sergey Kandaurov <pluknet@nginx.com>
parents:
5218
diff
changeset
|
995 return r->header_sent ? NGX_ERROR : NGX_HTTP_INTERNAL_SERVER_ERROR; |
457 | 996 } |
997 | |
998 | |
499 | 999 static void * |
1000 ngx_http_autoindex_create_loc_conf(ngx_conf_t *cf) | |
457 | 1001 { |
1002 ngx_http_autoindex_loc_conf_t *conf; | |
1003 | |
1004 conf = ngx_palloc(cf->pool, sizeof(ngx_http_autoindex_loc_conf_t)); | |
1005 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
|
1006 return NULL; |
457 | 1007 } |
1008 | |
1009 conf->enable = NGX_CONF_UNSET; | |
5944
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
1010 conf->format = NGX_CONF_UNSET_UINT; |
519 | 1011 conf->localtime = NGX_CONF_UNSET; |
527 | 1012 conf->exact_size = NGX_CONF_UNSET; |
457 | 1013 |
1014 return conf; | |
1015 } | |
1016 | |
1017 | |
499 | 1018 static char * |
1019 ngx_http_autoindex_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) | |
457 | 1020 { |
1021 ngx_http_autoindex_loc_conf_t *prev = parent; | |
1022 ngx_http_autoindex_loc_conf_t *conf = child; | |
1023 | |
1024 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
|
1025 ngx_conf_merge_uint_value(conf->format, prev->format, |
33c08d7e2915
Autoindex: implemented JSON output format.
Valentin Bartenev <vbart@nginx.com>
parents:
5943
diff
changeset
|
1026 NGX_HTTP_AUTOINDEX_HTML); |
519 | 1027 ngx_conf_merge_value(conf->localtime, prev->localtime, 0); |
527 | 1028 ngx_conf_merge_value(conf->exact_size, prev->exact_size, 1); |
457 | 1029 |
1030 return NGX_CONF_OK; | |
1031 } | |
681 | 1032 |
1033 | |
1034 static ngx_int_t | |
1035 ngx_http_autoindex_init(ngx_conf_t *cf) | |
1036 { | |
1037 ngx_http_handler_pt *h; | |
1038 ngx_http_core_main_conf_t *cmcf; | |
1039 | |
1040 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module); | |
1041 | |
1042 h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers); | |
1043 if (h == NULL) { | |
1044 return NGX_ERROR; | |
1045 } | |
1046 | |
1047 *h = ngx_http_autoindex_handler; | |
1048 | |
1049 return NGX_OK; | |
1050 } |