Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright (C) Igor Sysoev |
Maxim Konovalov | f8d59e3 | 2012-01-18 15:07:43 +0000 | [diff] [blame] | 4 | * Copyright (C) Nginx, Inc. |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | |
| 8 | #include <ngx_config.h> |
| 9 | #include <ngx_core.h> |
| 10 | #include <ngx_http.h> |
| 11 | |
| 12 | |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 13 | #define NGX_HTTP_DAV_OFF 2 |
| 14 | |
| 15 | |
| 16 | #define NGX_HTTP_DAV_NO_DEPTH -3 |
| 17 | #define NGX_HTTP_DAV_INVALID_DEPTH -2 |
| 18 | #define NGX_HTTP_DAV_INFINITY_DEPTH -1 |
| 19 | |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 20 | |
| 21 | typedef struct { |
| 22 | ngx_uint_t methods; |
Igor Sysoev | b71c690 | 2006-08-04 16:04:04 +0000 | [diff] [blame] | 23 | ngx_uint_t access; |
Igor Sysoev | 8dd4053 | 2007-12-30 11:46:03 +0000 | [diff] [blame] | 24 | ngx_uint_t min_delete_depth; |
| 25 | ngx_flag_t create_full_put_path; |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 26 | } ngx_http_dav_loc_conf_t; |
| 27 | |
| 28 | |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 29 | typedef struct { |
| 30 | ngx_str_t path; |
| 31 | size_t len; |
| 32 | } ngx_http_dav_copy_ctx_t; |
| 33 | |
| 34 | |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 35 | static ngx_int_t ngx_http_dav_handler(ngx_http_request_t *r); |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 36 | |
| 37 | static void ngx_http_dav_put_handler(ngx_http_request_t *r); |
| 38 | |
| 39 | static ngx_int_t ngx_http_dav_delete_handler(ngx_http_request_t *r); |
Igor Sysoev | a27b0b7 | 2008-01-03 19:18:25 +0000 | [diff] [blame] | 40 | static ngx_int_t ngx_http_dav_delete_path(ngx_http_request_t *r, |
| 41 | ngx_str_t *path, ngx_uint_t dir); |
Igor Sysoev | 1980cc1 | 2006-12-25 12:38:44 +0000 | [diff] [blame] | 42 | static ngx_int_t ngx_http_dav_delete_dir(ngx_tree_ctx_t *ctx, ngx_str_t *path); |
| 43 | static ngx_int_t ngx_http_dav_delete_file(ngx_tree_ctx_t *ctx, ngx_str_t *path); |
Igor Sysoev | a27b0b7 | 2008-01-03 19:18:25 +0000 | [diff] [blame] | 44 | static ngx_int_t ngx_http_dav_noop(ngx_tree_ctx_t *ctx, ngx_str_t *path); |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 45 | |
| 46 | static ngx_int_t ngx_http_dav_mkcol_handler(ngx_http_request_t *r, |
| 47 | ngx_http_dav_loc_conf_t *dlcf); |
| 48 | |
| 49 | static ngx_int_t ngx_http_dav_copy_move_handler(ngx_http_request_t *r); |
| 50 | static ngx_int_t ngx_http_dav_copy_dir(ngx_tree_ctx_t *ctx, ngx_str_t *path); |
| 51 | static ngx_int_t ngx_http_dav_copy_dir_time(ngx_tree_ctx_t *ctx, |
| 52 | ngx_str_t *path); |
Igor Sysoev | cea9034 | 2008-01-03 21:44:38 +0000 | [diff] [blame] | 53 | static ngx_int_t ngx_http_dav_copy_tree_file(ngx_tree_ctx_t *ctx, |
| 54 | ngx_str_t *path); |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 55 | |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 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, |
Igor Sysoev | 7bdb720 | 2006-04-19 15:30:56 +0000 | [diff] [blame] | 58 | ngx_int_t not_found, char *failed, u_char *path); |
Ruslan Ermilov | 1953260 | 2019-12-16 15:19:01 +0300 | [diff] [blame] | 59 | static ngx_int_t ngx_http_dav_location(ngx_http_request_t *r); |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 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); |
Igor Sysoev | da173ab | 2006-08-30 10:39:17 +0000 | [diff] [blame] | 63 | static ngx_int_t ngx_http_dav_init(ngx_conf_t *cf); |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 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 }, |
Igor Sysoev | 7bdb720 | 2006-04-19 15:30:56 +0000 | [diff] [blame] | 70 | { ngx_string("mkcol"), NGX_HTTP_MKCOL }, |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 71 | { ngx_string("copy"), NGX_HTTP_COPY }, |
| 72 | { ngx_string("move"), NGX_HTTP_MOVE }, |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 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 | |
Igor Sysoev | 7bdb720 | 2006-04-19 15:30:56 +0000 | [diff] [blame] | 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 | |
Igor Sysoev | 8dd4053 | 2007-12-30 11:46:03 +0000 | [diff] [blame] | 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 | |
Igor Sysoev | b71c690 | 2006-08-04 16:04:04 +0000 | [diff] [blame] | 100 | { ngx_string("dav_access"), |
Igor Sysoev | ef31643 | 2006-08-16 13:09:33 +0000 | [diff] [blame] | 101 | NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE123, |
Igor Sysoev | 471b92e | 2007-07-13 08:32:12 +0000 | [diff] [blame] | 102 | ngx_conf_set_access_slot, |
Igor Sysoev | b71c690 | 2006-08-04 16:04:04 +0000 | [diff] [blame] | 103 | NGX_HTTP_LOC_CONF_OFFSET, |
Igor Sysoev | 471b92e | 2007-07-13 08:32:12 +0000 | [diff] [blame] | 104 | offsetof(ngx_http_dav_loc_conf_t, access), |
Igor Sysoev | b71c690 | 2006-08-04 16:04:04 +0000 | [diff] [blame] | 105 | NULL }, |
| 106 | |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 107 | ngx_null_command |
| 108 | }; |
| 109 | |
| 110 | |
Igor Sysoev | 8f12558 | 2006-07-28 15:16:17 +0000 | [diff] [blame] | 111 | static ngx_http_module_t ngx_http_dav_module_ctx = { |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 112 | NULL, /* preconfiguration */ |
Igor Sysoev | da173ab | 2006-08-30 10:39:17 +0000 | [diff] [blame] | 113 | ngx_http_dav_init, /* postconfiguration */ |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 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 */ |
Igor Sysoev | da173ab | 2006-08-30 10:39:17 +0000 | [diff] [blame] | 132 | NULL, /* init module */ |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 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; |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 146 | ngx_http_dav_loc_conf_t *dlcf; |
| 147 | |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 148 | dlcf = ngx_http_get_module_loc_conf(r, ngx_http_dav_module); |
| 149 | |
| 150 | if (!(r->method & dlcf->methods)) { |
| 151 | return NGX_DECLINED; |
| 152 | } |
| 153 | |
| 154 | switch (r->method) { |
| 155 | |
| 156 | case NGX_HTTP_PUT: |
| 157 | |
| 158 | if (r->uri.data[r->uri.len - 1] == '/') { |
Igor Sysoev | ab32535 | 2007-12-30 09:44:02 +0000 | [diff] [blame] | 159 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
Ruslan Ermilov | a823c55 | 2011-09-19 14:48:29 +0000 | [diff] [blame] | 160 | "cannot PUT to a collection"); |
Igor Sysoev | ab32535 | 2007-12-30 09:44:02 +0000 | [diff] [blame] | 161 | return NGX_HTTP_CONFLICT; |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Maxim Dounin | 692ba44 | 2016-05-16 20:37:23 +0300 | [diff] [blame] | 164 | if (r->headers_in.content_range) { |
| 165 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
| 166 | "PUT with range is unsupported"); |
| 167 | return NGX_HTTP_NOT_IMPLEMENTED; |
| 168 | } |
| 169 | |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 170 | r->request_body_in_file_only = 1; |
| 171 | r->request_body_in_persistent_file = 1; |
Igor Sysoev | cd5b99a | 2007-01-25 08:45:04 +0000 | [diff] [blame] | 172 | r->request_body_in_clean_file = 1; |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 173 | r->request_body_file_group_access = 1; |
Igor Sysoev | 7bdb720 | 2006-04-19 15:30:56 +0000 | [diff] [blame] | 174 | r->request_body_file_log_level = 0; |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 175 | |
| 176 | rc = ngx_http_read_client_request_body(r, ngx_http_dav_put_handler); |
| 177 | |
| 178 | if (rc >= NGX_HTTP_SPECIAL_RESPONSE) { |
| 179 | return rc; |
| 180 | } |
| 181 | |
| 182 | return NGX_DONE; |
| 183 | |
| 184 | case NGX_HTTP_DELETE: |
| 185 | |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 186 | return ngx_http_dav_delete_handler(r); |
Igor Sysoev | 7bdb720 | 2006-04-19 15:30:56 +0000 | [diff] [blame] | 187 | |
| 188 | case NGX_HTTP_MKCOL: |
| 189 | |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 190 | return ngx_http_dav_mkcol_handler(r, dlcf); |
Igor Sysoev | 7bdb720 | 2006-04-19 15:30:56 +0000 | [diff] [blame] | 191 | |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 192 | case NGX_HTTP_COPY: |
Igor Sysoev | 7bdb720 | 2006-04-19 15:30:56 +0000 | [diff] [blame] | 193 | |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 194 | return ngx_http_dav_copy_move_handler(r); |
Igor Sysoev | 7bdb720 | 2006-04-19 15:30:56 +0000 | [diff] [blame] | 195 | |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 196 | case NGX_HTTP_MOVE: |
Igor Sysoev | 7bdb720 | 2006-04-19 15:30:56 +0000 | [diff] [blame] | 197 | |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 198 | return ngx_http_dav_copy_move_handler(r); |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | return NGX_DECLINED; |
| 202 | } |
| 203 | |
| 204 | |
| 205 | static void |
| 206 | ngx_http_dav_put_handler(ngx_http_request_t *r) |
| 207 | { |
Igor Sysoev | 0e5f86d | 2006-10-12 13:36:54 +0000 | [diff] [blame] | 208 | size_t root; |
Igor Sysoev | da173ab | 2006-08-30 10:39:17 +0000 | [diff] [blame] | 209 | time_t date; |
Igor Sysoev | 7bdb720 | 2006-04-19 15:30:56 +0000 | [diff] [blame] | 210 | ngx_str_t *temp, path; |
Igor Sysoev | 8192251 | 2008-01-03 22:18:21 +0000 | [diff] [blame] | 211 | ngx_uint_t status; |
Igor Sysoev | 7bdb720 | 2006-04-19 15:30:56 +0000 | [diff] [blame] | 212 | ngx_file_info_t fi; |
Igor Sysoev | 8192251 | 2008-01-03 22:18:21 +0000 | [diff] [blame] | 213 | ngx_ext_rename_file_t ext; |
Igor Sysoev | 7bdb720 | 2006-04-19 15:30:56 +0000 | [diff] [blame] | 214 | ngx_http_dav_loc_conf_t *dlcf; |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 215 | |
Roman Arutyunyan | ab9021f | 2018-02-07 16:44:29 +0300 | [diff] [blame] | 216 | if (r->request_body == NULL) { |
| 217 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
| 218 | "PUT request body is unavailable"); |
| 219 | ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); |
| 220 | return; |
| 221 | } |
| 222 | |
| 223 | if (r->request_body->temp_file == NULL) { |
| 224 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
| 225 | "PUT request body must be in a file"); |
Maxim Dounin | 248bc41 | 2012-11-21 00:51:37 +0000 | [diff] [blame] | 226 | ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); |
| 227 | return; |
| 228 | } |
| 229 | |
FengGu | bfa5673 | 2014-07-30 14:45:08 +0800 | [diff] [blame] | 230 | if (ngx_http_map_uri_to_path(r, &path, &root, 0) == NULL) { |
| 231 | ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); |
| 232 | return; |
| 233 | } |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 234 | |
Igor Sysoev | 9db33c9 | 2009-08-20 13:37:26 +0000 | [diff] [blame] | 235 | path.len--; |
| 236 | |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 237 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
| 238 | "http put filename: \"%s\"", path.data); |
| 239 | |
| 240 | temp = &r->request_body->temp_file->file.name; |
| 241 | |
Igor Sysoev | ef91975 | 2009-04-29 19:28:52 +0000 | [diff] [blame] | 242 | if (ngx_file_info(path.data, &fi) == NGX_FILE_ERROR) { |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 243 | status = NGX_HTTP_CREATED; |
| 244 | |
| 245 | } else { |
| 246 | status = NGX_HTTP_NO_CONTENT; |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 247 | |
Igor Sysoev | 1765f47 | 2006-07-07 16:33:19 +0000 | [diff] [blame] | 248 | if (ngx_is_dir(&fi)) { |
| 249 | ngx_log_error(NGX_LOG_ERR, r->connection->log, NGX_EISDIR, |
| 250 | "\"%s\" could not be created", path.data); |
| 251 | |
| 252 | if (ngx_delete_file(temp->data) == NGX_FILE_ERROR) { |
| 253 | ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno, |
| 254 | ngx_delete_file_n " \"%s\" failed", |
| 255 | temp->data); |
| 256 | } |
| 257 | |
| 258 | ngx_http_finalize_request(r, NGX_HTTP_CONFLICT); |
| 259 | return; |
Igor Sysoev | 3186bf5 | 2006-07-03 16:49:20 +0000 | [diff] [blame] | 260 | } |
Igor Sysoev | 7bdb720 | 2006-04-19 15:30:56 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Igor Sysoev | b71c690 | 2006-08-04 16:04:04 +0000 | [diff] [blame] | 263 | dlcf = ngx_http_get_module_loc_conf(r, ngx_http_dav_module); |
| 264 | |
Igor Sysoev | 8192251 | 2008-01-03 22:18:21 +0000 | [diff] [blame] | 265 | ext.access = dlcf->access; |
Igor Sysoev | 524f54f | 2008-12-10 14:53:45 +0000 | [diff] [blame] | 266 | ext.path_access = dlcf->access; |
Igor Sysoev | 8192251 | 2008-01-03 22:18:21 +0000 | [diff] [blame] | 267 | ext.time = -1; |
| 268 | ext.create_path = dlcf->create_full_put_path; |
Igor Sysoev | 34cec29 | 2008-03-03 16:32:16 +0000 | [diff] [blame] | 269 | ext.delete_file = 1; |
Igor Sysoev | 8192251 | 2008-01-03 22:18:21 +0000 | [diff] [blame] | 270 | ext.log = r->connection->log; |
Igor Sysoev | b71c690 | 2006-08-04 16:04:04 +0000 | [diff] [blame] | 271 | |
Igor Sysoev | da173ab | 2006-08-30 10:39:17 +0000 | [diff] [blame] | 272 | if (r->headers_in.date) { |
Maxim Dounin | 1e49bdf | 2015-06-11 20:42:31 +0300 | [diff] [blame] | 273 | date = ngx_parse_http_time(r->headers_in.date->value.data, |
Igor Sysoev | da173ab | 2006-08-30 10:39:17 +0000 | [diff] [blame] | 274 | r->headers_in.date->value.len); |
| 275 | |
| 276 | if (date != NGX_ERROR) { |
Igor Sysoev | 8192251 | 2008-01-03 22:18:21 +0000 | [diff] [blame] | 277 | ext.time = date; |
| 278 | ext.fd = r->request_body->temp_file->file.fd; |
Igor Sysoev | da173ab | 2006-08-30 10:39:17 +0000 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
Igor Sysoev | 8192251 | 2008-01-03 22:18:21 +0000 | [diff] [blame] | 282 | if (ngx_ext_rename_file(temp, &path, &ext) != NGX_OK) { |
| 283 | ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); |
| 284 | return; |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 287 | if (status == NGX_HTTP_CREATED) { |
Ruslan Ermilov | 1953260 | 2019-12-16 15:19:01 +0300 | [diff] [blame] | 288 | if (ngx_http_dav_location(r) != NGX_OK) { |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 289 | ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); |
| 290 | return; |
| 291 | } |
Igor Sysoev | 4ecb4d7 | 2006-04-21 12:06:44 +0000 | [diff] [blame] | 292 | |
| 293 | r->headers_out.content_length_n = 0; |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | r->headers_out.status = status; |
| 297 | r->header_only = 1; |
| 298 | |
| 299 | ngx_http_finalize_request(r, ngx_http_send_header(r)); |
| 300 | return; |
| 301 | } |
| 302 | |
| 303 | |
Igor Sysoev | 7bdb720 | 2006-04-19 15:30:56 +0000 | [diff] [blame] | 304 | static ngx_int_t |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 305 | ngx_http_dav_delete_handler(ngx_http_request_t *r) |
| 306 | { |
Igor Sysoev | 8dd4053 | 2007-12-30 11:46:03 +0000 | [diff] [blame] | 307 | size_t root; |
| 308 | ngx_err_t err; |
| 309 | ngx_int_t rc, depth; |
| 310 | ngx_uint_t i, d, dir; |
| 311 | ngx_str_t path; |
| 312 | ngx_file_info_t fi; |
| 313 | ngx_http_dav_loc_conf_t *dlcf; |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 314 | |
Maxim Dounin | f8071d0 | 2019-12-23 20:39:27 +0300 | [diff] [blame] | 315 | if (r->headers_in.content_length_n > 0 || r->headers_in.chunked) { |
Igor Sysoev | b06db42 | 2007-12-30 10:24:43 +0000 | [diff] [blame] | 316 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
| 317 | "DELETE with body is unsupported"); |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 318 | return NGX_HTTP_UNSUPPORTED_MEDIA_TYPE; |
| 319 | } |
| 320 | |
Igor Sysoev | 8dd4053 | 2007-12-30 11:46:03 +0000 | [diff] [blame] | 321 | dlcf = ngx_http_get_module_loc_conf(r, ngx_http_dav_module); |
| 322 | |
| 323 | if (dlcf->min_delete_depth) { |
| 324 | d = 0; |
| 325 | |
| 326 | for (i = 0; i < r->uri.len; /* void */) { |
| 327 | if (r->uri.data[i++] == '/') { |
| 328 | if (++d >= dlcf->min_delete_depth && i < r->uri.len) { |
| 329 | goto ok; |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
| 335 | "insufficient URI depth:%i to DELETE", d); |
| 336 | return NGX_HTTP_CONFLICT; |
| 337 | } |
| 338 | |
| 339 | ok: |
| 340 | |
FengGu | bfa5673 | 2014-07-30 14:45:08 +0800 | [diff] [blame] | 341 | if (ngx_http_map_uri_to_path(r, &path, &root, 0) == NULL) { |
| 342 | return NGX_HTTP_INTERNAL_SERVER_ERROR; |
| 343 | } |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 344 | |
| 345 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
| 346 | "http delete filename: \"%s\"", path.data); |
| 347 | |
Igor Sysoev | 3ec15dd | 2010-04-22 17:15:42 +0000 | [diff] [blame] | 348 | if (ngx_link_info(path.data, &fi) == NGX_FILE_ERROR) { |
Igor Sysoev | b06db42 | 2007-12-30 10:24:43 +0000 | [diff] [blame] | 349 | err = ngx_errno; |
| 350 | |
| 351 | rc = (err == NGX_ENOTDIR) ? NGX_HTTP_CONFLICT : NGX_HTTP_NOT_FOUND; |
| 352 | |
| 353 | return ngx_http_dav_error(r->connection->log, err, |
Igor Sysoev | 3ec15dd | 2010-04-22 17:15:42 +0000 | [diff] [blame] | 354 | rc, ngx_link_info_n, path.data); |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | if (ngx_is_dir(&fi)) { |
| 358 | |
| 359 | if (r->uri.data[r->uri.len - 1] != '/') { |
Igor Sysoev | b06db42 | 2007-12-30 10:24:43 +0000 | [diff] [blame] | 360 | ngx_log_error(NGX_LOG_ERR, r->connection->log, NGX_EISDIR, |
| 361 | "DELETE \"%s\" failed", path.data); |
| 362 | return NGX_HTTP_CONFLICT; |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | depth = ngx_http_dav_depth(r, NGX_HTTP_DAV_INFINITY_DEPTH); |
| 366 | |
| 367 | if (depth != NGX_HTTP_DAV_INFINITY_DEPTH) { |
Igor Sysoev | b06db42 | 2007-12-30 10:24:43 +0000 | [diff] [blame] | 368 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
| 369 | "\"Depth\" header must be infinity"); |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 370 | return NGX_HTTP_BAD_REQUEST; |
| 371 | } |
| 372 | |
| 373 | path.len -= 2; /* omit "/\0" */ |
| 374 | |
| 375 | dir = 1; |
| 376 | |
| 377 | } else { |
| 378 | |
Igor Sysoev | ac487bf | 2008-01-03 20:43:12 +0000 | [diff] [blame] | 379 | /* |
| 380 | * we do not need to test (r->uri.data[r->uri.len - 1] == '/') |
Igor Sysoev | 3ec15dd | 2010-04-22 17:15:42 +0000 | [diff] [blame] | 381 | * because ngx_link_info("/file/") returned NGX_ENOTDIR above |
Igor Sysoev | ac487bf | 2008-01-03 20:43:12 +0000 | [diff] [blame] | 382 | */ |
| 383 | |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 384 | depth = ngx_http_dav_depth(r, 0); |
| 385 | |
| 386 | if (depth != 0 && depth != NGX_HTTP_DAV_INFINITY_DEPTH) { |
Igor Sysoev | b06db42 | 2007-12-30 10:24:43 +0000 | [diff] [blame] | 387 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
| 388 | "\"Depth\" header must be 0 or infinity"); |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 389 | return NGX_HTTP_BAD_REQUEST; |
| 390 | } |
| 391 | |
| 392 | dir = 0; |
| 393 | } |
| 394 | |
| 395 | rc = ngx_http_dav_delete_path(r, &path, dir); |
| 396 | |
| 397 | if (rc == NGX_OK) { |
| 398 | return NGX_HTTP_NO_CONTENT; |
| 399 | } |
| 400 | |
| 401 | return rc; |
| 402 | } |
| 403 | |
| 404 | |
| 405 | static ngx_int_t |
Igor Sysoev | a27b0b7 | 2008-01-03 19:18:25 +0000 | [diff] [blame] | 406 | ngx_http_dav_delete_path(ngx_http_request_t *r, ngx_str_t *path, ngx_uint_t dir) |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 407 | { |
Igor Sysoev | a27b0b7 | 2008-01-03 19:18:25 +0000 | [diff] [blame] | 408 | char *failed; |
| 409 | ngx_tree_ctx_t tree; |
| 410 | |
| 411 | if (dir) { |
| 412 | |
| 413 | tree.init_handler = NULL; |
| 414 | tree.file_handler = ngx_http_dav_delete_file; |
| 415 | tree.pre_tree_handler = ngx_http_dav_noop; |
| 416 | tree.post_tree_handler = ngx_http_dav_delete_dir; |
| 417 | tree.spec_handler = ngx_http_dav_delete_file; |
| 418 | tree.data = NULL; |
| 419 | tree.alloc = 0; |
| 420 | tree.log = r->connection->log; |
| 421 | |
| 422 | /* TODO: 207 */ |
| 423 | |
| 424 | if (ngx_walk_tree(&tree, path) != NGX_OK) { |
| 425 | return NGX_HTTP_INTERNAL_SERVER_ERROR; |
| 426 | } |
| 427 | |
| 428 | if (ngx_delete_dir(path->data) != NGX_FILE_ERROR) { |
| 429 | return NGX_OK; |
| 430 | } |
| 431 | |
| 432 | failed = ngx_delete_dir_n; |
| 433 | |
| 434 | } else { |
| 435 | |
| 436 | if (ngx_delete_file(path->data) != NGX_FILE_ERROR) { |
| 437 | return NGX_OK; |
| 438 | } |
| 439 | |
| 440 | failed = ngx_delete_file_n; |
| 441 | } |
| 442 | |
| 443 | return ngx_http_dav_error(r->connection->log, ngx_errno, |
| 444 | NGX_HTTP_NOT_FOUND, failed, path->data); |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | |
| 448 | static ngx_int_t |
| 449 | ngx_http_dav_delete_dir(ngx_tree_ctx_t *ctx, ngx_str_t *path) |
| 450 | { |
| 451 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0, |
| 452 | "http delete dir: \"%s\"", path->data); |
| 453 | |
| 454 | if (ngx_delete_dir(path->data) == NGX_FILE_ERROR) { |
| 455 | |
| 456 | /* TODO: add to 207 */ |
| 457 | |
| 458 | (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_delete_dir_n, |
| 459 | path->data); |
| 460 | } |
| 461 | |
| 462 | return NGX_OK; |
| 463 | } |
| 464 | |
| 465 | |
| 466 | static ngx_int_t |
| 467 | ngx_http_dav_delete_file(ngx_tree_ctx_t *ctx, ngx_str_t *path) |
| 468 | { |
| 469 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0, |
| 470 | "http delete file: \"%s\"", path->data); |
| 471 | |
| 472 | if (ngx_delete_file(path->data) == NGX_FILE_ERROR) { |
| 473 | |
| 474 | /* TODO: add to 207 */ |
| 475 | |
| 476 | (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_delete_file_n, |
| 477 | path->data); |
| 478 | } |
| 479 | |
| 480 | return NGX_OK; |
| 481 | } |
| 482 | |
| 483 | |
| 484 | static ngx_int_t |
Igor Sysoev | a27b0b7 | 2008-01-03 19:18:25 +0000 | [diff] [blame] | 485 | ngx_http_dav_noop(ngx_tree_ctx_t *ctx, ngx_str_t *path) |
| 486 | { |
| 487 | return NGX_OK; |
| 488 | } |
| 489 | |
| 490 | |
| 491 | static ngx_int_t |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 492 | ngx_http_dav_mkcol_handler(ngx_http_request_t *r, ngx_http_dav_loc_conf_t *dlcf) |
| 493 | { |
Igor Sysoev | 369791a | 2008-01-03 19:13:04 +0000 | [diff] [blame] | 494 | u_char *p; |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 495 | size_t root; |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 496 | ngx_str_t path; |
| 497 | |
Maxim Dounin | f8071d0 | 2019-12-23 20:39:27 +0300 | [diff] [blame] | 498 | if (r->headers_in.content_length_n > 0 || r->headers_in.chunked) { |
Igor Sysoev | 369791a | 2008-01-03 19:13:04 +0000 | [diff] [blame] | 499 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
| 500 | "MKCOL with body is unsupported"); |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 501 | return NGX_HTTP_UNSUPPORTED_MEDIA_TYPE; |
| 502 | } |
| 503 | |
Igor Sysoev | 369791a | 2008-01-03 19:13:04 +0000 | [diff] [blame] | 504 | if (r->uri.data[r->uri.len - 1] != '/') { |
| 505 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
| 506 | "MKCOL can create a collection only"); |
| 507 | return NGX_HTTP_CONFLICT; |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 508 | } |
| 509 | |
Igor Sysoev | 369791a | 2008-01-03 19:13:04 +0000 | [diff] [blame] | 510 | p = ngx_http_map_uri_to_path(r, &path, &root, 0); |
FengGu | bfa5673 | 2014-07-30 14:45:08 +0800 | [diff] [blame] | 511 | if (p == NULL) { |
| 512 | return NGX_HTTP_INTERNAL_SERVER_ERROR; |
| 513 | } |
Igor Sysoev | 369791a | 2008-01-03 19:13:04 +0000 | [diff] [blame] | 514 | |
| 515 | *(p - 1) = '\0'; |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 516 | |
| 517 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
| 518 | "http mkcol path: \"%s\"", path.data); |
| 519 | |
| 520 | if (ngx_create_dir(path.data, ngx_dir_access(dlcf->access)) |
| 521 | != NGX_FILE_ERROR) |
| 522 | { |
Ruslan Ermilov | 1953260 | 2019-12-16 15:19:01 +0300 | [diff] [blame] | 523 | if (ngx_http_dav_location(r) != NGX_OK) { |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 524 | return NGX_HTTP_INTERNAL_SERVER_ERROR; |
| 525 | } |
| 526 | |
| 527 | return NGX_HTTP_CREATED; |
| 528 | } |
| 529 | |
| 530 | return ngx_http_dav_error(r->connection->log, ngx_errno, |
Igor Sysoev | 369791a | 2008-01-03 19:13:04 +0000 | [diff] [blame] | 531 | NGX_HTTP_CONFLICT, ngx_create_dir_n, path.data); |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | |
| 535 | static ngx_int_t |
| 536 | ngx_http_dav_copy_move_handler(ngx_http_request_t *r) |
| 537 | { |
Igor Sysoev | 5fd0931 | 2008-05-15 14:44:47 +0000 | [diff] [blame] | 538 | u_char *p, *host, *last, ch; |
Igor Sysoev | 3f24ae2 | 2007-12-29 15:30:39 +0000 | [diff] [blame] | 539 | size_t len, root; |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 540 | ngx_err_t err; |
| 541 | ngx_int_t rc, depth; |
Igor Sysoev | a552ab4 | 2009-09-25 09:30:06 +0000 | [diff] [blame] | 542 | ngx_uint_t overwrite, slash, dir, flags; |
| 543 | ngx_str_t path, uri, duri, args; |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 544 | ngx_tree_ctx_t tree; |
Igor Sysoev | b7a09c5 | 2009-08-12 12:05:33 +0000 | [diff] [blame] | 545 | ngx_copy_file_t cf; |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 546 | ngx_file_info_t fi; |
Igor Sysoev | 5fd0931 | 2008-05-15 14:44:47 +0000 | [diff] [blame] | 547 | ngx_table_elt_t *dest, *over; |
Igor Sysoev | 524f54f | 2008-12-10 14:53:45 +0000 | [diff] [blame] | 548 | ngx_ext_rename_file_t ext; |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 549 | ngx_http_dav_copy_ctx_t copy; |
Igor Sysoev | cea9034 | 2008-01-03 21:44:38 +0000 | [diff] [blame] | 550 | ngx_http_dav_loc_conf_t *dlcf; |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 551 | |
Maxim Dounin | f8071d0 | 2019-12-23 20:39:27 +0300 | [diff] [blame] | 552 | if (r->headers_in.content_length_n > 0 || r->headers_in.chunked) { |
| 553 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
| 554 | "COPY and MOVE with body are unsupported"); |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 555 | return NGX_HTTP_UNSUPPORTED_MEDIA_TYPE; |
| 556 | } |
| 557 | |
| 558 | dest = r->headers_in.destination; |
| 559 | |
| 560 | if (dest == NULL) { |
| 561 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
| 562 | "client sent no \"Destination\" header"); |
| 563 | return NGX_HTTP_BAD_REQUEST; |
| 564 | } |
| 565 | |
Igor Sysoev | 34cd1cc | 2010-06-10 08:17:16 +0000 | [diff] [blame] | 566 | p = dest->value.data; |
| 567 | /* there is always '\0' even after empty header value */ |
| 568 | if (p[0] == '/') { |
| 569 | last = p + dest->value.len; |
| 570 | goto destination_done; |
| 571 | } |
| 572 | |
Igor Sysoev | 5fd0931 | 2008-05-15 14:44:47 +0000 | [diff] [blame] | 573 | len = r->headers_in.server.len; |
Igor Sysoev | 3f24ae2 | 2007-12-29 15:30:39 +0000 | [diff] [blame] | 574 | |
Igor Sysoev | 5fd0931 | 2008-05-15 14:44:47 +0000 | [diff] [blame] | 575 | if (len == 0) { |
Igor Sysoev | 3f24ae2 | 2007-12-29 15:30:39 +0000 | [diff] [blame] | 576 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
| 577 | "client sent no \"Host\" header"); |
| 578 | return NGX_HTTP_BAD_REQUEST; |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | #if (NGX_HTTP_SSL) |
| 582 | |
| 583 | if (r->connection->ssl) { |
| 584 | if (ngx_strncmp(dest->value.data, "https://", sizeof("https://") - 1) |
| 585 | != 0) |
| 586 | { |
| 587 | goto invalid_destination; |
| 588 | } |
| 589 | |
Igor Sysoev | 5fd0931 | 2008-05-15 14:44:47 +0000 | [diff] [blame] | 590 | host = dest->value.data + sizeof("https://") - 1; |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 591 | |
| 592 | } else |
| 593 | #endif |
| 594 | { |
| 595 | if (ngx_strncmp(dest->value.data, "http://", sizeof("http://") - 1) |
| 596 | != 0) |
| 597 | { |
| 598 | goto invalid_destination; |
| 599 | } |
| 600 | |
Igor Sysoev | 5fd0931 | 2008-05-15 14:44:47 +0000 | [diff] [blame] | 601 | host = dest->value.data + sizeof("http://") - 1; |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 602 | } |
| 603 | |
Igor Sysoev | 5fd0931 | 2008-05-15 14:44:47 +0000 | [diff] [blame] | 604 | if (ngx_strncmp(host, r->headers_in.server.data, len) != 0) { |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 605 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
Igor Sysoev | 58d04fe | 2008-01-03 20:09:51 +0000 | [diff] [blame] | 606 | "\"Destination\" URI \"%V\" is handled by " |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 607 | "different repository than the source URI", |
| 608 | &dest->value); |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 609 | return NGX_HTTP_BAD_REQUEST; |
| 610 | } |
| 611 | |
| 612 | last = dest->value.data + dest->value.len; |
| 613 | |
Igor Sysoev | 5fd0931 | 2008-05-15 14:44:47 +0000 | [diff] [blame] | 614 | for (p = host + len; p < last; p++) { |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 615 | if (*p == '/') { |
| 616 | goto destination_done; |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | invalid_destination: |
| 621 | |
| 622 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
| 623 | "client sent invalid \"Destination\" header: \"%V\"", |
| 624 | &dest->value); |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 625 | return NGX_HTTP_BAD_REQUEST; |
| 626 | |
| 627 | destination_done: |
| 628 | |
Igor Sysoev | a552ab4 | 2009-09-25 09:30:06 +0000 | [diff] [blame] | 629 | duri.len = last - p; |
| 630 | duri.data = p; |
Ruslan Ermilov | 9b4a99c | 2013-12-23 18:12:03 +0400 | [diff] [blame] | 631 | flags = NGX_HTTP_LOG_UNSAFE; |
Igor Sysoev | a552ab4 | 2009-09-25 09:30:06 +0000 | [diff] [blame] | 632 | |
| 633 | if (ngx_http_parse_unsafe_uri(r, &duri, &args, &flags) != NGX_OK) { |
| 634 | goto invalid_destination; |
| 635 | } |
| 636 | |
Igor Sysoev | 58d04fe | 2008-01-03 20:09:51 +0000 | [diff] [blame] | 637 | if ((r->uri.data[r->uri.len - 1] == '/' && *(last - 1) != '/') |
| 638 | || (r->uri.data[r->uri.len - 1] != '/' && *(last - 1) == '/')) |
| 639 | { |
Ruslan Ermilov | 2161d8a | 2016-03-30 11:52:16 +0300 | [diff] [blame] | 640 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
| 641 | "both URI \"%V\" and \"Destination\" URI \"%V\" " |
| 642 | "should be either collections or non-collections", |
| 643 | &r->uri, &dest->value); |
| 644 | return NGX_HTTP_CONFLICT; |
Igor Sysoev | 58d04fe | 2008-01-03 20:09:51 +0000 | [diff] [blame] | 645 | } |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 646 | |
Igor Sysoev | 58d04fe | 2008-01-03 20:09:51 +0000 | [diff] [blame] | 647 | depth = ngx_http_dav_depth(r, NGX_HTTP_DAV_INFINITY_DEPTH); |
| 648 | |
| 649 | if (depth != NGX_HTTP_DAV_INFINITY_DEPTH) { |
| 650 | |
| 651 | if (r->method == NGX_HTTP_COPY) { |
| 652 | if (depth != 0) { |
| 653 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
| 654 | "\"Depth\" header must be 0 or infinity"); |
| 655 | return NGX_HTTP_BAD_REQUEST; |
| 656 | } |
| 657 | |
| 658 | } else { |
| 659 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
| 660 | "\"Depth\" header must be infinity"); |
| 661 | return NGX_HTTP_BAD_REQUEST; |
| 662 | } |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 663 | } |
Igor Sysoev | 6d93976 | 2007-04-19 18:07:39 +0000 | [diff] [blame] | 664 | |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 665 | over = r->headers_in.overwrite; |
| 666 | |
| 667 | if (over) { |
| 668 | if (over->value.len == 1) { |
| 669 | ch = over->value.data[0]; |
| 670 | |
| 671 | if (ch == 'T' || ch == 't') { |
| 672 | overwrite = 1; |
| 673 | goto overwrite_done; |
| 674 | } |
| 675 | |
| 676 | if (ch == 'F' || ch == 'f') { |
| 677 | overwrite = 0; |
| 678 | goto overwrite_done; |
| 679 | } |
| 680 | |
| 681 | } |
| 682 | |
| 683 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
| 684 | "client sent invalid \"Overwrite\" header: \"%V\"", |
| 685 | &over->value); |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 686 | return NGX_HTTP_BAD_REQUEST; |
| 687 | } |
| 688 | |
| 689 | overwrite = 1; |
| 690 | |
| 691 | overwrite_done: |
| 692 | |
FengGu | bfa5673 | 2014-07-30 14:45:08 +0800 | [diff] [blame] | 693 | if (ngx_http_map_uri_to_path(r, &path, &root, 0) == NULL) { |
| 694 | return NGX_HTTP_INTERNAL_SERVER_ERROR; |
| 695 | } |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 696 | |
| 697 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
| 698 | "http copy from: \"%s\"", path.data); |
| 699 | |
| 700 | uri = r->uri; |
Igor Sysoev | a552ab4 | 2009-09-25 09:30:06 +0000 | [diff] [blame] | 701 | r->uri = duri; |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 702 | |
FengGu | bfa5673 | 2014-07-30 14:45:08 +0800 | [diff] [blame] | 703 | if (ngx_http_map_uri_to_path(r, ©.path, &root, 0) == NULL) { |
| 704 | return NGX_HTTP_INTERNAL_SERVER_ERROR; |
| 705 | } |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 706 | |
| 707 | r->uri = uri; |
| 708 | |
| 709 | copy.path.len--; /* omit "\0" */ |
| 710 | |
| 711 | if (copy.path.data[copy.path.len - 1] == '/') { |
| 712 | slash = 1; |
| 713 | copy.path.len--; |
| 714 | copy.path.data[copy.path.len] = '\0'; |
| 715 | |
| 716 | } else { |
| 717 | slash = 0; |
| 718 | } |
| 719 | |
| 720 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
| 721 | "http copy to: \"%s\"", copy.path.data); |
| 722 | |
Igor Sysoev | 3ec15dd | 2010-04-22 17:15:42 +0000 | [diff] [blame] | 723 | if (ngx_link_info(copy.path.data, &fi) == NGX_FILE_ERROR) { |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 724 | err = ngx_errno; |
| 725 | |
| 726 | if (err != NGX_ENOENT) { |
| 727 | return ngx_http_dav_error(r->connection->log, err, |
Igor Sysoev | 3ec15dd | 2010-04-22 17:15:42 +0000 | [diff] [blame] | 728 | NGX_HTTP_NOT_FOUND, ngx_link_info_n, |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 729 | copy.path.data); |
| 730 | } |
| 731 | |
| 732 | /* destination does not exist */ |
| 733 | |
Igor Sysoev | cea9034 | 2008-01-03 21:44:38 +0000 | [diff] [blame] | 734 | overwrite = 0; |
| 735 | dir = 0; |
| 736 | |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 737 | } else { |
| 738 | |
| 739 | /* destination exists */ |
| 740 | |
| 741 | if (ngx_is_dir(&fi) && !slash) { |
Igor Sysoev | b98f27d | 2008-01-03 21:29:01 +0000 | [diff] [blame] | 742 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
| 743 | "\"%V\" could not be %Ved to collection \"%V\"", |
| 744 | &r->uri, &r->method_name, &dest->value); |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 745 | return NGX_HTTP_CONFLICT; |
| 746 | } |
| 747 | |
| 748 | if (!overwrite) { |
Igor Sysoev | b98f27d | 2008-01-03 21:29:01 +0000 | [diff] [blame] | 749 | ngx_log_error(NGX_LOG_ERR, r->connection->log, NGX_EEXIST, |
| 750 | "\"%s\" could not be created", copy.path.data); |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 751 | return NGX_HTTP_PRECONDITION_FAILED; |
| 752 | } |
| 753 | |
Igor Sysoev | cea9034 | 2008-01-03 21:44:38 +0000 | [diff] [blame] | 754 | dir = ngx_is_dir(&fi); |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 755 | } |
| 756 | |
Igor Sysoev | 3ec15dd | 2010-04-22 17:15:42 +0000 | [diff] [blame] | 757 | if (ngx_link_info(path.data, &fi) == NGX_FILE_ERROR) { |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 758 | return ngx_http_dav_error(r->connection->log, ngx_errno, |
Igor Sysoev | 3ec15dd | 2010-04-22 17:15:42 +0000 | [diff] [blame] | 759 | NGX_HTTP_NOT_FOUND, ngx_link_info_n, |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 760 | path.data); |
| 761 | } |
| 762 | |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 763 | if (ngx_is_dir(&fi)) { |
| 764 | |
| 765 | if (r->uri.data[r->uri.len - 1] != '/') { |
Igor Sysoev | cea9034 | 2008-01-03 21:44:38 +0000 | [diff] [blame] | 766 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
| 767 | "\"%V\" is collection", &r->uri); |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 768 | return NGX_HTTP_BAD_REQUEST; |
| 769 | } |
| 770 | |
Igor Sysoev | cea9034 | 2008-01-03 21:44:38 +0000 | [diff] [blame] | 771 | if (overwrite) { |
| 772 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
| 773 | "http delete: \"%s\"", copy.path.data); |
| 774 | |
| 775 | rc = ngx_http_dav_delete_path(r, ©.path, dir); |
| 776 | |
| 777 | if (rc != NGX_OK) { |
| 778 | return rc; |
| 779 | } |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | if (ngx_is_dir(&fi)) { |
| 784 | |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 785 | path.len -= 2; /* omit "/\0" */ |
| 786 | |
| 787 | if (r->method == NGX_HTTP_MOVE) { |
| 788 | if (ngx_rename_file(path.data, copy.path.data) != NGX_FILE_ERROR) { |
| 789 | return NGX_HTTP_CREATED; |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | if (ngx_create_dir(copy.path.data, ngx_file_access(&fi)) |
| 794 | == NGX_FILE_ERROR) |
| 795 | { |
| 796 | return ngx_http_dav_error(r->connection->log, ngx_errno, |
| 797 | NGX_HTTP_NOT_FOUND, |
| 798 | ngx_create_dir_n, copy.path.data); |
| 799 | } |
| 800 | |
| 801 | copy.len = path.len; |
| 802 | |
Igor Sysoev | 465a5ff | 2007-12-21 17:50:49 +0000 | [diff] [blame] | 803 | tree.init_handler = NULL; |
Igor Sysoev | cea9034 | 2008-01-03 21:44:38 +0000 | [diff] [blame] | 804 | tree.file_handler = ngx_http_dav_copy_tree_file; |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 805 | tree.pre_tree_handler = ngx_http_dav_copy_dir; |
| 806 | tree.post_tree_handler = ngx_http_dav_copy_dir_time; |
| 807 | tree.spec_handler = ngx_http_dav_noop; |
| 808 | tree.data = © |
| 809 | tree.alloc = 0; |
| 810 | tree.log = r->connection->log; |
| 811 | |
| 812 | if (ngx_walk_tree(&tree, &path) == NGX_OK) { |
| 813 | |
| 814 | if (r->method == NGX_HTTP_MOVE) { |
| 815 | rc = ngx_http_dav_delete_path(r, &path, 1); |
| 816 | |
| 817 | if (rc != NGX_OK) { |
| 818 | return rc; |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | return NGX_HTTP_CREATED; |
| 823 | } |
| 824 | |
| 825 | } else { |
| 826 | |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 827 | if (r->method == NGX_HTTP_MOVE) { |
Igor Sysoev | 524f54f | 2008-12-10 14:53:45 +0000 | [diff] [blame] | 828 | |
| 829 | dlcf = ngx_http_get_module_loc_conf(r, ngx_http_dav_module); |
| 830 | |
| 831 | ext.access = 0; |
| 832 | ext.path_access = dlcf->access; |
| 833 | ext.time = -1; |
| 834 | ext.create_path = 1; |
| 835 | ext.delete_file = 0; |
Igor Sysoev | 524f54f | 2008-12-10 14:53:45 +0000 | [diff] [blame] | 836 | ext.log = r->connection->log; |
| 837 | |
| 838 | if (ngx_ext_rename_file(&path, ©.path, &ext) == NGX_OK) { |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 839 | return NGX_HTTP_NO_CONTENT; |
| 840 | } |
Igor Sysoev | 524f54f | 2008-12-10 14:53:45 +0000 | [diff] [blame] | 841 | |
Igor Sysoev | b7a09c5 | 2009-08-12 12:05:33 +0000 | [diff] [blame] | 842 | return NGX_HTTP_INTERNAL_SERVER_ERROR; |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 843 | } |
| 844 | |
Igor Sysoev | b7a09c5 | 2009-08-12 12:05:33 +0000 | [diff] [blame] | 845 | cf.size = ngx_file_size(&fi); |
| 846 | cf.buf_size = 0; |
Maxim Dounin | 93a6553 | 2018-08-01 02:12:21 +0300 | [diff] [blame] | 847 | cf.access = ngx_file_access(&fi); |
Igor Sysoev | b7a09c5 | 2009-08-12 12:05:33 +0000 | [diff] [blame] | 848 | cf.time = ngx_file_mtime(&fi); |
| 849 | cf.log = r->connection->log; |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 850 | |
Igor Sysoev | b7a09c5 | 2009-08-12 12:05:33 +0000 | [diff] [blame] | 851 | if (ngx_copy_file(path.data, copy.path.data, &cf) == NGX_OK) { |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 852 | return NGX_HTTP_NO_CONTENT; |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | return NGX_HTTP_INTERNAL_SERVER_ERROR; |
| 857 | } |
| 858 | |
| 859 | |
| 860 | static ngx_int_t |
| 861 | ngx_http_dav_copy_dir(ngx_tree_ctx_t *ctx, ngx_str_t *path) |
| 862 | { |
| 863 | u_char *p, *dir; |
| 864 | size_t len; |
| 865 | ngx_http_dav_copy_ctx_t *copy; |
| 866 | |
| 867 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0, |
| 868 | "http copy dir: \"%s\"", path->data); |
| 869 | |
| 870 | copy = ctx->data; |
| 871 | |
| 872 | len = copy->path.len + path->len; |
| 873 | |
| 874 | dir = ngx_alloc(len + 1, ctx->log); |
| 875 | if (dir == NULL) { |
| 876 | return NGX_ABORT; |
| 877 | } |
| 878 | |
| 879 | p = ngx_cpymem(dir, copy->path.data, copy->path.len); |
| 880 | (void) ngx_cpystrn(p, path->data + copy->len, path->len - copy->len + 1); |
| 881 | |
| 882 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0, |
| 883 | "http copy dir to: \"%s\"", dir); |
| 884 | |
| 885 | if (ngx_create_dir(dir, ngx_dir_access(ctx->access)) == NGX_FILE_ERROR) { |
| 886 | (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_create_dir_n, |
| 887 | dir); |
| 888 | } |
| 889 | |
| 890 | ngx_free(dir); |
| 891 | |
| 892 | return NGX_OK; |
| 893 | } |
| 894 | |
| 895 | |
| 896 | static ngx_int_t |
| 897 | ngx_http_dav_copy_dir_time(ngx_tree_ctx_t *ctx, ngx_str_t *path) |
| 898 | { |
| 899 | u_char *p, *dir; |
| 900 | size_t len; |
| 901 | ngx_http_dav_copy_ctx_t *copy; |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 902 | |
| 903 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0, |
| 904 | "http copy dir time: \"%s\"", path->data); |
| 905 | |
| 906 | copy = ctx->data; |
| 907 | |
| 908 | len = copy->path.len + path->len; |
| 909 | |
| 910 | dir = ngx_alloc(len + 1, ctx->log); |
| 911 | if (dir == NULL) { |
| 912 | return NGX_ABORT; |
| 913 | } |
| 914 | |
| 915 | p = ngx_cpymem(dir, copy->path.data, copy->path.len); |
| 916 | (void) ngx_cpystrn(p, path->data + copy->len, path->len - copy->len + 1); |
| 917 | |
| 918 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0, |
| 919 | "http copy dir time to: \"%s\"", dir); |
| 920 | |
Igor Sysoev | 5dff77a | 2007-12-29 13:55:10 +0000 | [diff] [blame] | 921 | #if (NGX_WIN32) |
| 922 | { |
| 923 | ngx_fd_t fd; |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 924 | |
| 925 | fd = ngx_open_file(dir, NGX_FILE_RDWR, NGX_FILE_OPEN, 0); |
| 926 | |
| 927 | if (fd == NGX_INVALID_FILE) { |
| 928 | (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_open_file_n, dir); |
| 929 | goto failed; |
| 930 | } |
| 931 | |
| 932 | if (ngx_set_file_time(NULL, fd, ctx->mtime) != NGX_OK) { |
| 933 | ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno, |
| 934 | ngx_set_file_time_n " \"%s\" failed", dir); |
| 935 | } |
| 936 | |
| 937 | if (ngx_close_file(fd) == NGX_FILE_ERROR) { |
| 938 | ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno, |
| 939 | ngx_close_file_n " \"%s\" failed", dir); |
| 940 | } |
Igor Sysoev | 5dff77a | 2007-12-29 13:55:10 +0000 | [diff] [blame] | 941 | } |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 942 | |
| 943 | failed: |
| 944 | |
| 945 | #else |
| 946 | |
| 947 | if (ngx_set_file_time(dir, 0, ctx->mtime) != NGX_OK) { |
| 948 | ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno, |
| 949 | ngx_set_file_time_n " \"%s\" failed", dir); |
| 950 | } |
| 951 | |
| 952 | #endif |
| 953 | |
| 954 | ngx_free(dir); |
| 955 | |
| 956 | return NGX_OK; |
| 957 | } |
| 958 | |
| 959 | |
| 960 | static ngx_int_t |
Igor Sysoev | cea9034 | 2008-01-03 21:44:38 +0000 | [diff] [blame] | 961 | ngx_http_dav_copy_tree_file(ngx_tree_ctx_t *ctx, ngx_str_t *path) |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 962 | { |
| 963 | u_char *p, *file; |
| 964 | size_t len; |
Igor Sysoev | b7a09c5 | 2009-08-12 12:05:33 +0000 | [diff] [blame] | 965 | ngx_copy_file_t cf; |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 966 | ngx_http_dav_copy_ctx_t *copy; |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 967 | |
| 968 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0, |
| 969 | "http copy file: \"%s\"", path->data); |
| 970 | |
| 971 | copy = ctx->data; |
| 972 | |
| 973 | len = copy->path.len + path->len; |
| 974 | |
| 975 | file = ngx_alloc(len + 1, ctx->log); |
| 976 | if (file == NULL) { |
| 977 | return NGX_ABORT; |
| 978 | } |
| 979 | |
| 980 | p = ngx_cpymem(file, copy->path.data, copy->path.len); |
| 981 | (void) ngx_cpystrn(p, path->data + copy->len, path->len - copy->len + 1); |
| 982 | |
| 983 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0, |
| 984 | "http copy file to: \"%s\"", file); |
| 985 | |
Igor Sysoev | b7a09c5 | 2009-08-12 12:05:33 +0000 | [diff] [blame] | 986 | cf.size = ctx->size; |
| 987 | cf.buf_size = 0; |
| 988 | cf.access = ctx->access; |
| 989 | cf.time = ctx->mtime; |
| 990 | cf.log = ctx->log; |
| 991 | |
| 992 | (void) ngx_copy_file(path->data, file, &cf); |
Igor Sysoev | cea9034 | 2008-01-03 21:44:38 +0000 | [diff] [blame] | 993 | |
| 994 | ngx_free(file); |
| 995 | |
| 996 | return NGX_OK; |
| 997 | } |
| 998 | |
| 999 | |
| 1000 | static ngx_int_t |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 1001 | ngx_http_dav_depth(ngx_http_request_t *r, ngx_int_t dflt) |
| 1002 | { |
| 1003 | ngx_table_elt_t *depth; |
Igor Sysoev | 6d93976 | 2007-04-19 18:07:39 +0000 | [diff] [blame] | 1004 | |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 1005 | depth = r->headers_in.depth; |
| 1006 | |
| 1007 | if (depth == NULL) { |
| 1008 | return dflt; |
| 1009 | } |
| 1010 | |
| 1011 | if (depth->value.len == 1) { |
| 1012 | |
| 1013 | if (depth->value.data[0] == '0') { |
| 1014 | return 0; |
| 1015 | } |
| 1016 | |
| 1017 | if (depth->value.data[0] == '1') { |
| 1018 | return 1; |
| 1019 | } |
| 1020 | |
| 1021 | } else { |
| 1022 | |
| 1023 | if (depth->value.len == sizeof("infinity") - 1 |
| 1024 | && ngx_strcmp(depth->value.data, "infinity") == 0) |
| 1025 | { |
| 1026 | return NGX_HTTP_DAV_INFINITY_DEPTH; |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
| 1031 | "client sent invalid \"Depth\" header: \"%V\"", |
| 1032 | &depth->value); |
| 1033 | |
| 1034 | return NGX_HTTP_DAV_INVALID_DEPTH; |
| 1035 | } |
| 1036 | |
| 1037 | |
| 1038 | static ngx_int_t |
| 1039 | ngx_http_dav_error(ngx_log_t *log, ngx_err_t err, ngx_int_t not_found, |
Igor Sysoev | 7bdb720 | 2006-04-19 15:30:56 +0000 | [diff] [blame] | 1040 | char *failed, u_char *path) |
| 1041 | { |
| 1042 | ngx_int_t rc; |
| 1043 | ngx_uint_t level; |
| 1044 | |
| 1045 | if (err == NGX_ENOENT || err == NGX_ENOTDIR || err == NGX_ENAMETOOLONG) { |
| 1046 | level = NGX_LOG_ERR; |
| 1047 | rc = not_found; |
| 1048 | |
| 1049 | } else if (err == NGX_EACCES || err == NGX_EPERM) { |
| 1050 | level = NGX_LOG_ERR; |
| 1051 | rc = NGX_HTTP_FORBIDDEN; |
| 1052 | |
| 1053 | } else if (err == NGX_EEXIST) { |
| 1054 | level = NGX_LOG_ERR; |
| 1055 | rc = NGX_HTTP_NOT_ALLOWED; |
| 1056 | |
| 1057 | } else if (err == NGX_ENOSPC) { |
| 1058 | level = NGX_LOG_CRIT; |
| 1059 | rc = NGX_HTTP_INSUFFICIENT_STORAGE; |
| 1060 | |
| 1061 | } else { |
| 1062 | level = NGX_LOG_CRIT; |
| 1063 | rc = NGX_HTTP_INTERNAL_SERVER_ERROR; |
| 1064 | } |
| 1065 | |
Igor Sysoev | 5473662 | 2007-02-16 12:16:25 +0000 | [diff] [blame] | 1066 | ngx_log_error(level, log, err, "%s \"%s\" failed", failed, path); |
Igor Sysoev | 7bdb720 | 2006-04-19 15:30:56 +0000 | [diff] [blame] | 1067 | |
| 1068 | return rc; |
| 1069 | } |
| 1070 | |
| 1071 | |
| 1072 | static ngx_int_t |
Ruslan Ermilov | 1953260 | 2019-12-16 15:19:01 +0300 | [diff] [blame] | 1073 | ngx_http_dav_location(ngx_http_request_t *r) |
Igor Sysoev | 7bdb720 | 2006-04-19 15:30:56 +0000 | [diff] [blame] | 1074 | { |
Ruslan Ermilov | 0c5e110 | 2021-05-24 21:55:20 +0300 | [diff] [blame] | 1075 | u_char *p; |
| 1076 | size_t len; |
| 1077 | uintptr_t escape; |
| 1078 | |
Ruslan Ermilov | 8ca190c | 2016-12-22 11:58:52 +0300 | [diff] [blame] | 1079 | r->headers_out.location = ngx_list_push(&r->headers_out.headers); |
Igor Sysoev | 7bdb720 | 2006-04-19 15:30:56 +0000 | [diff] [blame] | 1080 | if (r->headers_out.location == NULL) { |
| 1081 | return NGX_ERROR; |
| 1082 | } |
| 1083 | |
Ruslan Ermilov | 8ca190c | 2016-12-22 11:58:52 +0300 | [diff] [blame] | 1084 | r->headers_out.location->hash = 1; |
| 1085 | ngx_str_set(&r->headers_out.location->key, "Location"); |
Ruslan Ermilov | 0c5e110 | 2021-05-24 21:55:20 +0300 | [diff] [blame] | 1086 | |
| 1087 | escape = 2 * ngx_escape_uri(NULL, r->uri.data, r->uri.len, NGX_ESCAPE_URI); |
| 1088 | |
| 1089 | if (escape) { |
| 1090 | len = r->uri.len + escape; |
| 1091 | |
| 1092 | p = ngx_pnalloc(r->pool, len); |
| 1093 | if (p == NULL) { |
| 1094 | ngx_http_clear_location(r); |
| 1095 | return NGX_ERROR; |
| 1096 | } |
| 1097 | |
| 1098 | r->headers_out.location->value.len = len; |
| 1099 | r->headers_out.location->value.data = p; |
| 1100 | |
| 1101 | ngx_escape_uri(p, r->uri.data, r->uri.len, NGX_ESCAPE_URI); |
| 1102 | |
| 1103 | } else { |
| 1104 | r->headers_out.location->value = r->uri; |
| 1105 | } |
Igor Sysoev | 7bdb720 | 2006-04-19 15:30:56 +0000 | [diff] [blame] | 1106 | |
| 1107 | return NGX_OK; |
| 1108 | } |
| 1109 | |
| 1110 | |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 1111 | static void * |
| 1112 | ngx_http_dav_create_loc_conf(ngx_conf_t *cf) |
| 1113 | { |
| 1114 | ngx_http_dav_loc_conf_t *conf; |
| 1115 | |
| 1116 | conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_dav_loc_conf_t)); |
| 1117 | if (conf == NULL) { |
Igor Sysoev | 260c432 | 2009-06-02 16:09:44 +0000 | [diff] [blame] | 1118 | return NULL; |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 1119 | } |
| 1120 | |
| 1121 | /* |
| 1122 | * set by ngx_pcalloc(): |
| 1123 | * |
| 1124 | * conf->methods = 0; |
| 1125 | */ |
| 1126 | |
Igor Sysoev | fa0d3e1 | 2008-01-22 15:16:38 +0000 | [diff] [blame] | 1127 | conf->min_delete_depth = NGX_CONF_UNSET_UINT; |
Igor Sysoev | b71c690 | 2006-08-04 16:04:04 +0000 | [diff] [blame] | 1128 | conf->access = NGX_CONF_UNSET_UINT; |
Igor Sysoev | 8dd4053 | 2007-12-30 11:46:03 +0000 | [diff] [blame] | 1129 | conf->create_full_put_path = NGX_CONF_UNSET; |
Igor Sysoev | 7bdb720 | 2006-04-19 15:30:56 +0000 | [diff] [blame] | 1130 | |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 1131 | return conf; |
| 1132 | } |
| 1133 | |
| 1134 | |
| 1135 | static char * |
| 1136 | ngx_http_dav_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) |
| 1137 | { |
| 1138 | ngx_http_dav_loc_conf_t *prev = parent; |
| 1139 | ngx_http_dav_loc_conf_t *conf = child; |
| 1140 | |
| 1141 | ngx_conf_merge_bitmask_value(conf->methods, prev->methods, |
Igor Sysoev | 7bdb720 | 2006-04-19 15:30:56 +0000 | [diff] [blame] | 1142 | (NGX_CONF_BITMASK_SET|NGX_HTTP_DAV_OFF)); |
| 1143 | |
Igor Sysoev | 8dd4053 | 2007-12-30 11:46:03 +0000 | [diff] [blame] | 1144 | ngx_conf_merge_uint_value(conf->min_delete_depth, |
| 1145 | prev->min_delete_depth, 0); |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 1146 | |
Igor Sysoev | b71c690 | 2006-08-04 16:04:04 +0000 | [diff] [blame] | 1147 | ngx_conf_merge_uint_value(conf->access, prev->access, 0600); |
| 1148 | |
Igor Sysoev | 8dd4053 | 2007-12-30 11:46:03 +0000 | [diff] [blame] | 1149 | ngx_conf_merge_value(conf->create_full_put_path, |
| 1150 | prev->create_full_put_path, 0); |
| 1151 | |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 1152 | return NGX_CONF_OK; |
| 1153 | } |
| 1154 | |
| 1155 | |
| 1156 | static ngx_int_t |
Igor Sysoev | da173ab | 2006-08-30 10:39:17 +0000 | [diff] [blame] | 1157 | ngx_http_dav_init(ngx_conf_t *cf) |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 1158 | { |
| 1159 | ngx_http_handler_pt *h; |
| 1160 | ngx_http_core_main_conf_t *cmcf; |
| 1161 | |
Igor Sysoev | da173ab | 2006-08-30 10:39:17 +0000 | [diff] [blame] | 1162 | cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module); |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 1163 | |
| 1164 | h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers); |
| 1165 | if (h == NULL) { |
| 1166 | return NGX_ERROR; |
| 1167 | } |
| 1168 | |
| 1169 | *h = ngx_http_dav_handler; |
| 1170 | |
| 1171 | return NGX_OK; |
| 1172 | } |