3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
12 #define NGX_HTTP_DAV_COPY_BLOCK 65536
14 #define NGX_HTTP_DAV_OFF 2
17 #define NGX_HTTP_DAV_NO_DEPTH -3
18 #define NGX_HTTP_DAV_INVALID_DEPTH -2
19 #define NGX_HTTP_DAV_INFINITY_DEPTH -1
25 ngx_uint_t min_delete_depth;
26 ngx_flag_t create_full_put_path;
27 } ngx_http_dav_loc_conf_t;
33 } ngx_http_dav_copy_ctx_t;
36 static ngx_int_t ngx_http_dav_handler(ngx_http_request_t *r);
38 static void ngx_http_dav_put_handler(ngx_http_request_t *r);
40 static ngx_int_t ngx_http_dav_delete_handler(ngx_http_request_t *r);
41 static ngx_int_t ngx_http_dav_delete_path(ngx_http_request_t *r,
42 ngx_str_t *path, ngx_uint_t dir);
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);
45 static ngx_int_t ngx_http_dav_noop(ngx_tree_ctx_t *ctx, ngx_str_t *path);
47 static ngx_int_t ngx_http_dav_mkcol_handler(ngx_http_request_t *r,
48 ngx_http_dav_loc_conf_t *dlcf);
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,
54 static ngx_int_t ngx_http_dav_copy_tree_file(ngx_tree_ctx_t *ctx,
56 static ngx_int_t ngx_http_dav_copy_file(ngx_tree_ctx_t *ctx, u_char *from,
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,
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);
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);
66 static ngx_int_t ngx_http_dav_init(ngx_conf_t *cf);
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 },
73 { ngx_string("mkcol"), NGX_HTTP_MKCOL },
74 { ngx_string("copy"), NGX_HTTP_COPY },
75 { ngx_string("move"), NGX_HTTP_MOVE },
76 { ngx_null_string, 0 }
80 static ngx_command_t ngx_http_dav_commands[] = {
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 },
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),
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),
103 { ngx_string("dav_access"),
104 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE123,
105 ngx_conf_set_access_slot,
106 NGX_HTTP_LOC_CONF_OFFSET,
107 offsetof(ngx_http_dav_loc_conf_t, access),
114 static ngx_http_module_t ngx_http_dav_module_ctx = {
115 NULL, /* preconfiguration */
116 ngx_http_dav_init, /* postconfiguration */
118 NULL, /* create main configuration */
119 NULL, /* init main configuration */
121 NULL, /* create server configuration */
122 NULL, /* merge server configuration */
124 ngx_http_dav_create_loc_conf, /* create location configuration */
125 ngx_http_dav_merge_loc_conf /* merge location configuration */
129 ngx_module_t ngx_http_dav_module = {
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 */
135 NULL, /* init module */
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
146 ngx_http_dav_handler(ngx_http_request_t *r)
149 ngx_http_dav_loc_conf_t *dlcf;
152 if (r->zero_in_uri) {
156 dlcf = ngx_http_get_module_loc_conf(r, ngx_http_dav_module);
158 if (!(r->method & dlcf->methods)) {
166 if (r->uri.data[r->uri.len - 1] == '/') {
167 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
168 "can not PUT to a collection");
169 return NGX_HTTP_CONFLICT;
172 r->request_body_in_file_only = 1;
173 r->request_body_in_persistent_file = 1;
174 r->request_body_in_clean_file = 1;
175 r->request_body_file_group_access = 1;
176 r->request_body_file_log_level = 0;
178 rc = ngx_http_read_client_request_body(r, ngx_http_dav_put_handler);
180 if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
186 case NGX_HTTP_DELETE:
188 return ngx_http_dav_delete_handler(r);
192 return ngx_http_dav_mkcol_handler(r, dlcf);
196 return ngx_http_dav_copy_move_handler(r);
200 return ngx_http_dav_copy_move_handler(r);
208 ngx_http_dav_put_handler(ngx_http_request_t *r)
212 ngx_str_t *temp, path;
215 ngx_ext_rename_file_t ext;
216 ngx_http_dav_loc_conf_t *dlcf;
218 ngx_http_map_uri_to_path(r, &path, &root, 0);
220 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
221 "http put filename: \"%s\"", path.data);
223 temp = &r->request_body->temp_file->file.name;
225 if (ngx_file_info(path.data, &fi) == -1) {
226 status = NGX_HTTP_CREATED;
229 status = NGX_HTTP_NO_CONTENT;
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);
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",
241 ngx_http_finalize_request(r, NGX_HTTP_CONFLICT);
246 dlcf = ngx_http_get_module_loc_conf(r, ngx_http_dav_module);
248 ext.access = dlcf->access;
249 ext.path_access = dlcf->access;
251 ext.create_path = dlcf->create_full_put_path;
253 ext.log_rename_error = 1;
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_file_info(path.data, &fi) == -1) {
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_file_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_file_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);
496 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
497 "http mkcol path: \"%s\"", path.data);
499 if (ngx_create_dir(path.data, ngx_dir_access(dlcf->access))
502 if (ngx_http_dav_location(r, path.data) != NGX_OK) {
503 return NGX_HTTP_INTERNAL_SERVER_ERROR;
506 return NGX_HTTP_CREATED;
509 return ngx_http_dav_error(r->connection->log, ngx_errno,
510 NGX_HTTP_CONFLICT, ngx_create_dir_n, path.data);
515 ngx_http_dav_copy_move_handler(ngx_http_request_t *r)
517 u_char *p, *host, *last, ch;
521 ngx_uint_t overwrite, slash, dir;
525 ngx_table_elt_t *dest, *over;
526 ngx_ext_rename_file_t ext;
527 ngx_http_dav_copy_ctx_t copy;
528 ngx_http_dav_loc_conf_t *dlcf;
530 if (r->headers_in.content_length_n > 0) {
531 return NGX_HTTP_UNSUPPORTED_MEDIA_TYPE;
534 dest = r->headers_in.destination;
537 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
538 "client sent no \"Destination\" header");
539 return NGX_HTTP_BAD_REQUEST;
542 len = r->headers_in.server.len;
545 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
546 "client sent no \"Host\" header");
547 return NGX_HTTP_BAD_REQUEST;
552 if (r->connection->ssl) {
553 if (ngx_strncmp(dest->value.data, "https://", sizeof("https://") - 1)
556 goto invalid_destination;
559 host = dest->value.data + sizeof("https://") - 1;
564 if (ngx_strncmp(dest->value.data, "http://", sizeof("http://") - 1)
567 goto invalid_destination;
570 host = dest->value.data + sizeof("http://") - 1;
573 if (ngx_strncmp(host, r->headers_in.server.data, len) != 0) {
574 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
575 "\"Destination\" URI \"%V\" is handled by "
576 "different repository than the source URI",
578 return NGX_HTTP_BAD_REQUEST;
581 last = dest->value.data + dest->value.len;
583 for (p = host + len; p < last; p++) {
585 goto destination_done;
591 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
592 "client sent invalid \"Destination\" header: \"%V\"",
594 return NGX_HTTP_BAD_REQUEST;
598 if ((r->uri.data[r->uri.len - 1] == '/' && *(last - 1) != '/')
599 || (r->uri.data[r->uri.len - 1] != '/' && *(last - 1) == '/'))
601 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
602 "both URI \"%V\" and \"Destination\" URI \"%V\" "
603 "should be either collections or non-collections",
604 &r->uri, &dest->value);
605 return NGX_HTTP_CONFLICT;
608 depth = ngx_http_dav_depth(r, NGX_HTTP_DAV_INFINITY_DEPTH);
610 if (depth != NGX_HTTP_DAV_INFINITY_DEPTH) {
612 if (r->method == NGX_HTTP_COPY) {
614 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
615 "\"Depth\" header must be 0 or infinity");
616 return NGX_HTTP_BAD_REQUEST;
620 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
621 "\"Depth\" header must be infinity");
622 return NGX_HTTP_BAD_REQUEST;
626 over = r->headers_in.overwrite;
629 if (over->value.len == 1) {
630 ch = over->value.data[0];
632 if (ch == 'T' || ch == 't') {
637 if (ch == 'F' || ch == 'f') {
644 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
645 "client sent invalid \"Overwrite\" header: \"%V\"",
647 return NGX_HTTP_BAD_REQUEST;
654 ngx_http_map_uri_to_path(r, &path, &root, 0);
656 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
657 "http copy from: \"%s\"", path.data);
661 r->uri.len = last - p;
664 ngx_http_map_uri_to_path(r, ©.path, &root, 0);
668 copy.path.len--; /* omit "\0" */
670 if (copy.path.data[copy.path.len - 1] == '/') {
673 copy.path.data[copy.path.len] = '\0';
679 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
680 "http copy to: \"%s\"", copy.path.data);
682 if (ngx_file_info(copy.path.data, &fi) == -1) {
685 if (err != NGX_ENOENT) {
686 return ngx_http_dav_error(r->connection->log, err,
687 NGX_HTTP_NOT_FOUND, ngx_file_info_n,
691 /* destination does not exist */
698 /* destination exists */
700 if (ngx_is_dir(&fi) && !slash) {
701 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
702 "\"%V\" could not be %Ved to collection \"%V\"",
703 &r->uri, &r->method_name, &dest->value);
704 return NGX_HTTP_CONFLICT;
708 ngx_log_error(NGX_LOG_ERR, r->connection->log, NGX_EEXIST,
709 "\"%s\" could not be created", copy.path.data);
710 return NGX_HTTP_PRECONDITION_FAILED;
713 dir = ngx_is_dir(&fi);
716 if (ngx_file_info(path.data, &fi) == -1) {
717 return ngx_http_dav_error(r->connection->log, ngx_errno,
718 NGX_HTTP_NOT_FOUND, ngx_file_info_n,
722 if (ngx_is_dir(&fi)) {
724 if (r->uri.data[r->uri.len - 1] != '/') {
725 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
726 "\"%V\" is collection", &r->uri);
727 return NGX_HTTP_BAD_REQUEST;
731 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
732 "http delete: \"%s\"", copy.path.data);
734 rc = ngx_http_dav_delete_path(r, ©.path, dir);
742 if (ngx_is_dir(&fi)) {
744 path.len -= 2; /* omit "/\0" */
746 if (r->method == NGX_HTTP_MOVE) {
747 if (ngx_rename_file(path.data, copy.path.data) != NGX_FILE_ERROR) {
748 return NGX_HTTP_CREATED;
752 if (ngx_create_dir(copy.path.data, ngx_file_access(&fi))
755 return ngx_http_dav_error(r->connection->log, ngx_errno,
757 ngx_create_dir_n, copy.path.data);
762 tree.init_handler = NULL;
763 tree.file_handler = ngx_http_dav_copy_tree_file;
764 tree.pre_tree_handler = ngx_http_dav_copy_dir;
765 tree.post_tree_handler = ngx_http_dav_copy_dir_time;
766 tree.spec_handler = ngx_http_dav_noop;
769 tree.log = r->connection->log;
771 if (ngx_walk_tree(&tree, &path) == NGX_OK) {
773 if (r->method == NGX_HTTP_MOVE) {
774 rc = ngx_http_dav_delete_path(r, &path, 1);
781 return NGX_HTTP_CREATED;
786 if (r->method == NGX_HTTP_MOVE) {
788 dlcf = ngx_http_get_module_loc_conf(r, ngx_http_dav_module);
791 ext.path_access = dlcf->access;
795 ext.log_rename_error = 0;
796 ext.log = r->connection->log;
798 if (ngx_ext_rename_file(&path, ©.path, &ext) == NGX_OK) {
799 return NGX_HTTP_NO_CONTENT;
802 if (ext.rename_error != NGX_EXDEV) {
804 if (ext.rename_error) {
805 ngx_log_error(NGX_LOG_CRIT, r->connection->log,
807 ngx_rename_file_n " \"%s\" to \"%s\" failed",
808 path.data, copy.path.data);
811 return NGX_HTTP_INTERNAL_SERVER_ERROR;
815 dlcf = ngx_http_get_module_loc_conf(r, ngx_http_dav_module);
817 tree.size = ngx_file_size(&fi);
818 tree.mtime = ngx_file_mtime(&fi);
819 tree.access = dlcf->access;
820 tree.log = r->connection->log;
822 if (ngx_http_dav_copy_file(&tree, path.data, copy.path.data) == NGX_OK)
824 if (r->method == NGX_HTTP_MOVE) {
825 rc = ngx_http_dav_delete_path(r, &path, 0);
832 return NGX_HTTP_NO_CONTENT;
836 return NGX_HTTP_INTERNAL_SERVER_ERROR;
841 ngx_http_dav_copy_dir(ngx_tree_ctx_t *ctx, ngx_str_t *path)
845 ngx_http_dav_copy_ctx_t *copy;
847 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0,
848 "http copy dir: \"%s\"", path->data);
852 len = copy->path.len + path->len;
854 dir = ngx_alloc(len + 1, ctx->log);
859 p = ngx_cpymem(dir, copy->path.data, copy->path.len);
860 (void) ngx_cpystrn(p, path->data + copy->len, path->len - copy->len + 1);
862 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0,
863 "http copy dir to: \"%s\"", dir);
865 if (ngx_create_dir(dir, ngx_dir_access(ctx->access)) == NGX_FILE_ERROR) {
866 (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_create_dir_n,
877 ngx_http_dav_copy_dir_time(ngx_tree_ctx_t *ctx, ngx_str_t *path)
881 ngx_http_dav_copy_ctx_t *copy;
883 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0,
884 "http copy dir time: \"%s\"", path->data);
888 len = copy->path.len + path->len;
890 dir = ngx_alloc(len + 1, ctx->log);
895 p = ngx_cpymem(dir, copy->path.data, copy->path.len);
896 (void) ngx_cpystrn(p, path->data + copy->len, path->len - copy->len + 1);
898 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0,
899 "http copy dir time to: \"%s\"", dir);
905 fd = ngx_open_file(dir, NGX_FILE_RDWR, NGX_FILE_OPEN, 0);
907 if (fd == NGX_INVALID_FILE) {
908 (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_open_file_n, dir);
912 if (ngx_set_file_time(NULL, fd, ctx->mtime) != NGX_OK) {
913 ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno,
914 ngx_set_file_time_n " \"%s\" failed", dir);
917 if (ngx_close_file(fd) == NGX_FILE_ERROR) {
918 ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno,
919 ngx_close_file_n " \"%s\" failed", dir);
927 if (ngx_set_file_time(dir, 0, ctx->mtime) != NGX_OK) {
928 ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno,
929 ngx_set_file_time_n " \"%s\" failed", dir);
941 ngx_http_dav_copy_tree_file(ngx_tree_ctx_t *ctx, ngx_str_t *path)
945 ngx_http_dav_copy_ctx_t *copy;
947 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0,
948 "http copy file: \"%s\"", path->data);
952 len = copy->path.len + path->len;
954 file = ngx_alloc(len + 1, ctx->log);
959 p = ngx_cpymem(file, copy->path.data, copy->path.len);
960 (void) ngx_cpystrn(p, path->data + copy->len, path->len - copy->len + 1);
962 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0,
963 "http copy file to: \"%s\"", file);
965 (void) ngx_http_dav_copy_file(ctx, path->data, file);
974 ngx_http_dav_copy_file(ngx_tree_ctx_t *ctx, u_char *from, u_char *to)
980 u_char buf[NGX_HTTP_DAV_COPY_BLOCK];
982 fd = ngx_open_file(from, NGX_FILE_RDONLY, NGX_FILE_OPEN, 0);
984 if (fd == NGX_INVALID_FILE) {
985 (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_open_file_n,
990 cfd = ngx_open_file(to, NGX_FILE_WRONLY, NGX_FILE_CREATE_OR_OPEN,
995 if (cfd == NGX_INVALID_FILE) {
996 (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_open_file_n, to);
1000 for (size = ctx->size; size > 0; size -= n) {
1002 n = ngx_read_fd(fd, buf, NGX_HTTP_DAV_COPY_BLOCK);
1004 if (n == NGX_FILE_ERROR) {
1005 ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno,
1006 ngx_read_fd_n " \"%s\" failed", from);
1010 if (ngx_write_fd(cfd, buf, n) == NGX_FILE_ERROR) {
1011 ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno,
1012 ngx_write_fd_n " \"%s\" failed", to);
1017 if (ngx_set_file_time(to, cfd, ctx->mtime) != NGX_OK) {
1018 ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno,
1019 ngx_set_file_time_n " \"%s\" failed", to);
1023 if (ngx_close_file(cfd) == NGX_FILE_ERROR) {
1024 ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno,
1025 ngx_close_file_n " \"%s\" failed", to);
1033 if (ngx_close_file(fd) == NGX_FILE_ERROR) {
1034 ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno,
1035 ngx_close_file_n " \"%s\" failed", from);
1043 ngx_http_dav_depth(ngx_http_request_t *r, ngx_int_t dflt)
1045 ngx_table_elt_t *depth;
1047 depth = r->headers_in.depth;
1049 if (depth == NULL) {
1053 if (depth->value.len == 1) {
1055 if (depth->value.data[0] == '0') {
1059 if (depth->value.data[0] == '1') {
1065 if (depth->value.len == sizeof("infinity") - 1
1066 && ngx_strcmp(depth->value.data, "infinity") == 0)
1068 return NGX_HTTP_DAV_INFINITY_DEPTH;
1072 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1073 "client sent invalid \"Depth\" header: \"%V\"",
1076 return NGX_HTTP_DAV_INVALID_DEPTH;
1081 ngx_http_dav_error(ngx_log_t *log, ngx_err_t err, ngx_int_t not_found,
1082 char *failed, u_char *path)
1087 if (err == NGX_ENOENT || err == NGX_ENOTDIR || err == NGX_ENAMETOOLONG) {
1088 level = NGX_LOG_ERR;
1091 } else if (err == NGX_EACCES || err == NGX_EPERM) {
1092 level = NGX_LOG_ERR;
1093 rc = NGX_HTTP_FORBIDDEN;
1095 } else if (err == NGX_EEXIST) {
1096 level = NGX_LOG_ERR;
1097 rc = NGX_HTTP_NOT_ALLOWED;
1099 } else if (err == NGX_ENOSPC) {
1100 level = NGX_LOG_CRIT;
1101 rc = NGX_HTTP_INSUFFICIENT_STORAGE;
1104 level = NGX_LOG_CRIT;
1105 rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
1108 ngx_log_error(level, log, err, "%s \"%s\" failed", failed, path);
1115 ngx_http_dav_location(ngx_http_request_t *r, u_char *path)
1118 ngx_http_core_loc_conf_t *clcf;
1120 r->headers_out.location = ngx_palloc(r->pool, sizeof(ngx_table_elt_t));
1121 if (r->headers_out.location == NULL) {
1125 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1127 if (!clcf->alias && clcf->root_lengths == NULL) {
1128 location = path + clcf->root.len;
1131 location = ngx_pnalloc(r->pool, r->uri.len);
1132 if (location == NULL) {
1136 ngx_memcpy(location, r->uri.data, r->uri.len);
1140 * we do not need to set the r->headers_out.location->hash and
1141 * r->headers_out.location->key fields
1144 r->headers_out.location->value.len = r->uri.len;
1145 r->headers_out.location->value.data = location;
1152 ngx_http_dav_create_loc_conf(ngx_conf_t *cf)
1154 ngx_http_dav_loc_conf_t *conf;
1156 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_dav_loc_conf_t));
1158 return NGX_CONF_ERROR;
1162 * set by ngx_pcalloc():
1164 * conf->methods = 0;
1167 conf->min_delete_depth = NGX_CONF_UNSET_UINT;
1168 conf->access = NGX_CONF_UNSET_UINT;
1169 conf->create_full_put_path = NGX_CONF_UNSET;
1176 ngx_http_dav_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
1178 ngx_http_dav_loc_conf_t *prev = parent;
1179 ngx_http_dav_loc_conf_t *conf = child;
1181 ngx_conf_merge_bitmask_value(conf->methods, prev->methods,
1182 (NGX_CONF_BITMASK_SET|NGX_HTTP_DAV_OFF));
1184 ngx_conf_merge_uint_value(conf->min_delete_depth,
1185 prev->min_delete_depth, 0);
1187 ngx_conf_merge_uint_value(conf->access, prev->access, 0600);
1189 ngx_conf_merge_value(conf->create_full_put_path,
1190 prev->create_full_put_path, 0);
1197 ngx_http_dav_init(ngx_conf_t *cf)
1199 ngx_http_handler_pt *h;
1200 ngx_http_core_main_conf_t *cmcf;
1202 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
1204 h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
1209 *h = ngx_http_dav_handler;