3 * Copyright (C) Igor Sysoev
4 * Copyright (C) Nginx, Inc.
8 #include <ngx_config.h>
13 #define NGX_HTTP_DAV_COPY_BLOCK 65536
15 #define NGX_HTTP_DAV_OFF 2
18 #define NGX_HTTP_DAV_NO_DEPTH -3
19 #define NGX_HTTP_DAV_INVALID_DEPTH -2
20 #define NGX_HTTP_DAV_INFINITY_DEPTH -1
26 ngx_uint_t min_delete_depth
;
27 ngx_flag_t create_full_put_path
;
28 } ngx_http_dav_loc_conf_t
;
34 } ngx_http_dav_copy_ctx_t
;
37 static ngx_int_t
ngx_http_dav_handler(ngx_http_request_t
*r
);
39 static void ngx_http_dav_put_handler(ngx_http_request_t
*r
);
41 static ngx_int_t
ngx_http_dav_delete_handler(ngx_http_request_t
*r
);
42 static ngx_int_t
ngx_http_dav_delete_path(ngx_http_request_t
*r
,
43 ngx_str_t
*path
, ngx_uint_t dir
);
44 static ngx_int_t
ngx_http_dav_delete_dir(ngx_tree_ctx_t
*ctx
, ngx_str_t
*path
);
45 static ngx_int_t
ngx_http_dav_delete_file(ngx_tree_ctx_t
*ctx
, ngx_str_t
*path
);
46 static ngx_int_t
ngx_http_dav_noop(ngx_tree_ctx_t
*ctx
, ngx_str_t
*path
);
48 static ngx_int_t
ngx_http_dav_mkcol_handler(ngx_http_request_t
*r
,
49 ngx_http_dav_loc_conf_t
*dlcf
);
51 static ngx_int_t
ngx_http_dav_copy_move_handler(ngx_http_request_t
*r
);
52 static ngx_int_t
ngx_http_dav_copy_dir(ngx_tree_ctx_t
*ctx
, ngx_str_t
*path
);
53 static ngx_int_t
ngx_http_dav_copy_dir_time(ngx_tree_ctx_t
*ctx
,
55 static ngx_int_t
ngx_http_dav_copy_tree_file(ngx_tree_ctx_t
*ctx
,
58 static ngx_int_t
ngx_http_dav_depth(ngx_http_request_t
*r
, ngx_int_t dflt
);
59 static ngx_int_t
ngx_http_dav_error(ngx_log_t
*log
, ngx_err_t err
,
60 ngx_int_t not_found
, char *failed
, u_char
*path
);
61 static ngx_int_t
ngx_http_dav_location(ngx_http_request_t
*r
, u_char
*path
);
62 static void *ngx_http_dav_create_loc_conf(ngx_conf_t
*cf
);
63 static char *ngx_http_dav_merge_loc_conf(ngx_conf_t
*cf
,
64 void *parent
, void *child
);
65 static ngx_int_t
ngx_http_dav_init(ngx_conf_t
*cf
);
68 static ngx_conf_bitmask_t ngx_http_dav_methods_mask
[] = {
69 { ngx_string("off"), NGX_HTTP_DAV_OFF
},
70 { ngx_string("put"), NGX_HTTP_PUT
},
71 { ngx_string("delete"), NGX_HTTP_DELETE
},
72 { ngx_string("mkcol"), NGX_HTTP_MKCOL
},
73 { ngx_string("copy"), NGX_HTTP_COPY
},
74 { ngx_string("move"), NGX_HTTP_MOVE
},
75 { ngx_null_string
, 0 }
79 static ngx_command_t ngx_http_dav_commands
[] = {
81 { ngx_string("dav_methods"),
82 NGX_HTTP_MAIN_CONF
|NGX_HTTP_SRV_CONF
|NGX_HTTP_LOC_CONF
|NGX_CONF_1MORE
,
83 ngx_conf_set_bitmask_slot
,
84 NGX_HTTP_LOC_CONF_OFFSET
,
85 offsetof(ngx_http_dav_loc_conf_t
, methods
),
86 &ngx_http_dav_methods_mask
},
88 { ngx_string("create_full_put_path"),
89 NGX_HTTP_MAIN_CONF
|NGX_HTTP_SRV_CONF
|NGX_HTTP_LOC_CONF
|NGX_CONF_FLAG
,
90 ngx_conf_set_flag_slot
,
91 NGX_HTTP_LOC_CONF_OFFSET
,
92 offsetof(ngx_http_dav_loc_conf_t
, create_full_put_path
),
95 { ngx_string("min_delete_depth"),
96 NGX_HTTP_MAIN_CONF
|NGX_HTTP_SRV_CONF
|NGX_HTTP_LOC_CONF
|NGX_CONF_TAKE1
,
97 ngx_conf_set_num_slot
,
98 NGX_HTTP_LOC_CONF_OFFSET
,
99 offsetof(ngx_http_dav_loc_conf_t
, min_delete_depth
),
102 { ngx_string("dav_access"),
103 NGX_HTTP_MAIN_CONF
|NGX_HTTP_SRV_CONF
|NGX_HTTP_LOC_CONF
|NGX_CONF_TAKE123
,
104 ngx_conf_set_access_slot
,
105 NGX_HTTP_LOC_CONF_OFFSET
,
106 offsetof(ngx_http_dav_loc_conf_t
, access
),
113 static ngx_http_module_t ngx_http_dav_module_ctx
= {
114 NULL
, /* preconfiguration */
115 ngx_http_dav_init
, /* postconfiguration */
117 NULL
, /* create main configuration */
118 NULL
, /* init main configuration */
120 NULL
, /* create server configuration */
121 NULL
, /* merge server configuration */
123 ngx_http_dav_create_loc_conf
, /* create location configuration */
124 ngx_http_dav_merge_loc_conf
/* merge location configuration */
128 ngx_module_t ngx_http_dav_module
= {
130 &ngx_http_dav_module_ctx
, /* module context */
131 ngx_http_dav_commands
, /* module directives */
132 NGX_HTTP_MODULE
, /* module type */
133 NULL
, /* init master */
134 NULL
, /* init module */
135 NULL
, /* init process */
136 NULL
, /* init thread */
137 NULL
, /* exit thread */
138 NULL
, /* exit process */
139 NULL
, /* exit master */
140 NGX_MODULE_V1_PADDING
145 ngx_http_dav_handler(ngx_http_request_t
*r
)
148 ngx_http_dav_loc_conf_t
*dlcf
;
150 dlcf
= ngx_http_get_module_loc_conf(r
, ngx_http_dav_module
);
152 if (!(r
->method
& dlcf
->methods
)) {
160 if (r
->uri
.data
[r
->uri
.len
- 1] == '/') {
161 ngx_log_error(NGX_LOG_ERR
, r
->connection
->log
, 0,
162 "cannot PUT to a collection");
163 return NGX_HTTP_CONFLICT
;
166 r
->request_body_in_file_only
= 1;
167 r
->request_body_in_persistent_file
= 1;
168 r
->request_body_in_clean_file
= 1;
169 r
->request_body_file_group_access
= 1;
170 r
->request_body_file_log_level
= 0;
172 rc
= ngx_http_read_client_request_body(r
, ngx_http_dav_put_handler
);
174 if (rc
>= NGX_HTTP_SPECIAL_RESPONSE
) {
180 case NGX_HTTP_DELETE
:
182 return ngx_http_dav_delete_handler(r
);
186 return ngx_http_dav_mkcol_handler(r
, dlcf
);
190 return ngx_http_dav_copy_move_handler(r
);
194 return ngx_http_dav_copy_move_handler(r
);
202 ngx_http_dav_put_handler(ngx_http_request_t
*r
)
206 ngx_str_t
*temp
, path
;
209 ngx_ext_rename_file_t ext
;
210 ngx_http_dav_loc_conf_t
*dlcf
;
212 if (r
->request_body
== NULL
|| r
->request_body
->temp_file
== NULL
) {
213 ngx_http_finalize_request(r
, NGX_HTTP_INTERNAL_SERVER_ERROR
);
217 ngx_http_map_uri_to_path(r
, &path
, &root
, 0);
221 ngx_log_debug1(NGX_LOG_DEBUG_HTTP
, r
->connection
->log
, 0,
222 "http put filename: \"%s\"", path
.data
);
224 temp
= &r
->request_body
->temp_file
->file
.name
;
226 if (ngx_file_info(path
.data
, &fi
) == NGX_FILE_ERROR
) {
227 status
= NGX_HTTP_CREATED
;
230 status
= NGX_HTTP_NO_CONTENT
;
232 if (ngx_is_dir(&fi
)) {
233 ngx_log_error(NGX_LOG_ERR
, r
->connection
->log
, NGX_EISDIR
,
234 "\"%s\" could not be created", path
.data
);
236 if (ngx_delete_file(temp
->data
) == NGX_FILE_ERROR
) {
237 ngx_log_error(NGX_LOG_CRIT
, r
->connection
->log
, ngx_errno
,
238 ngx_delete_file_n
" \"%s\" failed",
242 ngx_http_finalize_request(r
, NGX_HTTP_CONFLICT
);
247 dlcf
= ngx_http_get_module_loc_conf(r
, ngx_http_dav_module
);
249 ext
.access
= dlcf
->access
;
250 ext
.path_access
= dlcf
->access
;
252 ext
.create_path
= dlcf
->create_full_put_path
;
254 ext
.log
= r
->connection
->log
;
256 if (r
->headers_in
.date
) {
257 date
= ngx_http_parse_time(r
->headers_in
.date
->value
.data
,
258 r
->headers_in
.date
->value
.len
);
260 if (date
!= NGX_ERROR
) {
262 ext
.fd
= r
->request_body
->temp_file
->file
.fd
;
266 if (ngx_ext_rename_file(temp
, &path
, &ext
) != NGX_OK
) {
267 ngx_http_finalize_request(r
, NGX_HTTP_INTERNAL_SERVER_ERROR
);
271 if (status
== NGX_HTTP_CREATED
) {
272 if (ngx_http_dav_location(r
, path
.data
) != NGX_OK
) {
273 ngx_http_finalize_request(r
, NGX_HTTP_INTERNAL_SERVER_ERROR
);
277 r
->headers_out
.content_length_n
= 0;
280 r
->headers_out
.status
= status
;
283 ngx_http_finalize_request(r
, ngx_http_send_header(r
));
289 ngx_http_dav_delete_handler(ngx_http_request_t
*r
)
294 ngx_uint_t i
, d
, dir
;
297 ngx_http_dav_loc_conf_t
*dlcf
;
299 if (r
->headers_in
.content_length_n
> 0) {
300 ngx_log_error(NGX_LOG_ERR
, r
->connection
->log
, 0,
301 "DELETE with body is unsupported");
302 return NGX_HTTP_UNSUPPORTED_MEDIA_TYPE
;
305 dlcf
= ngx_http_get_module_loc_conf(r
, ngx_http_dav_module
);
307 if (dlcf
->min_delete_depth
) {
310 for (i
= 0; i
< r
->uri
.len
; /* void */) {
311 if (r
->uri
.data
[i
++] == '/') {
312 if (++d
>= dlcf
->min_delete_depth
&& i
< r
->uri
.len
) {
318 ngx_log_error(NGX_LOG_ERR
, r
->connection
->log
, 0,
319 "insufficient URI depth:%i to DELETE", d
);
320 return NGX_HTTP_CONFLICT
;
325 ngx_http_map_uri_to_path(r
, &path
, &root
, 0);
327 ngx_log_debug1(NGX_LOG_DEBUG_HTTP
, r
->connection
->log
, 0,
328 "http delete filename: \"%s\"", path
.data
);
330 if (ngx_link_info(path
.data
, &fi
) == NGX_FILE_ERROR
) {
333 rc
= (err
== NGX_ENOTDIR
) ? NGX_HTTP_CONFLICT
: NGX_HTTP_NOT_FOUND
;
335 return ngx_http_dav_error(r
->connection
->log
, err
,
336 rc
, ngx_link_info_n
, path
.data
);
339 if (ngx_is_dir(&fi
)) {
341 if (r
->uri
.data
[r
->uri
.len
- 1] != '/') {
342 ngx_log_error(NGX_LOG_ERR
, r
->connection
->log
, NGX_EISDIR
,
343 "DELETE \"%s\" failed", path
.data
);
344 return NGX_HTTP_CONFLICT
;
347 depth
= ngx_http_dav_depth(r
, NGX_HTTP_DAV_INFINITY_DEPTH
);
349 if (depth
!= NGX_HTTP_DAV_INFINITY_DEPTH
) {
350 ngx_log_error(NGX_LOG_ERR
, r
->connection
->log
, 0,
351 "\"Depth\" header must be infinity");
352 return NGX_HTTP_BAD_REQUEST
;
355 path
.len
-= 2; /* omit "/\0" */
362 * we do not need to test (r->uri.data[r->uri.len - 1] == '/')
363 * because ngx_link_info("/file/") returned NGX_ENOTDIR above
366 depth
= ngx_http_dav_depth(r
, 0);
368 if (depth
!= 0 && depth
!= NGX_HTTP_DAV_INFINITY_DEPTH
) {
369 ngx_log_error(NGX_LOG_ERR
, r
->connection
->log
, 0,
370 "\"Depth\" header must be 0 or infinity");
371 return NGX_HTTP_BAD_REQUEST
;
377 rc
= ngx_http_dav_delete_path(r
, &path
, dir
);
380 return NGX_HTTP_NO_CONTENT
;
388 ngx_http_dav_delete_path(ngx_http_request_t
*r
, ngx_str_t
*path
, ngx_uint_t dir
)
395 tree
.init_handler
= NULL
;
396 tree
.file_handler
= ngx_http_dav_delete_file
;
397 tree
.pre_tree_handler
= ngx_http_dav_noop
;
398 tree
.post_tree_handler
= ngx_http_dav_delete_dir
;
399 tree
.spec_handler
= ngx_http_dav_delete_file
;
402 tree
.log
= r
->connection
->log
;
406 if (ngx_walk_tree(&tree
, path
) != NGX_OK
) {
407 return NGX_HTTP_INTERNAL_SERVER_ERROR
;
410 if (ngx_delete_dir(path
->data
) != NGX_FILE_ERROR
) {
414 failed
= ngx_delete_dir_n
;
418 if (ngx_delete_file(path
->data
) != NGX_FILE_ERROR
) {
422 failed
= ngx_delete_file_n
;
425 return ngx_http_dav_error(r
->connection
->log
, ngx_errno
,
426 NGX_HTTP_NOT_FOUND
, failed
, path
->data
);
431 ngx_http_dav_delete_dir(ngx_tree_ctx_t
*ctx
, ngx_str_t
*path
)
433 ngx_log_debug1(NGX_LOG_DEBUG_HTTP
, ctx
->log
, 0,
434 "http delete dir: \"%s\"", path
->data
);
436 if (ngx_delete_dir(path
->data
) == NGX_FILE_ERROR
) {
438 /* TODO: add to 207 */
440 (void) ngx_http_dav_error(ctx
->log
, ngx_errno
, 0, ngx_delete_dir_n
,
449 ngx_http_dav_delete_file(ngx_tree_ctx_t
*ctx
, ngx_str_t
*path
)
451 ngx_log_debug1(NGX_LOG_DEBUG_HTTP
, ctx
->log
, 0,
452 "http delete file: \"%s\"", path
->data
);
454 if (ngx_delete_file(path
->data
) == NGX_FILE_ERROR
) {
456 /* TODO: add to 207 */
458 (void) ngx_http_dav_error(ctx
->log
, ngx_errno
, 0, ngx_delete_file_n
,
467 ngx_http_dav_noop(ngx_tree_ctx_t
*ctx
, ngx_str_t
*path
)
474 ngx_http_dav_mkcol_handler(ngx_http_request_t
*r
, ngx_http_dav_loc_conf_t
*dlcf
)
480 if (r
->headers_in
.content_length_n
> 0) {
481 ngx_log_error(NGX_LOG_ERR
, r
->connection
->log
, 0,
482 "MKCOL with body is unsupported");
483 return NGX_HTTP_UNSUPPORTED_MEDIA_TYPE
;
486 if (r
->uri
.data
[r
->uri
.len
- 1] != '/') {
487 ngx_log_error(NGX_LOG_ERR
, r
->connection
->log
, 0,
488 "MKCOL can create a collection only");
489 return NGX_HTTP_CONFLICT
;
492 p
= ngx_http_map_uri_to_path(r
, &path
, &root
, 0);
497 ngx_log_debug1(NGX_LOG_DEBUG_HTTP
, r
->connection
->log
, 0,
498 "http mkcol path: \"%s\"", path
.data
);
500 if (ngx_create_dir(path
.data
, ngx_dir_access(dlcf
->access
))
503 if (ngx_http_dav_location(r
, path
.data
) != NGX_OK
) {
504 return NGX_HTTP_INTERNAL_SERVER_ERROR
;
507 return NGX_HTTP_CREATED
;
510 return ngx_http_dav_error(r
->connection
->log
, ngx_errno
,
511 NGX_HTTP_CONFLICT
, ngx_create_dir_n
, path
.data
);
516 ngx_http_dav_copy_move_handler(ngx_http_request_t
*r
)
518 u_char
*p
, *host
, *last
, ch
;
522 ngx_uint_t overwrite
, slash
, dir
, flags
;
523 ngx_str_t path
, uri
, duri
, args
;
527 ngx_table_elt_t
*dest
, *over
;
528 ngx_ext_rename_file_t ext
;
529 ngx_http_dav_copy_ctx_t copy
;
530 ngx_http_dav_loc_conf_t
*dlcf
;
532 if (r
->headers_in
.content_length_n
> 0) {
533 return NGX_HTTP_UNSUPPORTED_MEDIA_TYPE
;
536 dest
= r
->headers_in
.destination
;
539 ngx_log_error(NGX_LOG_ERR
, r
->connection
->log
, 0,
540 "client sent no \"Destination\" header");
541 return NGX_HTTP_BAD_REQUEST
;
544 p
= dest
->value
.data
;
545 /* there is always '\0' even after empty header value */
547 last
= p
+ dest
->value
.len
;
548 goto destination_done
;
551 len
= r
->headers_in
.server
.len
;
554 ngx_log_error(NGX_LOG_ERR
, r
->connection
->log
, 0,
555 "client sent no \"Host\" header");
556 return NGX_HTTP_BAD_REQUEST
;
561 if (r
->connection
->ssl
) {
562 if (ngx_strncmp(dest
->value
.data
, "https://", sizeof("https://") - 1)
565 goto invalid_destination
;
568 host
= dest
->value
.data
+ sizeof("https://") - 1;
573 if (ngx_strncmp(dest
->value
.data
, "http://", sizeof("http://") - 1)
576 goto invalid_destination
;
579 host
= dest
->value
.data
+ sizeof("http://") - 1;
582 if (ngx_strncmp(host
, r
->headers_in
.server
.data
, len
) != 0) {
583 ngx_log_error(NGX_LOG_ERR
, r
->connection
->log
, 0,
584 "\"Destination\" URI \"%V\" is handled by "
585 "different repository than the source URI",
587 return NGX_HTTP_BAD_REQUEST
;
590 last
= dest
->value
.data
+ dest
->value
.len
;
592 for (p
= host
+ len
; p
< last
; p
++) {
594 goto destination_done
;
600 ngx_log_error(NGX_LOG_ERR
, r
->connection
->log
, 0,
601 "client sent invalid \"Destination\" header: \"%V\"",
603 return NGX_HTTP_BAD_REQUEST
;
611 if (ngx_http_parse_unsafe_uri(r
, &duri
, &args
, &flags
) != NGX_OK
) {
612 goto invalid_destination
;
615 if ((r
->uri
.data
[r
->uri
.len
- 1] == '/' && *(last
- 1) != '/')
616 || (r
->uri
.data
[r
->uri
.len
- 1] != '/' && *(last
- 1) == '/'))
618 ngx_log_error(NGX_LOG_ERR
, r
->connection
->log
, 0,
619 "both URI \"%V\" and \"Destination\" URI \"%V\" "
620 "should be either collections or non-collections",
621 &r
->uri
, &dest
->value
);
622 return NGX_HTTP_CONFLICT
;
625 depth
= ngx_http_dav_depth(r
, NGX_HTTP_DAV_INFINITY_DEPTH
);
627 if (depth
!= NGX_HTTP_DAV_INFINITY_DEPTH
) {
629 if (r
->method
== NGX_HTTP_COPY
) {
631 ngx_log_error(NGX_LOG_ERR
, r
->connection
->log
, 0,
632 "\"Depth\" header must be 0 or infinity");
633 return NGX_HTTP_BAD_REQUEST
;
637 ngx_log_error(NGX_LOG_ERR
, r
->connection
->log
, 0,
638 "\"Depth\" header must be infinity");
639 return NGX_HTTP_BAD_REQUEST
;
643 over
= r
->headers_in
.overwrite
;
646 if (over
->value
.len
== 1) {
647 ch
= over
->value
.data
[0];
649 if (ch
== 'T' || ch
== 't') {
654 if (ch
== 'F' || ch
== 'f') {
661 ngx_log_error(NGX_LOG_ERR
, r
->connection
->log
, 0,
662 "client sent invalid \"Overwrite\" header: \"%V\"",
664 return NGX_HTTP_BAD_REQUEST
;
671 ngx_http_map_uri_to_path(r
, &path
, &root
, 0);
673 ngx_log_debug1(NGX_LOG_DEBUG_HTTP
, r
->connection
->log
, 0,
674 "http copy from: \"%s\"", path
.data
);
679 ngx_http_map_uri_to_path(r
, ©
.path
, &root
, 0);
683 copy
.path
.len
--; /* omit "\0" */
685 if (copy
.path
.data
[copy
.path
.len
- 1] == '/') {
688 copy
.path
.data
[copy
.path
.len
] = '\0';
694 ngx_log_debug1(NGX_LOG_DEBUG_HTTP
, r
->connection
->log
, 0,
695 "http copy to: \"%s\"", copy
.path
.data
);
697 if (ngx_link_info(copy
.path
.data
, &fi
) == NGX_FILE_ERROR
) {
700 if (err
!= NGX_ENOENT
) {
701 return ngx_http_dav_error(r
->connection
->log
, err
,
702 NGX_HTTP_NOT_FOUND
, ngx_link_info_n
,
706 /* destination does not exist */
713 /* destination exists */
715 if (ngx_is_dir(&fi
) && !slash
) {
716 ngx_log_error(NGX_LOG_ERR
, r
->connection
->log
, 0,
717 "\"%V\" could not be %Ved to collection \"%V\"",
718 &r
->uri
, &r
->method_name
, &dest
->value
);
719 return NGX_HTTP_CONFLICT
;
723 ngx_log_error(NGX_LOG_ERR
, r
->connection
->log
, NGX_EEXIST
,
724 "\"%s\" could not be created", copy
.path
.data
);
725 return NGX_HTTP_PRECONDITION_FAILED
;
728 dir
= ngx_is_dir(&fi
);
731 if (ngx_link_info(path
.data
, &fi
) == NGX_FILE_ERROR
) {
732 return ngx_http_dav_error(r
->connection
->log
, ngx_errno
,
733 NGX_HTTP_NOT_FOUND
, ngx_link_info_n
,
737 if (ngx_is_dir(&fi
)) {
739 if (r
->uri
.data
[r
->uri
.len
- 1] != '/') {
740 ngx_log_error(NGX_LOG_ERR
, r
->connection
->log
, 0,
741 "\"%V\" is collection", &r
->uri
);
742 return NGX_HTTP_BAD_REQUEST
;
746 ngx_log_debug1(NGX_LOG_DEBUG_HTTP
, r
->connection
->log
, 0,
747 "http delete: \"%s\"", copy
.path
.data
);
749 rc
= ngx_http_dav_delete_path(r
, ©
.path
, dir
);
757 if (ngx_is_dir(&fi
)) {
759 path
.len
-= 2; /* omit "/\0" */
761 if (r
->method
== NGX_HTTP_MOVE
) {
762 if (ngx_rename_file(path
.data
, copy
.path
.data
) != NGX_FILE_ERROR
) {
763 return NGX_HTTP_CREATED
;
767 if (ngx_create_dir(copy
.path
.data
, ngx_file_access(&fi
))
770 return ngx_http_dav_error(r
->connection
->log
, ngx_errno
,
772 ngx_create_dir_n
, copy
.path
.data
);
777 tree
.init_handler
= NULL
;
778 tree
.file_handler
= ngx_http_dav_copy_tree_file
;
779 tree
.pre_tree_handler
= ngx_http_dav_copy_dir
;
780 tree
.post_tree_handler
= ngx_http_dav_copy_dir_time
;
781 tree
.spec_handler
= ngx_http_dav_noop
;
784 tree
.log
= r
->connection
->log
;
786 if (ngx_walk_tree(&tree
, &path
) == NGX_OK
) {
788 if (r
->method
== NGX_HTTP_MOVE
) {
789 rc
= ngx_http_dav_delete_path(r
, &path
, 1);
796 return NGX_HTTP_CREATED
;
801 if (r
->method
== NGX_HTTP_MOVE
) {
803 dlcf
= ngx_http_get_module_loc_conf(r
, ngx_http_dav_module
);
806 ext
.path_access
= dlcf
->access
;
810 ext
.log
= r
->connection
->log
;
812 if (ngx_ext_rename_file(&path
, ©
.path
, &ext
) == NGX_OK
) {
813 return NGX_HTTP_NO_CONTENT
;
816 return NGX_HTTP_INTERNAL_SERVER_ERROR
;
819 dlcf
= ngx_http_get_module_loc_conf(r
, ngx_http_dav_module
);
821 cf
.size
= ngx_file_size(&fi
);
823 cf
.access
= dlcf
->access
;
824 cf
.time
= ngx_file_mtime(&fi
);
825 cf
.log
= r
->connection
->log
;
827 if (ngx_copy_file(path
.data
, copy
.path
.data
, &cf
) == NGX_OK
) {
828 return NGX_HTTP_NO_CONTENT
;
832 return NGX_HTTP_INTERNAL_SERVER_ERROR
;
837 ngx_http_dav_copy_dir(ngx_tree_ctx_t
*ctx
, ngx_str_t
*path
)
841 ngx_http_dav_copy_ctx_t
*copy
;
843 ngx_log_debug1(NGX_LOG_DEBUG_HTTP
, ctx
->log
, 0,
844 "http copy dir: \"%s\"", path
->data
);
848 len
= copy
->path
.len
+ path
->len
;
850 dir
= ngx_alloc(len
+ 1, ctx
->log
);
855 p
= ngx_cpymem(dir
, copy
->path
.data
, copy
->path
.len
);
856 (void) ngx_cpystrn(p
, path
->data
+ copy
->len
, path
->len
- copy
->len
+ 1);
858 ngx_log_debug1(NGX_LOG_DEBUG_HTTP
, ctx
->log
, 0,
859 "http copy dir to: \"%s\"", dir
);
861 if (ngx_create_dir(dir
, ngx_dir_access(ctx
->access
)) == NGX_FILE_ERROR
) {
862 (void) ngx_http_dav_error(ctx
->log
, ngx_errno
, 0, ngx_create_dir_n
,
873 ngx_http_dav_copy_dir_time(ngx_tree_ctx_t
*ctx
, ngx_str_t
*path
)
877 ngx_http_dav_copy_ctx_t
*copy
;
879 ngx_log_debug1(NGX_LOG_DEBUG_HTTP
, ctx
->log
, 0,
880 "http copy dir time: \"%s\"", path
->data
);
884 len
= copy
->path
.len
+ path
->len
;
886 dir
= ngx_alloc(len
+ 1, ctx
->log
);
891 p
= ngx_cpymem(dir
, copy
->path
.data
, copy
->path
.len
);
892 (void) ngx_cpystrn(p
, path
->data
+ copy
->len
, path
->len
- copy
->len
+ 1);
894 ngx_log_debug1(NGX_LOG_DEBUG_HTTP
, ctx
->log
, 0,
895 "http copy dir time to: \"%s\"", dir
);
901 fd
= ngx_open_file(dir
, NGX_FILE_RDWR
, NGX_FILE_OPEN
, 0);
903 if (fd
== NGX_INVALID_FILE
) {
904 (void) ngx_http_dav_error(ctx
->log
, ngx_errno
, 0, ngx_open_file_n
, dir
);
908 if (ngx_set_file_time(NULL
, fd
, ctx
->mtime
) != NGX_OK
) {
909 ngx_log_error(NGX_LOG_ALERT
, ctx
->log
, ngx_errno
,
910 ngx_set_file_time_n
" \"%s\" failed", dir
);
913 if (ngx_close_file(fd
) == NGX_FILE_ERROR
) {
914 ngx_log_error(NGX_LOG_ALERT
, ctx
->log
, ngx_errno
,
915 ngx_close_file_n
" \"%s\" failed", dir
);
923 if (ngx_set_file_time(dir
, 0, ctx
->mtime
) != NGX_OK
) {
924 ngx_log_error(NGX_LOG_ALERT
, ctx
->log
, ngx_errno
,
925 ngx_set_file_time_n
" \"%s\" failed", dir
);
937 ngx_http_dav_copy_tree_file(ngx_tree_ctx_t
*ctx
, ngx_str_t
*path
)
942 ngx_http_dav_copy_ctx_t
*copy
;
944 ngx_log_debug1(NGX_LOG_DEBUG_HTTP
, ctx
->log
, 0,
945 "http copy file: \"%s\"", path
->data
);
949 len
= copy
->path
.len
+ path
->len
;
951 file
= ngx_alloc(len
+ 1, ctx
->log
);
956 p
= ngx_cpymem(file
, copy
->path
.data
, copy
->path
.len
);
957 (void) ngx_cpystrn(p
, path
->data
+ copy
->len
, path
->len
- copy
->len
+ 1);
959 ngx_log_debug1(NGX_LOG_DEBUG_HTTP
, ctx
->log
, 0,
960 "http copy file to: \"%s\"", file
);
964 cf
.access
= ctx
->access
;
965 cf
.time
= ctx
->mtime
;
968 (void) ngx_copy_file(path
->data
, file
, &cf
);
977 ngx_http_dav_depth(ngx_http_request_t
*r
, ngx_int_t dflt
)
979 ngx_table_elt_t
*depth
;
981 depth
= r
->headers_in
.depth
;
987 if (depth
->value
.len
== 1) {
989 if (depth
->value
.data
[0] == '0') {
993 if (depth
->value
.data
[0] == '1') {
999 if (depth
->value
.len
== sizeof("infinity") - 1
1000 && ngx_strcmp(depth
->value
.data
, "infinity") == 0)
1002 return NGX_HTTP_DAV_INFINITY_DEPTH
;
1006 ngx_log_error(NGX_LOG_ERR
, r
->connection
->log
, 0,
1007 "client sent invalid \"Depth\" header: \"%V\"",
1010 return NGX_HTTP_DAV_INVALID_DEPTH
;
1015 ngx_http_dav_error(ngx_log_t
*log
, ngx_err_t err
, ngx_int_t not_found
,
1016 char *failed
, u_char
*path
)
1021 if (err
== NGX_ENOENT
|| err
== NGX_ENOTDIR
|| err
== NGX_ENAMETOOLONG
) {
1022 level
= NGX_LOG_ERR
;
1025 } else if (err
== NGX_EACCES
|| err
== NGX_EPERM
) {
1026 level
= NGX_LOG_ERR
;
1027 rc
= NGX_HTTP_FORBIDDEN
;
1029 } else if (err
== NGX_EEXIST
) {
1030 level
= NGX_LOG_ERR
;
1031 rc
= NGX_HTTP_NOT_ALLOWED
;
1033 } else if (err
== NGX_ENOSPC
) {
1034 level
= NGX_LOG_CRIT
;
1035 rc
= NGX_HTTP_INSUFFICIENT_STORAGE
;
1038 level
= NGX_LOG_CRIT
;
1039 rc
= NGX_HTTP_INTERNAL_SERVER_ERROR
;
1042 ngx_log_error(level
, log
, err
, "%s \"%s\" failed", failed
, path
);
1049 ngx_http_dav_location(ngx_http_request_t
*r
, u_char
*path
)
1052 ngx_http_core_loc_conf_t
*clcf
;
1054 r
->headers_out
.location
= ngx_palloc(r
->pool
, sizeof(ngx_table_elt_t
));
1055 if (r
->headers_out
.location
== NULL
) {
1059 clcf
= ngx_http_get_module_loc_conf(r
, ngx_http_core_module
);
1061 if (!clcf
->alias
&& clcf
->root_lengths
== NULL
) {
1062 location
= path
+ clcf
->root
.len
;
1065 location
= ngx_pnalloc(r
->pool
, r
->uri
.len
);
1066 if (location
== NULL
) {
1070 ngx_memcpy(location
, r
->uri
.data
, r
->uri
.len
);
1074 * we do not need to set the r->headers_out.location->hash and
1075 * r->headers_out.location->key fields
1078 r
->headers_out
.location
->value
.len
= r
->uri
.len
;
1079 r
->headers_out
.location
->value
.data
= location
;
1086 ngx_http_dav_create_loc_conf(ngx_conf_t
*cf
)
1088 ngx_http_dav_loc_conf_t
*conf
;
1090 conf
= ngx_pcalloc(cf
->pool
, sizeof(ngx_http_dav_loc_conf_t
));
1096 * set by ngx_pcalloc():
1098 * conf->methods = 0;
1101 conf
->min_delete_depth
= NGX_CONF_UNSET_UINT
;
1102 conf
->access
= NGX_CONF_UNSET_UINT
;
1103 conf
->create_full_put_path
= NGX_CONF_UNSET
;
1110 ngx_http_dav_merge_loc_conf(ngx_conf_t
*cf
, void *parent
, void *child
)
1112 ngx_http_dav_loc_conf_t
*prev
= parent
;
1113 ngx_http_dav_loc_conf_t
*conf
= child
;
1115 ngx_conf_merge_bitmask_value(conf
->methods
, prev
->methods
,
1116 (NGX_CONF_BITMASK_SET
|NGX_HTTP_DAV_OFF
));
1118 ngx_conf_merge_uint_value(conf
->min_delete_depth
,
1119 prev
->min_delete_depth
, 0);
1121 ngx_conf_merge_uint_value(conf
->access
, prev
->access
, 0600);
1123 ngx_conf_merge_value(conf
->create_full_put_path
,
1124 prev
->create_full_put_path
, 0);
1131 ngx_http_dav_init(ngx_conf_t
*cf
)
1133 ngx_http_handler_pt
*h
;
1134 ngx_http_core_main_conf_t
*cmcf
;
1136 cmcf
= ngx_http_conf_get_module_main_conf(cf
, ngx_http_core_module
);
1138 h
= ngx_array_push(&cmcf
->phases
[NGX_HTTP_CONTENT_PHASE
].handlers
);
1143 *h
= ngx_http_dav_handler
;