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