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