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