Mercurial > hg > nginx-quic
annotate src/http/modules/ngx_http_dav_module.c @ 2235:03180d4e5af2
ngx_http_random_index_module
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Fri, 05 Sep 2008 15:45:55 +0000 |
parents | 2a92804f4109 |
children | dbe746851b31 |
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); |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
56 static ngx_int_t ngx_http_dav_copy_file(ngx_tree_ctx_t *ctx, u_char *from, |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
57 u_char *to); |
1116 | 58 |
59 static ngx_int_t ngx_http_dav_depth(ngx_http_request_t *r, ngx_int_t dflt); | |
60 static ngx_int_t ngx_http_dav_error(ngx_log_t *log, ngx_err_t err, | |
637 | 61 ngx_int_t not_found, char *failed, u_char *path); |
62 static ngx_int_t ngx_http_dav_location(ngx_http_request_t *r, u_char *path); | |
633 | 63 static void *ngx_http_dav_create_loc_conf(ngx_conf_t *cf); |
64 static char *ngx_http_dav_merge_loc_conf(ngx_conf_t *cf, | |
65 void *parent, void *child); | |
681 | 66 static ngx_int_t ngx_http_dav_init(ngx_conf_t *cf); |
633 | 67 |
68 | |
69 static ngx_conf_bitmask_t ngx_http_dav_methods_mask[] = { | |
70 { ngx_string("off"), NGX_HTTP_DAV_OFF }, | |
71 { ngx_string("put"), NGX_HTTP_PUT }, | |
72 { ngx_string("delete"), NGX_HTTP_DELETE }, | |
637 | 73 { ngx_string("mkcol"), NGX_HTTP_MKCOL }, |
1116 | 74 { ngx_string("copy"), NGX_HTTP_COPY }, |
75 { ngx_string("move"), NGX_HTTP_MOVE }, | |
633 | 76 { ngx_null_string, 0 } |
77 }; | |
78 | |
79 | |
80 static ngx_command_t ngx_http_dav_commands[] = { | |
81 | |
82 { ngx_string("dav_methods"), | |
83 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE, | |
84 ngx_conf_set_bitmask_slot, | |
85 NGX_HTTP_LOC_CONF_OFFSET, | |
86 offsetof(ngx_http_dav_loc_conf_t, methods), | |
87 &ngx_http_dav_methods_mask }, | |
88 | |
637 | 89 { ngx_string("create_full_put_path"), |
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_dav_loc_conf_t, create_full_put_path), | |
94 NULL }, | |
95 | |
1816 | 96 { ngx_string("min_delete_depth"), |
97 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
98 ngx_conf_set_num_slot, | |
99 NGX_HTTP_LOC_CONF_OFFSET, | |
100 offsetof(ngx_http_dav_loc_conf_t, min_delete_depth), | |
101 NULL }, | |
102 | |
669 | 103 { ngx_string("dav_access"), |
675 | 104 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
|
105 ngx_conf_set_access_slot, |
669 | 106 NGX_HTTP_LOC_CONF_OFFSET, |
1296
9fcfca54b208
dav_access uses ngx_conf_set_access_slot()
Igor Sysoev <igor@sysoev.ru>
parents:
1289
diff
changeset
|
107 offsetof(ngx_http_dav_loc_conf_t, access), |
669 | 108 NULL }, |
109 | |
633 | 110 ngx_null_command |
111 }; | |
112 | |
113 | |
667 | 114 static ngx_http_module_t ngx_http_dav_module_ctx = { |
633 | 115 NULL, /* preconfiguration */ |
681 | 116 ngx_http_dav_init, /* postconfiguration */ |
633 | 117 |
118 NULL, /* create main configuration */ | |
119 NULL, /* init main configuration */ | |
120 | |
121 NULL, /* create server configuration */ | |
122 NULL, /* merge server configuration */ | |
123 | |
124 ngx_http_dav_create_loc_conf, /* create location configuration */ | |
125 ngx_http_dav_merge_loc_conf /* merge location configuration */ | |
126 }; | |
127 | |
128 | |
129 ngx_module_t ngx_http_dav_module = { | |
130 NGX_MODULE_V1, | |
131 &ngx_http_dav_module_ctx, /* module context */ | |
132 ngx_http_dav_commands, /* module directives */ | |
133 NGX_HTTP_MODULE, /* module type */ | |
134 NULL, /* init master */ | |
681 | 135 NULL, /* init module */ |
633 | 136 NULL, /* init process */ |
137 NULL, /* init thread */ | |
138 NULL, /* exit thread */ | |
139 NULL, /* exit process */ | |
140 NULL, /* exit master */ | |
141 NGX_MODULE_V1_PADDING | |
142 }; | |
143 | |
144 | |
145 static ngx_int_t | |
146 ngx_http_dav_handler(ngx_http_request_t *r) | |
147 { | |
148 ngx_int_t rc; | |
149 ngx_http_dav_loc_conf_t *dlcf; | |
150 | |
151 /* TODO: Win32 */ | |
152 if (r->zero_in_uri) { | |
153 return NGX_DECLINED; | |
154 } | |
155 | |
156 dlcf = ngx_http_get_module_loc_conf(r, ngx_http_dav_module); | |
157 | |
158 if (!(r->method & dlcf->methods)) { | |
159 return NGX_DECLINED; | |
160 } | |
161 | |
162 switch (r->method) { | |
163 | |
164 case NGX_HTTP_PUT: | |
165 | |
166 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
|
167 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
|
168 "can not PUT to a collection"); |
d3f80e0be8fa
change status code and add log message
Igor Sysoev <igor@sysoev.ru>
parents:
1804
diff
changeset
|
169 return NGX_HTTP_CONFLICT; |
633 | 170 } |
171 | |
172 r->request_body_in_file_only = 1; | |
173 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
|
174 r->request_body_in_clean_file = 1; |
633 | 175 r->request_body_file_group_access = 1; |
637 | 176 r->request_body_file_log_level = 0; |
633 | 177 |
178 rc = ngx_http_read_client_request_body(r, ngx_http_dav_put_handler); | |
179 | |
180 if (rc >= NGX_HTTP_SPECIAL_RESPONSE) { | |
181 return rc; | |
182 } | |
183 | |
184 return NGX_DONE; | |
185 | |
186 case NGX_HTTP_DELETE: | |
187 | |
1116 | 188 return ngx_http_dav_delete_handler(r); |
637 | 189 |
190 case NGX_HTTP_MKCOL: | |
191 | |
1116 | 192 return ngx_http_dav_mkcol_handler(r, dlcf); |
637 | 193 |
1116 | 194 case NGX_HTTP_COPY: |
637 | 195 |
1116 | 196 return ngx_http_dav_copy_move_handler(r); |
637 | 197 |
1116 | 198 case NGX_HTTP_MOVE: |
637 | 199 |
1116 | 200 return ngx_http_dav_copy_move_handler(r); |
633 | 201 } |
202 | |
203 return NGX_DECLINED; | |
204 } | |
205 | |
206 | |
207 static void | |
208 ngx_http_dav_put_handler(ngx_http_request_t *r) | |
209 { | |
773 | 210 size_t root; |
681 | 211 time_t date; |
637 | 212 ngx_str_t *temp, path; |
1828
1f3c2b6607e0
use ngx_ext_rename_file() in PUT
Igor Sysoev <igor@sysoev.ru>
parents:
1825
diff
changeset
|
213 ngx_uint_t status; |
637 | 214 ngx_file_info_t fi; |
1828
1f3c2b6607e0
use ngx_ext_rename_file() in PUT
Igor Sysoev <igor@sysoev.ru>
parents:
1825
diff
changeset
|
215 ngx_ext_rename_file_t ext; |
637 | 216 ngx_http_dav_loc_conf_t *dlcf; |
633 | 217 |
773 | 218 ngx_http_map_uri_to_path(r, &path, &root, 0); |
633 | 219 |
220 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
221 "http put filename: \"%s\"", path.data); | |
222 | |
223 temp = &r->request_body->temp_file->file.name; | |
224 | |
225 if (ngx_file_info(path.data, &fi) == -1) { | |
226 status = NGX_HTTP_CREATED; | |
227 | |
228 } else { | |
229 status = NGX_HTTP_NO_CONTENT; | |
663 | 230 |
231 if (ngx_is_dir(&fi)) { | |
232 ngx_log_error(NGX_LOG_ERR, r->connection->log, NGX_EISDIR, | |
233 "\"%s\" could not be created", path.data); | |
633 | 234 |
663 | 235 if (ngx_delete_file(temp->data) == NGX_FILE_ERROR) { |
236 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno, | |
237 ngx_delete_file_n " \"%s\" failed", | |
238 temp->data); | |
239 } | |
240 | |
241 ngx_http_finalize_request(r, NGX_HTTP_CONFLICT); | |
242 return; | |
661 | 243 } |
637 | 244 } |
245 | |
669 | 246 dlcf = ngx_http_get_module_loc_conf(r, ngx_http_dav_module); |
247 | |
1828
1f3c2b6607e0
use ngx_ext_rename_file() in PUT
Igor Sysoev <igor@sysoev.ru>
parents:
1825
diff
changeset
|
248 ext.access = dlcf->access; |
1f3c2b6607e0
use ngx_ext_rename_file() in PUT
Igor Sysoev <igor@sysoev.ru>
parents:
1825
diff
changeset
|
249 ext.time = -1; |
1f3c2b6607e0
use ngx_ext_rename_file() in PUT
Igor Sysoev <igor@sysoev.ru>
parents:
1825
diff
changeset
|
250 ext.create_path = dlcf->create_full_put_path; |
1909 | 251 ext.delete_file = 1; |
1828
1f3c2b6607e0
use ngx_ext_rename_file() in PUT
Igor Sysoev <igor@sysoev.ru>
parents:
1825
diff
changeset
|
252 ext.log = r->connection->log; |
669 | 253 |
681 | 254 if (r->headers_in.date) { |
255 date = ngx_http_parse_time(r->headers_in.date->value.data, | |
256 r->headers_in.date->value.len); | |
257 | |
258 if (date != NGX_ERROR) { | |
1828
1f3c2b6607e0
use ngx_ext_rename_file() in PUT
Igor Sysoev <igor@sysoev.ru>
parents:
1825
diff
changeset
|
259 ext.time = date; |
1f3c2b6607e0
use ngx_ext_rename_file() in PUT
Igor Sysoev <igor@sysoev.ru>
parents:
1825
diff
changeset
|
260 ext.fd = r->request_body->temp_file->file.fd; |
681 | 261 } |
262 } | |
263 | |
1828
1f3c2b6607e0
use ngx_ext_rename_file() in PUT
Igor Sysoev <igor@sysoev.ru>
parents:
1825
diff
changeset
|
264 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
|
265 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
|
266 return; |
637 | 267 } |
268 | |
633 | 269 if (status == NGX_HTTP_CREATED) { |
637 | 270 if (ngx_http_dav_location(r, path.data) != NGX_OK) { |
633 | 271 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); |
272 return; | |
273 } | |
639 | 274 |
275 r->headers_out.content_length_n = 0; | |
633 | 276 } |
277 | |
278 r->headers_out.status = status; | |
279 r->header_only = 1; | |
280 | |
281 ngx_http_finalize_request(r, ngx_http_send_header(r)); | |
282 return; | |
283 } | |
284 | |
285 | |
637 | 286 static ngx_int_t |
1116 | 287 ngx_http_dav_delete_handler(ngx_http_request_t *r) |
288 { | |
1816 | 289 size_t root; |
290 ngx_err_t err; | |
291 ngx_int_t rc, depth; | |
292 ngx_uint_t i, d, dir; | |
293 ngx_str_t path; | |
294 ngx_file_info_t fi; | |
295 ngx_http_dav_loc_conf_t *dlcf; | |
1116 | 296 |
297 if (r->headers_in.content_length_n > 0) { | |
1815 | 298 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
299 "DELETE with body is unsupported"); | |
1116 | 300 return NGX_HTTP_UNSUPPORTED_MEDIA_TYPE; |
301 } | |
302 | |
1816 | 303 dlcf = ngx_http_get_module_loc_conf(r, ngx_http_dav_module); |
304 | |
305 if (dlcf->min_delete_depth) { | |
306 d = 0; | |
307 | |
308 for (i = 0; i < r->uri.len; /* void */) { | |
309 if (r->uri.data[i++] == '/') { | |
310 if (++d >= dlcf->min_delete_depth && i < r->uri.len) { | |
311 goto ok; | |
312 } | |
313 } | |
314 } | |
315 | |
316 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
317 "insufficient URI depth:%i to DELETE", d); | |
318 return NGX_HTTP_CONFLICT; | |
319 } | |
320 | |
321 ok: | |
322 | |
1116 | 323 ngx_http_map_uri_to_path(r, &path, &root, 0); |
324 | |
325 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
326 "http delete filename: \"%s\"", path.data); | |
327 | |
328 if (ngx_file_info(path.data, &fi) == -1) { | |
1815 | 329 err = ngx_errno; |
330 | |
331 rc = (err == NGX_ENOTDIR) ? NGX_HTTP_CONFLICT : NGX_HTTP_NOT_FOUND; | |
332 | |
333 return ngx_http_dav_error(r->connection->log, err, | |
334 rc, ngx_file_info_n, path.data); | |
1116 | 335 } |
336 | |
337 if (ngx_is_dir(&fi)) { | |
338 | |
339 if (r->uri.data[r->uri.len - 1] != '/') { | |
1815 | 340 ngx_log_error(NGX_LOG_ERR, r->connection->log, NGX_EISDIR, |
341 "DELETE \"%s\" failed", path.data); | |
342 return NGX_HTTP_CONFLICT; | |
1116 | 343 } |
344 | |
345 depth = ngx_http_dav_depth(r, NGX_HTTP_DAV_INFINITY_DEPTH); | |
346 | |
347 if (depth != NGX_HTTP_DAV_INFINITY_DEPTH) { | |
1815 | 348 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
349 "\"Depth\" header must be infinity"); | |
1116 | 350 return NGX_HTTP_BAD_REQUEST; |
351 } | |
352 | |
353 path.len -= 2; /* omit "/\0" */ | |
354 | |
355 dir = 1; | |
356 | |
357 } else { | |
358 | |
1823 | 359 /* |
360 * we do not need to test (r->uri.data[r->uri.len - 1] == '/') | |
1831 | 361 * because ngx_file_info("/file/") returned NGX_ENOTDIR above |
1823 | 362 */ |
363 | |
1116 | 364 depth = ngx_http_dav_depth(r, 0); |
365 | |
366 if (depth != 0 && depth != NGX_HTTP_DAV_INFINITY_DEPTH) { | |
1815 | 367 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
368 "\"Depth\" header must be 0 or infinity"); | |
1116 | 369 return NGX_HTTP_BAD_REQUEST; |
370 } | |
371 | |
372 dir = 0; | |
373 } | |
374 | |
375 rc = ngx_http_dav_delete_path(r, &path, dir); | |
376 | |
377 if (rc == NGX_OK) { | |
378 return NGX_HTTP_NO_CONTENT; | |
379 } | |
380 | |
381 return rc; | |
382 } | |
383 | |
384 | |
385 static ngx_int_t | |
1820 | 386 ngx_http_dav_delete_path(ngx_http_request_t *r, ngx_str_t *path, ngx_uint_t dir) |
1116 | 387 { |
1820 | 388 char *failed; |
389 ngx_tree_ctx_t tree; | |
390 | |
391 if (dir) { | |
392 | |
393 tree.init_handler = NULL; | |
394 tree.file_handler = ngx_http_dav_delete_file; | |
395 tree.pre_tree_handler = ngx_http_dav_noop; | |
396 tree.post_tree_handler = ngx_http_dav_delete_dir; | |
397 tree.spec_handler = ngx_http_dav_delete_file; | |
398 tree.data = NULL; | |
399 tree.alloc = 0; | |
400 tree.log = r->connection->log; | |
401 | |
402 /* TODO: 207 */ | |
403 | |
404 if (ngx_walk_tree(&tree, path) != NGX_OK) { | |
405 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
406 } | |
407 | |
408 if (ngx_delete_dir(path->data) != NGX_FILE_ERROR) { | |
409 return NGX_OK; | |
410 } | |
411 | |
412 failed = ngx_delete_dir_n; | |
413 | |
414 } else { | |
415 | |
416 if (ngx_delete_file(path->data) != NGX_FILE_ERROR) { | |
417 return NGX_OK; | |
418 } | |
419 | |
420 failed = ngx_delete_file_n; | |
421 } | |
422 | |
423 return ngx_http_dav_error(r->connection->log, ngx_errno, | |
424 NGX_HTTP_NOT_FOUND, failed, path->data); | |
1116 | 425 } |
426 | |
427 | |
428 static ngx_int_t | |
429 ngx_http_dav_delete_dir(ngx_tree_ctx_t *ctx, ngx_str_t *path) | |
430 { | |
431 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0, | |
432 "http delete dir: \"%s\"", path->data); | |
433 | |
434 if (ngx_delete_dir(path->data) == NGX_FILE_ERROR) { | |
435 | |
436 /* TODO: add to 207 */ | |
437 | |
438 (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_delete_dir_n, | |
439 path->data); | |
440 } | |
441 | |
442 return NGX_OK; | |
443 } | |
444 | |
445 | |
446 static ngx_int_t | |
447 ngx_http_dav_delete_file(ngx_tree_ctx_t *ctx, ngx_str_t *path) | |
448 { | |
449 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0, | |
450 "http delete file: \"%s\"", path->data); | |
451 | |
452 if (ngx_delete_file(path->data) == NGX_FILE_ERROR) { | |
453 | |
454 /* TODO: add to 207 */ | |
455 | |
456 (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_delete_file_n, | |
457 path->data); | |
458 } | |
459 | |
460 return NGX_OK; | |
461 } | |
462 | |
463 | |
464 static ngx_int_t | |
1820 | 465 ngx_http_dav_noop(ngx_tree_ctx_t *ctx, ngx_str_t *path) |
466 { | |
467 return NGX_OK; | |
468 } | |
469 | |
470 | |
471 static ngx_int_t | |
1116 | 472 ngx_http_dav_mkcol_handler(ngx_http_request_t *r, ngx_http_dav_loc_conf_t *dlcf) |
473 { | |
1819
0f907513dd3c
change status code, add log message, and test collection URI
Igor Sysoev <igor@sysoev.ru>
parents:
1818
diff
changeset
|
474 u_char *p; |
1116 | 475 size_t root; |
476 ngx_str_t path; | |
477 | |
478 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
|
479 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
|
480 "MKCOL with body is unsupported"); |
1116 | 481 return NGX_HTTP_UNSUPPORTED_MEDIA_TYPE; |
482 } | |
483 | |
1819
0f907513dd3c
change status code, add log message, and test collection URI
Igor Sysoev <igor@sysoev.ru>
parents:
1818
diff
changeset
|
484 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
|
485 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
|
486 "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
|
487 return NGX_HTTP_CONFLICT; |
1116 | 488 } |
489 | |
1819
0f907513dd3c
change status code, add log message, and test collection URI
Igor Sysoev <igor@sysoev.ru>
parents:
1818
diff
changeset
|
490 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
|
491 |
0f907513dd3c
change status code, add log message, and test collection URI
Igor Sysoev <igor@sysoev.ru>
parents:
1818
diff
changeset
|
492 *(p - 1) = '\0'; |
1116 | 493 |
494 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
495 "http mkcol path: \"%s\"", path.data); | |
496 | |
497 if (ngx_create_dir(path.data, ngx_dir_access(dlcf->access)) | |
498 != NGX_FILE_ERROR) | |
499 { | |
500 if (ngx_http_dav_location(r, path.data) != NGX_OK) { | |
501 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
502 } | |
503 | |
504 return NGX_HTTP_CREATED; | |
505 } | |
506 | |
507 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
|
508 NGX_HTTP_CONFLICT, ngx_create_dir_n, path.data); |
1116 | 509 } |
510 | |
511 | |
512 static ngx_int_t | |
513 ngx_http_dav_copy_move_handler(ngx_http_request_t *r) | |
514 { | |
2007
b9de93d804ea
*) host in request line has priority
Igor Sysoev <igor@sysoev.ru>
parents:
1909
diff
changeset
|
515 u_char *p, *host, *last, ch; |
1804
d457a1576532
several changes in server_name:
Igor Sysoev <igor@sysoev.ru>
parents:
1803
diff
changeset
|
516 size_t len, root; |
1116 | 517 ngx_err_t err; |
518 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
|
519 ngx_uint_t overwrite, slash, dir; |
1116 | 520 ngx_str_t path, uri; |
521 ngx_tree_ctx_t tree; | |
522 ngx_file_info_t fi; | |
2007
b9de93d804ea
*) host in request line has priority
Igor Sysoev <igor@sysoev.ru>
parents:
1909
diff
changeset
|
523 ngx_table_elt_t *dest, *over; |
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 | |
679 if (ngx_file_info(copy.path.data, &fi) == -1) { | |
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 | |
713 if (ngx_file_info(path.data, &fi) == -1) { | |
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) { | |
784 if (ngx_rename_file(path.data, copy.path.data) != NGX_FILE_ERROR) { | |
785 return NGX_HTTP_NO_CONTENT; | |
786 } | |
787 } | |
788 | |
1825
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
789 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
|
790 |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
791 tree.size = ngx_file_size(&fi); |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
792 tree.mtime = ngx_file_mtime(&fi); |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
793 tree.access = dlcf->access; |
1116 | 794 tree.log = r->connection->log; |
795 | |
1825
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
796 if (ngx_http_dav_copy_file(&tree, path.data, copy.path.data) == NGX_OK) |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
797 { |
1116 | 798 if (r->method == NGX_HTTP_MOVE) { |
799 rc = ngx_http_dav_delete_path(r, &path, 0); | |
800 | |
801 if (rc != NGX_OK) { | |
802 return rc; | |
803 } | |
804 } | |
805 | |
806 return NGX_HTTP_NO_CONTENT; | |
807 } | |
808 } | |
809 | |
810 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
811 } | |
812 | |
813 | |
814 static ngx_int_t | |
815 ngx_http_dav_copy_dir(ngx_tree_ctx_t *ctx, ngx_str_t *path) | |
816 { | |
817 u_char *p, *dir; | |
818 size_t len; | |
819 ngx_http_dav_copy_ctx_t *copy; | |
820 | |
821 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0, | |
822 "http copy dir: \"%s\"", path->data); | |
823 | |
824 copy = ctx->data; | |
825 | |
826 len = copy->path.len + path->len; | |
827 | |
828 dir = ngx_alloc(len + 1, ctx->log); | |
829 if (dir == NULL) { | |
830 return NGX_ABORT; | |
831 } | |
832 | |
833 p = ngx_cpymem(dir, copy->path.data, copy->path.len); | |
834 (void) ngx_cpystrn(p, path->data + copy->len, path->len - copy->len + 1); | |
835 | |
836 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0, | |
837 "http copy dir to: \"%s\"", dir); | |
838 | |
839 if (ngx_create_dir(dir, ngx_dir_access(ctx->access)) == NGX_FILE_ERROR) { | |
840 (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_create_dir_n, | |
841 dir); | |
842 } | |
843 | |
844 ngx_free(dir); | |
845 | |
846 return NGX_OK; | |
847 } | |
848 | |
849 | |
850 static ngx_int_t | |
851 ngx_http_dav_copy_dir_time(ngx_tree_ctx_t *ctx, ngx_str_t *path) | |
852 { | |
853 u_char *p, *dir; | |
854 size_t len; | |
855 ngx_http_dav_copy_ctx_t *copy; | |
856 | |
857 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0, | |
858 "http copy dir time: \"%s\"", path->data); | |
859 | |
860 copy = ctx->data; | |
861 | |
862 len = copy->path.len + path->len; | |
863 | |
864 dir = ngx_alloc(len + 1, ctx->log); | |
865 if (dir == NULL) { | |
866 return NGX_ABORT; | |
867 } | |
868 | |
869 p = ngx_cpymem(dir, copy->path.data, copy->path.len); | |
870 (void) ngx_cpystrn(p, path->data + copy->len, path->len - copy->len + 1); | |
871 | |
872 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0, | |
873 "http copy dir time to: \"%s\"", dir); | |
874 | |
1803
7405719e4848
condition declarations inside blocks update missed in r1705
Igor Sysoev <igor@sysoev.ru>
parents:
1769
diff
changeset
|
875 #if (NGX_WIN32) |
7405719e4848
condition declarations inside blocks update missed in r1705
Igor Sysoev <igor@sysoev.ru>
parents:
1769
diff
changeset
|
876 { |
7405719e4848
condition declarations inside blocks update missed in r1705
Igor Sysoev <igor@sysoev.ru>
parents:
1769
diff
changeset
|
877 ngx_fd_t fd; |
1116 | 878 |
879 fd = ngx_open_file(dir, NGX_FILE_RDWR, NGX_FILE_OPEN, 0); | |
880 | |
881 if (fd == NGX_INVALID_FILE) { | |
882 (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_open_file_n, dir); | |
883 goto failed; | |
884 } | |
885 | |
886 if (ngx_set_file_time(NULL, fd, ctx->mtime) != NGX_OK) { | |
887 ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno, | |
888 ngx_set_file_time_n " \"%s\" failed", dir); | |
889 } | |
890 | |
891 if (ngx_close_file(fd) == NGX_FILE_ERROR) { | |
892 ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno, | |
893 ngx_close_file_n " \"%s\" failed", dir); | |
894 } | |
1803
7405719e4848
condition declarations inside blocks update missed in r1705
Igor Sysoev <igor@sysoev.ru>
parents:
1769
diff
changeset
|
895 } |
1116 | 896 |
897 failed: | |
898 | |
899 #else | |
900 | |
901 if (ngx_set_file_time(dir, 0, ctx->mtime) != NGX_OK) { | |
902 ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno, | |
903 ngx_set_file_time_n " \"%s\" failed", dir); | |
904 } | |
905 | |
906 #endif | |
907 | |
908 ngx_free(dir); | |
909 | |
910 return NGX_OK; | |
911 } | |
912 | |
913 | |
914 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
|
915 ngx_http_dav_copy_tree_file(ngx_tree_ctx_t *ctx, ngx_str_t *path) |
1116 | 916 { |
917 u_char *p, *file; | |
918 size_t len; | |
919 ngx_http_dav_copy_ctx_t *copy; | |
920 | |
921 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0, | |
922 "http copy file: \"%s\"", path->data); | |
923 | |
924 copy = ctx->data; | |
925 | |
926 len = copy->path.len + path->len; | |
927 | |
928 file = ngx_alloc(len + 1, ctx->log); | |
929 if (file == NULL) { | |
930 return NGX_ABORT; | |
931 } | |
932 | |
933 p = ngx_cpymem(file, copy->path.data, copy->path.len); | |
934 (void) ngx_cpystrn(p, path->data + copy->len, path->len - copy->len + 1); | |
935 | |
936 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0, | |
937 "http copy file to: \"%s\"", file); | |
938 | |
1825
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
939 (void) ngx_http_dav_copy_file(ctx, path->data, file); |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
940 |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
941 ngx_free(file); |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
942 |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
943 return NGX_OK; |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
944 } |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
945 |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
946 |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
947 static ngx_int_t |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
948 ngx_http_dav_copy_file(ngx_tree_ctx_t *ctx, u_char *from, u_char *to) |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
949 { |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
950 off_t size; |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
951 ssize_t n; |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
952 ngx_fd_t fd, cfd; |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
953 ngx_int_t rc; |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
954 u_char buf[NGX_HTTP_DAV_COPY_BLOCK]; |
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 fd = ngx_open_file(from, NGX_FILE_RDONLY, NGX_FILE_OPEN, 0); |
1116 | 957 |
958 if (fd == NGX_INVALID_FILE) { | |
959 (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_open_file_n, | |
1825
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
960 from); |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
961 return NGX_ERROR; |
1116 | 962 } |
963 | |
1825
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
964 cfd = ngx_open_file(to, NGX_FILE_WRONLY, NGX_FILE_CREATE_OR_OPEN, |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
965 ctx->access); |
1116 | 966 |
1825
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
967 rc = NGX_ERROR; |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
968 |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
969 if (cfd == NGX_INVALID_FILE) { |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
970 (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_open_file_n, to); |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
971 goto failed; |
1116 | 972 } |
973 | |
974 for (size = ctx->size; size > 0; size -= n) { | |
975 | |
976 n = ngx_read_fd(fd, buf, NGX_HTTP_DAV_COPY_BLOCK); | |
977 | |
978 if (n == NGX_FILE_ERROR) { | |
979 ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno, | |
1825
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
980 ngx_read_fd_n " \"%s\" failed", from); |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
981 goto failed; |
1116 | 982 } |
983 | |
1825
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
984 if (ngx_write_fd(cfd, buf, n) == NGX_FILE_ERROR) { |
1116 | 985 ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno, |
1825
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
986 ngx_write_fd_n " \"%s\" failed", to); |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
987 goto failed; |
1116 | 988 } |
989 } | |
990 | |
1825
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
991 if (ngx_set_file_time(to, cfd, ctx->mtime) != NGX_OK) { |
1116 | 992 ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno, |
1825
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
993 ngx_set_file_time_n " \"%s\" failed", to); |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
994 goto failed; |
1116 | 995 } |
996 | |
1825
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
997 if (ngx_close_file(cfd) == NGX_FILE_ERROR) { |
1116 | 998 ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno, |
1825
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
999 ngx_close_file_n " \"%s\" failed", to); |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
1000 goto failed; |
1116 | 1001 } |
1002 | |
1825
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
1003 rc = NGX_OK; |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
1004 |
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
1005 failed: |
1116 | 1006 |
1007 if (ngx_close_file(fd) == NGX_FILE_ERROR) { | |
1008 ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno, | |
1825
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
1009 ngx_close_file_n " \"%s\" failed", from); |
1116 | 1010 } |
1011 | |
1825
2e0fbfef56d4
do not delete target until all tests will be done,
Igor Sysoev <igor@sysoev.ru>
parents:
1824
diff
changeset
|
1012 return rc; |
1116 | 1013 } |
1014 | |
1015 | |
1016 static ngx_int_t | |
1017 ngx_http_dav_depth(ngx_http_request_t *r, ngx_int_t dflt) | |
1018 { | |
1019 ngx_table_elt_t *depth; | |
1174
6be5ee17d80b
style fix: remove trailing spaces
Igor Sysoev <igor@sysoev.ru>
parents:
1116
diff
changeset
|
1020 |
1116 | 1021 depth = r->headers_in.depth; |
1022 | |
1023 if (depth == NULL) { | |
1024 return dflt; | |
1025 } | |
1026 | |
1027 if (depth->value.len == 1) { | |
1028 | |
1029 if (depth->value.data[0] == '0') { | |
1030 return 0; | |
1031 } | |
1032 | |
1033 if (depth->value.data[0] == '1') { | |
1034 return 1; | |
1035 } | |
1036 | |
1037 } else { | |
1038 | |
1039 if (depth->value.len == sizeof("infinity") - 1 | |
1040 && ngx_strcmp(depth->value.data, "infinity") == 0) | |
1041 { | |
1042 return NGX_HTTP_DAV_INFINITY_DEPTH; | |
1043 } | |
1044 } | |
1045 | |
1046 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
1047 "client sent invalid \"Depth\" header: \"%V\"", | |
1048 &depth->value); | |
1049 | |
1050 return NGX_HTTP_DAV_INVALID_DEPTH; | |
1051 } | |
1052 | |
1053 | |
1054 static ngx_int_t | |
1055 ngx_http_dav_error(ngx_log_t *log, ngx_err_t err, ngx_int_t not_found, | |
637 | 1056 char *failed, u_char *path) |
1057 { | |
1058 ngx_int_t rc; | |
1059 ngx_uint_t level; | |
1060 | |
1061 if (err == NGX_ENOENT || err == NGX_ENOTDIR || err == NGX_ENAMETOOLONG) { | |
1062 level = NGX_LOG_ERR; | |
1063 rc = not_found; | |
1064 | |
1065 } else if (err == NGX_EACCES || err == NGX_EPERM) { | |
1066 level = NGX_LOG_ERR; | |
1067 rc = NGX_HTTP_FORBIDDEN; | |
1068 | |
1069 } else if (err == NGX_EEXIST) { | |
1070 level = NGX_LOG_ERR; | |
1071 rc = NGX_HTTP_NOT_ALLOWED; | |
1072 | |
1073 } else if (err == NGX_ENOSPC) { | |
1074 level = NGX_LOG_CRIT; | |
1075 rc = NGX_HTTP_INSUFFICIENT_STORAGE; | |
1076 | |
1077 } else { | |
1078 level = NGX_LOG_CRIT; | |
1079 rc = NGX_HTTP_INTERNAL_SERVER_ERROR; | |
1080 } | |
1081 | |
1116 | 1082 ngx_log_error(level, log, err, "%s \"%s\" failed", failed, path); |
637 | 1083 |
1084 return rc; | |
1085 } | |
1086 | |
1087 | |
1088 static ngx_int_t | |
1089 ngx_http_dav_location(ngx_http_request_t *r, u_char *path) | |
1090 { | |
1091 u_char *location; | |
1092 ngx_http_core_loc_conf_t *clcf; | |
1093 | |
1094 r->headers_out.location = ngx_palloc(r->pool, sizeof(ngx_table_elt_t)); | |
1095 if (r->headers_out.location == NULL) { | |
1096 return NGX_ERROR; | |
1097 } | |
1098 | |
1099 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); | |
1100 | |
1101 if (!clcf->alias && clcf->root_lengths == NULL) { | |
1102 location = path + clcf->root.len; | |
1103 | |
1104 } else { | |
2049 | 1105 location = ngx_pnalloc(r->pool, r->uri.len); |
637 | 1106 if (location == NULL) { |
1107 return NGX_ERROR; | |
1108 } | |
1109 | |
1110 ngx_memcpy(location, r->uri.data, r->uri.len); | |
1111 } | |
1112 | |
1113 /* | |
1114 * we do not need to set the r->headers_out.location->hash and | |
1115 * r->headers_out.location->key fields | |
1116 */ | |
1117 | |
1118 r->headers_out.location->value.len = r->uri.len; | |
1119 r->headers_out.location->value.data = location; | |
1120 | |
1121 return NGX_OK; | |
1122 } | |
1123 | |
1124 | |
633 | 1125 static void * |
1126 ngx_http_dav_create_loc_conf(ngx_conf_t *cf) | |
1127 { | |
1128 ngx_http_dav_loc_conf_t *conf; | |
1129 | |
1130 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_dav_loc_conf_t)); | |
1131 if (conf == NULL) { | |
1132 return NGX_CONF_ERROR; | |
1133 } | |
1134 | |
1135 /* | |
1136 * set by ngx_pcalloc(): | |
1137 * | |
1138 * conf->methods = 0; | |
1139 */ | |
1140 | |
1864
524831ec3e69
fix building by msvc introduced in r1817
Igor Sysoev <igor@sysoev.ru>
parents:
1834
diff
changeset
|
1141 conf->min_delete_depth = NGX_CONF_UNSET_UINT; |
1816 | 1142 conf->access = NGX_CONF_UNSET_UINT; |
637 | 1143 conf->create_full_put_path = NGX_CONF_UNSET; |
1144 | |
633 | 1145 return conf; |
1146 } | |
1147 | |
1148 | |
1149 static char * | |
1150 ngx_http_dav_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) | |
1151 { | |
1152 ngx_http_dav_loc_conf_t *prev = parent; | |
1153 ngx_http_dav_loc_conf_t *conf = child; | |
1154 | |
1155 ngx_conf_merge_bitmask_value(conf->methods, prev->methods, | |
637 | 1156 (NGX_CONF_BITMASK_SET|NGX_HTTP_DAV_OFF)); |
1157 | |
1816 | 1158 ngx_conf_merge_uint_value(conf->min_delete_depth, |
1159 prev->min_delete_depth, 0); | |
633 | 1160 |
669 | 1161 ngx_conf_merge_uint_value(conf->access, prev->access, 0600); |
1162 | |
1816 | 1163 ngx_conf_merge_value(conf->create_full_put_path, |
1164 prev->create_full_put_path, 0); | |
1165 | |
633 | 1166 return NGX_CONF_OK; |
1167 } | |
1168 | |
1169 | |
1170 static ngx_int_t | |
681 | 1171 ngx_http_dav_init(ngx_conf_t *cf) |
633 | 1172 { |
1173 ngx_http_handler_pt *h; | |
1174 ngx_http_core_main_conf_t *cmcf; | |
1175 | |
681 | 1176 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module); |
633 | 1177 |
1178 h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers); | |
1179 if (h == NULL) { | |
1180 return NGX_ERROR; | |
1181 } | |
1182 | |
1183 *h = ngx_http_dav_handler; | |
1184 | |
1185 return NGX_OK; | |
1186 } |