Mercurial > hg > nginx-quic
annotate src/http/modules/ngx_http_dav_module.c @ 3031:98a8336c5b7c
fix memory leak if GeoIP City database was used
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Wed, 19 Aug 2009 17:44:33 +0000 |
parents | 8101d9101ed8 |
children | 524ba56ba9f7 |
rev | line source |
---|---|
633 | 1 |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4 */ | |
5 | |
6 | |
7 #include <ngx_config.h> | |
8 #include <ngx_core.h> | |
9 #include <ngx_http.h> | |
10 | |
11 | |
1116 | 12 #define NGX_HTTP_DAV_COPY_BLOCK 65536 |
13 | |
14 #define NGX_HTTP_DAV_OFF 2 | |
15 | |
16 | |
17 #define NGX_HTTP_DAV_NO_DEPTH -3 | |
18 #define NGX_HTTP_DAV_INVALID_DEPTH -2 | |
19 #define NGX_HTTP_DAV_INFINITY_DEPTH -1 | |
20 | |
633 | 21 |
22 typedef struct { | |
23 ngx_uint_t methods; | |
1816 | 24 ngx_uint_t access; |
25 ngx_uint_t min_delete_depth; | |
637 | 26 ngx_flag_t create_full_put_path; |
633 | 27 } ngx_http_dav_loc_conf_t; |
28 | |
29 | |
1116 | 30 typedef struct { |
31 ngx_str_t path; | |
32 size_t len; | |
33 } ngx_http_dav_copy_ctx_t; | |
34 | |
35 | |
633 | 36 static ngx_int_t ngx_http_dav_handler(ngx_http_request_t *r); |
1116 | 37 |
38 static void ngx_http_dav_put_handler(ngx_http_request_t *r); | |
39 | |
40 static ngx_int_t ngx_http_dav_delete_handler(ngx_http_request_t *r); | |
1820 | 41 static ngx_int_t ngx_http_dav_delete_path(ngx_http_request_t *r, |
42 ngx_str_t *path, ngx_uint_t dir); | |
957 | 43 static ngx_int_t ngx_http_dav_delete_dir(ngx_tree_ctx_t *ctx, ngx_str_t *path); |
44 static ngx_int_t ngx_http_dav_delete_file(ngx_tree_ctx_t *ctx, ngx_str_t *path); | |
1820 | 45 static ngx_int_t ngx_http_dav_noop(ngx_tree_ctx_t *ctx, ngx_str_t *path); |
1116 | 46 |
47 static ngx_int_t ngx_http_dav_mkcol_handler(ngx_http_request_t *r, | |
48 ngx_http_dav_loc_conf_t *dlcf); | |
49 | |
50 static ngx_int_t ngx_http_dav_copy_move_handler(ngx_http_request_t *r); | |
51 static ngx_int_t ngx_http_dav_copy_dir(ngx_tree_ctx_t *ctx, ngx_str_t *path); | |
52 static ngx_int_t ngx_http_dav_copy_dir_time(ngx_tree_ctx_t *ctx, | |
53 ngx_str_t *path); | |
1825
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
54 static ngx_int_t ngx_http_dav_copy_tree_file(ngx_tree_ctx_t *ctx, |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
55 ngx_str_t *path); |
1116 | 56 |
57 static ngx_int_t ngx_http_dav_depth(ngx_http_request_t *r, ngx_int_t dflt); | |
58 static ngx_int_t ngx_http_dav_error(ngx_log_t *log, ngx_err_t err, | |
637 | 59 ngx_int_t not_found, char *failed, u_char *path); |
60 static ngx_int_t ngx_http_dav_location(ngx_http_request_t *r, u_char *path); | |
633 | 61 static void *ngx_http_dav_create_loc_conf(ngx_conf_t *cf); |
62 static char *ngx_http_dav_merge_loc_conf(ngx_conf_t *cf, | |
63 void *parent, void *child); | |
681 | 64 static ngx_int_t ngx_http_dav_init(ngx_conf_t *cf); |
633 | 65 |
66 | |
67 static ngx_conf_bitmask_t ngx_http_dav_methods_mask[] = { | |
68 { ngx_string("off"), NGX_HTTP_DAV_OFF }, | |
69 { ngx_string("put"), NGX_HTTP_PUT }, | |
70 { ngx_string("delete"), NGX_HTTP_DELETE }, | |
637 | 71 { ngx_string("mkcol"), NGX_HTTP_MKCOL }, |
1116 | 72 { ngx_string("copy"), NGX_HTTP_COPY }, |
73 { ngx_string("move"), NGX_HTTP_MOVE }, | |
633 | 74 { ngx_null_string, 0 } |
75 }; | |
76 | |
77 | |
78 static ngx_command_t ngx_http_dav_commands[] = { | |
79 | |
80 { ngx_string("dav_methods"), | |
81 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE, | |
82 ngx_conf_set_bitmask_slot, | |
83 NGX_HTTP_LOC_CONF_OFFSET, | |
84 offsetof(ngx_http_dav_loc_conf_t, methods), | |
85 &ngx_http_dav_methods_mask }, | |
86 | |
637 | 87 { ngx_string("create_full_put_path"), |
88 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
89 ngx_conf_set_flag_slot, | |
90 NGX_HTTP_LOC_CONF_OFFSET, | |
91 offsetof(ngx_http_dav_loc_conf_t, create_full_put_path), | |
92 NULL }, | |
93 | |
1816 | 94 { ngx_string("min_delete_depth"), |
95 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
96 ngx_conf_set_num_slot, | |
97 NGX_HTTP_LOC_CONF_OFFSET, | |
98 offsetof(ngx_http_dav_loc_conf_t, min_delete_depth), | |
99 NULL }, | |
100 | |
669 | 101 { ngx_string("dav_access"), |
675 | 102 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE123, |
1296
9fcfca54b208
dav_access uses ngx_conf_set_access_slot()
Igor Sysoev <igor@sysoev.ru>
parents:
1289
diff
changeset
|
103 ngx_conf_set_access_slot, |
669 | 104 NGX_HTTP_LOC_CONF_OFFSET, |
1296
9fcfca54b208
dav_access uses ngx_conf_set_access_slot()
Igor Sysoev <igor@sysoev.ru>
parents:
1289
diff
changeset
|
105 offsetof(ngx_http_dav_loc_conf_t, access), |
669 | 106 NULL }, |
107 | |
633 | 108 ngx_null_command |
109 }; | |
110 | |
111 | |
667 | 112 static ngx_http_module_t ngx_http_dav_module_ctx = { |
633 | 113 NULL, /* preconfiguration */ |
681 | 114 ngx_http_dav_init, /* postconfiguration */ |
633 | 115 |
116 NULL, /* create main configuration */ | |
117 NULL, /* init main configuration */ | |
118 | |
119 NULL, /* create server configuration */ | |
120 NULL, /* merge server configuration */ | |
121 | |
122 ngx_http_dav_create_loc_conf, /* create location configuration */ | |
123 ngx_http_dav_merge_loc_conf /* merge location configuration */ | |
124 }; | |
125 | |
126 | |
127 ngx_module_t ngx_http_dav_module = { | |
128 NGX_MODULE_V1, | |
129 &ngx_http_dav_module_ctx, /* module context */ | |
130 ngx_http_dav_commands, /* module directives */ | |
131 NGX_HTTP_MODULE, /* module type */ | |
132 NULL, /* init master */ | |
681 | 133 NULL, /* init module */ |
633 | 134 NULL, /* init process */ |
135 NULL, /* init thread */ | |
136 NULL, /* exit thread */ | |
137 NULL, /* exit process */ | |
138 NULL, /* exit master */ | |
139 NGX_MODULE_V1_PADDING | |
140 }; | |
141 | |
142 | |
143 static ngx_int_t | |
144 ngx_http_dav_handler(ngx_http_request_t *r) | |
145 { | |
146 ngx_int_t rc; | |
147 ngx_http_dav_loc_conf_t *dlcf; | |
148 | |
149 if (r->zero_in_uri) { | |
150 return NGX_DECLINED; | |
151 } | |
152 | |
153 dlcf = ngx_http_get_module_loc_conf(r, ngx_http_dav_module); | |
154 | |
155 if (!(r->method & dlcf->methods)) { | |
156 return NGX_DECLINED; | |
157 } | |
158 | |
159 switch (r->method) { | |
160 | |
161 case NGX_HTTP_PUT: | |
162 | |
163 if (r->uri.data[r->uri.len - 1] == '/') { | |
1813
d3f80e0be8fa
change status code and add log message
Igor Sysoev <igor@sysoev.ru>
parents:
1804
diff
changeset
|
164 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
d3f80e0be8fa
change status code and add log message
Igor Sysoev <igor@sysoev.ru>
parents:
1804
diff
changeset
|
165 "can not PUT to a collection"); |
d3f80e0be8fa
change status code and add log message
Igor Sysoev <igor@sysoev.ru>
parents:
1804
diff
changeset
|
166 return NGX_HTTP_CONFLICT; |
633 | 167 } |
168 | |
169 r->request_body_in_file_only = 1; | |
170 r->request_body_in_persistent_file = 1; | |
1075
4d203f76b757
undo "client_body_in_file_only any"
Igor Sysoev <igor@sysoev.ru>
parents:
1060
diff
changeset
|
171 r->request_body_in_clean_file = 1; |
633 | 172 r->request_body_file_group_access = 1; |
637 | 173 r->request_body_file_log_level = 0; |
633 | 174 |
175 rc = ngx_http_read_client_request_body(r, ngx_http_dav_put_handler); | |
176 | |
177 if (rc >= NGX_HTTP_SPECIAL_RESPONSE) { | |
178 return rc; | |
179 } | |
180 | |
181 return NGX_DONE; | |
182 | |
183 case NGX_HTTP_DELETE: | |
184 | |
1116 | 185 return ngx_http_dav_delete_handler(r); |
637 | 186 |
187 case NGX_HTTP_MKCOL: | |
188 | |
1116 | 189 return ngx_http_dav_mkcol_handler(r, dlcf); |
637 | 190 |
1116 | 191 case NGX_HTTP_COPY: |
637 | 192 |
1116 | 193 return ngx_http_dav_copy_move_handler(r); |
637 | 194 |
1116 | 195 case NGX_HTTP_MOVE: |
637 | 196 |
1116 | 197 return ngx_http_dav_copy_move_handler(r); |
633 | 198 } |
199 | |
200 return NGX_DECLINED; | |
201 } | |
202 | |
203 | |
204 static void | |
205 ngx_http_dav_put_handler(ngx_http_request_t *r) | |
206 { | |
773 | 207 size_t root; |
681 | 208 time_t date; |
637 | 209 ngx_str_t *temp, path; |
1828
1f3c2b6607e0
use ngx_ext_rename_file() in PUT
Igor Sysoev <igor@sysoev.ru>
parents:
1825
diff
changeset
|
210 ngx_uint_t status; |
637 | 211 ngx_file_info_t fi; |
1828
1f3c2b6607e0
use ngx_ext_rename_file() in PUT
Igor Sysoev <igor@sysoev.ru>
parents:
1825
diff
changeset
|
212 ngx_ext_rename_file_t ext; |
637 | 213 ngx_http_dav_loc_conf_t *dlcf; |
633 | 214 |
773 | 215 ngx_http_map_uri_to_path(r, &path, &root, 0); |
633 | 216 |
217 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
218 "http put filename: \"%s\"", path.data); | |
219 | |
220 temp = &r->request_body->temp_file->file.name; | |
221 | |
2782
4bd7825fab80
uniform ngx_file_info() interface with ngx_fd_info()
Igor Sysoev <igor@sysoev.ru>
parents:
2721
diff
changeset
|
222 if (ngx_file_info(path.data, &fi) == NGX_FILE_ERROR) { |
633 | 223 status = NGX_HTTP_CREATED; |
224 | |
225 } else { | |
226 status = NGX_HTTP_NO_CONTENT; | |
663 | 227 |
228 if (ngx_is_dir(&fi)) { | |
229 ngx_log_error(NGX_LOG_ERR, r->connection->log, NGX_EISDIR, | |
230 "\"%s\" could not be created", path.data); | |
633 | 231 |
663 | 232 if (ngx_delete_file(temp->data) == NGX_FILE_ERROR) { |
233 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno, | |
234 ngx_delete_file_n " \"%s\" failed", | |
235 temp->data); | |
236 } | |
237 | |
238 ngx_http_finalize_request(r, NGX_HTTP_CONFLICT); | |
239 return; | |
661 | 240 } |
637 | 241 } |
242 | |
669 | 243 dlcf = ngx_http_get_module_loc_conf(r, ngx_http_dav_module); |
244 | |
1828
1f3c2b6607e0
use ngx_ext_rename_file() in PUT
Igor Sysoev <igor@sysoev.ru>
parents:
1825
diff
changeset
|
245 ext.access = dlcf->access; |
2394
dbe746851b31
use ngx_ext_rename_file() for single file MOVE
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
246 ext.path_access = dlcf->access; |
1828
1f3c2b6607e0
use ngx_ext_rename_file() in PUT
Igor Sysoev <igor@sysoev.ru>
parents:
1825
diff
changeset
|
247 ext.time = -1; |
1f3c2b6607e0
use ngx_ext_rename_file() in PUT
Igor Sysoev <igor@sysoev.ru>
parents:
1825
diff
changeset
|
248 ext.create_path = dlcf->create_full_put_path; |
1909 | 249 ext.delete_file = 1; |
1828
1f3c2b6607e0
use ngx_ext_rename_file() in PUT
Igor Sysoev <igor@sysoev.ru>
parents:
1825
diff
changeset
|
250 ext.log = r->connection->log; |
669 | 251 |
681 | 252 if (r->headers_in.date) { |
253 date = ngx_http_parse_time(r->headers_in.date->value.data, | |
254 r->headers_in.date->value.len); | |
255 | |
256 if (date != NGX_ERROR) { | |
1828
1f3c2b6607e0
use ngx_ext_rename_file() in PUT
Igor Sysoev <igor@sysoev.ru>
parents:
1825
diff
changeset
|
257 ext.time = date; |
1f3c2b6607e0
use ngx_ext_rename_file() in PUT
Igor Sysoev <igor@sysoev.ru>
parents:
1825
diff
changeset
|
258 ext.fd = r->request_body->temp_file->file.fd; |
681 | 259 } |
260 } | |
261 | |
1828
1f3c2b6607e0
use ngx_ext_rename_file() in PUT
Igor Sysoev <igor@sysoev.ru>
parents:
1825
diff
changeset
|
262 if (ngx_ext_rename_file(temp, &path, &ext) != NGX_OK) { |
1f3c2b6607e0
use ngx_ext_rename_file() in PUT
Igor Sysoev <igor@sysoev.ru>
parents:
1825
diff
changeset
|
263 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); |
1f3c2b6607e0
use ngx_ext_rename_file() in PUT
Igor Sysoev <igor@sysoev.ru>
parents:
1825
diff
changeset
|
264 return; |
637 | 265 } |
266 | |
633 | 267 if (status == NGX_HTTP_CREATED) { |
637 | 268 if (ngx_http_dav_location(r, path.data) != NGX_OK) { |
633 | 269 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); |
270 return; | |
271 } | |
639 | 272 |
273 r->headers_out.content_length_n = 0; | |
633 | 274 } |
275 | |
276 r->headers_out.status = status; | |
277 r->header_only = 1; | |
278 | |
279 ngx_http_finalize_request(r, ngx_http_send_header(r)); | |
280 return; | |
281 } | |
282 | |
283 | |
637 | 284 static ngx_int_t |
1116 | 285 ngx_http_dav_delete_handler(ngx_http_request_t *r) |
286 { | |
1816 | 287 size_t root; |
288 ngx_err_t err; | |
289 ngx_int_t rc, depth; | |
290 ngx_uint_t i, d, dir; | |
291 ngx_str_t path; | |
292 ngx_file_info_t fi; | |
293 ngx_http_dav_loc_conf_t *dlcf; | |
1116 | 294 |
295 if (r->headers_in.content_length_n > 0) { | |
1815 | 296 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
297 "DELETE with body is unsupported"); | |
1116 | 298 return NGX_HTTP_UNSUPPORTED_MEDIA_TYPE; |
299 } | |
300 | |
1816 | 301 dlcf = ngx_http_get_module_loc_conf(r, ngx_http_dav_module); |
302 | |
303 if (dlcf->min_delete_depth) { | |
304 d = 0; | |
305 | |
306 for (i = 0; i < r->uri.len; /* void */) { | |
307 if (r->uri.data[i++] == '/') { | |
308 if (++d >= dlcf->min_delete_depth && i < r->uri.len) { | |
309 goto ok; | |
310 } | |
311 } | |
312 } | |
313 | |
314 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
315 "insufficient URI depth:%i to DELETE", d); | |
316 return NGX_HTTP_CONFLICT; | |
317 } | |
318 | |
319 ok: | |
320 | |
1116 | 321 ngx_http_map_uri_to_path(r, &path, &root, 0); |
322 | |
323 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
324 "http delete filename: \"%s\"", path.data); | |
325 | |
2782
4bd7825fab80
uniform ngx_file_info() interface with ngx_fd_info()
Igor Sysoev <igor@sysoev.ru>
parents:
2721
diff
changeset
|
326 if (ngx_file_info(path.data, &fi) == NGX_FILE_ERROR) { |
1815 | 327 err = ngx_errno; |
328 | |
329 rc = (err == NGX_ENOTDIR) ? NGX_HTTP_CONFLICT : NGX_HTTP_NOT_FOUND; | |
330 | |
331 return ngx_http_dav_error(r->connection->log, err, | |
332 rc, ngx_file_info_n, path.data); | |
1116 | 333 } |
334 | |
335 if (ngx_is_dir(&fi)) { | |
336 | |
337 if (r->uri.data[r->uri.len - 1] != '/') { | |
1815 | 338 ngx_log_error(NGX_LOG_ERR, r->connection->log, NGX_EISDIR, |
339 "DELETE \"%s\" failed", path.data); | |
340 return NGX_HTTP_CONFLICT; | |
1116 | 341 } |
342 | |
343 depth = ngx_http_dav_depth(r, NGX_HTTP_DAV_INFINITY_DEPTH); | |
344 | |
345 if (depth != NGX_HTTP_DAV_INFINITY_DEPTH) { | |
1815 | 346 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
347 "\"Depth\" header must be infinity"); | |
1116 | 348 return NGX_HTTP_BAD_REQUEST; |
349 } | |
350 | |
351 path.len -= 2; /* omit "/\0" */ | |
352 | |
353 dir = 1; | |
354 | |
355 } else { | |
356 | |
1823 | 357 /* |
358 * we do not need to test (r->uri.data[r->uri.len - 1] == '/') | |
1831 | 359 * because ngx_file_info("/file/") returned NGX_ENOTDIR above |
1823 | 360 */ |
361 | |
1116 | 362 depth = ngx_http_dav_depth(r, 0); |
363 | |
364 if (depth != 0 && depth != NGX_HTTP_DAV_INFINITY_DEPTH) { | |
1815 | 365 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
366 "\"Depth\" header must be 0 or infinity"); | |
1116 | 367 return NGX_HTTP_BAD_REQUEST; |
368 } | |
369 | |
370 dir = 0; | |
371 } | |
372 | |
373 rc = ngx_http_dav_delete_path(r, &path, dir); | |
374 | |
375 if (rc == NGX_OK) { | |
376 return NGX_HTTP_NO_CONTENT; | |
377 } | |
378 | |
379 return rc; | |
380 } | |
381 | |
382 | |
383 static ngx_int_t | |
1820 | 384 ngx_http_dav_delete_path(ngx_http_request_t *r, ngx_str_t *path, ngx_uint_t dir) |
1116 | 385 { |
1820 | 386 char *failed; |
387 ngx_tree_ctx_t tree; | |
388 | |
389 if (dir) { | |
390 | |
391 tree.init_handler = NULL; | |
392 tree.file_handler = ngx_http_dav_delete_file; | |
393 tree.pre_tree_handler = ngx_http_dav_noop; | |
394 tree.post_tree_handler = ngx_http_dav_delete_dir; | |
395 tree.spec_handler = ngx_http_dav_delete_file; | |
396 tree.data = NULL; | |
397 tree.alloc = 0; | |
398 tree.log = r->connection->log; | |
399 | |
400 /* TODO: 207 */ | |
401 | |
402 if (ngx_walk_tree(&tree, path) != NGX_OK) { | |
403 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
404 } | |
405 | |
406 if (ngx_delete_dir(path->data) != NGX_FILE_ERROR) { | |
407 return NGX_OK; | |
408 } | |
409 | |
410 failed = ngx_delete_dir_n; | |
411 | |
412 } else { | |
413 | |
414 if (ngx_delete_file(path->data) != NGX_FILE_ERROR) { | |
415 return NGX_OK; | |
416 } | |
417 | |
418 failed = ngx_delete_file_n; | |
419 } | |
420 | |
421 return ngx_http_dav_error(r->connection->log, ngx_errno, | |
422 NGX_HTTP_NOT_FOUND, failed, path->data); | |
1116 | 423 } |
424 | |
425 | |
426 static ngx_int_t | |
427 ngx_http_dav_delete_dir(ngx_tree_ctx_t *ctx, ngx_str_t *path) | |
428 { | |
429 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0, | |
430 "http delete dir: \"%s\"", path->data); | |
431 | |
432 if (ngx_delete_dir(path->data) == NGX_FILE_ERROR) { | |
433 | |
434 /* TODO: add to 207 */ | |
435 | |
436 (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_delete_dir_n, | |
437 path->data); | |
438 } | |
439 | |
440 return NGX_OK; | |
441 } | |
442 | |
443 | |
444 static ngx_int_t | |
445 ngx_http_dav_delete_file(ngx_tree_ctx_t *ctx, ngx_str_t *path) | |
446 { | |
447 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0, | |
448 "http delete file: \"%s\"", path->data); | |
449 | |
450 if (ngx_delete_file(path->data) == NGX_FILE_ERROR) { | |
451 | |
452 /* TODO: add to 207 */ | |
453 | |
454 (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_delete_file_n, | |
455 path->data); | |
456 } | |
457 | |
458 return NGX_OK; | |
459 } | |
460 | |
461 | |
462 static ngx_int_t | |
1820 | 463 ngx_http_dav_noop(ngx_tree_ctx_t *ctx, ngx_str_t *path) |
464 { | |
465 return NGX_OK; | |
466 } | |
467 | |
468 | |
469 static ngx_int_t | |
1116 | 470 ngx_http_dav_mkcol_handler(ngx_http_request_t *r, ngx_http_dav_loc_conf_t *dlcf) |
471 { | |
1819
0f907513dd3c
change status code, add log message, and test collection URI
Igor Sysoev <igor@sysoev.ru>
parents:
1818
diff
changeset
|
472 u_char *p; |
1116 | 473 size_t root; |
474 ngx_str_t path; | |
475 | |
476 if (r->headers_in.content_length_n > 0) { | |
1819
0f907513dd3c
change status code, add log message, and test collection URI
Igor Sysoev <igor@sysoev.ru>
parents:
1818
diff
changeset
|
477 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
0f907513dd3c
change status code, add log message, and test collection URI
Igor Sysoev <igor@sysoev.ru>
parents:
1818
diff
changeset
|
478 "MKCOL with body is unsupported"); |
1116 | 479 return NGX_HTTP_UNSUPPORTED_MEDIA_TYPE; |
480 } | |
481 | |
1819
0f907513dd3c
change status code, add log message, and test collection URI
Igor Sysoev <igor@sysoev.ru>
parents:
1818
diff
changeset
|
482 if (r->uri.data[r->uri.len - 1] != '/') { |
0f907513dd3c
change status code, add log message, and test collection URI
Igor Sysoev <igor@sysoev.ru>
parents:
1818
diff
changeset
|
483 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
0f907513dd3c
change status code, add log message, and test collection URI
Igor Sysoev <igor@sysoev.ru>
parents:
1818
diff
changeset
|
484 "MKCOL can create a collection only"); |
0f907513dd3c
change status code, add log message, and test collection URI
Igor Sysoev <igor@sysoev.ru>
parents:
1818
diff
changeset
|
485 return NGX_HTTP_CONFLICT; |
1116 | 486 } |
487 | |
1819
0f907513dd3c
change status code, add log message, and test collection URI
Igor Sysoev <igor@sysoev.ru>
parents:
1818
diff
changeset
|
488 p = ngx_http_map_uri_to_path(r, &path, &root, 0); |
0f907513dd3c
change status code, add log message, and test collection URI
Igor Sysoev <igor@sysoev.ru>
parents:
1818
diff
changeset
|
489 |
0f907513dd3c
change status code, add log message, and test collection URI
Igor Sysoev <igor@sysoev.ru>
parents:
1818
diff
changeset
|
490 *(p - 1) = '\0'; |
1116 | 491 |
492 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
493 "http mkcol path: \"%s\"", path.data); | |
494 | |
495 if (ngx_create_dir(path.data, ngx_dir_access(dlcf->access)) | |
496 != NGX_FILE_ERROR) | |
497 { | |
498 if (ngx_http_dav_location(r, path.data) != NGX_OK) { | |
499 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
500 } | |
501 | |
502 return NGX_HTTP_CREATED; | |
503 } | |
504 | |
505 return ngx_http_dav_error(r->connection->log, ngx_errno, | |
1819
0f907513dd3c
change status code, add log message, and test collection URI
Igor Sysoev <igor@sysoev.ru>
parents:
1818
diff
changeset
|
506 NGX_HTTP_CONFLICT, ngx_create_dir_n, path.data); |
1116 | 507 } |
508 | |
509 | |
510 static ngx_int_t | |
511 ngx_http_dav_copy_move_handler(ngx_http_request_t *r) | |
512 { | |
2007
b9de93d804ea
*) host in request line has priority
Igor Sysoev <igor@sysoev.ru>
parents:
1909
diff
changeset
|
513 u_char *p, *host, *last, ch; |
1804
d457a1576532
several changes in server_name:
Igor Sysoev <igor@sysoev.ru>
parents:
1803
diff
changeset
|
514 size_t len, root; |
1116 | 515 ngx_err_t err; |
516 ngx_int_t rc, depth; | |
1825
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
517 ngx_uint_t overwrite, slash, dir; |
1116 | 518 ngx_str_t path, uri; |
519 ngx_tree_ctx_t tree; | |
3024
8101d9101ed8
allow cross device temporary files atomic copying:
Igor Sysoev <igor@sysoev.ru>
parents:
2912
diff
changeset
|
520 ngx_copy_file_t cf; |
1116 | 521 ngx_file_info_t fi; |
2007
b9de93d804ea
*) host in request line has priority
Igor Sysoev <igor@sysoev.ru>
parents:
1909
diff
changeset
|
522 ngx_table_elt_t *dest, *over; |
2394
dbe746851b31
use ngx_ext_rename_file() for single file MOVE
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
523 ngx_ext_rename_file_t ext; |
1116 | 524 ngx_http_dav_copy_ctx_t copy; |
1825
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
525 ngx_http_dav_loc_conf_t *dlcf; |
1116 | 526 |
527 if (r->headers_in.content_length_n > 0) { | |
528 return NGX_HTTP_UNSUPPORTED_MEDIA_TYPE; | |
529 } | |
530 | |
531 dest = r->headers_in.destination; | |
532 | |
533 if (dest == NULL) { | |
534 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
535 "client sent no \"Destination\" header"); | |
536 return NGX_HTTP_BAD_REQUEST; | |
537 } | |
538 | |
2007
b9de93d804ea
*) host in request line has priority
Igor Sysoev <igor@sysoev.ru>
parents:
1909
diff
changeset
|
539 len = r->headers_in.server.len; |
1804
d457a1576532
several changes in server_name:
Igor Sysoev <igor@sysoev.ru>
parents:
1803
diff
changeset
|
540 |
2007
b9de93d804ea
*) host in request line has priority
Igor Sysoev <igor@sysoev.ru>
parents:
1909
diff
changeset
|
541 if (len == 0) { |
1804
d457a1576532
several changes in server_name:
Igor Sysoev <igor@sysoev.ru>
parents:
1803
diff
changeset
|
542 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
d457a1576532
several changes in server_name:
Igor Sysoev <igor@sysoev.ru>
parents:
1803
diff
changeset
|
543 "client sent no \"Host\" header"); |
d457a1576532
several changes in server_name:
Igor Sysoev <igor@sysoev.ru>
parents:
1803
diff
changeset
|
544 return NGX_HTTP_BAD_REQUEST; |
1116 | 545 } |
546 | |
547 #if (NGX_HTTP_SSL) | |
548 | |
549 if (r->connection->ssl) { | |
550 if (ngx_strncmp(dest->value.data, "https://", sizeof("https://") - 1) | |
551 != 0) | |
552 { | |
553 goto invalid_destination; | |
554 } | |
555 | |
2007
b9de93d804ea
*) host in request line has priority
Igor Sysoev <igor@sysoev.ru>
parents:
1909
diff
changeset
|
556 host = dest->value.data + sizeof("https://") - 1; |
1116 | 557 |
558 } else | |
559 #endif | |
560 { | |
561 if (ngx_strncmp(dest->value.data, "http://", sizeof("http://") - 1) | |
562 != 0) | |
563 { | |
564 goto invalid_destination; | |
565 } | |
566 | |
2007
b9de93d804ea
*) host in request line has priority
Igor Sysoev <igor@sysoev.ru>
parents:
1909
diff
changeset
|
567 host = dest->value.data + sizeof("http://") - 1; |
1116 | 568 } |
569 | |
2007
b9de93d804ea
*) host in request line has priority
Igor Sysoev <igor@sysoev.ru>
parents:
1909
diff
changeset
|
570 if (ngx_strncmp(host, r->headers_in.server.data, len) != 0) { |
1116 | 571 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
1822
935427c55e04
test URI/Destination collection/non-collection and Depth
Igor Sysoev <igor@sysoev.ru>
parents:
1821
diff
changeset
|
572 "\"Destination\" URI \"%V\" is handled by " |
1116 | 573 "different repository than the source URI", |
574 &dest->value); | |
575 return NGX_HTTP_BAD_REQUEST; | |
576 } | |
577 | |
578 last = dest->value.data + dest->value.len; | |
579 | |
2007
b9de93d804ea
*) host in request line has priority
Igor Sysoev <igor@sysoev.ru>
parents:
1909
diff
changeset
|
580 for (p = host + len; p < last; p++) { |
1116 | 581 if (*p == '/') { |
582 goto destination_done; | |
583 } | |
584 } | |
585 | |
586 invalid_destination: | |
587 | |
588 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
589 "client sent invalid \"Destination\" header: \"%V\"", | |
590 &dest->value); | |
591 return NGX_HTTP_BAD_REQUEST; | |
592 | |
593 destination_done: | |
594 | |
1822
935427c55e04
test URI/Destination collection/non-collection and Depth
Igor Sysoev <igor@sysoev.ru>
parents:
1821
diff
changeset
|
595 if ((r->uri.data[r->uri.len - 1] == '/' && *(last - 1) != '/') |
935427c55e04
test URI/Destination collection/non-collection and Depth
Igor Sysoev <igor@sysoev.ru>
parents:
1821
diff
changeset
|
596 || (r->uri.data[r->uri.len - 1] != '/' && *(last - 1) == '/')) |
935427c55e04
test URI/Destination collection/non-collection and Depth
Igor Sysoev <igor@sysoev.ru>
parents:
1821
diff
changeset
|
597 { |
935427c55e04
test URI/Destination collection/non-collection and Depth
Igor Sysoev <igor@sysoev.ru>
parents:
1821
diff
changeset
|
598 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
935427c55e04
test URI/Destination collection/non-collection and Depth
Igor Sysoev <igor@sysoev.ru>
parents:
1821
diff
changeset
|
599 "both URI \"%V\" and \"Destination\" URI \"%V\" " |
1834
aa343f669f11
style fix: remove trailing space
Igor Sysoev <igor@sysoev.ru>
parents:
1831
diff
changeset
|
600 "should be either collections or non-collections", |
1822
935427c55e04
test URI/Destination collection/non-collection and Depth
Igor Sysoev <igor@sysoev.ru>
parents:
1821
diff
changeset
|
601 &r->uri, &dest->value); |
935427c55e04
test URI/Destination collection/non-collection and Depth
Igor Sysoev <igor@sysoev.ru>
parents:
1821
diff
changeset
|
602 return NGX_HTTP_CONFLICT; |
935427c55e04
test URI/Destination collection/non-collection and Depth
Igor Sysoev <igor@sysoev.ru>
parents:
1821
diff
changeset
|
603 } |
935427c55e04
test URI/Destination collection/non-collection and Depth
Igor Sysoev <igor@sysoev.ru>
parents:
1821
diff
changeset
|
604 |
935427c55e04
test URI/Destination collection/non-collection and Depth
Igor Sysoev <igor@sysoev.ru>
parents:
1821
diff
changeset
|
605 depth = ngx_http_dav_depth(r, NGX_HTTP_DAV_INFINITY_DEPTH); |
1116 | 606 |
1822
935427c55e04
test URI/Destination collection/non-collection and Depth
Igor Sysoev <igor@sysoev.ru>
parents:
1821
diff
changeset
|
607 if (depth != NGX_HTTP_DAV_INFINITY_DEPTH) { |
935427c55e04
test URI/Destination collection/non-collection and Depth
Igor Sysoev <igor@sysoev.ru>
parents:
1821
diff
changeset
|
608 |
935427c55e04
test URI/Destination collection/non-collection and Depth
Igor Sysoev <igor@sysoev.ru>
parents:
1821
diff
changeset
|
609 if (r->method == NGX_HTTP_COPY) { |
935427c55e04
test URI/Destination collection/non-collection and Depth
Igor Sysoev <igor@sysoev.ru>
parents:
1821
diff
changeset
|
610 if (depth != 0) { |
935427c55e04
test URI/Destination collection/non-collection and Depth
Igor Sysoev <igor@sysoev.ru>
parents:
1821
diff
changeset
|
611 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
935427c55e04
test URI/Destination collection/non-collection and Depth
Igor Sysoev <igor@sysoev.ru>
parents:
1821
diff
changeset
|
612 "\"Depth\" header must be 0 or infinity"); |
935427c55e04
test URI/Destination collection/non-collection and Depth
Igor Sysoev <igor@sysoev.ru>
parents:
1821
diff
changeset
|
613 return NGX_HTTP_BAD_REQUEST; |
935427c55e04
test URI/Destination collection/non-collection and Depth
Igor Sysoev <igor@sysoev.ru>
parents:
1821
diff
changeset
|
614 } |
935427c55e04
test URI/Destination collection/non-collection and Depth
Igor Sysoev <igor@sysoev.ru>
parents:
1821
diff
changeset
|
615 |
935427c55e04
test URI/Destination collection/non-collection and Depth
Igor Sysoev <igor@sysoev.ru>
parents:
1821
diff
changeset
|
616 } else { |
935427c55e04
test URI/Destination collection/non-collection and Depth
Igor Sysoev <igor@sysoev.ru>
parents:
1821
diff
changeset
|
617 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
935427c55e04
test URI/Destination collection/non-collection and Depth
Igor Sysoev <igor@sysoev.ru>
parents:
1821
diff
changeset
|
618 "\"Depth\" header must be infinity"); |
935427c55e04
test URI/Destination collection/non-collection and Depth
Igor Sysoev <igor@sysoev.ru>
parents:
1821
diff
changeset
|
619 return NGX_HTTP_BAD_REQUEST; |
935427c55e04
test URI/Destination collection/non-collection and Depth
Igor Sysoev <igor@sysoev.ru>
parents:
1821
diff
changeset
|
620 } |
1116 | 621 } |
1174
6be5ee17d80b
style fix: remove trailing spaces
Igor Sysoev <igor@sysoev.ru>
parents:
1116
diff
changeset
|
622 |
1116 | 623 over = r->headers_in.overwrite; |
624 | |
625 if (over) { | |
626 if (over->value.len == 1) { | |
627 ch = over->value.data[0]; | |
628 | |
629 if (ch == 'T' || ch == 't') { | |
630 overwrite = 1; | |
631 goto overwrite_done; | |
632 } | |
633 | |
634 if (ch == 'F' || ch == 'f') { | |
635 overwrite = 0; | |
636 goto overwrite_done; | |
637 } | |
638 | |
639 } | |
640 | |
641 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
642 "client sent invalid \"Overwrite\" header: \"%V\"", | |
643 &over->value); | |
644 return NGX_HTTP_BAD_REQUEST; | |
645 } | |
646 | |
647 overwrite = 1; | |
648 | |
649 overwrite_done: | |
650 | |
651 ngx_http_map_uri_to_path(r, &path, &root, 0); | |
652 | |
653 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
654 "http copy from: \"%s\"", path.data); | |
655 | |
656 uri = r->uri; | |
657 | |
658 r->uri.len = last - p; | |
659 r->uri.data = p; | |
660 | |
661 ngx_http_map_uri_to_path(r, ©.path, &root, 0); | |
662 | |
663 r->uri = uri; | |
664 | |
665 copy.path.len--; /* omit "\0" */ | |
666 | |
667 if (copy.path.data[copy.path.len - 1] == '/') { | |
668 slash = 1; | |
669 copy.path.len--; | |
670 copy.path.data[copy.path.len] = '\0'; | |
671 | |
672 } else { | |
673 slash = 0; | |
674 } | |
675 | |
676 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
677 "http copy to: \"%s\"", copy.path.data); | |
678 | |
2782
4bd7825fab80
uniform ngx_file_info() interface with ngx_fd_info()
Igor Sysoev <igor@sysoev.ru>
parents:
2721
diff
changeset
|
679 if (ngx_file_info(copy.path.data, &fi) == NGX_FILE_ERROR) { |
1116 | 680 err = ngx_errno; |
681 | |
682 if (err != NGX_ENOENT) { | |
683 return ngx_http_dav_error(r->connection->log, err, | |
684 NGX_HTTP_NOT_FOUND, ngx_file_info_n, | |
685 copy.path.data); | |
686 } | |
687 | |
688 /* destination does not exist */ | |
689 | |
1825
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
690 overwrite = 0; |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
691 dir = 0; |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
692 |
1116 | 693 } else { |
694 | |
695 /* destination exists */ | |
696 | |
697 if (ngx_is_dir(&fi) && !slash) { | |
1824 | 698 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
699 "\"%V\" could not be %Ved to collection \"%V\"", | |
700 &r->uri, &r->method_name, &dest->value); | |
1116 | 701 return NGX_HTTP_CONFLICT; |
702 } | |
703 | |
704 if (!overwrite) { | |
1824 | 705 ngx_log_error(NGX_LOG_ERR, r->connection->log, NGX_EEXIST, |
706 "\"%s\" could not be created", copy.path.data); | |
1116 | 707 return NGX_HTTP_PRECONDITION_FAILED; |
708 } | |
709 | |
1825
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
710 dir = ngx_is_dir(&fi); |
1116 | 711 } |
712 | |
2782
4bd7825fab80
uniform ngx_file_info() interface with ngx_fd_info()
Igor Sysoev <igor@sysoev.ru>
parents:
2721
diff
changeset
|
713 if (ngx_file_info(path.data, &fi) == NGX_FILE_ERROR) { |
1116 | 714 return ngx_http_dav_error(r->connection->log, ngx_errno, |
715 NGX_HTTP_NOT_FOUND, ngx_file_info_n, | |
716 path.data); | |
717 } | |
718 | |
719 if (ngx_is_dir(&fi)) { | |
720 | |
721 if (r->uri.data[r->uri.len - 1] != '/') { | |
1825
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
722 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
723 "\"%V\" is collection", &r->uri); |
1116 | 724 return NGX_HTTP_BAD_REQUEST; |
725 } | |
726 | |
1825
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
727 if (overwrite) { |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
728 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
729 "http delete: \"%s\"", copy.path.data); |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
730 |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
731 rc = ngx_http_dav_delete_path(r, ©.path, dir); |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
732 |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
733 if (rc != NGX_OK) { |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
734 return rc; |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
735 } |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
736 } |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
737 } |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
738 |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
739 if (ngx_is_dir(&fi)) { |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
740 |
1116 | 741 path.len -= 2; /* omit "/\0" */ |
742 | |
743 if (r->method == NGX_HTTP_MOVE) { | |
744 if (ngx_rename_file(path.data, copy.path.data) != NGX_FILE_ERROR) { | |
745 return NGX_HTTP_CREATED; | |
746 } | |
747 } | |
748 | |
749 if (ngx_create_dir(copy.path.data, ngx_file_access(&fi)) | |
750 == NGX_FILE_ERROR) | |
751 { | |
752 return ngx_http_dav_error(r->connection->log, ngx_errno, | |
753 NGX_HTTP_NOT_FOUND, | |
754 ngx_create_dir_n, copy.path.data); | |
755 } | |
756 | |
757 copy.len = path.len; | |
758 | |
1769
a35bc4007ec3
tree.init_handler is never called
Igor Sysoev <igor@sysoev.ru>
parents:
1749
diff
changeset
|
759 tree.init_handler = NULL; |
1825
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
760 tree.file_handler = ngx_http_dav_copy_tree_file; |
1116 | 761 tree.pre_tree_handler = ngx_http_dav_copy_dir; |
762 tree.post_tree_handler = ngx_http_dav_copy_dir_time; | |
763 tree.spec_handler = ngx_http_dav_noop; | |
764 tree.data = © | |
765 tree.alloc = 0; | |
766 tree.log = r->connection->log; | |
767 | |
768 if (ngx_walk_tree(&tree, &path) == NGX_OK) { | |
769 | |
770 if (r->method == NGX_HTTP_MOVE) { | |
771 rc = ngx_http_dav_delete_path(r, &path, 1); | |
772 | |
773 if (rc != NGX_OK) { | |
774 return rc; | |
775 } | |
776 } | |
777 | |
778 return NGX_HTTP_CREATED; | |
779 } | |
780 | |
781 } else { | |
782 | |
783 if (r->method == NGX_HTTP_MOVE) { | |
2394
dbe746851b31
use ngx_ext_rename_file() for single file MOVE
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
784 |
dbe746851b31
use ngx_ext_rename_file() for single file MOVE
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
785 dlcf = ngx_http_get_module_loc_conf(r, ngx_http_dav_module); |
dbe746851b31
use ngx_ext_rename_file() for single file MOVE
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
786 |
dbe746851b31
use ngx_ext_rename_file() for single file MOVE
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
787 ext.access = 0; |
dbe746851b31
use ngx_ext_rename_file() for single file MOVE
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
788 ext.path_access = dlcf->access; |
dbe746851b31
use ngx_ext_rename_file() for single file MOVE
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
789 ext.time = -1; |
dbe746851b31
use ngx_ext_rename_file() for single file MOVE
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
790 ext.create_path = 1; |
dbe746851b31
use ngx_ext_rename_file() for single file MOVE
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
791 ext.delete_file = 0; |
dbe746851b31
use ngx_ext_rename_file() for single file MOVE
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
792 ext.log = r->connection->log; |
dbe746851b31
use ngx_ext_rename_file() for single file MOVE
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
793 |
dbe746851b31
use ngx_ext_rename_file() for single file MOVE
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
794 if (ngx_ext_rename_file(&path, ©.path, &ext) == NGX_OK) { |
1116 | 795 return NGX_HTTP_NO_CONTENT; |
796 } | |
2394
dbe746851b31
use ngx_ext_rename_file() for single file MOVE
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
797 |
3024
8101d9101ed8
allow cross device temporary files atomic copying:
Igor Sysoev <igor@sysoev.ru>
parents:
2912
diff
changeset
|
798 return NGX_HTTP_INTERNAL_SERVER_ERROR; |
1116 | 799 } |
800 | |
1825
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
801 dlcf = ngx_http_get_module_loc_conf(r, ngx_http_dav_module); |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
802 |
3024
8101d9101ed8
allow cross device temporary files atomic copying:
Igor Sysoev <igor@sysoev.ru>
parents:
2912
diff
changeset
|
803 cf.size = ngx_file_size(&fi); |
8101d9101ed8
allow cross device temporary files atomic copying:
Igor Sysoev <igor@sysoev.ru>
parents:
2912
diff
changeset
|
804 cf.buf_size = 0; |
8101d9101ed8
allow cross device temporary files atomic copying:
Igor Sysoev <igor@sysoev.ru>
parents:
2912
diff
changeset
|
805 cf.access = dlcf->access; |
8101d9101ed8
allow cross device temporary files atomic copying:
Igor Sysoev <igor@sysoev.ru>
parents:
2912
diff
changeset
|
806 cf.time = ngx_file_mtime(&fi); |
8101d9101ed8
allow cross device temporary files atomic copying:
Igor Sysoev <igor@sysoev.ru>
parents:
2912
diff
changeset
|
807 cf.log = r->connection->log; |
1116 | 808 |
3024
8101d9101ed8
allow cross device temporary files atomic copying:
Igor Sysoev <igor@sysoev.ru>
parents:
2912
diff
changeset
|
809 if (ngx_copy_file(path.data, copy.path.data, &cf) == NGX_OK) { |
1116 | 810 return NGX_HTTP_NO_CONTENT; |
811 } | |
812 } | |
813 | |
814 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
815 } | |
816 | |
817 | |
818 static ngx_int_t | |
819 ngx_http_dav_copy_dir(ngx_tree_ctx_t *ctx, ngx_str_t *path) | |
820 { | |
821 u_char *p, *dir; | |
822 size_t len; | |
823 ngx_http_dav_copy_ctx_t *copy; | |
824 | |
825 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0, | |
826 "http copy dir: \"%s\"", path->data); | |
827 | |
828 copy = ctx->data; | |
829 | |
830 len = copy->path.len + path->len; | |
831 | |
832 dir = ngx_alloc(len + 1, ctx->log); | |
833 if (dir == NULL) { | |
834 return NGX_ABORT; | |
835 } | |
836 | |
837 p = ngx_cpymem(dir, copy->path.data, copy->path.len); | |
838 (void) ngx_cpystrn(p, path->data + copy->len, path->len - copy->len + 1); | |
839 | |
840 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0, | |
841 "http copy dir to: \"%s\"", dir); | |
842 | |
843 if (ngx_create_dir(dir, ngx_dir_access(ctx->access)) == NGX_FILE_ERROR) { | |
844 (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_create_dir_n, | |
845 dir); | |
846 } | |
847 | |
848 ngx_free(dir); | |
849 | |
850 return NGX_OK; | |
851 } | |
852 | |
853 | |
854 static ngx_int_t | |
855 ngx_http_dav_copy_dir_time(ngx_tree_ctx_t *ctx, ngx_str_t *path) | |
856 { | |
857 u_char *p, *dir; | |
858 size_t len; | |
859 ngx_http_dav_copy_ctx_t *copy; | |
860 | |
861 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0, | |
862 "http copy dir time: \"%s\"", path->data); | |
863 | |
864 copy = ctx->data; | |
865 | |
866 len = copy->path.len + path->len; | |
867 | |
868 dir = ngx_alloc(len + 1, ctx->log); | |
869 if (dir == NULL) { | |
870 return NGX_ABORT; | |
871 } | |
872 | |
873 p = ngx_cpymem(dir, copy->path.data, copy->path.len); | |
874 (void) ngx_cpystrn(p, path->data + copy->len, path->len - copy->len + 1); | |
875 | |
876 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0, | |
877 "http copy dir time to: \"%s\"", dir); | |
878 | |
1803
7405719e4848
condition declarations inside blocks update missed in r1705
Igor Sysoev <igor@sysoev.ru>
parents:
1769
diff
changeset
|
879 #if (NGX_WIN32) |
7405719e4848
condition declarations inside blocks update missed in r1705
Igor Sysoev <igor@sysoev.ru>
parents:
1769
diff
changeset
|
880 { |
7405719e4848
condition declarations inside blocks update missed in r1705
Igor Sysoev <igor@sysoev.ru>
parents:
1769
diff
changeset
|
881 ngx_fd_t fd; |
1116 | 882 |
883 fd = ngx_open_file(dir, NGX_FILE_RDWR, NGX_FILE_OPEN, 0); | |
884 | |
885 if (fd == NGX_INVALID_FILE) { | |
886 (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_open_file_n, dir); | |
887 goto failed; | |
888 } | |
889 | |
890 if (ngx_set_file_time(NULL, fd, ctx->mtime) != NGX_OK) { | |
891 ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno, | |
892 ngx_set_file_time_n " \"%s\" failed", dir); | |
893 } | |
894 | |
895 if (ngx_close_file(fd) == NGX_FILE_ERROR) { | |
896 ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno, | |
897 ngx_close_file_n " \"%s\" failed", dir); | |
898 } | |
1803
7405719e4848
condition declarations inside blocks update missed in r1705
Igor Sysoev <igor@sysoev.ru>
parents:
1769
diff
changeset
|
899 } |
1116 | 900 |
901 failed: | |
902 | |
903 #else | |
904 | |
905 if (ngx_set_file_time(dir, 0, ctx->mtime) != NGX_OK) { | |
906 ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno, | |
907 ngx_set_file_time_n " \"%s\" failed", dir); | |
908 } | |
909 | |
910 #endif | |
911 | |
912 ngx_free(dir); | |
913 | |
914 return NGX_OK; | |
915 } | |
916 | |
917 | |
918 static ngx_int_t | |
1825
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
919 ngx_http_dav_copy_tree_file(ngx_tree_ctx_t *ctx, ngx_str_t *path) |
1116 | 920 { |
921 u_char *p, *file; | |
922 size_t len; | |
3024
8101d9101ed8
allow cross device temporary files atomic copying:
Igor Sysoev <igor@sysoev.ru>
parents:
2912
diff
changeset
|
923 ngx_copy_file_t cf; |
1116 | 924 ngx_http_dav_copy_ctx_t *copy; |
925 | |
926 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0, | |
927 "http copy file: \"%s\"", path->data); | |
928 | |
929 copy = ctx->data; | |
930 | |
931 len = copy->path.len + path->len; | |
932 | |
933 file = ngx_alloc(len + 1, ctx->log); | |
934 if (file == NULL) { | |
935 return NGX_ABORT; | |
936 } | |
937 | |
938 p = ngx_cpymem(file, copy->path.data, copy->path.len); | |
939 (void) ngx_cpystrn(p, path->data + copy->len, path->len - copy->len + 1); | |
940 | |
941 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0, | |
942 "http copy file to: \"%s\"", file); | |
943 | |
3024
8101d9101ed8
allow cross device temporary files atomic copying:
Igor Sysoev <igor@sysoev.ru>
parents:
2912
diff
changeset
|
944 cf.size = ctx->size; |
8101d9101ed8
allow cross device temporary files atomic copying:
Igor Sysoev <igor@sysoev.ru>
parents:
2912
diff
changeset
|
945 cf.buf_size = 0; |
8101d9101ed8
allow cross device temporary files atomic copying:
Igor Sysoev <igor@sysoev.ru>
parents:
2912
diff
changeset
|
946 cf.access = ctx->access; |
8101d9101ed8
allow cross device temporary files atomic copying:
Igor Sysoev <igor@sysoev.ru>
parents:
2912
diff
changeset
|
947 cf.time = ctx->mtime; |
8101d9101ed8
allow cross device temporary files atomic copying:
Igor Sysoev <igor@sysoev.ru>
parents:
2912
diff
changeset
|
948 cf.log = ctx->log; |
8101d9101ed8
allow cross device temporary files atomic copying:
Igor Sysoev <igor@sysoev.ru>
parents:
2912
diff
changeset
|
949 |
8101d9101ed8
allow cross device temporary files atomic copying:
Igor Sysoev <igor@sysoev.ru>
parents:
2912
diff
changeset
|
950 (void) ngx_copy_file(path->data, file, &cf); |
1825
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
951 |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
952 ngx_free(file); |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
953 |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
954 return NGX_OK; |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
955 } |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
956 |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
957 |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
958 static ngx_int_t |
1116 | 959 ngx_http_dav_depth(ngx_http_request_t *r, ngx_int_t dflt) |
960 { | |
961 ngx_table_elt_t *depth; | |
1174
6be5ee17d80b
style fix: remove trailing spaces
Igor Sysoev <igor@sysoev.ru>
parents:
1116
diff
changeset
|
962 |
1116 | 963 depth = r->headers_in.depth; |
964 | |
965 if (depth == NULL) { | |
966 return dflt; | |
967 } | |
968 | |
969 if (depth->value.len == 1) { | |
970 | |
971 if (depth->value.data[0] == '0') { | |
972 return 0; | |
973 } | |
974 | |
975 if (depth->value.data[0] == '1') { | |
976 return 1; | |
977 } | |
978 | |
979 } else { | |
980 | |
981 if (depth->value.len == sizeof("infinity") - 1 | |
982 && ngx_strcmp(depth->value.data, "infinity") == 0) | |
983 { | |
984 return NGX_HTTP_DAV_INFINITY_DEPTH; | |
985 } | |
986 } | |
987 | |
988 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
989 "client sent invalid \"Depth\" header: \"%V\"", | |
990 &depth->value); | |
991 | |
992 return NGX_HTTP_DAV_INVALID_DEPTH; | |
993 } | |
994 | |
995 | |
996 static ngx_int_t | |
997 ngx_http_dav_error(ngx_log_t *log, ngx_err_t err, ngx_int_t not_found, | |
637 | 998 char *failed, u_char *path) |
999 { | |
1000 ngx_int_t rc; | |
1001 ngx_uint_t level; | |
1002 | |
1003 if (err == NGX_ENOENT || err == NGX_ENOTDIR || err == NGX_ENAMETOOLONG) { | |
1004 level = NGX_LOG_ERR; | |
1005 rc = not_found; | |
1006 | |
1007 } else if (err == NGX_EACCES || err == NGX_EPERM) { | |
1008 level = NGX_LOG_ERR; | |
1009 rc = NGX_HTTP_FORBIDDEN; | |
1010 | |
1011 } else if (err == NGX_EEXIST) { | |
1012 level = NGX_LOG_ERR; | |
1013 rc = NGX_HTTP_NOT_ALLOWED; | |
1014 | |
1015 } else if (err == NGX_ENOSPC) { | |
1016 level = NGX_LOG_CRIT; | |
1017 rc = NGX_HTTP_INSUFFICIENT_STORAGE; | |
1018 | |
1019 } else { | |
1020 level = NGX_LOG_CRIT; | |
1021 rc = NGX_HTTP_INTERNAL_SERVER_ERROR; | |
1022 } | |
1023 | |
1116 | 1024 ngx_log_error(level, log, err, "%s \"%s\" failed", failed, path); |
637 | 1025 |
1026 return rc; | |
1027 } | |
1028 | |
1029 | |
1030 static ngx_int_t | |
1031 ngx_http_dav_location(ngx_http_request_t *r, u_char *path) | |
1032 { | |
1033 u_char *location; | |
1034 ngx_http_core_loc_conf_t *clcf; | |
1035 | |
1036 r->headers_out.location = ngx_palloc(r->pool, sizeof(ngx_table_elt_t)); | |
1037 if (r->headers_out.location == NULL) { | |
1038 return NGX_ERROR; | |
1039 } | |
1040 | |
1041 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); | |
1042 | |
1043 if (!clcf->alias && clcf->root_lengths == NULL) { | |
1044 location = path + clcf->root.len; | |
1045 | |
1046 } else { | |
2049 | 1047 location = ngx_pnalloc(r->pool, r->uri.len); |
637 | 1048 if (location == NULL) { |
1049 return NGX_ERROR; | |
1050 } | |
1051 | |
1052 ngx_memcpy(location, r->uri.data, r->uri.len); | |
1053 } | |
1054 | |
1055 /* | |
1056 * we do not need to set the r->headers_out.location->hash and | |
1057 * r->headers_out.location->key fields | |
1058 */ | |
1059 | |
1060 r->headers_out.location->value.len = r->uri.len; | |
1061 r->headers_out.location->value.data = location; | |
1062 | |
1063 return NGX_OK; | |
1064 } | |
1065 | |
1066 | |
633 | 1067 static void * |
1068 ngx_http_dav_create_loc_conf(ngx_conf_t *cf) | |
1069 { | |
1070 ngx_http_dav_loc_conf_t *conf; | |
1071 | |
1072 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_dav_loc_conf_t)); | |
1073 if (conf == NULL) { | |
2912
c7d57b539248
return NULL instead of NGX_CONF_ERROR on a create conf failure
Igor Sysoev <igor@sysoev.ru>
parents:
2782
diff
changeset
|
1074 return NULL; |
633 | 1075 } |
1076 | |
1077 /* | |
1078 * set by ngx_pcalloc(): | |
1079 * | |
1080 * conf->methods = 0; | |
1081 */ | |
1082 | |
1864
524831ec3e69
fix building by msvc introduced in r1817
Igor Sysoev <igor@sysoev.ru>
parents:
1834
diff
changeset
|
1083 conf->min_delete_depth = NGX_CONF_UNSET_UINT; |
1816 | 1084 conf->access = NGX_CONF_UNSET_UINT; |
637 | 1085 conf->create_full_put_path = NGX_CONF_UNSET; |
1086 | |
633 | 1087 return conf; |
1088 } | |
1089 | |
1090 | |
1091 static char * | |
1092 ngx_http_dav_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) | |
1093 { | |
1094 ngx_http_dav_loc_conf_t *prev = parent; | |
1095 ngx_http_dav_loc_conf_t *conf = child; | |
1096 | |
1097 ngx_conf_merge_bitmask_value(conf->methods, prev->methods, | |
637 | 1098 (NGX_CONF_BITMASK_SET|NGX_HTTP_DAV_OFF)); |
1099 | |
1816 | 1100 ngx_conf_merge_uint_value(conf->min_delete_depth, |
1101 prev->min_delete_depth, 0); | |
633 | 1102 |
669 | 1103 ngx_conf_merge_uint_value(conf->access, prev->access, 0600); |
1104 | |
1816 | 1105 ngx_conf_merge_value(conf->create_full_put_path, |
1106 prev->create_full_put_path, 0); | |
1107 | |
633 | 1108 return NGX_CONF_OK; |
1109 } | |
1110 | |
1111 | |
1112 static ngx_int_t | |
681 | 1113 ngx_http_dav_init(ngx_conf_t *cf) |
633 | 1114 { |
1115 ngx_http_handler_pt *h; | |
1116 ngx_http_core_main_conf_t *cmcf; | |
1117 | |
681 | 1118 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module); |
633 | 1119 |
1120 h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers); | |
1121 if (h == NULL) { | |
1122 return NGX_ERROR; | |
1123 } | |
1124 | |
1125 *h = ngx_http_dav_handler; | |
1126 | |
1127 return NGX_OK; | |
1128 } |